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
) {
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') 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
);
129 if (raFile
!= null) {
132 } catch (IOException e
) {
139 public double getLocationRatio(ITmfLocation
<?
> location
) {
140 RandomAccessFile raFile
= null;
142 if (location
.getLocation() instanceof Long
) {
143 raFile
= new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$
144 return (double) ((Long
) location
.getLocation()) / raFile
.length();
146 } catch (FileNotFoundException e
) {
148 } catch (IOException e
) {
151 if (raFile
!= null) {
154 } catch (IOException e
) {
162 public ITmfLocation
<?
> getCurrentLocation() {
163 // TODO Auto-generated method stub
168 public synchronized TmfEvent
getNextEvent(ITmfContext context
) {
169 ITmfContext savedContext
= context
.clone();
170 TmfEvent event
= parseEvent(context
);
172 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
173 context
.increaseRank();
179 public TmfEvent
parseEvent(ITmfContext tmfContext
) {
180 if (!(tmfContext
instanceof CustomXmlTraceContext
)) {
184 CustomXmlTraceContext context
= (CustomXmlTraceContext
) tmfContext
;
185 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) {
193 context
.raFile
.seek((Long
)context
.getLocation().getLocation() + 1); // +1 is for the <
195 StringBuffer elementBuffer
= new StringBuffer("<"); //$NON-NLS-1$
196 readElement(elementBuffer
, context
.raFile
);
197 Element element
= parseElementBuffer(elementBuffer
);
199 event
= extractEvent(element
, fRecordInputElement
);
200 ((StringBuffer
) event
.getContent().getValue()).append(elementBuffer
);
203 String recordElementStart
= "<" + fRecordInputElement
.elementName
; //$NON-NLS-1$
204 long rawPos
= context
.raFile
.getFilePointer();
206 while ((line
= context
.raFile
.getNextLine()) != null) {
207 int idx
= line
.indexOf(recordElementStart
);
209 context
.setLocation(new TmfLocation
<Long
>(rawPos
+ idx
));
212 rawPos
= context
.raFile
.getFilePointer();
214 } catch (IOException e
) {
217 context
.setLocation(NULL_LOCATION
);
222 private Element
parseElementBuffer(StringBuffer elementBuffer
) {
224 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
225 DocumentBuilder db
= dbf
.newDocumentBuilder();
227 // The following allows xml parsing without access to the dtd
228 EntityResolver resolver
= new EntityResolver () {
230 public InputSource
resolveEntity (String publicId
, String systemId
) {
231 String empty
= ""; //$NON-NLS-1$
232 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
233 return new InputSource(bais
);
236 db
.setEntityResolver(resolver
);
238 // The following catches xml parsing exceptions
239 db
.setErrorHandler(new ErrorHandler(){
241 public void error(SAXParseException saxparseexception
) throws SAXException
{}
243 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
245 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
246 throw saxparseexception
;
249 Document doc
= db
.parse(new ByteArrayInputStream(elementBuffer
.toString().getBytes()));
250 return doc
.getDocumentElement();
251 } catch (ParserConfigurationException e
) {
253 } catch (SAXException e
) {
255 } catch (IOException e
) {
261 private void readElement(StringBuffer buffer
, RandomAccessFile raFile
) {
264 boolean startTagClosed
= false;
266 while ((i
= raFile
.read()) != -1) {
271 readQuote(buffer
, raFile
, '"');
272 } else if (c
== '\'') {
273 readQuote(buffer
, raFile
, '\'');
274 } else if (c
== '<') {
275 readElement(buffer
, raFile
);
276 } else if (c
== '/' && numRead
== 1) {
278 } else if (c
== '-' && numRead
== 3 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("!-")) { //$NON-NLS-1$
279 readComment(buffer
, raFile
); // found "<!--"
280 } else if (i
== '>') {
281 if (buffer
.charAt(buffer
.length() - 2) == '/') {
283 } else if (startTagClosed
) {
284 break; // found "<...>...</...>"
286 startTagClosed
= true; // found "<...>"
291 } catch (IOException e
) {
296 private void readQuote(StringBuffer buffer
, RandomAccessFile raFile
, char eq
) {
299 while ((i
= raFile
.read()) != -1) {
303 break; // found matching end-quote
307 } catch (IOException e
) {
312 private void readComment(StringBuffer buffer
, RandomAccessFile raFile
) {
316 while ((i
= raFile
.read()) != -1) {
320 if (c
== '>' && numRead
>= 2 && buffer
.substring(buffer
.length() - 3, buffer
.length() - 1).equals("--")) { //$NON-NLS-1$
321 break; // found "-->"
325 } catch (IOException e
) {
330 public static StringBuffer
parseElement(Element parentElement
, StringBuffer buffer
) {
331 NodeList nodeList
= parentElement
.getChildNodes();
332 String separator
= null;
333 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
334 Node node
= nodeList
.item(i
);
335 if (node
.getNodeType() == Node
.ELEMENT_NODE
) {
336 if (separator
== null) {
337 separator
= " | "; //$NON-NLS-1$
339 buffer
.append(separator
);
341 Element element
= (Element
) node
;
342 if (element
.hasChildNodes() == false) {
343 buffer
.append(element
.getNodeName());
344 } else if (element
.getChildNodes().getLength() == 1 && element
.getFirstChild().getNodeType() == Node
.TEXT_NODE
) {
345 buffer
.append(element
.getNodeName() + ":" + element
.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
347 buffer
.append(element
.getNodeName());
348 buffer
.append(" [ "); //$NON-NLS-1$
349 parseElement(element
, buffer
);
350 buffer
.append(" ]"); //$NON-NLS-1$
352 } else if (node
.getNodeType() == Node
.TEXT_NODE
) {
353 if (node
.getNodeValue().trim().length() != 0) {
354 buffer
.append(node
.getNodeValue().trim());
361 public InputElement
getRecordInputElement(InputElement inputElement
) {
362 if (inputElement
.logEntry
) {
364 } else if (inputElement
.childElements
!= null) {
365 for (InputElement childInputElement
: inputElement
.childElements
) {
366 InputElement recordInputElement
= getRecordInputElement(childInputElement
);
367 if (recordInputElement
!= null) {
368 return recordInputElement
;
375 public CustomXmlEvent
extractEvent(Element element
, InputElement inputElement
) {
376 CustomXmlEvent event
= new CustomXmlEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
,""); //$NON-NLS-1$ //$NON-NLS-2$
377 event
.setContent(new CustomEventContent(event
, "")); //$NON-NLS-1$
378 parseElement(element
, event
, inputElement
);
382 private void parseElement(Element element
, CustomXmlEvent event
, InputElement inputElement
) {
383 if (inputElement
.inputName
!= null && !inputElement
.inputName
.equals(CustomXmlTraceDefinition
.TAG_IGNORE
)) {
384 event
.parseInput(parseElement(element
, new StringBuffer()).toString(), inputElement
.inputName
, inputElement
.inputAction
, inputElement
.inputFormat
);
386 if (inputElement
.attributes
!= null) {
387 for (InputAttribute attribute
: inputElement
.attributes
) {
388 event
.parseInput(element
.getAttribute(attribute
.attributeName
), attribute
.inputName
, attribute
.inputAction
, attribute
.inputFormat
);
391 NodeList childNodes
= element
.getChildNodes();
392 if (inputElement
.childElements
!= null) {
393 for (int i
= 0; i
< childNodes
.getLength(); i
++) {
394 Node node
= childNodes
.item(i
);
395 if (node
instanceof Element
) {
396 for (InputElement child
: inputElement
.childElements
) {
397 if (node
.getNodeName().equals(child
.elementName
)) {
398 parseElement((Element
) node
, event
, child
);
408 public CustomTraceDefinition
getDefinition() {