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
.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
.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 CACHE_SIZE
= 100;
51 private CustomXmlTraceDefinition fDefinition
;
52 private CustomXmlEventType fEventType
;
53 private InputElement fRecordInputElement
;
55 public CustomXmlTrace(CustomXmlTraceDefinition definition
) {
56 fDefinition
= definition
;
57 fEventType
= new CustomXmlEventType(fDefinition
);
58 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
61 public CustomXmlTrace(String name
, CustomXmlTraceDefinition definition
, String path
, int cacheSize
) throws FileNotFoundException
{
62 super(name
, CustomXmlEvent
.class, path
, cacheSize
);
63 fDefinition
= definition
;
64 fEventType
= new CustomXmlEventType(fDefinition
);
65 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
69 public void initTrace(String name
, String path
, Class
<CustomXmlEvent
> eventType
, boolean indexTrace
) throws FileNotFoundException
{
70 super.initTrace(name
, path
, eventType
, CACHE_SIZE
, indexTrace
);
74 public TmfContext
seekLocation(ITmfLocation
<?
> location
) {
75 CustomXmlTraceContext context
= new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
76 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 String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
87 long rawPos
= context
.raFile
.getFilePointer();
89 while ((line
= context
.raFile
.getNextLine()) != null) {
90 int idx
= line
.indexOf(recordElementStart
);
92 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
95 rawPos
= context
.raFile
.getFilePointer();
98 } catch (FileNotFoundException e
) {
101 } catch (IOException e
) {
109 public TmfContext
seekLocation(double ratio
) {
111 BufferedRandomAccessFile 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') break;
118 ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
119 TmfContext context
= seekLocation(location
);
120 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
122 } catch (FileNotFoundException e
) {
124 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
125 } catch (IOException e
) {
127 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
132 public double getLocationRatio(ITmfLocation
<?
> location
) {
134 if (location
.getLocation() instanceof Long
) {
135 RandomAccessFile raFile
= new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$
136 return (double) ((Long
) location
.getLocation()) / raFile
.length();
138 } catch (FileNotFoundException e
) {
140 } catch (IOException e
) {
147 @SuppressWarnings({ "rawtypes", "unchecked" })
148 public ITmfTrace
copy() {
149 // TODO Auto-generated method stub
154 public ITmfLocation
<?
> getCurrentLocation() {
155 // TODO Auto-generated method stub
160 public synchronized TmfEvent
getNextEvent(ITmfContext context
) {
161 ITmfContext savedContext
= context
.clone();
162 TmfEvent event
= parseEvent(context
);
164 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
165 context
.updateRank(1);
171 public TmfEvent
parseEvent(ITmfContext tmfContext
) {
172 if (!(tmfContext
instanceof CustomXmlTraceContext
)) {
176 CustomXmlTraceContext context
= (CustomXmlTraceContext
) tmfContext
;
177 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
181 synchronized (context
.raFile
) {
182 CustomXmlEvent event
= null;
184 if (context
.raFile
.getFilePointer() != (Long
)context
.getLocation().getLocation() + 1) {
185 context
.raFile
.seek((Long
)context
.getLocation().getLocation() + 1); // +1 is for the <
187 StringBuffer elementBuffer
= new StringBuffer("<"); //$NON-NLS-1$
188 readElement(elementBuffer
, context
.raFile
);
189 Element element
= parseElementBuffer(elementBuffer
);
191 event
= extractEvent(element
, fRecordInputElement
);
192 ((StringBuffer
) event
.getContent().getValue()).append(elementBuffer
);
195 String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
196 long rawPos
= context
.raFile
.getFilePointer();
198 while ((line
= context
.raFile
.getNextLine()) != null) {
199 int idx
= line
.indexOf(recordElementStart
);
201 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
204 rawPos
= context
.raFile
.getFilePointer();
206 } catch (IOException e
) {
209 context
.setLocation(NULL_LOCATION
);
214 private Element
parseElementBuffer(StringBuffer elementBuffer
) {
216 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
217 DocumentBuilder db
= dbf
.newDocumentBuilder();
219 // The following allows xml parsing without access to the dtd
220 EntityResolver resolver
= new EntityResolver () {
222 public InputSource
resolveEntity (String publicId
, String systemId
) {
223 String empty
= ""; //$NON-NLS-1$
224 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
225 return new InputSource(bais
);
228 db
.setEntityResolver(resolver
);
230 // The following catches xml parsing exceptions
231 db
.setErrorHandler(new ErrorHandler(){
233 public void error(SAXParseException saxparseexception
) throws SAXException
{}
235 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
237 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
238 throw saxparseexception
;
241 Document doc
= db
.parse(new ByteArrayInputStream(elementBuffer
.toString().getBytes()));
242 return doc
.getDocumentElement();
243 } catch (ParserConfigurationException e
) {
245 } catch (SAXException e
) {
247 } catch (IOException e
) {
253 private void readElement(StringBuffer buffer
, RandomAccessFile raFile
) {
256 boolean startTagClosed
= false;
258 while ((i
= raFile
.read()) != -1) {
263 readQuote(buffer
, raFile
, '"');
264 } else if (c
== '\'') {
265 readQuote(buffer
, raFile
, '\'');
266 } else if (c
== '<') {
267 readElement(buffer
, raFile
);
268 } else if (c
== '/' && numRead
== 1) {
270 } else if (c
== '-' && numRead
== 3 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("!-")) { //$NON-NLS-1$
271 readComment(buffer
, raFile
); // found "<!--"
272 } else if (i
== '>') {
273 if (buffer
.charAt(buffer
.length() - 2) == '/') {
275 } else if (startTagClosed
) {
276 break; // found "<...>...</...>"
278 startTagClosed
= true; // found "<...>"
283 } catch (IOException e
) {
288 private void readQuote(StringBuffer buffer
, RandomAccessFile raFile
, char eq
) {
291 while ((i
= raFile
.read()) != -1) {
295 break; // found matching end-quote
299 } catch (IOException e
) {
304 private void readComment(StringBuffer buffer
, RandomAccessFile raFile
) {
308 while ((i
= raFile
.read()) != -1) {
312 if (c
== '>' && numRead
>= 2 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("--")) { //$NON-NLS-1$
313 break; // found "-->"
317 } catch (IOException e
) {
322 public static StringBuffer
parseElement(Element parentElement
, StringBuffer buffer
) {
323 NodeList nodeList
= parentElement
.getChildNodes();
324 String separator
= null;
325 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
326 Node node
= nodeList
.item(i
);
327 if (node
.getNodeType() == Node
.ELEMENT_NODE
) {
328 if (separator
== null) {
329 separator
= " | "; //$NON-NLS-1$
331 buffer
.append(separator
);
333 Element element
= (Element
) node
;
334 if (element
.hasChildNodes() == false) {
335 buffer
.append(element
.getNodeName());
336 } else if (element
.getChildNodes().getLength() == 1 && element
.getFirstChild().getNodeType() == Node
.TEXT_NODE
) {
337 buffer
.append(element
.getNodeName() + ":" + element
.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
339 buffer
.append(element
.getNodeName());
340 buffer
.append(" [ "); //$NON-NLS-1$
341 parseElement(element
, buffer
);
342 buffer
.append(" ]"); //$NON-NLS-1$
344 } else if (node
.getNodeType() == Node
.TEXT_NODE
) {
345 if (node
.getNodeValue().trim().length() != 0) {
346 buffer
.append(node
.getNodeValue().trim());
353 public InputElement
getRecordInputElement(InputElement inputElement
) {
354 if (inputElement
.logEntry
) {
356 } else if (inputElement
.childElements
!= null) {
357 for (InputElement childInputElement
: inputElement
.childElements
) {
358 InputElement recordInputElement
= getRecordInputElement(childInputElement
);
359 if (recordInputElement
!= null) {
360 return recordInputElement
;
367 public CustomXmlEvent
extractEvent(Element element
, InputElement inputElement
) {
368 CustomXmlEvent event
= new CustomXmlEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
,""); //$NON-NLS-1$ //$NON-NLS-2$
369 event
.setContent(new CustomEventContent(event
, "")); //$NON-NLS-1$
370 parseElement(element
, event
, inputElement
);
374 private void parseElement(Element element
, CustomXmlEvent event
, InputElement inputElement
) {
375 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
376 event
.parseInput(parseElement(element
, new StringBuffer()).toString(), inputElement
.inputName
, inputElement
.inputAction
, inputElement
.inputFormat
);
378 if (inputElement
.attributes
!= null) {
379 for (InputAttribute attribute
: inputElement
.attributes
) {
380 event
.parseInput(element
.getAttribute(attribute
.attributeName
), attribute
.inputName
, attribute
.inputAction
, attribute
.inputFormat
);
383 NodeList childNodes
= element
.getChildNodes();
384 if (inputElement
.childElements
!= null) {
385 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
386 Node node
= childNodes
.item(i
);
387 if (node
instanceof Element
) {
388 for (InputElement child
: inputElement
.childElements
) {
389 if (node
.getNodeName().equals(child
.elementName
)) {
390 parseElement((Element
) node
, event
, child
);
400 public CustomTraceDefinition
getDefinition() {