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
;
17 import java
.io
.FileNotFoundException
;
18 import java
.io
.IOException
;
19 import java
.io
.RandomAccessFile
;
21 import javax
.xml
.parsers
.DocumentBuilder
;
22 import javax
.xml
.parsers
.DocumentBuilderFactory
;
23 import javax
.xml
.parsers
.ParserConfigurationException
;
25 import org
.eclipse
.core
.resources
.IResource
;
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
.TmfEvent
;
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
.TmfLocation
;
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
<CustomXmlEvent
> implements ITmfEventParser
<CustomXmlEvent
> {
50 private static final TmfLocation
<Long
> NULL_LOCATION
= new TmfLocation
<Long
>((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
;
57 public CustomXmlTrace(final CustomXmlTraceDefinition definition
) {
58 fDefinition
= definition
;
59 fEventType
= new CustomXmlEventType(fDefinition
);
60 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
63 public CustomXmlTrace(final IResource resource
, final CustomXmlTraceDefinition definition
, final String path
, final int pageSize
) throws TmfTraceException
{
64 super(null, CustomXmlEvent
.class, path
, (pageSize
> 0) ? pageSize
: DEFAULT_CACHE_SIZE
);
65 fDefinition
= definition
;
66 fEventType
= new CustomXmlEventType(fDefinition
);
67 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
71 public void initTrace(final IResource resource
, final String path
, final Class
<CustomXmlEvent
> eventType
) throws TmfTraceException
{
72 super.initTrace(resource
, path
, eventType
);
76 public TmfContext
seekEvent(final ITmfLocation
<?
> location
) {
77 final CustomXmlTraceContext context
= new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
78 if (NULL_LOCATION
.equals(location
) || !new File(getPath()).isFile())
81 context
.raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
82 if (location
!= null && location
.getLocation() instanceof Long
) {
83 context
.raFile
.seek((Long
)location
.getLocation());
87 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
88 long rawPos
= context
.raFile
.getFilePointer();
90 while ((line
= context
.raFile
.getNextLine()) != null) {
91 final int idx
= line
.indexOf(recordElementStart
);
93 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
96 rawPos
= context
.raFile
.getFilePointer();
99 } catch (final FileNotFoundException e
) {
102 } catch (final IOException e
) {
110 public TmfContext
seekEvent(final double ratio
) {
111 BufferedRandomAccessFile raFile
= null;
113 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
114 long pos
= (long) (ratio
* raFile
.length());
116 raFile
.seek(pos
- 1);
117 if (raFile
.read() == '\n') {
122 final ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
123 final TmfContext context
= seekEvent(location
);
124 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
126 } catch (final FileNotFoundException e
) {
128 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
129 } catch (final IOException e
) {
131 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
133 if (raFile
!= null) {
136 } catch (final IOException e
) {
143 public double getLocationRatio(final ITmfLocation
<?
> location
) {
144 RandomAccessFile raFile
= null;
146 if (location
.getLocation() instanceof Long
) {
147 raFile
= new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$
148 return (double) ((Long
) location
.getLocation()) / raFile
.length();
150 } catch (final FileNotFoundException e
) {
152 } catch (final IOException e
) {
155 if (raFile
!= null) {
158 } catch (final IOException e
) {
166 public ITmfLocation
<?
> getCurrentLocation() {
167 // TODO Auto-generated method stub
172 public synchronized TmfEvent
readNextEvent(final ITmfContext context
) {
173 final ITmfContext savedContext
= context
.clone();
174 final TmfEvent event
= parseEvent(context
);
176 updateAttributes(savedContext
, event
.getTimestamp());
177 context
.increaseRank();
183 public TmfEvent
parseEvent(final ITmfContext tmfContext
) {
184 if (!(tmfContext
instanceof CustomXmlTraceContext
))
187 final CustomXmlTraceContext context
= (CustomXmlTraceContext
) tmfContext
;
188 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation()))
191 synchronized (context
.raFile
) {
192 CustomXmlEvent event
= null;
194 if (context
.raFile
.getFilePointer() != (Long
)context
.getLocation().getLocation() + 1)
196 context
.raFile
.seek((Long
)context
.getLocation().getLocation() + 1); // +1 is for the <
198 final StringBuffer elementBuffer
= new StringBuffer("<"); //$NON-NLS-1$
199 readElement(elementBuffer
, context
.raFile
);
200 final Element element
= parseElementBuffer(elementBuffer
);
202 event
= extractEvent(element
, fRecordInputElement
);
203 ((StringBuffer
) event
.getContent().getValue()).append(elementBuffer
);
206 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
207 long rawPos
= context
.raFile
.getFilePointer();
209 while ((line
= context
.raFile
.getNextLine()) != null) {
210 final int idx
= line
.indexOf(recordElementStart
);
212 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
215 rawPos
= context
.raFile
.getFilePointer();
217 } catch (final IOException e
) {
220 context
.setLocation(NULL_LOCATION
);
225 private Element
parseElementBuffer(final StringBuffer elementBuffer
) {
227 final DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
228 final DocumentBuilder db
= dbf
.newDocumentBuilder();
230 // The following allows xml parsing without access to the dtd
231 final EntityResolver resolver
= new EntityResolver () {
233 public InputSource
resolveEntity (final String publicId
, final String systemId
) {
234 final String empty
= ""; //$NON-NLS-1$
235 final ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
236 return new InputSource(bais
);
239 db
.setEntityResolver(resolver
);
241 // The following catches xml parsing exceptions
242 db
.setErrorHandler(new ErrorHandler(){
244 public void error(final SAXParseException saxparseexception
) throws SAXException
{}
246 public void warning(final SAXParseException saxparseexception
) throws SAXException
{}
248 public void fatalError(final SAXParseException saxparseexception
) throws SAXException
{
249 throw saxparseexception
;
252 final Document doc
= db
.parse(new ByteArrayInputStream(elementBuffer
.toString().getBytes()));
253 return doc
.getDocumentElement();
254 } catch (final ParserConfigurationException e
) {
256 } catch (final SAXException e
) {
258 } catch (final IOException e
) {
264 private void readElement(final StringBuffer buffer
, final RandomAccessFile raFile
) {
267 boolean startTagClosed
= false;
269 while ((i
= raFile
.read()) != -1) {
271 final char c
= (char)i
;
274 readQuote(buffer
, raFile
, '"');
275 } else if (c
== '\'') {
276 readQuote(buffer
, raFile
, '\'');
277 } else if (c
== '<') {
278 readElement(buffer
, raFile
);
279 } else if (c
== '/' && numRead
== 1) {
281 } else if (c
== '-' && numRead
== 3 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("!-")) {
282 readComment(buffer
, raFile
); // found "<!--"
284 if (buffer
.charAt(buffer
.length() - 2) == '/') {
286 } else if (startTagClosed
) {
287 break; // found "<...>...</...>"
290 startTagClosed
= true; // found "<...>"
294 } catch (final IOException e
) {
299 private void readQuote(final StringBuffer buffer
, final RandomAccessFile raFile
, final char eq
) {
302 while ((i
= raFile
.read()) != -1) {
303 final char c
= (char)i
;
307 break; // found matching end-quote
311 } catch (final IOException e
) {
316 private void readComment(final StringBuffer buffer
, final RandomAccessFile raFile
) {
320 while ((i
= raFile
.read()) != -1) {
322 final char c
= (char)i
;
324 if (c
== '>' && numRead
>= 2 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("--"))
326 break; // found "-->"
330 } catch (final IOException e
) {
335 public static StringBuffer
parseElement(final Element parentElement
, final StringBuffer buffer
) {
336 final NodeList nodeList
= parentElement
.getChildNodes();
337 String separator
= null;
338 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
339 final Node node
= nodeList
.item(i
);
340 if (node
.getNodeType() == Node
.ELEMENT_NODE
) {
341 if (separator
== null) {
342 separator
= " | "; //$NON-NLS-1$
344 buffer
.append(separator
);
346 final Element element
= (Element
) node
;
347 if (element
.hasChildNodes() == false) {
348 buffer
.append(element
.getNodeName());
349 } else if (element
.getChildNodes().getLength() == 1 && element
.getFirstChild().getNodeType() == Node
.TEXT_NODE
) {
350 buffer
.append(element
.getNodeName() + ":" + element
.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
352 buffer
.append(element
.getNodeName());
353 buffer
.append(" [ "); //$NON-NLS-1$
354 parseElement(element
, buffer
);
355 buffer
.append(" ]"); //$NON-NLS-1$
357 } else if (node
.getNodeType() == Node
.TEXT_NODE
)
358 if (node
.getNodeValue().trim().length() != 0) {
359 buffer
.append(node
.getNodeValue().trim());
365 public InputElement
getRecordInputElement(final InputElement inputElement
) {
366 if (inputElement
.logEntry
)
368 else if (inputElement
.childElements
!= null) {
369 for (final InputElement childInputElement
: inputElement
.childElements
) {
370 final InputElement recordInputElement
= getRecordInputElement(childInputElement
);
371 if (recordInputElement
!= null)
372 return recordInputElement
;
378 public CustomXmlEvent
extractEvent(final Element element
, final InputElement inputElement
) {
379 final CustomXmlEvent event
= new CustomXmlEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
,""); //$NON-NLS-1$ //$NON-NLS-2$
380 event
.setContent(new CustomEventContent(event
, "")); //$NON-NLS-1$
381 parseElement(element
, event
, inputElement
);
385 private void parseElement(final Element element
, final CustomXmlEvent event
, final InputElement inputElement
) {
386 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
387 event
.parseInput(parseElement(element
, new StringBuffer()).toString(), inputElement
.inputName
, inputElement
.inputAction
, inputElement
.inputFormat
);
389 if (inputElement
.attributes
!= null) {
390 for (final InputAttribute attribute
: inputElement
.attributes
) {
391 event
.parseInput(element
.getAttribute(attribute
.attributeName
), attribute
.inputName
, attribute
.inputAction
, attribute
.inputFormat
);
394 final NodeList childNodes
= element
.getChildNodes();
395 if (inputElement
.childElements
!= null) {
396 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
397 final Node node
= childNodes
.item(i
);
398 if (node
instanceof Element
) {
399 for (final InputElement child
: inputElement
.childElements
)
400 if (node
.getNodeName().equals(child
.elementName
)) {
401 parseElement((Element
) node
, event
, child
);
410 public CustomTraceDefinition
getDefinition() {