1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
;
15 import java
.io
.ByteArrayInputStream
;
17 import java
.io
.IOException
;
18 import java
.io
.RandomAccessFile
;
19 import java
.nio
.ByteBuffer
;
21 import javax
.xml
.parsers
.DocumentBuilder
;
22 import javax
.xml
.parsers
.DocumentBuilderFactory
;
23 import javax
.xml
.parsers
.ParserConfigurationException
;
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
;
59 * Trace object for custom XML trace parsers.
61 * @author Patrick Tassé
64 public class CustomXmlTrace
extends TmfTrace
implements ITmfEventParser
, ITmfPersistentlyIndexable
{
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;
71 private final CustomXmlTraceDefinition fDefinition
;
72 private final CustomXmlEventType fEventType
;
73 private final InputElement fRecordInputElement
;
74 private BufferedRandomAccessFile fFile
;
82 public CustomXmlTrace(final CustomXmlTraceDefinition definition
) {
83 fDefinition
= definition
;
84 fEventType
= new CustomXmlEventType(fDefinition
);
85 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
86 setCacheSize(DEFAULT_CACHE_SIZE
);
97 * Path to the trace/log file
100 * @throws TmfTraceException
101 * If the trace/log couldn't be opened
103 public CustomXmlTrace(final IResource resource
,
104 final CustomXmlTraceDefinition definition
, final String path
,
105 final int pageSize
) throws TmfTraceException
{
107 setCacheSize((pageSize
> 0) ? pageSize
: DEFAULT_CACHE_SIZE
);
108 initTrace(resource
, path
, CustomXmlEvent
.class);
112 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
) throws TmfTraceException
{
113 super.initTrace(resource
, path
, eventType
);
115 fFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
116 } catch (IOException e
) {
117 throw new TmfTraceException(e
.getMessage(), e
);
122 public synchronized void dispose() {
127 } catch (IOException e
) {
135 public ITmfTraceIndexer
getIndexer() {
136 return super.getIndexer();
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) {
146 if (location
== null) {
148 } else if (location
.getLocationInfo() instanceof Long
) {
149 fFile
.seek((Long
) location
.getLocationInfo());
151 long rawPos
= fFile
.getFilePointer();
152 String line
= fFile
.getNextLine();
153 while (line
!= null) {
154 final int idx
= indexOfElement(fRecordInputElement
.elementName
, line
, 0);
156 context
.setLocation(new TmfLongLocation(rawPos
+ idx
));
159 rawPos
= fFile
.getFilePointer();
160 line
= fFile
.getNextLine();
163 } catch (final IOException e
) {
164 Activator
.logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
171 public synchronized TmfContext
seekEvent(final double ratio
) {
173 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
176 long pos
= Math
.round(ratio
* fFile
.length());
179 if (fFile
.read() == '\n') {
184 final ITmfLocation location
= new TmfLongLocation(pos
);
185 final TmfContext context
= seekEvent(location
);
186 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
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
);
195 public synchronized double getLocationRatio(final ITmfLocation location
) {
200 if (location
.getLocationInfo() instanceof Long
) {
201 return ((Long
) location
.getLocationInfo()).doubleValue() / fFile
.length();
203 } catch (final IOException e
) {
204 Activator
.logError("Error getting location ration. File: " + getPath(), e
); //$NON-NLS-1$
210 public ITmfLocation
getCurrentLocation() {
211 // TODO Auto-generated method stub
216 public synchronized CustomXmlEvent
parseEvent(final ITmfContext tmfContext
) {
217 ITmfContext context
= seekEvent(tmfContext
.getLocation());
218 return parse(context
);
222 public synchronized CustomXmlEvent
getNext(final ITmfContext context
) {
223 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
224 final CustomXmlEvent event
= parse(context
);
226 updateAttributes(savedContext
, event
.getTimestamp());
227 context
.increaseRank();
232 private synchronized CustomXmlEvent
parse(final ITmfContext tmfContext
) {
236 if (!(tmfContext
instanceof CustomXmlTraceContext
)) {
240 final CustomXmlTraceContext context
= (CustomXmlTraceContext
) tmfContext
;
241 if (context
.getLocation() == null || !(context
.getLocation().getLocationInfo() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
245 CustomXmlEvent event
= null;
247 if (fFile
.getFilePointer() != (Long
) context
.getLocation().getLocationInfo() + 1)
249 fFile
.seek((Long
) context
.getLocation().getLocationInfo() + 1); // +1 is for the <
251 final StringBuffer elementBuffer
= new StringBuffer("<"); //$NON-NLS-1$
252 readElement(elementBuffer
, fFile
);
253 final Element element
= parseElementBuffer(elementBuffer
);
255 event
= extractEvent(element
, fRecordInputElement
);
256 ((StringBuffer
) event
.getContent().getValue()).append(elementBuffer
);
258 long rawPos
= fFile
.getFilePointer();
259 String line
= fFile
.getNextLine();
260 while (line
!= null) {
261 final int idx
= indexOfElement(fRecordInputElement
.elementName
, line
, 0);
263 context
.setLocation(new TmfLongLocation(rawPos
+ idx
));
266 rawPos
= fFile
.getFilePointer();
267 line
= fFile
.getNextLine();
269 } catch (final IOException e
) {
270 Activator
.logError("Error parsing event. File: " + getPath(), e
); //$NON-NLS-1$
273 context
.setLocation(NULL_LOCATION
);
277 private Element
parseElementBuffer(final StringBuffer elementBuffer
) {
279 final DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
280 final DocumentBuilder db
= dbf
.newDocumentBuilder();
282 // The following allows xml parsing without access to the dtd
283 final EntityResolver resolver
= new EntityResolver() {
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
);
291 db
.setEntityResolver(resolver
);
293 // The following catches xml parsing exceptions
294 db
.setErrorHandler(new ErrorHandler() {
296 public void error(final SAXParseException saxparseexception
) throws SAXException
{}
299 public void warning(final SAXParseException saxparseexception
) throws SAXException
{}
302 public void fatalError(final SAXParseException saxparseexception
) throws SAXException
{
303 throw saxparseexception
;
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$
319 private static int indexOfElement(String elementName
, String line
, int fromIndex
) {
320 final String recordElementStart
= '<' + elementName
;
321 int index
= line
.indexOf(recordElementStart
, fromIndex
);
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
);
336 private void readElement(final StringBuffer buffer
, final RandomAccessFile raFile
) {
339 boolean startTagClosed
= false;
341 while ((i
= raFile
.read()) != -1) {
343 final char c
= (char) i
;
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) {
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) == '/') {
358 } else if (startTagClosed
) {
359 break; // found "<...>...</...>"
362 startTagClosed
= true; // found "<...>"
367 } catch (final IOException e
) {
372 private static void readQuote(final StringBuffer buffer
,
373 final RandomAccessFile raFile
, final char eq
) {
376 while ((i
= raFile
.read()) != -1) {
377 final char c
= (char) i
;
381 break; // found matching end-quote
385 } catch (final IOException e
) {
390 private static void readComment(final StringBuffer buffer
,
391 final RandomAccessFile raFile
) {
395 while ((i
= raFile
.read()) != -1) {
397 final char c
= (char) i
;
399 if (c
== '>' && numRead
>= 2 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("--")) //$NON-NLS-1$
401 break; // found "-->"
405 } catch (final IOException e
) {
411 * Parse an XML element.
413 * @param parentElement
416 * The contents to parse
417 * @return The parsed content
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$
428 buffer
.append(separator
);
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$
436 buffer
.append(element
.getNodeName());
437 buffer
.append(" [ "); //$NON-NLS-1$
438 parseElement(element
, buffer
);
439 buffer
.append(" ]"); //$NON-NLS-1$
441 } else if (node
.getNodeType() == Node
.TEXT_NODE
) {
442 if (node
.getNodeValue().trim().length() != 0) {
443 buffer
.append(node
.getNodeValue().trim());
451 * Get an input element if it is a valid record input. If not, we will look
452 * into its children for valid inputs.
454 * @param inputElement
455 * The main element to check for.
456 * @return The record element
458 public InputElement
getRecordInputElement(final InputElement inputElement
) {
459 if (inputElement
.logEntry
) {
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
;
473 * Extract a trace event from an XML element.
477 * @param inputElement
479 * @return The extracted event
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
);
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
);
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
);
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
);
515 * Retrieve the trace definition.
517 * @return The trace definition
519 public CustomTraceDefinition
getDefinition() {
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
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$
536 try (BufferedRandomAccessFile rafile
= new BufferedRandomAccessFile(path
, "r")) { //$NON-NLS-1$
539 String line
= rafile
.getNextLine();
540 while ((line
!= null) && (lineCount
++ < MAX_LINES
)) {
541 final int idx
= indexOfElement(fRecordInputElement
.elementName
, line
, 0);
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) {
549 return new TraceValidationStatus(CONFIDENCE
, Activator
.PLUGIN_ID
);
552 rawPos
= rafile
.getFilePointer();
553 line
= rafile
.getNextLine();
555 } catch (IOException e
) {
556 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "IOException validating file: " + path
, e
); //$NON-NLS-1$
558 return new TraceValidationStatus(0, Activator
.PLUGIN_ID
);
561 private static int fCheckpointSize
= -1;
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
);
570 fCheckpointSize
= b
.position();
573 return fCheckpointSize
;
577 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
578 return new TmfLongLocation(bufferIn
);
582 protected ITmfTraceIndexer
createIndexer(int interval
) {
583 return new TmfBTreeTraceIndexer(this, interval
);