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
.ITmfLocation
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
36 import org
.w3c
.dom
.Document
;
37 import org
.w3c
.dom
.Element
;
38 import org
.w3c
.dom
.Node
;
39 import org
.w3c
.dom
.NodeList
;
40 import org
.xml
.sax
.EntityResolver
;
41 import org
.xml
.sax
.ErrorHandler
;
42 import org
.xml
.sax
.InputSource
;
43 import org
.xml
.sax
.SAXException
;
44 import org
.xml
.sax
.SAXParseException
;
46 public class CustomXmlTrace
extends TmfTrace
<CustomXmlEvent
> {
48 private static final TmfLocation
<Long
> NULL_LOCATION
= new TmfLocation
<Long
>((Long
) null);
49 private static final int DEFAULT_CACHE_SIZE
= 100;
51 private final CustomXmlTraceDefinition fDefinition
;
52 private final CustomXmlEventType fEventType
;
53 private final InputElement fRecordInputElement
;
55 public CustomXmlTrace(final CustomXmlTraceDefinition definition
) {
56 fDefinition
= definition
;
57 fEventType
= new CustomXmlEventType(fDefinition
);
58 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
61 public CustomXmlTrace(final IResource resource
, final CustomXmlTraceDefinition definition
, final String path
, final int pageSize
) throws FileNotFoundException
{
62 super(null, CustomXmlEvent
.class, path
, (pageSize
> 0) ? pageSize
: DEFAULT_CACHE_SIZE
);
63 fDefinition
= definition
;
64 fEventType
= new CustomXmlEventType(fDefinition
);
65 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
69 public void initTrace(final IResource resource
, final String path
, final Class
<CustomXmlEvent
> eventType
) throws FileNotFoundException
{
70 super.initTrace(resource
, path
, eventType
);
74 public TmfContext
seekLocation(final ITmfLocation
<?
> location
) {
75 final CustomXmlTraceContext context
= new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
76 if (NULL_LOCATION
.equals(location
) || !new File(getPath()).isFile())
79 context
.raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
80 if (location
!= null && location
.getLocation() instanceof Long
) {
81 context
.raFile
.seek((Long
)location
.getLocation());
85 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
86 long rawPos
= context
.raFile
.getFilePointer();
88 while ((line
= context
.raFile
.getNextLine()) != null) {
89 final int idx
= line
.indexOf(recordElementStart
);
91 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
94 rawPos
= context
.raFile
.getFilePointer();
97 } catch (final FileNotFoundException e
) {
100 } catch (final IOException e
) {
108 public TmfContext
seekLocation(final double ratio
) {
109 BufferedRandomAccessFile raFile
= null;
111 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
112 long pos
= (long) (ratio
* raFile
.length());
114 raFile
.seek(pos
- 1);
115 if (raFile
.read() == '\n') {
120 final ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
121 final TmfContext context
= seekLocation(location
);
122 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
124 } catch (final FileNotFoundException e
) {
126 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
127 } catch (final IOException e
) {
129 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
131 if (raFile
!= null) {
134 } catch (final IOException e
) {
141 public double getLocationRatio(final ITmfLocation
<?
> location
) {
142 RandomAccessFile raFile
= null;
144 if (location
.getLocation() instanceof Long
) {
145 raFile
= new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$
146 return (double) ((Long
) location
.getLocation()) / raFile
.length();
148 } catch (final FileNotFoundException e
) {
150 } catch (final IOException e
) {
153 if (raFile
!= null) {
156 } catch (final IOException e
) {
164 public ITmfLocation
<?
> getCurrentLocation() {
165 // TODO Auto-generated method stub
170 public synchronized TmfEvent
getNextEvent(final ITmfContext context
) {
171 final ITmfContext savedContext
= context
.clone();
172 final TmfEvent event
= parseEvent(context
);
174 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
175 context
.increaseRank();
181 public TmfEvent
parseEvent(final ITmfContext tmfContext
) {
182 if (!(tmfContext
instanceof CustomXmlTraceContext
))
185 final CustomXmlTraceContext context
= (CustomXmlTraceContext
) tmfContext
;
186 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation()))
189 synchronized (context
.raFile
) {
190 CustomXmlEvent event
= null;
192 if (context
.raFile
.getFilePointer() != (Long
)context
.getLocation().getLocation() + 1)
194 context
.raFile
.seek((Long
)context
.getLocation().getLocation() + 1); // +1 is for the <
196 final StringBuffer elementBuffer
= new StringBuffer("<"); //$NON-NLS-1$
197 readElement(elementBuffer
, context
.raFile
);
198 final Element element
= parseElementBuffer(elementBuffer
);
200 event
= extractEvent(element
, fRecordInputElement
);
201 ((StringBuffer
) event
.getContent().getValue()).append(elementBuffer
);
204 final String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
205 long rawPos
= context
.raFile
.getFilePointer();
207 while ((line
= context
.raFile
.getNextLine()) != null) {
208 final int idx
= line
.indexOf(recordElementStart
);
210 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
213 rawPos
= context
.raFile
.getFilePointer();
215 } catch (final IOException e
) {
218 context
.setLocation(NULL_LOCATION
);
223 private Element
parseElementBuffer(final StringBuffer elementBuffer
) {
225 final DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
226 final DocumentBuilder db
= dbf
.newDocumentBuilder();
228 // The following allows xml parsing without access to the dtd
229 final EntityResolver resolver
= new EntityResolver () {
231 public InputSource
resolveEntity (final String publicId
, final String systemId
) {
232 final String empty
= ""; //$NON-NLS-1$
233 final ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
234 return new InputSource(bais
);
237 db
.setEntityResolver(resolver
);
239 // The following catches xml parsing exceptions
240 db
.setErrorHandler(new ErrorHandler(){
242 public void error(final SAXParseException saxparseexception
) throws SAXException
{}
244 public void warning(final SAXParseException saxparseexception
) throws SAXException
{}
246 public void fatalError(final SAXParseException saxparseexception
) throws SAXException
{
247 throw saxparseexception
;
250 final Document doc
= db
.parse(new ByteArrayInputStream(elementBuffer
.toString().getBytes()));
251 return doc
.getDocumentElement();
252 } catch (final ParserConfigurationException e
) {
254 } catch (final SAXException e
) {
256 } catch (final IOException e
) {
262 private void readElement(final StringBuffer buffer
, final RandomAccessFile raFile
) {
265 boolean startTagClosed
= false;
267 while ((i
= raFile
.read()) != -1) {
269 final char c
= (char)i
;
272 readQuote(buffer
, raFile
, '"');
273 } else if (c
== '\'') {
274 readQuote(buffer
, raFile
, '\'');
275 } else if (c
== '<') {
276 readElement(buffer
, raFile
);
277 } else if (c
== '/' && numRead
== 1) {
279 } else if (c
== '-' && numRead
== 3 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("!-")) {
280 readComment(buffer
, raFile
); // found "<!--"
282 if (buffer
.charAt(buffer
.length() - 2) == '/') {
284 } else if (startTagClosed
) {
285 break; // found "<...>...</...>"
288 startTagClosed
= true; // found "<...>"
292 } catch (final IOException e
) {
297 private void readQuote(final StringBuffer buffer
, final RandomAccessFile raFile
, final char eq
) {
300 while ((i
= raFile
.read()) != -1) {
301 final char c
= (char)i
;
305 break; // found matching end-quote
309 } catch (final IOException e
) {
314 private void readComment(final StringBuffer buffer
, final RandomAccessFile raFile
) {
318 while ((i
= raFile
.read()) != -1) {
320 final char c
= (char)i
;
322 if (c
== '>' && numRead
>= 2 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("--"))
324 break; // found "-->"
328 } catch (final IOException e
) {
333 public static StringBuffer
parseElement(final Element parentElement
, final StringBuffer buffer
) {
334 final NodeList nodeList
= parentElement
.getChildNodes();
335 String separator
= null;
336 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
337 final Node node
= nodeList
.item(i
);
338 if (node
.getNodeType() == Node
.ELEMENT_NODE
) {
339 if (separator
== null) {
340 separator
= " | "; //$NON-NLS-1$
342 buffer
.append(separator
);
344 final Element element
= (Element
) node
;
345 if (element
.hasChildNodes() == false) {
346 buffer
.append(element
.getNodeName());
347 } else if (element
.getChildNodes().getLength() == 1 && element
.getFirstChild().getNodeType() == Node
.TEXT_NODE
) {
348 buffer
.append(element
.getNodeName() + ":" + element
.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
350 buffer
.append(element
.getNodeName());
351 buffer
.append(" [ "); //$NON-NLS-1$
352 parseElement(element
, buffer
);
353 buffer
.append(" ]"); //$NON-NLS-1$
355 } else if (node
.getNodeType() == Node
.TEXT_NODE
)
356 if (node
.getNodeValue().trim().length() != 0) {
357 buffer
.append(node
.getNodeValue().trim());
363 public InputElement
getRecordInputElement(final InputElement inputElement
) {
364 if (inputElement
.logEntry
)
366 else if (inputElement
.childElements
!= null) {
367 for (final InputElement childInputElement
: inputElement
.childElements
) {
368 final InputElement recordInputElement
= getRecordInputElement(childInputElement
);
369 if (recordInputElement
!= null)
370 return recordInputElement
;
376 public CustomXmlEvent
extractEvent(final Element element
, final InputElement inputElement
) {
377 final CustomXmlEvent event
= new CustomXmlEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
,""); //$NON-NLS-1$ //$NON-NLS-2$
378 event
.setContent(new CustomEventContent(event
, "")); //$NON-NLS-1$
379 parseElement(element
, event
, inputElement
);
383 private void parseElement(final Element element
, final CustomXmlEvent event
, final InputElement inputElement
) {
384 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
385 event
.parseInput(parseElement(element
, new StringBuffer()).toString(), inputElement
.inputName
, inputElement
.inputAction
, inputElement
.inputFormat
);
387 if (inputElement
.attributes
!= null) {
388 for (final InputAttribute attribute
: inputElement
.attributes
) {
389 event
.parseInput(element
.getAttribute(attribute
.attributeName
), attribute
.inputName
, attribute
.inputAction
, attribute
.inputFormat
);
392 final NodeList childNodes
= element
.getChildNodes();
393 if (inputElement
.childElements
!= null) {
394 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
395 final Node node
= childNodes
.item(i
);
396 if (node
instanceof Element
) {
397 for (final InputElement child
: inputElement
.childElements
)
398 if (node
.getNodeName().equals(child
.elementName
)) {
399 parseElement((Element
) node
, event
, child
);
408 public CustomTraceDefinition
getDefinition() {