ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / parsers / custom / CustomXmlTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 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.tmf.core.parsers.custom;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.File;
17 import java.io.IOException;
18 import java.io.RandomAccessFile;
19 import java.nio.ByteBuffer;
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.IProject;
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.linuxtools.internal.tmf.core.Activator;
30 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
31 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
32 import org.eclipse.linuxtools.tmf.core.io.BufferedRandomAccessFile;
33 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTraceDefinition.InputAttribute;
34 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTraceDefinition.InputElement;
35 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
36 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
37 import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
38 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
39 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
40 import org.eclipse.linuxtools.tmf.core.trace.TraceValidationStatus;
41 import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
42 import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
43 import org.eclipse.linuxtools.tmf.core.trace.indexer.TmfBTreeTraceIndexer;
44 import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
45 import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
46 import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
47 import org.eclipse.linuxtools.tmf.core.trace.location.TmfLongLocation;
48 import org.w3c.dom.Document;
49 import org.w3c.dom.Element;
50 import org.w3c.dom.Node;
51 import org.w3c.dom.NodeList;
52 import org.xml.sax.EntityResolver;
53 import org.xml.sax.ErrorHandler;
54 import org.xml.sax.InputSource;
55 import org.xml.sax.SAXException;
56 import org.xml.sax.SAXParseException;
57
58 /**
59 * Trace object for custom XML trace parsers.
60 *
61 * @author Patrick Tassé
62 * @since 3.0
63 */
64 public class CustomXmlTrace extends TmfTrace implements ITmfEventParser, ITmfPersistentlyIndexable {
65
66 private static final TmfLongLocation NULL_LOCATION = new TmfLongLocation(-1L);
67 private static final int DEFAULT_CACHE_SIZE = 100;
68 private static final int MAX_LINES = 100;
69 private static final int CONFIDENCE = 100;
70
71 private final CustomXmlTraceDefinition fDefinition;
72 private final CustomXmlEventType fEventType;
73 private final InputElement fRecordInputElement;
74 private BufferedRandomAccessFile fFile;
75
76 /**
77 * Basic constructor
78 *
79 * @param definition
80 * Trace definition
81 */
82 public CustomXmlTrace(final CustomXmlTraceDefinition definition) {
83 fDefinition = definition;
84 fEventType = new CustomXmlEventType(fDefinition);
85 fRecordInputElement = getRecordInputElement(fDefinition.rootInputElement);
86 setCacheSize(DEFAULT_CACHE_SIZE);
87 }
88
89 /**
90 * Full constructor
91 *
92 * @param resource
93 * Trace resource
94 * @param definition
95 * Trace definition
96 * @param path
97 * Path to the trace/log file
98 * @param pageSize
99 * Page size to use
100 * @throws TmfTraceException
101 * If the trace/log couldn't be opened
102 */
103 public CustomXmlTrace(final IResource resource,
104 final CustomXmlTraceDefinition definition, final String path,
105 final int pageSize) throws TmfTraceException {
106 this(definition);
107 setCacheSize((pageSize > 0) ? pageSize : DEFAULT_CACHE_SIZE);
108 initTrace(resource, path, CustomXmlEvent.class);
109 }
110
111 @Override
112 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType) throws TmfTraceException {
113 super.initTrace(resource, path, eventType);
114 try {
115 fFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
116 } catch (IOException e) {
117 throw new TmfTraceException(e.getMessage(), e);
118 }
119 }
120
121 @Override
122 public synchronized void dispose() {
123 super.dispose();
124 if (fFile != null) {
125 try {
126 fFile.close();
127 } catch (IOException e) {
128 } finally {
129 fFile = null;
130 }
131 }
132 }
133
134 @Override
135 public ITmfTraceIndexer getIndexer() {
136 return super.getIndexer();
137 }
138
139 @Override
140 public synchronized TmfContext seekEvent(final ITmfLocation location) {
141 final CustomXmlTraceContext context = new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
142 if (NULL_LOCATION.equals(location) || fFile == null) {
143 return context;
144 }
145 try {
146 if (location == null) {
147 fFile.seek(0);
148 } else if (location.getLocationInfo() instanceof Long) {
149 fFile.seek((Long) location.getLocationInfo());
150 }
151 long rawPos = fFile.getFilePointer();
152 String line = fFile.getNextLine();
153 while (line != null) {
154 final int idx = indexOfElement(fRecordInputElement.elementName, line, 0);
155 if (idx != -1) {
156 context.setLocation(new TmfLongLocation(rawPos + idx));
157 return context;
158 }
159 rawPos = fFile.getFilePointer();
160 line = fFile.getNextLine();
161 }
162 return context;
163 } catch (final IOException e) {
164 Activator.logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
165 return context;
166 }
167
168 }
169
170 @Override
171 public synchronized TmfContext seekEvent(final double ratio) {
172 if (fFile == null) {
173 return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
174 }
175 try {
176 long pos = Math.round(ratio * fFile.length());
177 while (pos > 0) {
178 fFile.seek(pos - 1);
179 if (fFile.read() == '\n') {
180 break;
181 }
182 pos--;
183 }
184 final ITmfLocation location = new TmfLongLocation(pos);
185 final TmfContext context = seekEvent(location);
186 context.setRank(ITmfContext.UNKNOWN_RANK);
187 return context;
188 } catch (final IOException e) {
189 Activator.logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
190 return new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
191 }
192 }
193
194 @Override
195 public synchronized double getLocationRatio(final ITmfLocation location) {
196 if (fFile == null) {
197 return 0;
198 }
199 try {
200 if (location.getLocationInfo() instanceof Long) {
201 return ((Long) location.getLocationInfo()).doubleValue() / fFile.length();
202 }
203 } catch (final IOException e) {
204 Activator.logError("Error getting location ration. File: " + getPath(), e); //$NON-NLS-1$
205 }
206 return 0;
207 }
208
209 @Override
210 public ITmfLocation getCurrentLocation() {
211 // TODO Auto-generated method stub
212 return null;
213 }
214
215 @Override
216 public synchronized CustomXmlEvent parseEvent(final ITmfContext tmfContext) {
217 ITmfContext context = seekEvent(tmfContext.getLocation());
218 return parse(context);
219 }
220
221 @Override
222 public synchronized CustomXmlEvent getNext(final ITmfContext context) {
223 final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank());
224 final CustomXmlEvent event = parse(context);
225 if (event != null) {
226 updateAttributes(savedContext, event.getTimestamp());
227 context.increaseRank();
228 }
229 return event;
230 }
231
232 private synchronized CustomXmlEvent parse(final ITmfContext tmfContext) {
233 if (fFile == null) {
234 return null;
235 }
236 if (!(tmfContext instanceof CustomXmlTraceContext)) {
237 return null;
238 }
239
240 final CustomXmlTraceContext context = (CustomXmlTraceContext) tmfContext;
241 if (context.getLocation() == null || !(context.getLocation().getLocationInfo() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
242 return null;
243 }
244
245 CustomXmlEvent event = null;
246 try {
247 if (fFile.getFilePointer() != (Long) context.getLocation().getLocationInfo() + 1)
248 {
249 fFile.seek((Long) context.getLocation().getLocationInfo() + 1); // +1 is for the <
250 }
251 final StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$
252 readElement(elementBuffer, fFile);
253 final Element element = parseElementBuffer(elementBuffer);
254
255 event = extractEvent(element, fRecordInputElement);
256 ((StringBuffer) event.getContent().getValue()).append(elementBuffer);
257
258 long rawPos = fFile.getFilePointer();
259 String line = fFile.getNextLine();
260 while (line != null) {
261 final int idx = indexOfElement(fRecordInputElement.elementName, line, 0);
262 if (idx != -1) {
263 context.setLocation(new TmfLongLocation(rawPos + idx));
264 return event;
265 }
266 rawPos = fFile.getFilePointer();
267 line = fFile.getNextLine();
268 }
269 } catch (final IOException e) {
270 Activator.logError("Error parsing event. File: " + getPath(), e); //$NON-NLS-1$
271
272 }
273 context.setLocation(NULL_LOCATION);
274 return event;
275 }
276
277 private Element parseElementBuffer(final StringBuffer elementBuffer) {
278 try {
279 final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
280 final DocumentBuilder db = dbf.newDocumentBuilder();
281
282 // The following allows xml parsing without access to the dtd
283 final EntityResolver resolver = new EntityResolver() {
284 @Override
285 public InputSource resolveEntity(final String publicId, final String systemId) {
286 final String empty = ""; //$NON-NLS-1$
287 final ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
288 return new InputSource(bais);
289 }
290 };
291 db.setEntityResolver(resolver);
292
293 // The following catches xml parsing exceptions
294 db.setErrorHandler(new ErrorHandler() {
295 @Override
296 public void error(final SAXParseException saxparseexception) throws SAXException {}
297
298 @Override
299 public void warning(final SAXParseException saxparseexception) throws SAXException {}
300
301 @Override
302 public void fatalError(final SAXParseException saxparseexception) throws SAXException {
303 throw saxparseexception;
304 }
305 });
306
307 final Document doc = db.parse(new ByteArrayInputStream(elementBuffer.toString().getBytes()));
308 return doc.getDocumentElement();
309 } catch (final ParserConfigurationException e) {
310 Activator.logError("Error parsing element buffer. File:" + getPath(), e); //$NON-NLS-1$
311 } catch (final SAXException e) {
312 Activator.logError("Error parsing element buffer. File:" + getPath(), e); //$NON-NLS-1$
313 } catch (final IOException e) {
314 Activator.logError("Error parsing element buffer. File: " + getPath(), e); //$NON-NLS-1$
315 }
316 return null;
317 }
318
319 private static int indexOfElement(String elementName, String line, int fromIndex) {
320 final String recordElementStart = '<' + elementName;
321 int index = line.indexOf(recordElementStart, fromIndex);
322 if (index == -1) {
323 return index;
324 }
325 int nextCharIndex = index + recordElementStart.length();
326 if (nextCharIndex < line.length()) {
327 char c = line.charAt(nextCharIndex);
328 // Check that the match is not just a substring of another element
329 if (Character.isLetterOrDigit(c)) {
330 return indexOfElement(elementName, line, nextCharIndex);
331 }
332 }
333 return index;
334 }
335
336 private void readElement(final StringBuffer buffer, final RandomAccessFile raFile) {
337 try {
338 int numRead = 0;
339 boolean startTagClosed = false;
340 int i;
341 while ((i = raFile.read()) != -1) {
342 numRead++;
343 final char c = (char) i;
344 buffer.append(c);
345 if (c == '"') {
346 readQuote(buffer, raFile, '"');
347 } else if (c == '\'') {
348 readQuote(buffer, raFile, '\'');
349 } else if (c == '<') {
350 readElement(buffer, raFile);
351 } else if (c == '/' && numRead == 1) {
352 break; // found "</"
353 } else if (c == '-' && numRead == 3 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("!-")) { //$NON-NLS-1$
354 readComment(buffer, raFile); // found "<!--"
355 } else if (i == '>') {
356 if (buffer.charAt(buffer.length() - 2) == '/') {
357 break; // found "/>"
358 } else if (startTagClosed) {
359 break; // found "<...>...</...>"
360 }
361 else {
362 startTagClosed = true; // found "<...>"
363 }
364 }
365 }
366 return;
367 } catch (final IOException e) {
368 return;
369 }
370 }
371
372 private static void readQuote(final StringBuffer buffer,
373 final RandomAccessFile raFile, final char eq) {
374 try {
375 int i;
376 while ((i = raFile.read()) != -1) {
377 final char c = (char) i;
378 buffer.append(c);
379 if (c == eq)
380 {
381 break; // found matching end-quote
382 }
383 }
384 return;
385 } catch (final IOException e) {
386 return;
387 }
388 }
389
390 private static void readComment(final StringBuffer buffer,
391 final RandomAccessFile raFile) {
392 try {
393 int numRead = 0;
394 int i;
395 while ((i = raFile.read()) != -1) {
396 numRead++;
397 final char c = (char) i;
398 buffer.append(c);
399 if (c == '>' && numRead >= 2 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("--")) //$NON-NLS-1$
400 {
401 break; // found "-->"
402 }
403 }
404 return;
405 } catch (final IOException e) {
406 return;
407 }
408 }
409
410 /**
411 * Parse an XML element.
412 *
413 * @param parentElement
414 * The parent element
415 * @param buffer
416 * The contents to parse
417 * @return The parsed content
418 */
419 public static StringBuffer parseElement(final Element parentElement, final StringBuffer buffer) {
420 final NodeList nodeList = parentElement.getChildNodes();
421 String separator = null;
422 for (int i = 0; i < nodeList.getLength(); i++) {
423 final Node node = nodeList.item(i);
424 if (node.getNodeType() == Node.ELEMENT_NODE) {
425 if (separator == null) {
426 separator = " | "; //$NON-NLS-1$
427 } else {
428 buffer.append(separator);
429 }
430 final Element element = (Element) node;
431 if (!element.hasChildNodes()) {
432 buffer.append(element.getNodeName());
433 } else if (element.getChildNodes().getLength() == 1 && element.getFirstChild().getNodeType() == Node.TEXT_NODE) {
434 buffer.append(element.getNodeName() + ":" + element.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
435 } else {
436 buffer.append(element.getNodeName());
437 buffer.append(" [ "); //$NON-NLS-1$
438 parseElement(element, buffer);
439 buffer.append(" ]"); //$NON-NLS-1$
440 }
441 } else if (node.getNodeType() == Node.TEXT_NODE) {
442 if (node.getNodeValue().trim().length() != 0) {
443 buffer.append(node.getNodeValue().trim());
444 }
445 }
446 }
447 return buffer;
448 }
449
450 /**
451 * Get an input element if it is a valid record input. If not, we will look
452 * into its children for valid inputs.
453 *
454 * @param inputElement
455 * The main element to check for.
456 * @return The record element
457 */
458 public InputElement getRecordInputElement(final InputElement inputElement) {
459 if (inputElement.logEntry) {
460 return inputElement;
461 } else if (inputElement.childElements != null) {
462 for (final InputElement childInputElement : inputElement.childElements) {
463 final InputElement recordInputElement = getRecordInputElement(childInputElement);
464 if (recordInputElement != null) {
465 return recordInputElement;
466 }
467 }
468 }
469 return null;
470 }
471
472 /**
473 * Extract a trace event from an XML element.
474 *
475 * @param element
476 * The element
477 * @param inputElement
478 * The input element
479 * @return The extracted event
480 */
481 public CustomXmlEvent extractEvent(final Element element, final InputElement inputElement) {
482 final CustomXmlEvent event = new CustomXmlEvent(fDefinition, this, TmfTimestamp.ZERO, "", fEventType, ""); //$NON-NLS-1$ //$NON-NLS-2$
483 event.setContent(new CustomEventContent(event, new StringBuffer()));
484 parseElement(element, event, inputElement);
485 return event;
486 }
487
488 private void parseElement(final Element element, final CustomXmlEvent event, final InputElement inputElement) {
489 if (inputElement.inputName != null && !inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_IGNORE)) {
490 event.parseInput(parseElement(element, new StringBuffer()).toString(), inputElement.inputName, inputElement.inputAction, inputElement.inputFormat);
491 }
492 if (inputElement.attributes != null) {
493 for (final InputAttribute attribute : inputElement.attributes) {
494 event.parseInput(element.getAttribute(attribute.attributeName), attribute.inputName, attribute.inputAction, attribute.inputFormat);
495 }
496 }
497 final NodeList childNodes = element.getChildNodes();
498 if (inputElement.childElements != null) {
499 for (int i = 0; i < childNodes.getLength(); i++) {
500 final Node node = childNodes.item(i);
501 if (node instanceof Element) {
502 for (final InputElement child : inputElement.childElements) {
503 if (node.getNodeName().equals(child.elementName)) {
504 parseElement((Element) node, event, child);
505 break;
506 }
507 }
508 }
509 }
510 }
511 return;
512 }
513
514 /**
515 * Retrieve the trace definition.
516 *
517 * @return The trace definition
518 */
519 public CustomTraceDefinition getDefinition() {
520 return fDefinition;
521 }
522
523 /**
524 * {@inheritDoc}
525 * <p>
526 * The default implementation sets the confidence to 100 if any of the first
527 * 100 lines of the file contains a valid record input element, and 0
528 * otherwise.
529 */
530 @Override
531 public IStatus validate(IProject project, String path) {
532 File file = new File(path);
533 if (!file.exists() || !file.isFile() || !file.canRead()) {
534 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CustomTrace_FileNotFound + ": " + path); //$NON-NLS-1$
535 }
536 try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
537 int lineCount = 0;
538 long rawPos = 0;
539 String line = rafile.getNextLine();
540 while ((line != null) && (lineCount++ < MAX_LINES)) {
541 final int idx = indexOfElement(fRecordInputElement.elementName, line, 0);
542 if (idx != -1) {
543 rafile.seek(rawPos + idx + 1); // +1 is for the <
544 final StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$
545 readElement(elementBuffer, rafile);
546 final Element element = parseElementBuffer(elementBuffer);
547 if (element != null) {
548 rafile.close();
549 return new TraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID);
550 }
551 }
552 rawPos = rafile.getFilePointer();
553 line = rafile.getNextLine();
554 }
555 } catch (IOException e) {
556 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "IOException validating file: " + path, e); //$NON-NLS-1$
557 }
558 return new TraceValidationStatus(0, Activator.PLUGIN_ID);
559 }
560
561 private static int fCheckpointSize = -1;
562
563 @Override
564 public synchronized int getCheckpointSize() {
565 if (fCheckpointSize == -1) {
566 TmfCheckpoint c = new TmfCheckpoint(TmfTimestamp.ZERO, new TmfLongLocation(0L), 0);
567 ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
568 b.clear();
569 c.serialize(b);
570 fCheckpointSize = b.position();
571 }
572
573 return fCheckpointSize;
574 }
575
576 @Override
577 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
578 return new TmfLongLocation(bufferIn);
579 }
580
581 @Override
582 protected ITmfTraceIndexer createIndexer(int interval) {
583 return new TmfBTreeTraceIndexer(this, interval);
584 }
585 }
This page took 0.043576 seconds and 5 git commands to generate.