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
.internal
.tmf
.ui
.parsers
.custom
;
15 import java
.io
.ByteArrayInputStream
;
17 import java
.io
.FileInputStream
;
18 import java
.io
.FileNotFoundException
;
19 import java
.io
.IOException
;
20 import java
.io
.RandomAccessFile
;
21 import java
.nio
.ByteBuffer
;
23 import javax
.xml
.parsers
.DocumentBuilder
;
24 import javax
.xml
.parsers
.DocumentBuilderFactory
;
25 import javax
.xml
.parsers
.ParserConfigurationException
;
27 import org
.eclipse
.core
.resources
.IProject
;
28 import org
.eclipse
.core
.resources
.IResource
;
29 import org
.eclipse
.core
.runtime
.IStatus
;
30 import org
.eclipse
.core
.runtime
.Status
;
31 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
32 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
.InputAttribute
;
33 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
.InputElement
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.ITmfPersistentlyIndexable
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
46 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpoint
;
47 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.ITmfLocation
;
48 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.TmfLongLocation
;
49 import org
.w3c
.dom
.Document
;
50 import org
.w3c
.dom
.Element
;
51 import org
.w3c
.dom
.Node
;
52 import org
.w3c
.dom
.NodeList
;
53 import org
.xml
.sax
.EntityResolver
;
54 import org
.xml
.sax
.ErrorHandler
;
55 import org
.xml
.sax
.InputSource
;
56 import org
.xml
.sax
.SAXException
;
57 import org
.xml
.sax
.SAXParseException
;
60 * Trace object for custom XML trace parsers.
62 * @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;
69 private final CustomXmlTraceDefinition fDefinition
;
70 private final CustomXmlEventType fEventType
;
71 private final InputElement fRecordInputElement
;
72 private BufferedRandomAccessFile fFile
;
80 public CustomXmlTrace(final CustomXmlTraceDefinition definition
) {
81 fDefinition
= definition
;
82 fEventType
= new CustomXmlEventType(fDefinition
);
83 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
84 setCacheSize(DEFAULT_CACHE_SIZE
);
95 * Path to the trace/log file
98 * @throws TmfTraceException
99 * If the trace/log couldn't be opened
101 public CustomXmlTrace(final IResource resource
,
102 final CustomXmlTraceDefinition definition
, final String path
,
103 final int pageSize
) throws TmfTraceException
{
105 setCacheSize((pageSize
> 0) ? pageSize
: DEFAULT_CACHE_SIZE
);
106 initTrace(resource
, path
, CustomXmlEvent
.class);
110 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
) throws TmfTraceException
{
111 super.initTrace(resource
, path
, eventType
);
113 fFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
114 } catch (IOException e
) {
115 throw new TmfTraceException(e
.getMessage(), e
);
120 public synchronized void dispose() {
125 } catch (IOException e
) {
133 public ITmfTraceIndexer
getIndexer() {
134 return super.getIndexer();
138 public synchronized TmfContext
seekEvent(final ITmfLocation location
) {
139 final CustomXmlTraceContext context
= new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
140 if (NULL_LOCATION
.equals(location
) || fFile
== null) {
144 if (location
== null) {
146 } else if (location
.getLocationInfo() instanceof Long
) {
147 fFile
.seek((Long
) location
.getLocationInfo());
150 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
151 long rawPos
= fFile
.getFilePointer();
153 while ((line
= fFile
.getNextLine()) != null) {
154 final int idx
= line
.indexOf(recordElementStart
);
156 context
.setLocation(new TmfLongLocation(rawPos
+ idx
));
159 rawPos
= fFile
.getFilePointer();
162 } catch (final IOException e
) {
163 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
170 public synchronized TmfContext
seekEvent(final double ratio
) {
172 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
175 long pos
= Math
.round(ratio
* fFile
.length());
178 if (fFile
.read() == '\n') {
183 final ITmfLocation location
= new TmfLongLocation(pos
);
184 final TmfContext context
= seekEvent(location
);
185 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
187 } catch (final IOException e
) {
188 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
189 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
194 public synchronized double getLocationRatio(final ITmfLocation location
) {
199 if (location
.getLocationInfo() instanceof Long
) {
200 return (double) ((Long
) location
.getLocationInfo()) / fFile
.length();
202 } catch (final IOException e
) {
203 Activator
.getDefault().logError("Error getting location ration. File: " + getPath(), e
); //$NON-NLS-1$
209 public ITmfLocation
getCurrentLocation() {
210 // TODO Auto-generated method stub
215 public synchronized CustomXmlEvent
parseEvent(final ITmfContext tmfContext
) {
216 ITmfContext context
= seekEvent(tmfContext
.getLocation());
217 return parse(context
);
221 public synchronized CustomXmlEvent
getNext(final ITmfContext context
) {
222 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
223 final CustomXmlEvent event
= parse(context
);
225 updateAttributes(savedContext
, event
.getTimestamp());
226 context
.increaseRank();
231 private synchronized CustomXmlEvent
parse(final ITmfContext tmfContext
) {
235 if (!(tmfContext
instanceof CustomXmlTraceContext
)) {
239 final CustomXmlTraceContext context
= (CustomXmlTraceContext
) tmfContext
;
240 if (context
.getLocation() == null || !(context
.getLocation().getLocationInfo() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
244 CustomXmlEvent event
= null;
246 if (fFile
.getFilePointer() != (Long
) context
.getLocation().getLocationInfo() + 1)
248 fFile
.seek((Long
) context
.getLocation().getLocationInfo() + 1); // +1 is for the <
250 final StringBuffer elementBuffer
= new StringBuffer("<"); //$NON-NLS-1$
251 readElement(elementBuffer
, fFile
);
252 final Element element
= parseElementBuffer(elementBuffer
);
254 event
= extractEvent(element
, fRecordInputElement
);
255 ((StringBuffer
) event
.getContent().getValue()).append(elementBuffer
);
258 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
259 long rawPos
= fFile
.getFilePointer();
261 while ((line
= fFile
.getNextLine()) != null) {
262 final int idx
= line
.indexOf(recordElementStart
);
264 context
.setLocation(new TmfLongLocation(rawPos
+ idx
));
267 rawPos
= fFile
.getFilePointer();
269 } catch (final IOException e
) {
270 Activator
.getDefault().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
.getDefault().logError("Error parsing element buffer. File:" + getPath(), e
); //$NON-NLS-1$
311 } catch (final SAXException e
) {
312 Activator
.getDefault().logError("Error parsing element buffer. File:" + getPath(), e
); //$NON-NLS-1$
313 } catch (final IOException e
) {
314 Activator
.getDefault().logError("Error parsing element buffer. File: " + getPath(), e
); //$NON-NLS-1$
319 private void readElement(final StringBuffer buffer
, final RandomAccessFile raFile
) {
322 boolean startTagClosed
= false;
324 while ((i
= raFile
.read()) != -1) {
326 final char c
= (char) i
;
329 readQuote(buffer
, raFile
, '"');
330 } else if (c
== '\'') {
331 readQuote(buffer
, raFile
, '\'');
332 } else if (c
== '<') {
333 readElement(buffer
, raFile
);
334 } else if (c
== '/' && numRead
== 1) {
336 } else if (c
== '-' && numRead
== 3 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("!-")) { //$NON-NLS-1$
337 readComment(buffer
, raFile
); // found "<!--"
338 } else if (i
== '>') {
339 if (buffer
.charAt(buffer
.length() - 2) == '/') {
341 } else if (startTagClosed
) {
342 break; // found "<...>...</...>"
345 startTagClosed
= true; // found "<...>"
350 } catch (final IOException e
) {
355 private static void readQuote(final StringBuffer buffer
,
356 final RandomAccessFile raFile
, final char eq
) {
359 while ((i
= raFile
.read()) != -1) {
360 final char c
= (char) i
;
364 break; // found matching end-quote
368 } catch (final IOException e
) {
373 private static void readComment(final StringBuffer buffer
,
374 final RandomAccessFile raFile
) {
378 while ((i
= raFile
.read()) != -1) {
380 final char c
= (char) i
;
382 if (c
== '>' && numRead
>= 2 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("--")) //$NON-NLS-1$
384 break; // found "-->"
388 } catch (final IOException e
) {
394 * Parse an XML element.
396 * @param parentElement
399 * The contents to parse
400 * @return The parsed content
402 public static StringBuffer
parseElement(final Element parentElement
, final StringBuffer buffer
) {
403 final NodeList nodeList
= parentElement
.getChildNodes();
404 String separator
= null;
405 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
406 final Node node
= nodeList
.item(i
);
407 if (node
.getNodeType() == Node
.ELEMENT_NODE
) {
408 if (separator
== null) {
409 separator
= " | "; //$NON-NLS-1$
411 buffer
.append(separator
);
413 final Element element
= (Element
) node
;
414 if (!element
.hasChildNodes()) {
415 buffer
.append(element
.getNodeName());
416 } else if (element
.getChildNodes().getLength() == 1 && element
.getFirstChild().getNodeType() == Node
.TEXT_NODE
) {
417 buffer
.append(element
.getNodeName() + ":" + element
.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
419 buffer
.append(element
.getNodeName());
420 buffer
.append(" [ "); //$NON-NLS-1$
421 parseElement(element
, buffer
);
422 buffer
.append(" ]"); //$NON-NLS-1$
424 } else if (node
.getNodeType() == Node
.TEXT_NODE
) {
425 if (node
.getNodeValue().trim().length() != 0) {
426 buffer
.append(node
.getNodeValue().trim());
434 * Get an input element if it is a valid record input. If not, we will look
435 * into its children for valid inputs.
437 * @param inputElement
438 * The main element to check for.
439 * @return The record element
441 public InputElement
getRecordInputElement(final InputElement inputElement
) {
442 if (inputElement
.logEntry
) {
444 } else if (inputElement
.childElements
!= null) {
445 for (final InputElement childInputElement
: inputElement
.childElements
) {
446 final InputElement recordInputElement
= getRecordInputElement(childInputElement
);
447 if (recordInputElement
!= null) {
448 return recordInputElement
;
456 * Extract a trace event from an XML element.
460 * @param inputElement
462 * @return The extracted event
464 public CustomXmlEvent
extractEvent(final Element element
, final InputElement inputElement
) {
465 final CustomXmlEvent event
= new CustomXmlEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
466 event
.setContent(new CustomEventContent(event
, new StringBuffer()));
467 parseElement(element
, event
, inputElement
);
471 private void parseElement(final Element element
, final CustomXmlEvent event
, final InputElement inputElement
) {
472 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
473 event
.parseInput(parseElement(element
, new StringBuffer()).toString(), inputElement
.inputName
, inputElement
.inputAction
, inputElement
.inputFormat
);
475 if (inputElement
.attributes
!= null) {
476 for (final InputAttribute attribute
: inputElement
.attributes
) {
477 event
.parseInput(element
.getAttribute(attribute
.attributeName
), attribute
.inputName
, attribute
.inputAction
, attribute
.inputFormat
);
480 final NodeList childNodes
= element
.getChildNodes();
481 if (inputElement
.childElements
!= null) {
482 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
483 final Node node
= childNodes
.item(i
);
484 if (node
instanceof Element
) {
485 for (final InputElement child
: inputElement
.childElements
) {
486 if (node
.getNodeName().equals(child
.elementName
)) {
487 parseElement((Element
) node
, event
, child
);
498 * Retrieve the trace definition.
500 * @return The trace definition
502 public CustomTraceDefinition
getDefinition() {
507 public IStatus
validate(IProject project
, String path
) {
508 File xmlFile
= new File(path
);
509 if (xmlFile
.exists() && xmlFile
.isFile() && xmlFile
.canRead() && xmlFile
.length() > 0) {
510 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
513 db
= dbf
.newDocumentBuilder();
515 // The following allows xml parsing without access to the dtd
516 EntityResolver resolver
= new EntityResolver() {
518 public InputSource
resolveEntity(String publicId
, String systemId
) {
519 return new InputSource(new ByteArrayInputStream(new byte[0]));
522 db
.setEntityResolver(resolver
);
524 // The following catches xml parsing exceptions
525 db
.setErrorHandler(new ErrorHandler() {
527 public void error(SAXParseException saxparseexception
) throws SAXException
{
531 public void warning(SAXParseException saxparseexception
) throws SAXException
{
535 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
536 throw saxparseexception
;
539 db
.parse(new FileInputStream(xmlFile
));
540 return Status
.OK_STATUS
;
541 } catch (ParserConfigurationException e
) {
542 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, e
.getMessage(), e
);
543 } catch (FileNotFoundException e
) {
544 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, e
.getMessage(), e
);
545 } catch (SAXException e
) {
546 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, e
.getMessage(), e
);
547 } catch (IOException e
) {
548 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, e
.getMessage(), e
);
551 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CustomTrace_FileNotFound
+ ": " + path
); //$NON-NLS-1$
554 private static int fCheckpointSize
= -1;
557 public synchronized int getCheckpointSize() {
558 if (fCheckpointSize
== -1) {
559 TmfCheckpoint c
= new TmfCheckpoint(TmfTimestamp
.ZERO
, new TmfLongLocation(0L), 0);
560 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
563 fCheckpointSize
= b
.position();
566 return fCheckpointSize
;
570 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
571 return new TmfLongLocation(bufferIn
);
575 protected ITmfTraceIndexer
createIndexer(int interval
) {
576 return new TmfBTreeTraceIndexer(this, interval
);