697726b227be351d84406baa47f623973b4a7772
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / parsers / custom / CustomXmlTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.ui.parsers.custom;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.File;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.io.RandomAccessFile;
20
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import javax.xml.parsers.ParserConfigurationException;
24
25 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition.InputAttribute;
26 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition.InputElement;
27 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
28 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
29 import org.eclipse.linuxtools.tmf.core.io.BufferedRandomAccessFile;
30 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
31 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
32 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
33 import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
34 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37 import org.w3c.dom.Node;
38 import org.w3c.dom.NodeList;
39 import org.xml.sax.EntityResolver;
40 import org.xml.sax.ErrorHandler;
41 import org.xml.sax.InputSource;
42 import org.xml.sax.SAXException;
43 import org.xml.sax.SAXParseException;
44
45 public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
46
47 private static final TmfLocation<Long> NULL_LOCATION = new TmfLocation<Long>((Long) null);
48 private static final int DEFAULT_CACHE_SIZE = 100;
49
50 private CustomXmlTraceDefinition fDefinition;
51 private CustomXmlEventType fEventType;
52 private InputElement fRecordInputElement;
53
54 public CustomXmlTrace(CustomXmlTraceDefinition definition) {
55 fDefinition = definition;
56 fEventType = new CustomXmlEventType(fDefinition);
57 fRecordInputElement = getRecordInputElement(fDefinition.rootInputElement);
58 }
59
60 public CustomXmlTrace(String name, CustomXmlTraceDefinition definition, String path, int pageSize) throws FileNotFoundException {
61 super(name, CustomXmlEvent.class, path, (pageSize > 0) ? pageSize : DEFAULT_CACHE_SIZE, true);
62 fDefinition = definition;
63 fEventType = new CustomXmlEventType(fDefinition);
64 fRecordInputElement = getRecordInputElement(fDefinition.rootInputElement);
65 }
66
67 @Override
68 public void initTrace(String name, String path, Class<CustomXmlEvent> eventType) throws FileNotFoundException {
69 super.initTrace(name, path, eventType);
70 }
71
72 @Override
73 public TmfContext seekLocation(ITmfLocation<?> location) {
74 CustomXmlTraceContext context = new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
75 if (NULL_LOCATION.equals(location) || !new File(getPath()).isFile()) {
76 return context;
77 }
78 try {
79 context.raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
80 if (location != null && location.getLocation() instanceof Long) {
81 context.raFile.seek((Long)location.getLocation());
82 }
83
84 String line;
85 String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$
86 long rawPos = context.raFile.getFilePointer();
87
88 while ((line = context.raFile.getNextLine()) != null) {
89 int idx = line.indexOf(recordElementStart);
90 if (idx != -1) {
91 context.setLocation(new TmfLocation<Long>(rawPos + idx));
92 return context;
93 }
94 rawPos = context.raFile.getFilePointer();
95 }
96 return context;
97 } catch (FileNotFoundException e) {
98 e.printStackTrace();
99 return context;
100 } catch (IOException e) {
101 e.printStackTrace();
102 return context;
103 }
104
105 }
106
107 @Override
108 public TmfContext seekLocation(double ratio) {
109 try {
110 BufferedRandomAccessFile raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
111 long pos = (long) (ratio * raFile.length());
112 while (pos > 0) {
113 raFile.seek(pos - 1);
114 if (raFile.read() == '\n') break;
115 pos--;
116 }
117 ITmfLocation<?> location = new TmfLocation<Long>(pos);
118 TmfContext context = seekLocation(location);
119 context.setRank(ITmfContext.UNKNOWN_RANK);
120 return context;
121 } catch (FileNotFoundException e) {
122 e.printStackTrace();
123 return new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
124 } catch (IOException e) {
125 e.printStackTrace();
126 return new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
127 }
128 }
129
130 @Override
131 public double getLocationRatio(ITmfLocation<?> location) {
132 try {
133 if (location.getLocation() instanceof Long) {
134 RandomAccessFile raFile = new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$
135 return (double) ((Long) location.getLocation()) / raFile.length();
136 }
137 } catch (FileNotFoundException e) {
138 e.printStackTrace();
139 } catch (IOException e) {
140 e.printStackTrace();
141 }
142 return 0;
143 }
144
145 @Override
146 public ITmfLocation<?> getCurrentLocation() {
147 // TODO Auto-generated method stub
148 return null;
149 }
150
151 @Override
152 public synchronized TmfEvent getNextEvent(ITmfContext context) {
153 ITmfContext savedContext = context.clone();
154 TmfEvent event = parseEvent(context);
155 if (event != null) {
156 updateIndex(savedContext, savedContext.getRank(), event.getTimestamp());
157 context.updateRank(1);
158 }
159 return event;
160 }
161
162 @Override
163 public TmfEvent parseEvent(ITmfContext tmfContext) {
164 if (!(tmfContext instanceof CustomXmlTraceContext)) {
165 return null;
166 }
167
168 CustomXmlTraceContext context = (CustomXmlTraceContext) tmfContext;
169 if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
170 return null;
171 }
172
173 synchronized (context.raFile) {
174 CustomXmlEvent event = null;
175 try {
176 if (context.raFile.getFilePointer() != (Long)context.getLocation().getLocation() + 1) {
177 context.raFile.seek((Long)context.getLocation().getLocation() + 1); // +1 is for the <
178 }
179 StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$
180 readElement(elementBuffer, context.raFile);
181 Element element = parseElementBuffer(elementBuffer);
182
183 event = extractEvent(element, fRecordInputElement);
184 ((StringBuffer) event.getContent().getValue()).append(elementBuffer);
185
186 String line;
187 String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$
188 long rawPos = context.raFile.getFilePointer();
189
190 while ((line = context.raFile.getNextLine()) != null) {
191 int idx = line.indexOf(recordElementStart);
192 if (idx != -1) {
193 context.setLocation(new TmfLocation<Long>(rawPos + idx));
194 return event;
195 }
196 rawPos = context.raFile.getFilePointer();
197 }
198 } catch (IOException e) {
199 e.printStackTrace();
200 }
201 context.setLocation(NULL_LOCATION);
202 return event;
203 }
204 }
205
206 private Element parseElementBuffer(StringBuffer elementBuffer) {
207 try {
208 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
209 DocumentBuilder db = dbf.newDocumentBuilder();
210
211 // The following allows xml parsing without access to the dtd
212 EntityResolver resolver = new EntityResolver () {
213 @Override
214 public InputSource resolveEntity (String publicId, String systemId) {
215 String empty = ""; //$NON-NLS-1$
216 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
217 return new InputSource(bais);
218 }
219 };
220 db.setEntityResolver(resolver);
221
222 // The following catches xml parsing exceptions
223 db.setErrorHandler(new ErrorHandler(){
224 @Override
225 public void error(SAXParseException saxparseexception) throws SAXException {}
226 @Override
227 public void warning(SAXParseException saxparseexception) throws SAXException {}
228 @Override
229 public void fatalError(SAXParseException saxparseexception) throws SAXException {
230 throw saxparseexception;
231 }});
232
233 Document doc = db.parse(new ByteArrayInputStream(elementBuffer.toString().getBytes()));
234 return doc.getDocumentElement();
235 } catch (ParserConfigurationException e) {
236 e.printStackTrace();
237 } catch (SAXException e) {
238 e.printStackTrace();
239 } catch (IOException e) {
240 e.printStackTrace();
241 }
242 return null;
243 }
244
245 private void readElement(StringBuffer buffer, RandomAccessFile raFile) {
246 try {
247 int numRead = 0;
248 boolean startTagClosed = false;
249 int i;
250 while ((i = raFile.read()) != -1) {
251 numRead++;
252 char c = (char)i;
253 buffer.append(c);
254 if (c == '"') {
255 readQuote(buffer, raFile, '"');
256 } else if (c == '\'') {
257 readQuote(buffer, raFile, '\'');
258 } else if (c == '<') {
259 readElement(buffer, raFile);
260 } else if (c == '/' && numRead == 1) {
261 break; // found "</"
262 } else if (c == '-' && numRead == 3 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("!-")) { //$NON-NLS-1$
263 readComment(buffer, raFile); // found "<!--"
264 } else if (i == '>') {
265 if (buffer.charAt(buffer.length() - 2) == '/') {
266 break; // found "/>"
267 } else if (startTagClosed) {
268 break; // found "<...>...</...>"
269 } else {
270 startTagClosed = true; // found "<...>"
271 }
272 }
273 }
274 return;
275 } catch (IOException e) {
276 return;
277 }
278 }
279
280 private void readQuote(StringBuffer buffer, RandomAccessFile raFile, char eq) {
281 try {
282 int i;
283 while ((i = raFile.read()) != -1) {
284 char c = (char)i;
285 buffer.append(c);
286 if (c == eq) {
287 break; // found matching end-quote
288 }
289 }
290 return;
291 } catch (IOException e) {
292 return;
293 }
294 }
295
296 private void readComment(StringBuffer buffer, RandomAccessFile raFile) {
297 try {
298 int numRead = 0;
299 int i;
300 while ((i = raFile.read()) != -1) {
301 numRead++;
302 char c = (char)i;
303 buffer.append(c);
304 if (c == '>' && numRead >= 2 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("--")) { //$NON-NLS-1$
305 break; // found "-->"
306 }
307 }
308 return;
309 } catch (IOException e) {
310 return;
311 }
312 }
313
314 public static StringBuffer parseElement(Element parentElement, StringBuffer buffer) {
315 NodeList nodeList = parentElement.getChildNodes();
316 String separator = null;
317 for (int i = 0; i < nodeList.getLength(); i++) {
318 Node node = nodeList.item(i);
319 if (node.getNodeType() == Node.ELEMENT_NODE) {
320 if (separator == null) {
321 separator = " | "; //$NON-NLS-1$
322 } else {
323 buffer.append(separator);
324 }
325 Element element = (Element) node;
326 if (element.hasChildNodes() == false) {
327 buffer.append(element.getNodeName());
328 } else if (element.getChildNodes().getLength() == 1 && element.getFirstChild().getNodeType() == Node.TEXT_NODE) {
329 buffer.append(element.getNodeName() + ":" + element.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
330 } else {
331 buffer.append(element.getNodeName());
332 buffer.append(" [ "); //$NON-NLS-1$
333 parseElement(element, buffer);
334 buffer.append(" ]"); //$NON-NLS-1$
335 }
336 } else if (node.getNodeType() == Node.TEXT_NODE) {
337 if (node.getNodeValue().trim().length() != 0) {
338 buffer.append(node.getNodeValue().trim());
339 }
340 }
341 }
342 return buffer;
343 }
344
345 public InputElement getRecordInputElement(InputElement inputElement) {
346 if (inputElement.logEntry) {
347 return inputElement;
348 } else if (inputElement.childElements != null) {
349 for (InputElement childInputElement : inputElement.childElements) {
350 InputElement recordInputElement = getRecordInputElement(childInputElement);
351 if (recordInputElement != null) {
352 return recordInputElement;
353 }
354 }
355 }
356 return null;
357 }
358
359 public CustomXmlEvent extractEvent(Element element, InputElement inputElement) {
360 CustomXmlEvent event = new CustomXmlEvent(fDefinition, this, TmfTimestamp.ZERO, "", fEventType,""); //$NON-NLS-1$ //$NON-NLS-2$
361 event.setContent(new CustomEventContent(event, "")); //$NON-NLS-1$
362 parseElement(element, event, inputElement);
363 return event;
364 }
365
366 private void parseElement(Element element, CustomXmlEvent event, InputElement inputElement) {
367 if (inputElement.inputName != null && !inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_IGNORE)) {
368 event.parseInput(parseElement(element, new StringBuffer()).toString(), inputElement.inputName, inputElement.inputAction, inputElement.inputFormat);
369 }
370 if (inputElement.attributes != null) {
371 for (InputAttribute attribute : inputElement.attributes) {
372 event.parseInput(element.getAttribute(attribute.attributeName), attribute.inputName, attribute.inputAction, attribute.inputFormat);
373 }
374 }
375 NodeList childNodes = element.getChildNodes();
376 if (inputElement.childElements != null) {
377 for (int i = 0; i < childNodes.getLength(); i++) {
378 Node node = childNodes.item(i);
379 if (node instanceof Element) {
380 for (InputElement child : inputElement.childElements) {
381 if (node.getNodeName().equals(child.elementName)) {
382 parseElement((Element) node, event, child);
383 break;
384 }
385 }
386 }
387 }
388 }
389 return;
390 }
391
392 public CustomTraceDefinition getDefinition() {
393 return fDefinition;
394 }
395 }
This page took 0.038667 seconds and 4 git commands to generate.