1 /*******************************************************************************
2 * Copyright (c) 2010 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
;
16 import java
.io
.IOException
;
17 import java
.io
.RandomAccessFile
;
19 import javax
.xml
.parsers
.DocumentBuilder
;
20 import javax
.xml
.parsers
.DocumentBuilderFactory
;
21 import javax
.xml
.parsers
.ParserConfigurationException
;
23 import org
.eclipse
.core
.resources
.IProject
;
24 import org
.eclipse
.core
.resources
.IResource
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
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
.ITmfEvent
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTraceIndexer
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLongLocation
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
39 import org
.w3c
.dom
.Document
;
40 import org
.w3c
.dom
.Element
;
41 import org
.w3c
.dom
.Node
;
42 import org
.w3c
.dom
.NodeList
;
43 import org
.xml
.sax
.EntityResolver
;
44 import org
.xml
.sax
.ErrorHandler
;
45 import org
.xml
.sax
.InputSource
;
46 import org
.xml
.sax
.SAXException
;
47 import org
.xml
.sax
.SAXParseException
;
50 * Trace object for custom XML trace parsers.
52 * @author Patrick Tassé
54 public class CustomXmlTrace
extends TmfTrace
implements ITmfEventParser
{
56 private static final TmfLongLocation NULL_LOCATION
= new TmfLongLocation((Long
) null);
57 private static final int DEFAULT_CACHE_SIZE
= 100;
59 private final CustomXmlTraceDefinition fDefinition
;
60 private final CustomXmlEventType fEventType
;
61 private final InputElement fRecordInputElement
;
62 private BufferedRandomAccessFile fFile
;
67 * @param definition Trace definition
69 public CustomXmlTrace(final CustomXmlTraceDefinition definition
) {
70 fDefinition
= definition
;
71 fEventType
= new CustomXmlEventType(fDefinition
);
72 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
73 setCacheSize(DEFAULT_CACHE_SIZE
);
84 * Path to the trace/log file
87 * @throws TmfTraceException
88 * If the trace/log couldn't be opened
90 public CustomXmlTrace(final IResource resource
,
91 final CustomXmlTraceDefinition definition
, final String path
,
92 final int pageSize
) throws TmfTraceException
{
94 setCacheSize((pageSize
> 0) ? pageSize
: DEFAULT_CACHE_SIZE
);
95 initTrace(resource
, path
, CustomXmlEvent
.class);
99 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
) throws TmfTraceException
{
100 super.initTrace(resource
, path
, eventType
);
102 fFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
103 } catch (IOException e
) {
104 throw new TmfTraceException(e
.getMessage(), e
);
109 public synchronized void dispose() {
114 } catch (IOException e
) {
122 public ITmfTraceIndexer
getIndexer() {
123 return super.getIndexer();
127 public synchronized TmfContext
seekEvent(final ITmfLocation location
) {
128 final CustomXmlTraceContext context
= new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
129 if (NULL_LOCATION
.equals(location
) || fFile
== null) {
133 if (location
== null) {
135 } else if (location
.getLocationInfo() instanceof Long
) {
136 fFile
.seek((Long
) location
.getLocationInfo());
139 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
140 long rawPos
= fFile
.getFilePointer();
142 while ((line
= fFile
.getNextLine()) != null) {
143 final int idx
= line
.indexOf(recordElementStart
);
145 context
.setLocation(new TmfLongLocation(rawPos
+ idx
));
148 rawPos
= fFile
.getFilePointer();
151 } catch (final IOException e
) {
152 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
159 public synchronized TmfContext
seekEvent(final double ratio
) {
161 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
164 long pos
= Math
.round(ratio
* fFile
.length());
167 if (fFile
.read() == '\n') {
172 final ITmfLocation location
= new TmfLongLocation(pos
);
173 final TmfContext context
= seekEvent(location
);
174 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
176 } catch (final IOException e
) {
177 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
178 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
183 public synchronized double getLocationRatio(final ITmfLocation location
) {
188 if (location
.getLocationInfo() instanceof Long
) {
189 return (double) ((Long
) location
.getLocationInfo()) / fFile
.length();
191 } catch (final IOException e
) {
192 Activator
.getDefault().logError("Error getting location ration. File: " + getPath(), e
); //$NON-NLS-1$
198 public ITmfLocation
getCurrentLocation() {
199 // TODO Auto-generated method stub
204 public synchronized CustomXmlEvent
parseEvent(final ITmfContext tmfContext
) {
205 ITmfContext context
= seekEvent(tmfContext
.getLocation());
206 return parse(context
);
210 public synchronized CustomXmlEvent
getNext(final ITmfContext context
) {
211 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
212 final CustomXmlEvent event
= parse(context
);
214 updateAttributes(savedContext
, event
.getTimestamp());
215 context
.increaseRank();
220 private synchronized CustomXmlEvent
parse(final ITmfContext tmfContext
) {
224 if (!(tmfContext
instanceof CustomXmlTraceContext
)) {
228 final CustomXmlTraceContext context
= (CustomXmlTraceContext
) tmfContext
;
229 if (context
.getLocation() == null || !(context
.getLocation().getLocationInfo() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
233 CustomXmlEvent event
= null;
235 if (fFile
.getFilePointer() != (Long
)context
.getLocation().getLocationInfo() + 1)
237 fFile
.seek((Long
)context
.getLocation().getLocationInfo() + 1); // +1 is for the <
239 final StringBuffer elementBuffer
= new StringBuffer("<"); //$NON-NLS-1$
240 readElement(elementBuffer
, fFile
);
241 final Element element
= parseElementBuffer(elementBuffer
);
243 event
= extractEvent(element
, fRecordInputElement
);
244 ((StringBuffer
) event
.getContent().getValue()).append(elementBuffer
);
247 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
248 long rawPos
= fFile
.getFilePointer();
250 while ((line
= fFile
.getNextLine()) != null) {
251 final int idx
= line
.indexOf(recordElementStart
);
253 context
.setLocation(new TmfLongLocation(rawPos
+ idx
));
256 rawPos
= fFile
.getFilePointer();
258 } catch (final IOException e
) {
259 Activator
.getDefault().logError("Error parsing event. File: " + getPath(), e
); //$NON-NLS-1$
262 context
.setLocation(NULL_LOCATION
);
266 private Element
parseElementBuffer(final StringBuffer elementBuffer
) {
268 final DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
269 final DocumentBuilder db
= dbf
.newDocumentBuilder();
271 // The following allows xml parsing without access to the dtd
272 final EntityResolver resolver
= new EntityResolver () {
274 public InputSource
resolveEntity (final String publicId
, final String systemId
) {
275 final String empty
= ""; //$NON-NLS-1$
276 final ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
277 return new InputSource(bais
);
280 db
.setEntityResolver(resolver
);
282 // The following catches xml parsing exceptions
283 db
.setErrorHandler(new ErrorHandler(){
285 public void error(final SAXParseException saxparseexception
) throws SAXException
{}
287 public void warning(final SAXParseException saxparseexception
) throws SAXException
{}
289 public void fatalError(final SAXParseException saxparseexception
) throws SAXException
{
290 throw saxparseexception
;
293 final Document doc
= db
.parse(new ByteArrayInputStream(elementBuffer
.toString().getBytes()));
294 return doc
.getDocumentElement();
295 } catch (final ParserConfigurationException e
) {
296 Activator
.getDefault().logError("Error parsing element buffer. File:" + getPath(), e
); //$NON-NLS-1$
297 } catch (final SAXException e
) {
298 Activator
.getDefault().logError("Error parsing element buffer. File:" + getPath(), e
); //$NON-NLS-1$
299 } catch (final IOException e
) {
300 Activator
.getDefault().logError("Error parsing element buffer. File: " + getPath(), e
); //$NON-NLS-1$
305 private void readElement(final StringBuffer buffer
, final RandomAccessFile raFile
) {
308 boolean startTagClosed
= false;
310 while ((i
= raFile
.read()) != -1) {
312 final char c
= (char)i
;
315 readQuote(buffer
, raFile
, '"');
316 } else if (c
== '\'') {
317 readQuote(buffer
, raFile
, '\'');
318 } else if (c
== '<') {
319 readElement(buffer
, raFile
);
320 } else if (c
== '/' && numRead
== 1) {
322 } else if (c
== '-' && numRead
== 3 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("!-")) { //$NON-NLS-1$
323 readComment(buffer
, raFile
); // found "<!--"
324 } else if (i
== '>') {
325 if (buffer
.charAt(buffer
.length() - 2) == '/') {
327 } else if (startTagClosed
) {
328 break; // found "<...>...</...>"
331 startTagClosed
= true; // found "<...>"
336 } catch (final IOException e
) {
341 private static void readQuote(final StringBuffer buffer
,
342 final RandomAccessFile raFile
, final char eq
) {
345 while ((i
= raFile
.read()) != -1) {
346 final char c
= (char)i
;
350 break; // found matching end-quote
354 } catch (final IOException e
) {
359 private static void readComment(final StringBuffer buffer
,
360 final RandomAccessFile raFile
) {
364 while ((i
= raFile
.read()) != -1) {
366 final char c
= (char)i
;
368 if (c
== '>' && numRead
>= 2 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("--")) //$NON-NLS-1$
370 break; // found "-->"
374 } catch (final IOException e
) {
380 * Parse an XML element.
382 * @param parentElement
385 * The contents to parse
386 * @return The parsed content
388 public static StringBuffer
parseElement(final Element parentElement
, final StringBuffer buffer
) {
389 final NodeList nodeList
= parentElement
.getChildNodes();
390 String separator
= null;
391 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
392 final Node node
= nodeList
.item(i
);
393 if (node
.getNodeType() == Node
.ELEMENT_NODE
) {
394 if (separator
== null) {
395 separator
= " | "; //$NON-NLS-1$
397 buffer
.append(separator
);
399 final Element element
= (Element
) node
;
400 if (!element
.hasChildNodes()) {
401 buffer
.append(element
.getNodeName());
402 } else if (element
.getChildNodes().getLength() == 1 && element
.getFirstChild().getNodeType() == Node
.TEXT_NODE
) {
403 buffer
.append(element
.getNodeName() + ":" + element
.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
405 buffer
.append(element
.getNodeName());
406 buffer
.append(" [ "); //$NON-NLS-1$
407 parseElement(element
, buffer
);
408 buffer
.append(" ]"); //$NON-NLS-1$
410 } else if (node
.getNodeType() == Node
.TEXT_NODE
) {
411 if (node
.getNodeValue().trim().length() != 0) {
412 buffer
.append(node
.getNodeValue().trim());
420 * Get an input element if it is a valid record input. If not, we will look
421 * into its children for valid inputs.
423 * @param inputElement
424 * The main element to check for.
425 * @return The record element
427 public InputElement
getRecordInputElement(final InputElement inputElement
) {
428 if (inputElement
.logEntry
) {
430 } else if (inputElement
.childElements
!= null) {
431 for (final InputElement childInputElement
: inputElement
.childElements
) {
432 final InputElement recordInputElement
= getRecordInputElement(childInputElement
);
433 if (recordInputElement
!= null) {
434 return recordInputElement
;
442 * Extract a trace event from an XML element.
446 * @param inputElement
448 * @return The extracted event
450 public CustomXmlEvent
extractEvent(final Element element
, final InputElement inputElement
) {
451 final CustomXmlEvent event
= new CustomXmlEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
,""); //$NON-NLS-1$ //$NON-NLS-2$
452 event
.setContent(new CustomEventContent(event
, new StringBuffer()));
453 parseElement(element
, event
, inputElement
);
457 private void parseElement(final Element element
, final CustomXmlEvent event
, final InputElement inputElement
) {
458 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
459 event
.parseInput(parseElement(element
, new StringBuffer()).toString(), inputElement
.inputName
, inputElement
.inputAction
, inputElement
.inputFormat
);
461 if (inputElement
.attributes
!= null) {
462 for (final InputAttribute attribute
: inputElement
.attributes
) {
463 event
.parseInput(element
.getAttribute(attribute
.attributeName
), attribute
.inputName
, attribute
.inputAction
, attribute
.inputFormat
);
466 final NodeList childNodes
= element
.getChildNodes();
467 if (inputElement
.childElements
!= null) {
468 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
469 final Node node
= childNodes
.item(i
);
470 if (node
instanceof Element
) {
471 for (final InputElement child
: inputElement
.childElements
) {
472 if (node
.getNodeName().equals(child
.elementName
)) {
473 parseElement((Element
) node
, event
, child
);
484 * Retrieve the trace definition.
486 * @return The trace definition
488 public CustomTraceDefinition
getDefinition() {
493 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
496 public boolean validate(IProject project
, String path
) {
497 return fileExists(path
);