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
.io
.BufferedRandomAccessFile
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
37 import org
.w3c
.dom
.Document
;
38 import org
.w3c
.dom
.Element
;
39 import org
.w3c
.dom
.Node
;
40 import org
.w3c
.dom
.NodeList
;
41 import org
.xml
.sax
.EntityResolver
;
42 import org
.xml
.sax
.ErrorHandler
;
43 import org
.xml
.sax
.InputSource
;
44 import org
.xml
.sax
.SAXException
;
45 import org
.xml
.sax
.SAXParseException
;
47 public class CustomXmlTrace
extends TmfTrace
<CustomXmlEvent
> implements ITmfEventParser
<CustomXmlEvent
> {
49 private static final TmfLocation
<Long
> NULL_LOCATION
= new TmfLocation
<Long
>((Long
) null);
50 private static final int DEFAULT_CACHE_SIZE
= 100;
52 private final CustomXmlTraceDefinition fDefinition
;
53 private final CustomXmlEventType fEventType
;
54 private final InputElement fRecordInputElement
;
56 public CustomXmlTrace(final CustomXmlTraceDefinition definition
) {
57 fDefinition
= definition
;
58 fEventType
= new CustomXmlEventType(fDefinition
);
59 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
62 public CustomXmlTrace(final IResource resource
, final CustomXmlTraceDefinition definition
, final String path
, final int pageSize
) throws FileNotFoundException
{
63 super(null, CustomXmlEvent
.class, path
, (pageSize
> 0) ? pageSize
: DEFAULT_CACHE_SIZE
);
64 fDefinition
= definition
;
65 fEventType
= new CustomXmlEventType(fDefinition
);
66 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
70 public void initTrace(final IResource resource
, final String path
, final Class
<CustomXmlEvent
> eventType
) throws FileNotFoundException
{
71 super.initTrace(resource
, path
, eventType
);
75 public TmfContext
seekEvent(final ITmfLocation
<?
> location
) {
76 final CustomXmlTraceContext context
= new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
77 if (NULL_LOCATION
.equals(location
) || !new File(getPath()).isFile())
80 context
.raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
81 if (location
!= null && location
.getLocation() instanceof Long
) {
82 context
.raFile
.seek((Long
)location
.getLocation());
86 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
87 long rawPos
= context
.raFile
.getFilePointer();
89 while ((line
= context
.raFile
.getNextLine()) != null) {
90 final int idx
= line
.indexOf(recordElementStart
);
92 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
95 rawPos
= context
.raFile
.getFilePointer();
98 } catch (final FileNotFoundException e
) {
101 } catch (final IOException e
) {
109 public TmfContext
seekEvent(final double ratio
) {
110 BufferedRandomAccessFile raFile
= null;
112 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
113 long pos
= (long) (ratio
* raFile
.length());
115 raFile
.seek(pos
- 1);
116 if (raFile
.read() == '\n') {
121 final ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
122 final TmfContext context
= seekEvent(location
);
123 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
125 } catch (final FileNotFoundException e
) {
127 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
128 } catch (final IOException e
) {
130 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
132 if (raFile
!= null) {
135 } catch (final IOException e
) {
142 public double getLocationRatio(final ITmfLocation
<?
> location
) {
143 RandomAccessFile raFile
= null;
145 if (location
.getLocation() instanceof Long
) {
146 raFile
= new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$
147 return (double) ((Long
) location
.getLocation()) / raFile
.length();
149 } catch (final FileNotFoundException e
) {
151 } catch (final IOException e
) {
154 if (raFile
!= null) {
157 } catch (final IOException e
) {
165 public ITmfLocation
<?
> getCurrentLocation() {
166 // TODO Auto-generated method stub
171 public synchronized TmfEvent
readEvent(final ITmfContext context
) {
172 final ITmfContext savedContext
= context
.clone();
173 final TmfEvent event
= parseEvent(context
);
175 updateAttributes(savedContext
, event
.getTimestamp());
176 context
.increaseRank();
182 public TmfEvent
parseEvent(final ITmfContext tmfContext
) {
183 if (!(tmfContext
instanceof CustomXmlTraceContext
))
186 final CustomXmlTraceContext context
= (CustomXmlTraceContext
) tmfContext
;
187 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation()))
190 synchronized (context
.raFile
) {
191 CustomXmlEvent event
= null;
193 if (context
.raFile
.getFilePointer() != (Long
)context
.getLocation().getLocation() + 1)
195 context
.raFile
.seek((Long
)context
.getLocation().getLocation() + 1); // +1 is for the <
197 final StringBuffer elementBuffer
= new StringBuffer("<"); //$NON-NLS-1$
198 readElement(elementBuffer
, context
.raFile
);
199 final Element element
= parseElementBuffer(elementBuffer
);
201 event
= extractEvent(element
, fRecordInputElement
);
202 ((StringBuffer
) event
.getContent().getValue()).append(elementBuffer
);
205 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
206 long rawPos
= context
.raFile
.getFilePointer();
208 while ((line
= context
.raFile
.getNextLine()) != null) {
209 final int idx
= line
.indexOf(recordElementStart
);
211 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
214 rawPos
= context
.raFile
.getFilePointer();
216 } catch (final IOException e
) {
219 context
.setLocation(NULL_LOCATION
);
224 private Element
parseElementBuffer(final StringBuffer elementBuffer
) {
226 final DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
227 final DocumentBuilder db
= dbf
.newDocumentBuilder();
229 // The following allows xml parsing without access to the dtd
230 final EntityResolver resolver
= new EntityResolver () {
232 public InputSource
resolveEntity (final String publicId
, final String systemId
) {
233 final String empty
= ""; //$NON-NLS-1$
234 final ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
235 return new InputSource(bais
);
238 db
.setEntityResolver(resolver
);
240 // The following catches xml parsing exceptions
241 db
.setErrorHandler(new ErrorHandler(){
243 public void error(final SAXParseException saxparseexception
) throws SAXException
{}
245 public void warning(final SAXParseException saxparseexception
) throws SAXException
{}
247 public void fatalError(final SAXParseException saxparseexception
) throws SAXException
{
248 throw saxparseexception
;
251 final Document doc
= db
.parse(new ByteArrayInputStream(elementBuffer
.toString().getBytes()));
252 return doc
.getDocumentElement();
253 } catch (final ParserConfigurationException e
) {
255 } catch (final SAXException e
) {
257 } catch (final IOException e
) {
263 private void readElement(final StringBuffer buffer
, final RandomAccessFile raFile
) {
266 boolean startTagClosed
= false;
268 while ((i
= raFile
.read()) != -1) {
270 final char c
= (char)i
;
273 readQuote(buffer
, raFile
, '"');
274 } else if (c
== '\'') {
275 readQuote(buffer
, raFile
, '\'');
276 } else if (c
== '<') {
277 readElement(buffer
, raFile
);
278 } else if (c
== '/' && numRead
== 1) {
280 } else if (c
== '-' && numRead
== 3 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("!-")) {
281 readComment(buffer
, raFile
); // found "<!--"
283 if (buffer
.charAt(buffer
.length() - 2) == '/') {
285 } else if (startTagClosed
) {
286 break; // found "<...>...</...>"
289 startTagClosed
= true; // found "<...>"
293 } catch (final IOException e
) {
298 private void readQuote(final StringBuffer buffer
, final RandomAccessFile raFile
, final char eq
) {
301 while ((i
= raFile
.read()) != -1) {
302 final char c
= (char)i
;
306 break; // found matching end-quote
310 } catch (final IOException e
) {
315 private void readComment(final StringBuffer buffer
, final RandomAccessFile raFile
) {
319 while ((i
= raFile
.read()) != -1) {
321 final char c
= (char)i
;
323 if (c
== '>' && numRead
>= 2 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("--"))
325 break; // found "-->"
329 } catch (final IOException e
) {
334 public static StringBuffer
parseElement(final Element parentElement
, final StringBuffer buffer
) {
335 final NodeList nodeList
= parentElement
.getChildNodes();
336 String separator
= null;
337 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
338 final Node node
= nodeList
.item(i
);
339 if (node
.getNodeType() == Node
.ELEMENT_NODE
) {
340 if (separator
== null) {
341 separator
= " | "; //$NON-NLS-1$
343 buffer
.append(separator
);
345 final Element element
= (Element
) node
;
346 if (element
.hasChildNodes() == false) {
347 buffer
.append(element
.getNodeName());
348 } else if (element
.getChildNodes().getLength() == 1 && element
.getFirstChild().getNodeType() == Node
.TEXT_NODE
) {
349 buffer
.append(element
.getNodeName() + ":" + element
.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
351 buffer
.append(element
.getNodeName());
352 buffer
.append(" [ "); //$NON-NLS-1$
353 parseElement(element
, buffer
);
354 buffer
.append(" ]"); //$NON-NLS-1$
356 } else if (node
.getNodeType() == Node
.TEXT_NODE
)
357 if (node
.getNodeValue().trim().length() != 0) {
358 buffer
.append(node
.getNodeValue().trim());
364 public InputElement
getRecordInputElement(final InputElement inputElement
) {
365 if (inputElement
.logEntry
)
367 else if (inputElement
.childElements
!= null) {
368 for (final InputElement childInputElement
: inputElement
.childElements
) {
369 final InputElement recordInputElement
= getRecordInputElement(childInputElement
);
370 if (recordInputElement
!= null)
371 return recordInputElement
;
377 public CustomXmlEvent
extractEvent(final Element element
, final InputElement inputElement
) {
378 final CustomXmlEvent event
= new CustomXmlEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
,""); //$NON-NLS-1$ //$NON-NLS-2$
379 event
.setContent(new CustomEventContent(event
, "")); //$NON-NLS-1$
380 parseElement(element
, event
, inputElement
);
384 private void parseElement(final Element element
, final CustomXmlEvent event
, final InputElement inputElement
) {
385 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
386 event
.parseInput(parseElement(element
, new StringBuffer()).toString(), inputElement
.inputName
, inputElement
.inputAction
, inputElement
.inputFormat
);
388 if (inputElement
.attributes
!= null) {
389 for (final InputAttribute attribute
: inputElement
.attributes
) {
390 event
.parseInput(element
.getAttribute(attribute
.attributeName
), attribute
.inputName
, attribute
.inputAction
, attribute
.inputFormat
);
393 final NodeList childNodes
= element
.getChildNodes();
394 if (inputElement
.childElements
!= null) {
395 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
396 final Node node
= childNodes
.item(i
);
397 if (node
instanceof Element
) {
398 for (final InputElement child
: inputElement
.childElements
)
399 if (node
.getNodeName().equals(child
.elementName
)) {
400 parseElement((Element
) node
, event
, child
);
409 public CustomTraceDefinition
getDefinition() {