Contribute CNF based TMF project handling
[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.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;
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(String name, CustomXmlTraceDefinition definition, String path, int cacheSize) throws FileNotFoundException {
57 super(name, CustomXmlEvent.class, path, cacheSize);
58 fDefinition = definition;
59 fEventType = new CustomXmlEventType(fDefinition);
60 fRecordInputElement = getRecordInputElement(fDefinition.rootInputElement);
61 }
62
63 @Override
64 public TmfContext seekLocation(ITmfLocation<?> location) {
65 //System.out.println(Thread.currentThread().getName() + "::" + getName() + " seekLocation(" + ((location == null || location.getLocation() == null) ? "null" : location) + ")");
66 //new Throwable().printStackTrace();
67 CustomXmlTraceContext context = new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
68 if (NULL_LOCATION.equals(location) || !new File(getPath()).isFile()) {
69 return context;
70 }
71 try {
72 context.raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
73 if (location != null && location.getLocation() instanceof Long) {
74 context.raFile.seek((Long)location.getLocation());
75 }
76
77 String line;
78 String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$
79 long rawPos = context.raFile.getFilePointer();
80
81 while ((line = context.raFile.getNextLine()) != null) {
82 int idx = line.indexOf(recordElementStart);
83 if (idx != -1) {
84 context.setLocation(new TmfLocation<Long>(rawPos + idx));
85 return context;
86 }
87 rawPos = context.raFile.getFilePointer();
88 }
89 return context;
90 } catch (FileNotFoundException e) {
91 e.printStackTrace();
92 return context;
93 } catch (IOException e) {
94 e.printStackTrace();
95 return context;
96 }
97
98 }
99
100 @Override
101 public TmfContext seekLocation(double ratio) {
102 try {
103 BufferedRandomAccessFile raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
104 long pos = (long) (ratio * raFile.length());
105 while (pos > 0) {
106 raFile.seek(pos - 1);
107 if (raFile.read() == '\n') break;
108 pos--;
109 }
110 ITmfLocation<?> location = new TmfLocation<Long>(new Long(pos));
111 TmfContext context = seekLocation(location);
112 context.setRank(ITmfContext.UNKNOWN_RANK);
113 return context;
114 } catch (FileNotFoundException e) {
115 e.printStackTrace();
116 return new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
117 } catch (IOException e) {
118 e.printStackTrace();
119 return new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
120 }
121 }
122
123 @Override
124 public double getLocationRatio(ITmfLocation<?> location) {
125 try {
126 if (location.getLocation() instanceof Long) {
127 RandomAccessFile raFile = new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$
128 return (double) ((Long) location.getLocation()) / raFile.length();
129 }
130 } catch (FileNotFoundException e) {
131 e.printStackTrace();
132 } catch (IOException e) {
133 e.printStackTrace();
134 }
135 return 0;
136 }
137
138 @Override
139 @SuppressWarnings({ "rawtypes", "unchecked" })
140 public ITmfTrace copy() {
141 // TODO Auto-generated method stub
142 return null;
143 }
144
145 @Override
146 public ITmfLocation<?> getCurrentLocation() {
147 // TODO Auto-generated method stub
148 return null;
149 }
150
151 @Override
152 public synchronized TmfEvent getNextEvent(TmfContext context) {
153 ITmfContext savedContext = context.clone();
154 TmfEvent event = parseEvent(context);
155 if (event != null) {
156 updateIndex(savedContext, savedContext.getRank(), event.getTimestamp());
157 context.updateRank(1);
158 }
159 return event;
160 }
161
162 @Override
163 public TmfEvent parseEvent(TmfContext tmfContext) {
164 //System.out.println(Thread.currentThread().getName() + ":: " + getName() + " parseEvent(" + tmfContext.getRank() + " @ " + (tmfContext.getLocation().getLocation() == null ? "null" : tmfContext.getLocation()));
165 if (!(tmfContext instanceof CustomXmlTraceContext)) {
166 return null;
167 }
168
169 CustomXmlTraceContext context = (CustomXmlTraceContext) tmfContext;
170 if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
171 return null;
172 }
173
174 synchronized (context.raFile) {
175 CustomXmlEvent event = null;
176 try {
177 if (context.raFile.getFilePointer() != (Long)context.getLocation().getLocation() + 1) {
178 context.raFile.seek((Long)context.getLocation().getLocation() + 1); // +1 is for the <
179 }
180 StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$
181 readElement(elementBuffer, context.raFile);
182 Element element = parseElementBuffer(elementBuffer);
183
184 event = extractEvent(element, fRecordInputElement);
185 ((StringBuffer) event.getContent().getContent()).append(elementBuffer);
186
187 String line;
188 String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$
189 long rawPos = context.raFile.getFilePointer();
190
191 while ((line = context.raFile.getNextLine()) != null) {
192 int idx = line.indexOf(recordElementStart);
193 if (idx != -1) {
194 context.setLocation(new TmfLocation<Long>(rawPos + idx));
195 return event;
196 }
197 rawPos = context.raFile.getFilePointer();
198 }
199 } catch (IOException e) {
200 e.printStackTrace();
201 }
202 context.setLocation(NULL_LOCATION);
203 return event;
204 }
205 }
206
207 private Element parseElementBuffer(StringBuffer elementBuffer) {
208 try {
209 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
210 DocumentBuilder db = dbf.newDocumentBuilder();
211
212 // The following allows xml parsing without access to the dtd
213 EntityResolver resolver = new EntityResolver () {
214 @Override
215 public InputSource resolveEntity (String publicId, String systemId) {
216 String empty = ""; //$NON-NLS-1$
217 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
218 return new InputSource(bais);
219 }
220 };
221 db.setEntityResolver(resolver);
222
223 // The following catches xml parsing exceptions
224 db.setErrorHandler(new ErrorHandler(){
225 @Override
226 public void error(SAXParseException saxparseexception) throws SAXException {}
227 @Override
228 public void warning(SAXParseException saxparseexception) throws SAXException {}
229 @Override
230 public void fatalError(SAXParseException saxparseexception) throws SAXException {
231 throw saxparseexception;
232 }});
233
234 Document doc = db.parse(new ByteArrayInputStream(elementBuffer.toString().getBytes()));
235 return doc.getDocumentElement();
236 } catch (ParserConfigurationException e) {
237 e.printStackTrace();
238 } catch (SAXException e) {
239 e.printStackTrace();
240 } catch (IOException e) {
241 e.printStackTrace();
242 }
243 return null;
244 }
245
246 private void readElement(StringBuffer buffer, RandomAccessFile raFile) {
247 try {
248 int numRead = 0;
249 boolean startTagClosed = false;
250 int i;
251 while ((i = raFile.read()) != -1) {
252 numRead++;
253 char c = (char)i;
254 buffer.append(c);
255 if (c == '"') {
256 readQuote(buffer, raFile, '"');
257 } else if (c == '\'') {
258 readQuote(buffer, raFile, '\'');
259 } else if (c == '<') {
260 readElement(buffer, raFile);
261 } else if (c == '/' && numRead == 1) {
262 break; // found "</"
263 } else if (c == '-' && numRead == 3 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("!-")) { //$NON-NLS-1$
264 readComment(buffer, raFile); // found "<!--"
265 } else if (i == '>') {
266 if (buffer.charAt(buffer.length() - 2) == '/') {
267 break; // found "/>"
268 } else if (startTagClosed) {
269 break; // found "<...>...</...>"
270 } else {
271 startTagClosed = true; // found "<...>"
272 }
273 }
274 }
275 return;
276 } catch (IOException e) {
277 return;
278 }
279 }
280
281 private void readQuote(StringBuffer buffer, RandomAccessFile raFile, char eq) {
282 try {
283 int i;
284 while ((i = raFile.read()) != -1) {
285 char c = (char)i;
286 buffer.append(c);
287 if (c == eq) {
288 break; // found matching end-quote
289 }
290 }
291 return;
292 } catch (IOException e) {
293 return;
294 }
295 }
296
297 private void readComment(StringBuffer buffer, RandomAccessFile raFile) {
298 try {
299 int numRead = 0;
300 int i;
301 while ((i = raFile.read()) != -1) {
302 numRead++;
303 char c = (char)i;
304 buffer.append(c);
305 if (c == '>' && numRead >= 2 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("--")) { //$NON-NLS-1$
306 break; // found "-->"
307 }
308 }
309 return;
310 } catch (IOException e) {
311 return;
312 }
313 }
314
315 public static StringBuffer parseElement(Element parentElement, StringBuffer buffer) {
316 NodeList nodeList = parentElement.getChildNodes();
317 String separator = null;
318 for (int i = 0; i < nodeList.getLength(); i++) {
319 Node node = nodeList.item(i);
320 if (node.getNodeType() == Node.ELEMENT_NODE) {
321 if (separator == null) {
322 separator = " | "; //$NON-NLS-1$
323 } else {
324 buffer.append(separator);
325 }
326 Element element = (Element) node;
327 if (element.hasChildNodes() == false) {
328 buffer.append(element.getNodeName());
329 } else if (element.getChildNodes().getLength() == 1 && element.getFirstChild().getNodeType() == Node.TEXT_NODE) {
330 buffer.append(element.getNodeName() + ":" + element.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
331 } else {
332 buffer.append(element.getNodeName());
333 buffer.append(" [ "); //$NON-NLS-1$
334 parseElement(element, buffer);
335 buffer.append(" ]"); //$NON-NLS-1$
336 }
337 } else if (node.getNodeType() == Node.TEXT_NODE) {
338 if (node.getNodeValue().trim().length() != 0) {
339 buffer.append(node.getNodeValue().trim());
340 }
341 }
342 }
343 return buffer;
344 }
345
346 public InputElement getRecordInputElement(InputElement inputElement) {
347 if (inputElement.logEntry) {
348 return inputElement;
349 } else if (inputElement.childElements != null) {
350 for (InputElement childInputElement : inputElement.childElements) {
351 InputElement recordInputElement = getRecordInputElement(childInputElement);
352 if (recordInputElement != null) {
353 return recordInputElement;
354 }
355 }
356 }
357 return null;
358 }
359
360 public CustomXmlEvent extractEvent(Element element, InputElement inputElement) {
361 CustomXmlEvent event = new CustomXmlEvent(fDefinition, TmfTimestamp.Zero, new TmfEventSource(""), fEventType, new TmfEventReference("")); //$NON-NLS-1$ //$NON-NLS-2$
362 event.setContent(new CustomEventContent(event, new StringBuffer()));
363 parseElement(element, event, inputElement);
364 return event;
365 }
366
367 private void parseElement(Element element, CustomXmlEvent event, InputElement inputElement) {
368 if (inputElement.inputName != null && !inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_IGNORE)) {
369 event.parseInput(parseElement(element, new StringBuffer()).toString(), inputElement.inputName, inputElement.inputAction, inputElement.inputFormat);
370 }
371 if (inputElement.attributes != null) {
372 for (InputAttribute attribute : inputElement.attributes) {
373 event.parseInput(element.getAttribute(attribute.attributeName), attribute.inputName, attribute.inputAction, attribute.inputFormat);
374 }
375 }
376 NodeList childNodes = element.getChildNodes();
377 if (inputElement.childElements != null) {
378 for (int i = 0; i < childNodes.getLength(); i++) {
379 Node node = childNodes.item(i);
380 if (node instanceof Element) {
381 for (InputElement child : inputElement.childElements) {
382 if (node.getNodeName().equals(child.elementName)) {
383 parseElement((Element) node, event, child);
384 break;
385 }
386 }
387 }
388 }
389 }
390 return;
391 }
392
393 public CustomTraceDefinition getDefinition() {
394 return fDefinition;
395 }
396 }
This page took 0.038503 seconds and 5 git commands to generate.