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