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
.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
.tmf
.core
.event
.TmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventReference
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventSource
;
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
.ITmfTrace
;
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
.eclipse
.linuxtools
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
.InputAttribute
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
.InputElement
;
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
> {
50 private static final TmfLocation
<Long
> NULL_LOCATION
= new TmfLocation
<Long
>((Long
) null);
51 private static final int CACHE_SIZE
= 100;
53 private CustomXmlTraceDefinition fDefinition
;
54 private CustomXmlEventType fEventType
;
55 private InputElement fRecordInputElement
;
57 public CustomXmlTrace(CustomXmlTraceDefinition definition
) {
58 fDefinition
= definition
;
59 fEventType
= new CustomXmlEventType(fDefinition
);
60 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
63 public CustomXmlTrace(String name
, CustomXmlTraceDefinition definition
, String path
, int cacheSize
) throws FileNotFoundException
{
64 super(name
, CustomXmlEvent
.class, path
, cacheSize
);
65 fDefinition
= definition
;
66 fEventType
= new CustomXmlEventType(fDefinition
);
67 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
71 public void initTrace(String path
, Class
<CustomXmlEvent
> eventType
, boolean indexTrace
) throws FileNotFoundException
{
72 super.initTrace(path
, eventType
, CACHE_SIZE
, indexTrace
);
76 public TmfContext
seekLocation(ITmfLocation
<?
> location
) {
77 CustomXmlTraceContext context
= new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
78 if (NULL_LOCATION
.equals(location
) || !new File(getPath()).isFile()) {
82 context
.raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
83 if (location
!= null && location
.getLocation() instanceof Long
) {
84 context
.raFile
.seek((Long
)location
.getLocation());
88 String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
89 long rawPos
= context
.raFile
.getFilePointer();
91 while ((line
= context
.raFile
.getNextLine()) != null) {
92 int idx
= line
.indexOf(recordElementStart
);
94 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
97 rawPos
= context
.raFile
.getFilePointer();
100 } catch (FileNotFoundException e
) {
103 } catch (IOException e
) {
111 public TmfContext
seekLocation(double ratio
) {
113 BufferedRandomAccessFile 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') break;
120 ITmfLocation
<?
> location
= new TmfLocation
<Long
>(new Long(pos
));
121 TmfContext context
= seekLocation(location
);
122 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
124 } catch (FileNotFoundException e
) {
126 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
127 } catch (IOException e
) {
129 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
134 public double getLocationRatio(ITmfLocation
<?
> location
) {
136 if (location
.getLocation() instanceof Long
) {
137 RandomAccessFile raFile
= new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$
138 return (double) ((Long
) location
.getLocation()) / raFile
.length();
140 } catch (FileNotFoundException e
) {
142 } catch (IOException e
) {
149 @SuppressWarnings({ "rawtypes", "unchecked" })
150 public ITmfTrace
copy() {
151 // TODO Auto-generated method stub
156 public ITmfLocation
<?
> getCurrentLocation() {
157 // TODO Auto-generated method stub
162 public synchronized TmfEvent
getNextEvent(TmfContext context
) {
163 ITmfContext savedContext
= context
.clone();
164 TmfEvent event
= parseEvent(context
);
166 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
167 context
.updateRank(1);
173 public TmfEvent
parseEvent(TmfContext tmfContext
) {
174 if (!(tmfContext
instanceof CustomXmlTraceContext
)) {
178 CustomXmlTraceContext context
= (CustomXmlTraceContext
) tmfContext
;
179 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
183 synchronized (context
.raFile
) {
184 CustomXmlEvent event
= null;
186 if (context
.raFile
.getFilePointer() != (Long
)context
.getLocation().getLocation() + 1) {
187 context
.raFile
.seek((Long
)context
.getLocation().getLocation() + 1); // +1 is for the <
189 StringBuffer elementBuffer
= new StringBuffer("<"); //$NON-NLS-1$
190 readElement(elementBuffer
, context
.raFile
);
191 Element element
= parseElementBuffer(elementBuffer
);
193 event
= extractEvent(element
, fRecordInputElement
);
194 ((StringBuffer
) event
.getContent().getContent()).append(elementBuffer
);
197 String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
198 long rawPos
= context
.raFile
.getFilePointer();
200 while ((line
= context
.raFile
.getNextLine()) != null) {
201 int idx
= line
.indexOf(recordElementStart
);
203 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
206 rawPos
= context
.raFile
.getFilePointer();
208 } catch (IOException e
) {
211 context
.setLocation(NULL_LOCATION
);
216 private Element
parseElementBuffer(StringBuffer elementBuffer
) {
218 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
219 DocumentBuilder db
= dbf
.newDocumentBuilder();
221 // The following allows xml parsing without access to the dtd
222 EntityResolver resolver
= new EntityResolver () {
224 public InputSource
resolveEntity (String publicId
, String systemId
) {
225 String empty
= ""; //$NON-NLS-1$
226 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
227 return new InputSource(bais
);
230 db
.setEntityResolver(resolver
);
232 // The following catches xml parsing exceptions
233 db
.setErrorHandler(new ErrorHandler(){
235 public void error(SAXParseException saxparseexception
) throws SAXException
{}
237 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
239 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
240 throw saxparseexception
;
243 Document doc
= db
.parse(new ByteArrayInputStream(elementBuffer
.toString().getBytes()));
244 return doc
.getDocumentElement();
245 } catch (ParserConfigurationException e
) {
247 } catch (SAXException e
) {
249 } catch (IOException e
) {
255 private void readElement(StringBuffer buffer
, RandomAccessFile raFile
) {
258 boolean startTagClosed
= false;
260 while ((i
= raFile
.read()) != -1) {
265 readQuote(buffer
, raFile
, '"');
266 } else if (c
== '\'') {
267 readQuote(buffer
, raFile
, '\'');
268 } else if (c
== '<') {
269 readElement(buffer
, raFile
);
270 } else if (c
== '/' && numRead
== 1) {
272 } else if (c
== '-' && numRead
== 3 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("!-")) { //$NON-NLS-1$
273 readComment(buffer
, raFile
); // found "<!--"
274 } else if (i
== '>') {
275 if (buffer
.charAt(buffer
.length() - 2) == '/') {
277 } else if (startTagClosed
) {
278 break; // found "<...>...</...>"
280 startTagClosed
= true; // found "<...>"
285 } catch (IOException e
) {
290 private void readQuote(StringBuffer buffer
, RandomAccessFile raFile
, char eq
) {
293 while ((i
= raFile
.read()) != -1) {
297 break; // found matching end-quote
301 } catch (IOException e
) {
306 private void readComment(StringBuffer buffer
, RandomAccessFile raFile
) {
310 while ((i
= raFile
.read()) != -1) {
314 if (c
== '>' && numRead
>= 2 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("--")) { //$NON-NLS-1$
315 break; // found "-->"
319 } catch (IOException e
) {
324 public static StringBuffer
parseElement(Element parentElement
, StringBuffer buffer
) {
325 NodeList nodeList
= parentElement
.getChildNodes();
326 String separator
= null;
327 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
328 Node node
= nodeList
.item(i
);
329 if (node
.getNodeType() == Node
.ELEMENT_NODE
) {
330 if (separator
== null) {
331 separator
= " | "; //$NON-NLS-1$
333 buffer
.append(separator
);
335 Element element
= (Element
) node
;
336 if (element
.hasChildNodes() == false) {
337 buffer
.append(element
.getNodeName());
338 } else if (element
.getChildNodes().getLength() == 1 && element
.getFirstChild().getNodeType() == Node
.TEXT_NODE
) {
339 buffer
.append(element
.getNodeName() + ":" + element
.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
341 buffer
.append(element
.getNodeName());
342 buffer
.append(" [ "); //$NON-NLS-1$
343 parseElement(element
, buffer
);
344 buffer
.append(" ]"); //$NON-NLS-1$
346 } else if (node
.getNodeType() == Node
.TEXT_NODE
) {
347 if (node
.getNodeValue().trim().length() != 0) {
348 buffer
.append(node
.getNodeValue().trim());
355 public InputElement
getRecordInputElement(InputElement inputElement
) {
356 if (inputElement
.logEntry
) {
358 } else if (inputElement
.childElements
!= null) {
359 for (InputElement childInputElement
: inputElement
.childElements
) {
360 InputElement recordInputElement
= getRecordInputElement(childInputElement
);
361 if (recordInputElement
!= null) {
362 return recordInputElement
;
369 public CustomXmlEvent
extractEvent(Element element
, InputElement inputElement
) {
370 CustomXmlEvent event
= new CustomXmlEvent(fDefinition
, this, TmfTimestamp
.Zero
, new TmfEventSource(""), fEventType
, new TmfEventReference("")); //$NON-NLS-1$ //$NON-NLS-2$
371 event
.setContent(new CustomEventContent(event
, new StringBuffer()));
372 parseElement(element
, event
, inputElement
);
376 private void parseElement(Element element
, CustomXmlEvent event
, InputElement inputElement
) {
377 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
378 event
.parseInput(parseElement(element
, new StringBuffer()).toString(), inputElement
.inputName
, inputElement
.inputAction
, inputElement
.inputFormat
);
380 if (inputElement
.attributes
!= null) {
381 for (InputAttribute attribute
: inputElement
.attributes
) {
382 event
.parseInput(element
.getAttribute(attribute
.attributeName
), attribute
.inputName
, attribute
.inputAction
, attribute
.inputFormat
);
385 NodeList childNodes
= element
.getChildNodes();
386 if (inputElement
.childElements
!= null) {
387 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
388 Node node
= childNodes
.item(i
);
389 if (node
instanceof Element
) {
390 for (InputElement child
: inputElement
.childElements
) {
391 if (node
.getNodeName().equals(child
.elementName
)) {
392 parseElement((Element
) node
, event
, child
);
402 public CustomTraceDefinition
getDefinition() {