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
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
.InputAttribute
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
.InputElement
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
35 import org
.w3c
.dom
.Document
;
36 import org
.w3c
.dom
.Element
;
37 import org
.w3c
.dom
.Node
;
38 import org
.w3c
.dom
.NodeList
;
39 import org
.xml
.sax
.EntityResolver
;
40 import org
.xml
.sax
.ErrorHandler
;
41 import org
.xml
.sax
.InputSource
;
42 import org
.xml
.sax
.SAXException
;
43 import org
.xml
.sax
.SAXParseException
;
45 public class CustomXmlTrace
extends TmfTrace
<CustomXmlEvent
> {
47 private static final TmfLocation
<Long
> NULL_LOCATION
= new TmfLocation
<Long
>((Long
) null);
48 private static final int DEFAULT_CACHE_SIZE
= 100;
50 private CustomXmlTraceDefinition fDefinition
;
51 private CustomXmlEventType fEventType
;
52 private InputElement fRecordInputElement
;
54 public CustomXmlTrace(CustomXmlTraceDefinition definition
) {
55 fDefinition
= definition
;
56 fEventType
= new CustomXmlEventType(fDefinition
);
57 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
60 public CustomXmlTrace(String name
, CustomXmlTraceDefinition definition
, String path
, int pageSize
) throws FileNotFoundException
{
61 super(name
, CustomXmlEvent
.class, path
, (pageSize
> 0) ? pageSize
: DEFAULT_CACHE_SIZE
, true);
62 fDefinition
= definition
;
63 fEventType
= new CustomXmlEventType(fDefinition
);
64 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
68 public void initTrace(String name
, String path
, Class
<CustomXmlEvent
> eventType
) throws FileNotFoundException
{
69 super.initTrace(name
, path
, eventType
);
73 public TmfContext
seekLocation(ITmfLocation
<?
> location
) {
74 CustomXmlTraceContext context
= new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
75 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 String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
86 long rawPos
= context
.raFile
.getFilePointer();
88 while ((line
= context
.raFile
.getNextLine()) != null) {
89 int idx
= line
.indexOf(recordElementStart
);
91 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
94 rawPos
= context
.raFile
.getFilePointer();
97 } catch (FileNotFoundException e
) {
100 } catch (IOException e
) {
108 public TmfContext
seekLocation(double ratio
) {
110 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
111 long pos
= (long) (ratio
* raFile
.length());
113 raFile
.seek(pos
- 1);
114 if (raFile
.read() == '\n') break;
117 ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
118 TmfContext context
= seekLocation(location
);
119 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
121 } catch (FileNotFoundException e
) {
123 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
124 } catch (IOException e
) {
126 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
131 public double getLocationRatio(ITmfLocation
<?
> location
) {
133 if (location
.getLocation() instanceof Long
) {
134 RandomAccessFile raFile
= new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$
135 return (double) ((Long
) location
.getLocation()) / raFile
.length();
137 } catch (FileNotFoundException e
) {
139 } catch (IOException e
) {
146 public ITmfLocation
<?
> getCurrentLocation() {
147 // TODO Auto-generated method stub
152 public synchronized TmfEvent
getNextEvent(ITmfContext context
) {
153 ITmfContext savedContext
= context
.clone();
154 TmfEvent event
= parseEvent(context
);
156 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
157 context
.updateRank(1);
163 public TmfEvent
parseEvent(ITmfContext tmfContext
) {
164 if (!(tmfContext
instanceof CustomXmlTraceContext
)) {
168 CustomXmlTraceContext context
= (CustomXmlTraceContext
) tmfContext
;
169 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
173 synchronized (context
.raFile
) {
174 CustomXmlEvent event
= null;
176 if (context
.raFile
.getFilePointer() != (Long
)context
.getLocation().getLocation() + 1) {
177 context
.raFile
.seek((Long
)context
.getLocation().getLocation() + 1); // +1 is for the <
179 StringBuffer elementBuffer
= new StringBuffer("<"); //$NON-NLS-1$
180 readElement(elementBuffer
, context
.raFile
);
181 Element element
= parseElementBuffer(elementBuffer
);
183 event
= extractEvent(element
, fRecordInputElement
);
184 ((StringBuffer
) event
.getContent().getValue()).append(elementBuffer
);
187 String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
188 long rawPos
= context
.raFile
.getFilePointer();
190 while ((line
= context
.raFile
.getNextLine()) != null) {
191 int idx
= line
.indexOf(recordElementStart
);
193 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
196 rawPos
= context
.raFile
.getFilePointer();
198 } catch (IOException e
) {
201 context
.setLocation(NULL_LOCATION
);
206 private Element
parseElementBuffer(StringBuffer elementBuffer
) {
208 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
209 DocumentBuilder db
= dbf
.newDocumentBuilder();
211 // The following allows xml parsing without access to the dtd
212 EntityResolver resolver
= new EntityResolver () {
214 public InputSource
resolveEntity (String publicId
, String systemId
) {
215 String empty
= ""; //$NON-NLS-1$
216 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
217 return new InputSource(bais
);
220 db
.setEntityResolver(resolver
);
222 // The following catches xml parsing exceptions
223 db
.setErrorHandler(new ErrorHandler(){
225 public void error(SAXParseException saxparseexception
) throws SAXException
{}
227 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
229 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
230 throw saxparseexception
;
233 Document doc
= db
.parse(new ByteArrayInputStream(elementBuffer
.toString().getBytes()));
234 return doc
.getDocumentElement();
235 } catch (ParserConfigurationException e
) {
237 } catch (SAXException e
) {
239 } catch (IOException e
) {
245 private void readElement(StringBuffer buffer
, RandomAccessFile raFile
) {
248 boolean startTagClosed
= false;
250 while ((i
= raFile
.read()) != -1) {
255 readQuote(buffer
, raFile
, '"');
256 } else if (c
== '\'') {
257 readQuote(buffer
, raFile
, '\'');
258 } else if (c
== '<') {
259 readElement(buffer
, raFile
);
260 } else if (c
== '/' && numRead
== 1) {
262 } else if (c
== '-' && numRead
== 3 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("!-")) { //$NON-NLS-1$
263 readComment(buffer
, raFile
); // found "<!--"
264 } else if (i
== '>') {
265 if (buffer
.charAt(buffer
.length() - 2) == '/') {
267 } else if (startTagClosed
) {
268 break; // found "<...>...</...>"
270 startTagClosed
= true; // found "<...>"
275 } catch (IOException e
) {
280 private void readQuote(StringBuffer buffer
, RandomAccessFile raFile
, char eq
) {
283 while ((i
= raFile
.read()) != -1) {
287 break; // found matching end-quote
291 } catch (IOException e
) {
296 private void readComment(StringBuffer buffer
, RandomAccessFile raFile
) {
300 while ((i
= raFile
.read()) != -1) {
304 if (c
== '>' && numRead
>= 2 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("--")) { //$NON-NLS-1$
305 break; // found "-->"
309 } catch (IOException e
) {
314 public static StringBuffer
parseElement(Element parentElement
, StringBuffer buffer
) {
315 NodeList nodeList
= parentElement
.getChildNodes();
316 String separator
= null;
317 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
318 Node node
= nodeList
.item(i
);
319 if (node
.getNodeType() == Node
.ELEMENT_NODE
) {
320 if (separator
== null) {
321 separator
= " | "; //$NON-NLS-1$
323 buffer
.append(separator
);
325 Element element
= (Element
) node
;
326 if (element
.hasChildNodes() == false) {
327 buffer
.append(element
.getNodeName());
328 } else if (element
.getChildNodes().getLength() == 1 && element
.getFirstChild().getNodeType() == Node
.TEXT_NODE
) {
329 buffer
.append(element
.getNodeName() + ":" + element
.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
331 buffer
.append(element
.getNodeName());
332 buffer
.append(" [ "); //$NON-NLS-1$
333 parseElement(element
, buffer
);
334 buffer
.append(" ]"); //$NON-NLS-1$
336 } else if (node
.getNodeType() == Node
.TEXT_NODE
) {
337 if (node
.getNodeValue().trim().length() != 0) {
338 buffer
.append(node
.getNodeValue().trim());
345 public InputElement
getRecordInputElement(InputElement inputElement
) {
346 if (inputElement
.logEntry
) {
348 } else if (inputElement
.childElements
!= null) {
349 for (InputElement childInputElement
: inputElement
.childElements
) {
350 InputElement recordInputElement
= getRecordInputElement(childInputElement
);
351 if (recordInputElement
!= null) {
352 return recordInputElement
;
359 public CustomXmlEvent
extractEvent(Element element
, InputElement inputElement
) {
360 CustomXmlEvent event
= new CustomXmlEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
,""); //$NON-NLS-1$ //$NON-NLS-2$
361 event
.setContent(new CustomEventContent(event
, "")); //$NON-NLS-1$
362 parseElement(element
, event
, inputElement
);
366 private void parseElement(Element element
, CustomXmlEvent event
, InputElement inputElement
) {
367 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
368 event
.parseInput(parseElement(element
, new StringBuffer()).toString(), inputElement
.inputName
, inputElement
.inputAction
, inputElement
.inputFormat
);
370 if (inputElement
.attributes
!= null) {
371 for (InputAttribute attribute
: inputElement
.attributes
) {
372 event
.parseInput(element
.getAttribute(attribute
.attributeName
), attribute
.inputName
, attribute
.inputAction
, attribute
.inputFormat
);
375 NodeList childNodes
= element
.getChildNodes();
376 if (inputElement
.childElements
!= null) {
377 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
378 Node node
= childNodes
.item(i
);
379 if (node
instanceof Element
) {
380 for (InputElement child
: inputElement
.childElements
) {
381 if (node
.getNodeName().equals(child
.elementName
)) {
382 parseElement((Element
) node
, event
, child
);
392 public CustomTraceDefinition
getDefinition() {