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
.event
.TmfTimestamp
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
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
.TmfContext
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLongLocation
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
38 import org
.w3c
.dom
.Document
;
39 import org
.w3c
.dom
.Element
;
40 import org
.w3c
.dom
.Node
;
41 import org
.w3c
.dom
.NodeList
;
42 import org
.xml
.sax
.EntityResolver
;
43 import org
.xml
.sax
.ErrorHandler
;
44 import org
.xml
.sax
.InputSource
;
45 import org
.xml
.sax
.SAXException
;
46 import org
.xml
.sax
.SAXParseException
;
48 public class CustomXmlTrace
extends TmfTrace
implements ITmfEventParser
{
50 private static final TmfLongLocation NULL_LOCATION
= new TmfLongLocation((Long
) null);
51 private static final int DEFAULT_CACHE_SIZE
= 100;
53 private final CustomXmlTraceDefinition fDefinition
;
54 private final CustomXmlEventType fEventType
;
55 private final InputElement fRecordInputElement
;
56 private BufferedRandomAccessFile fFile
;
58 public CustomXmlTrace(final CustomXmlTraceDefinition definition
) {
59 fDefinition
= definition
;
60 fEventType
= new CustomXmlEventType(fDefinition
);
61 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
62 setCacheSize(DEFAULT_CACHE_SIZE
);
65 public CustomXmlTrace(final IResource resource
, final CustomXmlTraceDefinition definition
, final String path
, final int pageSize
) throws TmfTraceException
{
67 setCacheSize((pageSize
> 0) ? pageSize
: DEFAULT_CACHE_SIZE
);
68 initTrace(resource
, path
, CustomXmlEvent
.class);
72 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
) throws TmfTraceException
{
73 super.initTrace(resource
, path
, eventType
);
75 fFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
76 } catch (IOException e
) {
77 throw new TmfTraceException(e
.getMessage(), e
);
82 public synchronized void dispose() {
87 } catch (IOException e
) {
95 public synchronized TmfContext
seekEvent(final ITmfLocation location
) {
96 final CustomXmlTraceContext context
= new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
97 if (NULL_LOCATION
.equals(location
) || fFile
== null) {
101 if (location
== null) {
103 } else if (location
.getLocationInfo() instanceof Long
) {
104 fFile
.seek((Long
) location
.getLocationInfo());
107 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
108 long rawPos
= fFile
.getFilePointer();
110 while ((line
= fFile
.getNextLine()) != null) {
111 final int idx
= line
.indexOf(recordElementStart
);
113 context
.setLocation(new TmfLongLocation(rawPos
+ idx
));
116 rawPos
= fFile
.getFilePointer();
119 } catch (final IOException e
) {
120 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
127 public synchronized TmfContext
seekEvent(final double ratio
) {
129 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
132 long pos
= Math
.round(ratio
* fFile
.length());
135 if (fFile
.read() == '\n') {
140 final ITmfLocation location
= new TmfLongLocation(pos
);
141 final TmfContext context
= seekEvent(location
);
142 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
144 } catch (final IOException e
) {
145 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
146 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
151 public synchronized double getLocationRatio(final ITmfLocation location
) {
156 if (location
.getLocationInfo() instanceof Long
) {
157 return (double) ((Long
) location
.getLocationInfo()) / fFile
.length();
159 } catch (final IOException e
) {
160 Activator
.getDefault().logError("Error getting location ration. File: " + getPath(), e
); //$NON-NLS-1$
166 public ITmfLocation
getCurrentLocation() {
167 // TODO Auto-generated method stub
172 public synchronized CustomXmlEvent
parseEvent(final ITmfContext tmfContext
) {
173 ITmfContext context
= seekEvent(tmfContext
.getLocation());
174 return parse(context
);
178 public synchronized CustomXmlEvent
getNext(final ITmfContext context
) {
179 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
180 final CustomXmlEvent event
= parse(context
);
182 updateAttributes(savedContext
, event
.getTimestamp());
183 context
.increaseRank();
188 private synchronized CustomXmlEvent
parse(final ITmfContext tmfContext
) {
192 if (!(tmfContext
instanceof CustomXmlTraceContext
)) {
196 final CustomXmlTraceContext context
= (CustomXmlTraceContext
) tmfContext
;
197 if (context
.getLocation() == null || !(context
.getLocation().getLocationInfo() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
201 CustomXmlEvent event
= null;
203 if (fFile
.getFilePointer() != (Long
)context
.getLocation().getLocationInfo() + 1)
205 fFile
.seek((Long
)context
.getLocation().getLocationInfo() + 1); // +1 is for the <
207 final StringBuffer elementBuffer
= new StringBuffer("<"); //$NON-NLS-1$
208 readElement(elementBuffer
, fFile
);
209 final Element element
= parseElementBuffer(elementBuffer
);
211 event
= extractEvent(element
, fRecordInputElement
);
212 ((StringBuffer
) event
.getContent().getValue()).append(elementBuffer
);
215 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
216 long rawPos
= fFile
.getFilePointer();
218 while ((line
= fFile
.getNextLine()) != null) {
219 final int idx
= line
.indexOf(recordElementStart
);
221 context
.setLocation(new TmfLongLocation(rawPos
+ idx
));
224 rawPos
= fFile
.getFilePointer();
226 } catch (final IOException e
) {
227 Activator
.getDefault().logError("Error parsing event. File: " + getPath(), e
); //$NON-NLS-1$
230 context
.setLocation(NULL_LOCATION
);
234 private Element
parseElementBuffer(final StringBuffer elementBuffer
) {
236 final DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
237 final DocumentBuilder db
= dbf
.newDocumentBuilder();
239 // The following allows xml parsing without access to the dtd
240 final EntityResolver resolver
= new EntityResolver () {
242 public InputSource
resolveEntity (final String publicId
, final String systemId
) {
243 final String empty
= ""; //$NON-NLS-1$
244 final ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
245 return new InputSource(bais
);
248 db
.setEntityResolver(resolver
);
250 // The following catches xml parsing exceptions
251 db
.setErrorHandler(new ErrorHandler(){
253 public void error(final SAXParseException saxparseexception
) throws SAXException
{}
255 public void warning(final SAXParseException saxparseexception
) throws SAXException
{}
257 public void fatalError(final SAXParseException saxparseexception
) throws SAXException
{
258 throw saxparseexception
;
261 final Document doc
= db
.parse(new ByteArrayInputStream(elementBuffer
.toString().getBytes()));
262 return doc
.getDocumentElement();
263 } catch (final ParserConfigurationException e
) {
264 Activator
.getDefault().logError("Error parsing element buffer. File:" + getPath(), e
); //$NON-NLS-1$
265 } catch (final SAXException e
) {
266 Activator
.getDefault().logError("Error parsing element buffer. File:" + getPath(), e
); //$NON-NLS-1$
267 } catch (final IOException e
) {
268 Activator
.getDefault().logError("Error parsing element buffer. File: " + getPath(), e
); //$NON-NLS-1$
273 private void readElement(final StringBuffer buffer
, final RandomAccessFile raFile
) {
276 boolean startTagClosed
= false;
278 while ((i
= raFile
.read()) != -1) {
280 final char c
= (char)i
;
283 readQuote(buffer
, raFile
, '"');
284 } else if (c
== '\'') {
285 readQuote(buffer
, raFile
, '\'');
286 } else if (c
== '<') {
287 readElement(buffer
, raFile
);
288 } else if (c
== '/' && numRead
== 1) {
290 } else if (c
== '-' && numRead
== 3 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("!-")) { //$NON-NLS-1$
291 readComment(buffer
, raFile
); // found "<!--"
292 } else if (i
== '>') {
293 if (buffer
.charAt(buffer
.length() - 2) == '/') {
295 } else if (startTagClosed
) {
296 break; // found "<...>...</...>"
299 startTagClosed
= true; // found "<...>"
304 } catch (final IOException e
) {
309 private static void readQuote(final StringBuffer buffer
,
310 final RandomAccessFile raFile
, final char eq
) {
313 while ((i
= raFile
.read()) != -1) {
314 final char c
= (char)i
;
318 break; // found matching end-quote
322 } catch (final IOException e
) {
327 private static void readComment(final StringBuffer buffer
,
328 final RandomAccessFile raFile
) {
332 while ((i
= raFile
.read()) != -1) {
334 final char c
= (char)i
;
336 if (c
== '>' && numRead
>= 2 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("--")) //$NON-NLS-1$
338 break; // found "-->"
342 } catch (final IOException e
) {
347 public static StringBuffer
parseElement(final Element parentElement
, final StringBuffer buffer
) {
348 final NodeList nodeList
= parentElement
.getChildNodes();
349 String separator
= null;
350 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
351 final Node node
= nodeList
.item(i
);
352 if (node
.getNodeType() == Node
.ELEMENT_NODE
) {
353 if (separator
== null) {
354 separator
= " | "; //$NON-NLS-1$
356 buffer
.append(separator
);
358 final Element element
= (Element
) node
;
359 if (!element
.hasChildNodes()) {
360 buffer
.append(element
.getNodeName());
361 } else if (element
.getChildNodes().getLength() == 1 && element
.getFirstChild().getNodeType() == Node
.TEXT_NODE
) {
362 buffer
.append(element
.getNodeName() + ":" + element
.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
364 buffer
.append(element
.getNodeName());
365 buffer
.append(" [ "); //$NON-NLS-1$
366 parseElement(element
, buffer
);
367 buffer
.append(" ]"); //$NON-NLS-1$
369 } else if (node
.getNodeType() == Node
.TEXT_NODE
) {
370 if (node
.getNodeValue().trim().length() != 0) {
371 buffer
.append(node
.getNodeValue().trim());
378 public InputElement
getRecordInputElement(final InputElement inputElement
) {
379 if (inputElement
.logEntry
) {
381 } else if (inputElement
.childElements
!= null) {
382 for (final InputElement childInputElement
: inputElement
.childElements
) {
383 final InputElement recordInputElement
= getRecordInputElement(childInputElement
);
384 if (recordInputElement
!= null) {
385 return recordInputElement
;
392 public CustomXmlEvent
extractEvent(final Element element
, final InputElement inputElement
) {
393 final CustomXmlEvent event
= new CustomXmlEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
,""); //$NON-NLS-1$ //$NON-NLS-2$
394 event
.setContent(new CustomEventContent(event
, new StringBuffer()));
395 parseElement(element
, event
, inputElement
);
399 private void parseElement(final Element element
, final CustomXmlEvent event
, final InputElement inputElement
) {
400 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
401 event
.parseInput(parseElement(element
, new StringBuffer()).toString(), inputElement
.inputName
, inputElement
.inputAction
, inputElement
.inputFormat
);
403 if (inputElement
.attributes
!= null) {
404 for (final InputAttribute attribute
: inputElement
.attributes
) {
405 event
.parseInput(element
.getAttribute(attribute
.attributeName
), attribute
.inputName
, attribute
.inputAction
, attribute
.inputFormat
);
408 final NodeList childNodes
= element
.getChildNodes();
409 if (inputElement
.childElements
!= null) {
410 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
411 final Node node
= childNodes
.item(i
);
412 if (node
instanceof Element
) {
413 for (final InputElement child
: inputElement
.childElements
) {
414 if (node
.getNodeName().equals(child
.elementName
)) {
415 parseElement((Element
) node
, event
, child
);
425 public CustomTraceDefinition
getDefinition() {
430 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
433 public boolean validate(IProject project
, String path
) {
434 return fileExists(path
);