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
.event
.TmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventReference
;
27 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventSource
;
28 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
29 import org
.eclipse
.linuxtools
.tmf
.io
.BufferedRandomAccessFile
;
30 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfContext
;
31 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfLocation
;
32 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfTrace
;
33 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfContext
;
34 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfLocation
;
35 import org
.eclipse
.linuxtools
.tmf
.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);
52 private CustomXmlTraceDefinition fDefinition
;
53 private CustomXmlEventType fEventType
;
54 private InputElement fRecordInputElement
;
56 public CustomXmlTrace(CustomXmlTraceDefinition definition
) {
57 fDefinition
= definition
;
58 fEventType
= new CustomXmlEventType(fDefinition
);
59 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
62 public CustomXmlTrace(String name
, CustomXmlTraceDefinition definition
, String path
, int cacheSize
) throws FileNotFoundException
{
63 super(name
, CustomXmlEvent
.class, path
, cacheSize
);
64 fDefinition
= definition
;
65 fEventType
= new CustomXmlEventType(fDefinition
);
66 fRecordInputElement
= getRecordInputElement(fDefinition
.rootInputElement
);
70 public TmfContext
seekLocation(ITmfLocation
<?
> location
) {
71 //System.out.println(Thread.currentThread().getName() + "::" + getName() + " seekLocation(" + ((location == null || location.getLocation() == null) ? "null" : location) + ")");
72 //new Throwable().printStackTrace();
73 CustomXmlTraceContext context
= new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
74 if (NULL_LOCATION
.equals(location
) || !new File(getPath()).isFile()) {
78 context
.raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
79 if (location
!= null && location
.getLocation() instanceof Long
) {
80 context
.raFile
.seek((Long
)location
.getLocation());
84 String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
85 long rawPos
= context
.raFile
.getFilePointer();
87 while ((line
= context
.raFile
.getNextLine()) != null) {
88 int idx
= line
.indexOf(recordElementStart
);
90 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
93 rawPos
= context
.raFile
.getFilePointer();
96 } catch (FileNotFoundException e
) {
99 } catch (IOException e
) {
107 public TmfContext
seekLocation(double ratio
) {
109 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
110 long pos
= (long) (ratio
* raFile
.length());
112 raFile
.seek(pos
- 1);
113 if (raFile
.read() == '\n') break;
116 ITmfLocation
<?
> location
= new TmfLocation
<Long
>(new Long(pos
));
117 TmfContext context
= seekLocation(location
);
118 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
120 } catch (FileNotFoundException e
) {
122 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
123 } catch (IOException e
) {
125 return new CustomXmlTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
130 public double getLocationRatio(ITmfLocation
<?
> location
) {
132 if (location
.getLocation() instanceof Long
) {
133 RandomAccessFile raFile
= new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$
134 return (double) ((Long
) location
.getLocation()) / raFile
.length();
136 } catch (FileNotFoundException e
) {
138 } catch (IOException e
) {
145 @SuppressWarnings({ "rawtypes", "unchecked" })
146 public ITmfTrace
copy() {
147 // TODO Auto-generated method stub
152 public ITmfLocation
<?
> getCurrentLocation() {
153 // TODO Auto-generated method stub
158 public synchronized TmfEvent
getNextEvent(TmfContext context
) {
159 ITmfContext savedContext
= context
.clone();
160 TmfEvent event
= parseEvent(context
);
162 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
163 context
.updateRank(1);
169 public TmfEvent
parseEvent(TmfContext tmfContext
) {
170 //System.out.println(Thread.currentThread().getName() + ":: " + getName() + " parseEvent(" + tmfContext.getRank() + " @ " + (tmfContext.getLocation().getLocation() == null ? "null" : tmfContext.getLocation()));
171 if (!(tmfContext
instanceof CustomXmlTraceContext
)) {
175 CustomXmlTraceContext context
= (CustomXmlTraceContext
) tmfContext
;
176 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
180 synchronized (context
.raFile
) {
181 CustomXmlEvent event
= null;
183 if (context
.raFile
.getFilePointer() != (Long
)context
.getLocation().getLocation() + 1) {
184 context
.raFile
.seek((Long
)context
.getLocation().getLocation() + 1); // +1 is for the <
186 StringBuffer elementBuffer
= new StringBuffer("<"); //$NON-NLS-1$
187 readElement(elementBuffer
, context
.raFile
);
188 Element element
= parseElementBuffer(elementBuffer
);
190 event
= extractEvent(element
, fRecordInputElement
);
191 ((StringBuffer
) event
.getContent().getContent()).append(elementBuffer
);
194 String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
195 long rawPos
= context
.raFile
.getFilePointer();
197 while ((line
= context
.raFile
.getNextLine()) != null) {
198 int idx
= line
.indexOf(recordElementStart
);
200 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
203 rawPos
= context
.raFile
.getFilePointer();
205 } catch (IOException e
) {
208 context
.setLocation(NULL_LOCATION
);
213 private Element
parseElementBuffer(StringBuffer elementBuffer
) {
215 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
216 DocumentBuilder db
= dbf
.newDocumentBuilder();
218 // The following allows xml parsing without access to the dtd
219 EntityResolver resolver
= new EntityResolver () {
221 public InputSource
resolveEntity (String publicId
, String systemId
) {
222 String empty
= ""; //$NON-NLS-1$
223 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
224 return new InputSource(bais
);
227 db
.setEntityResolver(resolver
);
229 // The following catches xml parsing exceptions
230 db
.setErrorHandler(new ErrorHandler(){
232 public void error(SAXParseException saxparseexception
) throws SAXException
{}
234 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
236 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
237 throw saxparseexception
;
240 Document doc
= db
.parse(new ByteArrayInputStream(elementBuffer
.toString().getBytes()));
241 return doc
.getDocumentElement();
242 } catch (ParserConfigurationException e
) {
244 } catch (SAXException e
) {
246 } catch (IOException e
) {
252 private void readElement(StringBuffer buffer
, RandomAccessFile raFile
) {
255 boolean startTagClosed
= false;
257 while ((i
= raFile
.read()) != -1) {
262 readQuote(buffer
, raFile
, '"');
263 } else if (c
== '\'') {
264 readQuote(buffer
, raFile
, '\'');
265 } else if (c
== '<') {
266 readElement(buffer
, raFile
);
267 } else if (c
== '/' && numRead
== 1) {
269 } else if (c
== '-' && numRead
== 3 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("!-")) { //$NON-NLS-1$
270 readComment(buffer
, raFile
); // found "<!--"
271 } else if (i
== '>') {
272 if (buffer
.charAt(buffer
.length() - 2) == '/') {
274 } else if (startTagClosed
) {
275 break; // found "<...>...</...>"
277 startTagClosed
= true; // found "<...>"
282 } catch (IOException e
) {
287 private void readQuote(StringBuffer buffer
, RandomAccessFile raFile
, char eq
) {
290 while ((i
= raFile
.read()) != -1) {
294 break; // found matching end-quote
298 } catch (IOException e
) {
303 private void readComment(StringBuffer buffer
, RandomAccessFile raFile
) {
307 while ((i
= raFile
.read()) != -1) {
311 if (c
== '>' && numRead
>= 2 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("--")) { //$NON-NLS-1$
312 break; // found "-->"
316 } catch (IOException e
) {
321 public static StringBuffer
parseElement(Element parentElement
, StringBuffer buffer
) {
322 NodeList nodeList
= parentElement
.getChildNodes();
323 String separator
= null;
324 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
325 Node node
= nodeList
.item(i
);
326 if (node
.getNodeType() == Node
.ELEMENT_NODE
) {
327 if (separator
== null) {
328 separator
= " | "; //$NON-NLS-1$
330 buffer
.append(separator
);
332 Element element
= (Element
) node
;
333 if (element
.hasChildNodes() == false) {
334 buffer
.append(element
.getNodeName());
335 } else if (element
.getChildNodes().getLength() == 1 && element
.getFirstChild().getNodeType() == Node
.TEXT_NODE
) {
336 buffer
.append(element
.getNodeName() + ":" + element
.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
338 buffer
.append(element
.getNodeName());
339 buffer
.append(" [ "); //$NON-NLS-1$
340 parseElement(element
, buffer
);
341 buffer
.append(" ]"); //$NON-NLS-1$
343 } else if (node
.getNodeType() == Node
.TEXT_NODE
) {
344 if (node
.getNodeValue().trim().length() != 0) {
345 buffer
.append(node
.getNodeValue().trim());
352 public InputElement
getRecordInputElement(InputElement inputElement
) {
353 if (inputElement
.logEntry
) {
355 } else if (inputElement
.childElements
!= null) {
356 for (InputElement childInputElement
: inputElement
.childElements
) {
357 InputElement recordInputElement
= getRecordInputElement(childInputElement
);
358 if (recordInputElement
!= null) {
359 return recordInputElement
;
366 public CustomXmlEvent
extractEvent(Element element
, InputElement inputElement
) {
367 CustomXmlEvent event
= new CustomXmlEvent(fDefinition
, this, TmfTimestamp
.Zero
, new TmfEventSource(""), fEventType
, new TmfEventReference("")); //$NON-NLS-1$ //$NON-NLS-2$
368 event
.setContent(new CustomEventContent(event
, new StringBuffer()));
369 parseElement(element
, event
, inputElement
);
373 private void parseElement(Element element
, CustomXmlEvent event
, InputElement inputElement
) {
374 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
375 event
.parseInput(parseElement(element
, new StringBuffer()).toString(), inputElement
.inputName
, inputElement
.inputAction
, inputElement
.inputFormat
);
377 if (inputElement
.attributes
!= null) {
378 for (InputAttribute attribute
: inputElement
.attributes
) {
379 event
.parseInput(element
.getAttribute(attribute
.attributeName
), attribute
.inputName
, attribute
.inputAction
, attribute
.inputFormat
);
382 NodeList childNodes
= element
.getChildNodes();
383 if (inputElement
.childElements
!= null) {
384 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
385 Node node
= childNodes
.item(i
);
386 if (node
instanceof Element
) {
387 for (InputElement child
: inputElement
.childElements
) {
388 if (node
.getNodeName().equals(child
.elementName
)) {
389 parseElement((Element
) node
, event
, child
);
399 public CustomTraceDefinition
getDefinition() {