Re-structure LTTng sub-project as per the Linux Tools guidelines
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / parsers / custom / CustomXmlTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.parsers.custom;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.File;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.io.RandomAccessFile;
20
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import javax.xml.parsers.ParserConfigurationException;
24
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;
47
48 public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
49
50 private static final TmfLocation<Long> NULL_LOCATION = new TmfLocation<Long>((Long) null);
51
52 private CustomXmlTraceDefinition fDefinition;
53 private CustomXmlEventType fEventType;
54 private InputElement fRecordInputElement;
55
56 public CustomXmlTrace(CustomXmlTraceDefinition definition) {
57 fDefinition = definition;
58 fEventType = new CustomXmlEventType(fDefinition);
59 fRecordInputElement = getRecordInputElement(fDefinition.rootInputElement);
60 }
61
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);
67 }
68
69 @Override
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()) {
75 return context;
76 }
77 try {
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());
81 }
82
83 String line;
84 String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$
85 long rawPos = context.raFile.getFilePointer();
86
87 while ((line = context.raFile.getNextLine()) != null) {
88 int idx = line.indexOf(recordElementStart);
89 if (idx != -1) {
90 context.setLocation(new TmfLocation<Long>(rawPos + idx));
91 return context;
92 }
93 rawPos = context.raFile.getFilePointer();
94 }
95 return context;
96 } catch (FileNotFoundException e) {
97 e.printStackTrace();
98 return context;
99 } catch (IOException e) {
100 e.printStackTrace();
101 return context;
102 }
103
104 }
105
106 @Override
107 public TmfContext seekLocation(double ratio) {
108 try {
109 BufferedRandomAccessFile raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
110 long pos = (long) (ratio * raFile.length());
111 while (pos > 0) {
112 raFile.seek(pos - 1);
113 if (raFile.read() == '\n') break;
114 pos--;
115 }
116 ITmfLocation<?> location = new TmfLocation<Long>(new Long(pos));
117 TmfContext context = seekLocation(location);
118 context.setRank(ITmfContext.UNKNOWN_RANK);
119 return context;
120 } catch (FileNotFoundException e) {
121 e.printStackTrace();
122 return new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
123 } catch (IOException e) {
124 e.printStackTrace();
125 return new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
126 }
127 }
128
129 @Override
130 public double getLocationRatio(ITmfLocation<?> location) {
131 try {
132 if (location.getLocation() instanceof Long) {
133 RandomAccessFile raFile = new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$
134 return (double) ((Long) location.getLocation()) / raFile.length();
135 }
136 } catch (FileNotFoundException e) {
137 e.printStackTrace();
138 } catch (IOException e) {
139 e.printStackTrace();
140 }
141 return 0;
142 }
143
144 @Override
145 @SuppressWarnings({ "rawtypes", "unchecked" })
146 public ITmfTrace copy() {
147 // TODO Auto-generated method stub
148 return null;
149 }
150
151 @Override
152 public ITmfLocation<?> getCurrentLocation() {
153 // TODO Auto-generated method stub
154 return null;
155 }
156
157 @Override
158 public synchronized TmfEvent getNextEvent(TmfContext context) {
159 ITmfContext savedContext = context.clone();
160 TmfEvent event = parseEvent(context);
161 if (event != null) {
162 updateIndex(savedContext, savedContext.getRank(), event.getTimestamp());
163 context.updateRank(1);
164 }
165 return event;
166 }
167
168 @Override
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)) {
172 return null;
173 }
174
175 CustomXmlTraceContext context = (CustomXmlTraceContext) tmfContext;
176 if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
177 return null;
178 }
179
180 synchronized (context.raFile) {
181 CustomXmlEvent event = null;
182 try {
183 if (context.raFile.getFilePointer() != (Long)context.getLocation().getLocation() + 1) {
184 context.raFile.seek((Long)context.getLocation().getLocation() + 1); // +1 is for the <
185 }
186 StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$
187 readElement(elementBuffer, context.raFile);
188 Element element = parseElementBuffer(elementBuffer);
189
190 event = extractEvent(element, fRecordInputElement);
191 ((StringBuffer) event.getContent().getContent()).append(elementBuffer);
192
193 String line;
194 String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$
195 long rawPos = context.raFile.getFilePointer();
196
197 while ((line = context.raFile.getNextLine()) != null) {
198 int idx = line.indexOf(recordElementStart);
199 if (idx != -1) {
200 context.setLocation(new TmfLocation<Long>(rawPos + idx));
201 return event;
202 }
203 rawPos = context.raFile.getFilePointer();
204 }
205 } catch (IOException e) {
206 e.printStackTrace();
207 }
208 context.setLocation(NULL_LOCATION);
209 return event;
210 }
211 }
212
213 private Element parseElementBuffer(StringBuffer elementBuffer) {
214 try {
215 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
216 DocumentBuilder db = dbf.newDocumentBuilder();
217
218 // The following allows xml parsing without access to the dtd
219 EntityResolver resolver = new EntityResolver () {
220 @Override
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);
225 }
226 };
227 db.setEntityResolver(resolver);
228
229 // The following catches xml parsing exceptions
230 db.setErrorHandler(new ErrorHandler(){
231 @Override
232 public void error(SAXParseException saxparseexception) throws SAXException {}
233 @Override
234 public void warning(SAXParseException saxparseexception) throws SAXException {}
235 @Override
236 public void fatalError(SAXParseException saxparseexception) throws SAXException {
237 throw saxparseexception;
238 }});
239
240 Document doc = db.parse(new ByteArrayInputStream(elementBuffer.toString().getBytes()));
241 return doc.getDocumentElement();
242 } catch (ParserConfigurationException e) {
243 e.printStackTrace();
244 } catch (SAXException e) {
245 e.printStackTrace();
246 } catch (IOException e) {
247 e.printStackTrace();
248 }
249 return null;
250 }
251
252 private void readElement(StringBuffer buffer, RandomAccessFile raFile) {
253 try {
254 int numRead = 0;
255 boolean startTagClosed = false;
256 int i;
257 while ((i = raFile.read()) != -1) {
258 numRead++;
259 char c = (char)i;
260 buffer.append(c);
261 if (c == '"') {
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) {
268 break; // found "</"
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) == '/') {
273 break; // found "/>"
274 } else if (startTagClosed) {
275 break; // found "<...>...</...>"
276 } else {
277 startTagClosed = true; // found "<...>"
278 }
279 }
280 }
281 return;
282 } catch (IOException e) {
283 return;
284 }
285 }
286
287 private void readQuote(StringBuffer buffer, RandomAccessFile raFile, char eq) {
288 try {
289 int i;
290 while ((i = raFile.read()) != -1) {
291 char c = (char)i;
292 buffer.append(c);
293 if (c == eq) {
294 break; // found matching end-quote
295 }
296 }
297 return;
298 } catch (IOException e) {
299 return;
300 }
301 }
302
303 private void readComment(StringBuffer buffer, RandomAccessFile raFile) {
304 try {
305 int numRead = 0;
306 int i;
307 while ((i = raFile.read()) != -1) {
308 numRead++;
309 char c = (char)i;
310 buffer.append(c);
311 if (c == '>' && numRead >= 2 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("--")) { //$NON-NLS-1$
312 break; // found "-->"
313 }
314 }
315 return;
316 } catch (IOException e) {
317 return;
318 }
319 }
320
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$
329 } else {
330 buffer.append(separator);
331 }
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$
337 } else {
338 buffer.append(element.getNodeName());
339 buffer.append(" [ "); //$NON-NLS-1$
340 parseElement(element, buffer);
341 buffer.append(" ]"); //$NON-NLS-1$
342 }
343 } else if (node.getNodeType() == Node.TEXT_NODE) {
344 if (node.getNodeValue().trim().length() != 0) {
345 buffer.append(node.getNodeValue().trim());
346 }
347 }
348 }
349 return buffer;
350 }
351
352 public InputElement getRecordInputElement(InputElement inputElement) {
353 if (inputElement.logEntry) {
354 return inputElement;
355 } else if (inputElement.childElements != null) {
356 for (InputElement childInputElement : inputElement.childElements) {
357 InputElement recordInputElement = getRecordInputElement(childInputElement);
358 if (recordInputElement != null) {
359 return recordInputElement;
360 }
361 }
362 }
363 return null;
364 }
365
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);
370 return event;
371 }
372
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);
376 }
377 if (inputElement.attributes != null) {
378 for (InputAttribute attribute : inputElement.attributes) {
379 event.parseInput(element.getAttribute(attribute.attributeName), attribute.inputName, attribute.inputAction, attribute.inputFormat);
380 }
381 }
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);
390 break;
391 }
392 }
393 }
394 }
395 }
396 return;
397 }
398
399 public CustomTraceDefinition getDefinition() {
400 return fDefinition;
401 }
402 }
This page took 0.042895 seconds and 6 git commands to generate.