1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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((Long
) null);
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 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
152 long rawPos
= fFile
.getFilePointer();
153 String line
= fFile
.getNextLine();
154 while (line
!= null) {
155 final int idx
= line
.indexOf(recordElementStart
);
157 context
.setLocation(new TmfLongLocation(rawPos
+ idx
));
160 rawPos
= fFile
.getFilePointer();
161 line
= fFile
.getNextLine();
164 } catch (final IOException e
) {
165 Activator
.logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
172 public synchronized TmfContext
seekEvent(final double ratio
) {
174 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
177 long pos
= Math
.round(ratio
* fFile
.length());
180 if (fFile
.read() == '\n') {
185 final ITmfLocation location
= new TmfLongLocation(pos
);
186 final TmfContext context
= seekEvent(location
);
187 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
189 } catch (final IOException e
) {
190 Activator
.logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
191 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
196 public synchronized double getLocationRatio(final ITmfLocation location
) {
201 if (location
.getLocationInfo() instanceof Long
) {
202 return ((Long
) location
.getLocationInfo()).doubleValue() / fFile
.length();
204 } catch (final IOException e
) {
205 Activator
.logError("Error getting location ration. File: " + getPath(), e
); //$NON-NLS-1$
211 public ITmfLocation
getCurrentLocation() {
212 // TODO Auto-generated method stub
217 public synchronized CustomXmlEvent
parseEvent(final ITmfContext tmfContext
) {
218 ITmfContext context
= seekEvent(tmfContext
.getLocation());
219 return parse(context
);
223 public synchronized CustomXmlEvent
getNext(final ITmfContext context
) {
224 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
225 final CustomXmlEvent event
= parse(context
);
227 updateAttributes(savedContext
, event
.getTimestamp());
228 context
.increaseRank();
233 private synchronized CustomXmlEvent
parse(final ITmfContext tmfContext
) {
237 if (!(tmfContext
instanceof CustomXmlTraceContext
)) {
241 final CustomXmlTraceContext context
= (CustomXmlTraceContext
) tmfContext
;
242 if (context
.getLocation() == null || !(context
.getLocation().getLocationInfo() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
246 CustomXmlEvent event
= null;
248 if (fFile
.getFilePointer() != (Long
) context
.getLocation().getLocationInfo() + 1)
250 fFile
.seek((Long
) context
.getLocation().getLocationInfo() + 1); // +1 is for the <
252 final StringBuffer elementBuffer
= new StringBuffer("<"); //$NON-NLS-1$
253 readElement(elementBuffer
, fFile
);
254 final Element element
= parseElementBuffer(elementBuffer
);
256 event
= extractEvent(element
, fRecordInputElement
);
257 ((StringBuffer
) event
.getContent().getValue()).append(elementBuffer
);
260 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
261 long rawPos
= fFile
.getFilePointer();
262 String line
= fFile
.getNextLine();
263 while (line
!= null) {
264 final int idx
= line
.indexOf(recordElementStart
);
266 context
.setLocation(new TmfLongLocation(rawPos
+ idx
));
269 rawPos
= fFile
.getFilePointer();
270 line
= fFile
.getNextLine();
272 } catch (final IOException e
) {
273 Activator
.logError("Error parsing event. File: " + getPath(), e
); //$NON-NLS-1$
276 context
.setLocation(NULL_LOCATION
);
280 private Element
parseElementBuffer(final StringBuffer elementBuffer
) {
282 final DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
283 final DocumentBuilder db
= dbf
.newDocumentBuilder();
285 // The following allows xml parsing without access to the dtd
286 final EntityResolver resolver
= new EntityResolver() {
288 public InputSource
resolveEntity(final String publicId
, final String systemId
) {
289 final String empty
= ""; //$NON-NLS-1$
290 final ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
291 return new InputSource(bais
);
294 db
.setEntityResolver(resolver
);
296 // The following catches xml parsing exceptions
297 db
.setErrorHandler(new ErrorHandler() {
299 public void error(final SAXParseException saxparseexception
) throws SAXException
{}
302 public void warning(final SAXParseException saxparseexception
) throws SAXException
{}
305 public void fatalError(final SAXParseException saxparseexception
) throws SAXException
{
306 throw saxparseexception
;
310 final Document doc
= db
.parse(new ByteArrayInputStream(elementBuffer
.toString().getBytes()));
311 return doc
.getDocumentElement();
312 } catch (final ParserConfigurationException e
) {
313 Activator
.logError("Error parsing element buffer. File:" + getPath(), e
); //$NON-NLS-1$
314 } catch (final SAXException e
) {
315 Activator
.logError("Error parsing element buffer. File:" + getPath(), e
); //$NON-NLS-1$
316 } catch (final IOException e
) {
317 Activator
.logError("Error parsing element buffer. File: " + getPath(), e
); //$NON-NLS-1$
322 private void readElement(final StringBuffer buffer
, final RandomAccessFile raFile
) {
325 boolean startTagClosed
= false;
327 while ((i
= raFile
.read()) != -1) {
329 final char c
= (char) i
;
332 readQuote(buffer
, raFile
, '"');
333 } else if (c
== '\'') {
334 readQuote(buffer
, raFile
, '\'');
335 } else if (c
== '<') {
336 readElement(buffer
, raFile
);
337 } else if (c
== '/' && numRead
== 1) {
339 } else if (c
== '-' && numRead
== 3 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("!-")) { //$NON-NLS-1$
340 readComment(buffer
, raFile
); // found "<!--"
341 } else if (i
== '>') {
342 if (buffer
.charAt(buffer
.length() - 2) == '/') {
344 } else if (startTagClosed
) {
345 break; // found "<...>...</...>"
348 startTagClosed
= true; // found "<...>"
353 } catch (final IOException e
) {
358 private static void readQuote(final StringBuffer buffer
,
359 final RandomAccessFile raFile
, final char eq
) {
362 while ((i
= raFile
.read()) != -1) {
363 final char c
= (char) i
;
367 break; // found matching end-quote
371 } catch (final IOException e
) {
376 private static void readComment(final StringBuffer buffer
,
377 final RandomAccessFile raFile
) {
381 while ((i
= raFile
.read()) != -1) {
383 final char c
= (char) i
;
385 if (c
== '>' && numRead
>= 2 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("--")) //$NON-NLS-1$
387 break; // found "-->"
391 } catch (final IOException e
) {
397 * Parse an XML element.
399 * @param parentElement
402 * The contents to parse
403 * @return The parsed content
405 public static StringBuffer
parseElement(final Element parentElement
, final StringBuffer buffer
) {
406 final NodeList nodeList
= parentElement
.getChildNodes();
407 String separator
= null;
408 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
409 final Node node
= nodeList
.item(i
);
410 if (node
.getNodeType() == Node
.ELEMENT_NODE
) {
411 if (separator
== null) {
412 separator
= " | "; //$NON-NLS-1$
414 buffer
.append(separator
);
416 final Element element
= (Element
) node
;
417 if (!element
.hasChildNodes()) {
418 buffer
.append(element
.getNodeName());
419 } else if (element
.getChildNodes().getLength() == 1 && element
.getFirstChild().getNodeType() == Node
.TEXT_NODE
) {
420 buffer
.append(element
.getNodeName() + ":" + element
.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
422 buffer
.append(element
.getNodeName());
423 buffer
.append(" [ "); //$NON-NLS-1$
424 parseElement(element
, buffer
);
425 buffer
.append(" ]"); //$NON-NLS-1$
427 } else if (node
.getNodeType() == Node
.TEXT_NODE
) {
428 if (node
.getNodeValue().trim().length() != 0) {
429 buffer
.append(node
.getNodeValue().trim());
437 * Get an input element if it is a valid record input. If not, we will look
438 * into its children for valid inputs.
440 * @param inputElement
441 * The main element to check for.
442 * @return The record element
444 public InputElement
getRecordInputElement(final InputElement inputElement
) {
445 if (inputElement
.logEntry
) {
447 } else if (inputElement
.childElements
!= null) {
448 for (final InputElement childInputElement
: inputElement
.childElements
) {
449 final InputElement recordInputElement
= getRecordInputElement(childInputElement
);
450 if (recordInputElement
!= null) {
451 return recordInputElement
;
459 * Extract a trace event from an XML element.
463 * @param inputElement
465 * @return The extracted event
467 public CustomXmlEvent
extractEvent(final Element element
, final InputElement inputElement
) {
468 final CustomXmlEvent event
= new CustomXmlEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
469 event
.setContent(new CustomEventContent(event
, new StringBuffer()));
470 parseElement(element
, event
, inputElement
);
474 private void parseElement(final Element element
, final CustomXmlEvent event
, final InputElement inputElement
) {
475 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
476 event
.parseInput(parseElement(element
, new StringBuffer()).toString(), inputElement
.inputName
, inputElement
.inputAction
, inputElement
.inputFormat
);
478 if (inputElement
.attributes
!= null) {
479 for (final InputAttribute attribute
: inputElement
.attributes
) {
480 event
.parseInput(element
.getAttribute(attribute
.attributeName
), attribute
.inputName
, attribute
.inputAction
, attribute
.inputFormat
);
483 final NodeList childNodes
= element
.getChildNodes();
484 if (inputElement
.childElements
!= null) {
485 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
486 final Node node
= childNodes
.item(i
);
487 if (node
instanceof Element
) {
488 for (final InputElement child
: inputElement
.childElements
) {
489 if (node
.getNodeName().equals(child
.elementName
)) {
490 parseElement((Element
) node
, event
, child
);
501 * Retrieve the trace definition.
503 * @return The trace definition
505 public CustomTraceDefinition
getDefinition() {
512 * The default implementation sets the confidence to 100 if any of the first
513 * 100 lines of the file contains a valid record input element, and 0
517 public IStatus
validate(IProject project
, String path
) {
518 File file
= new File(path
);
519 if (!file
.exists() || !file
.isFile() || !file
.canRead()) {
520 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CustomTrace_FileNotFound
+ ": " + path
); //$NON-NLS-1$
522 try (BufferedRandomAccessFile rafile
= new BufferedRandomAccessFile(path
, "r")) { //$NON-NLS-1$
524 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
526 String line
= rafile
.getNextLine();
527 while ((line
!= null) && (lineCount
++ < MAX_LINES
)) {
528 final int idx
= line
.indexOf(recordElementStart
);
530 rafile
.seek(rawPos
+ idx
+ 1); // +1 is for the <
531 final StringBuffer elementBuffer
= new StringBuffer("<"); //$NON-NLS-1$
532 readElement(elementBuffer
, rafile
);
533 final Element element
= parseElementBuffer(elementBuffer
);
534 if (element
!= null) {
536 return new TraceValidationStatus(CONFIDENCE
, Activator
.PLUGIN_ID
);
539 rawPos
= rafile
.getFilePointer();
540 line
= rafile
.getNextLine();
542 } catch (IOException e
) {
543 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "IOException validating file: " + path
, e
); //$NON-NLS-1$
545 return new TraceValidationStatus(0, Activator
.PLUGIN_ID
);
548 private static int fCheckpointSize
= -1;
551 public synchronized int getCheckpointSize() {
552 if (fCheckpointSize
== -1) {
553 TmfCheckpoint c
= new TmfCheckpoint(TmfTimestamp
.ZERO
, new TmfLongLocation(0L), 0);
554 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
557 fCheckpointSize
= b
.position();
560 return fCheckpointSize
;
564 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
565 return new TmfLongLocation(bufferIn
);
569 protected ITmfTraceIndexer
createIndexer(int interval
) {
570 return new TmfBTreeTraceIndexer(this, interval
);