Merge branch 'master' into TmfTraceModel-new
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / 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.internal.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.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;
44
45 public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
46
47 private static final TmfLocation<Long> NULL_LOCATION = new TmfLocation<Long>((Long) null);
48 private static final int DEFAULT_CACHE_SIZE = 100;
49
50 private CustomXmlTraceDefinition fDefinition;
51 private CustomXmlEventType fEventType;
52 private InputElement fRecordInputElement;
53
54 public CustomXmlTrace(CustomXmlTraceDefinition definition) {
55 fDefinition = definition;
56 fEventType = new CustomXmlEventType(fDefinition);
57 fRecordInputElement = getRecordInputElement(fDefinition.rootInputElement);
58 }
59
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);
65 }
66
67 @Override
68 public void initTrace(String name, String path, Class<CustomXmlEvent> eventType) throws FileNotFoundException {
69 super.initTrace(name, path, eventType);
70 }
71
72 @Override
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()) {
76 return context;
77 }
78 try {
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());
82 }
83
84 String line;
85 String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$
86 long rawPos = context.raFile.getFilePointer();
87
88 while ((line = context.raFile.getNextLine()) != null) {
89 int idx = line.indexOf(recordElementStart);
90 if (idx != -1) {
91 context.setLocation(new TmfLocation<Long>(rawPos + idx));
92 return context;
93 }
94 rawPos = context.raFile.getFilePointer();
95 }
96 return context;
97 } catch (FileNotFoundException e) {
98 e.printStackTrace();
99 return context;
100 } catch (IOException e) {
101 e.printStackTrace();
102 return context;
103 }
104
105 }
106
107 @Override
108 public TmfContext seekLocation(double ratio) {
109 BufferedRandomAccessFile raFile = null;
110 try {
111 raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
112 long pos = (long) (ratio * raFile.length());
113 while (pos > 0) {
114 raFile.seek(pos - 1);
115 if (raFile.read() == '\n') break;
116 pos--;
117 }
118 ITmfLocation<?> location = new TmfLocation<Long>(pos);
119 TmfContext context = seekLocation(location);
120 context.setRank(ITmfContext.UNKNOWN_RANK);
121 return context;
122 } catch (FileNotFoundException e) {
123 e.printStackTrace();
124 return new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
125 } catch (IOException e) {
126 e.printStackTrace();
127 return new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
128 } finally {
129 if (raFile != null) {
130 try {
131 raFile.close();
132 } catch (IOException e) {
133 }
134 }
135 }
136 }
137
138 @Override
139 public double getLocationRatio(ITmfLocation<?> location) {
140 RandomAccessFile raFile = null;
141 try {
142 if (location.getLocation() instanceof Long) {
143 raFile = new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$
144 return (double) ((Long) location.getLocation()) / raFile.length();
145 }
146 } catch (FileNotFoundException e) {
147 e.printStackTrace();
148 } catch (IOException e) {
149 e.printStackTrace();
150 } finally {
151 if (raFile != null) {
152 try {
153 raFile.close();
154 } catch (IOException e) {
155 }
156 }
157 }
158 return 0;
159 }
160
161 @Override
162 public ITmfLocation<?> getCurrentLocation() {
163 // TODO Auto-generated method stub
164 return null;
165 }
166
167 @Override
168 public synchronized TmfEvent getNextEvent(ITmfContext context) {
169 ITmfContext savedContext = context.clone();
170 TmfEvent event = parseEvent(context);
171 if (event != null) {
172 updateIndex(savedContext, savedContext.getRank(), event.getTimestamp());
173 context.increaseRank();
174 }
175 return event;
176 }
177
178 @Override
179 public TmfEvent parseEvent(ITmfContext tmfContext) {
180 if (!(tmfContext instanceof CustomXmlTraceContext)) {
181 return null;
182 }
183
184 CustomXmlTraceContext context = (CustomXmlTraceContext) tmfContext;
185 if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
186 return null;
187 }
188
189 synchronized (context.raFile) {
190 CustomXmlEvent event = null;
191 try {
192 if (context.raFile.getFilePointer() != (Long)context.getLocation().getLocation() + 1) {
193 context.raFile.seek((Long)context.getLocation().getLocation() + 1); // +1 is for the <
194 }
195 StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$
196 readElement(elementBuffer, context.raFile);
197 Element element = parseElementBuffer(elementBuffer);
198
199 event = extractEvent(element, fRecordInputElement);
200 ((StringBuffer) event.getContent().getValue()).append(elementBuffer);
201
202 String line;
203 String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$
204 long rawPos = context.raFile.getFilePointer();
205
206 while ((line = context.raFile.getNextLine()) != null) {
207 int idx = line.indexOf(recordElementStart);
208 if (idx != -1) {
209 context.setLocation(new TmfLocation<Long>(rawPos + idx));
210 return event;
211 }
212 rawPos = context.raFile.getFilePointer();
213 }
214 } catch (IOException e) {
215 e.printStackTrace();
216 }
217 context.setLocation(NULL_LOCATION);
218 return event;
219 }
220 }
221
222 private Element parseElementBuffer(StringBuffer elementBuffer) {
223 try {
224 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
225 DocumentBuilder db = dbf.newDocumentBuilder();
226
227 // The following allows xml parsing without access to the dtd
228 EntityResolver resolver = new EntityResolver () {
229 @Override
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);
234 }
235 };
236 db.setEntityResolver(resolver);
237
238 // The following catches xml parsing exceptions
239 db.setErrorHandler(new ErrorHandler(){
240 @Override
241 public void error(SAXParseException saxparseexception) throws SAXException {}
242 @Override
243 public void warning(SAXParseException saxparseexception) throws SAXException {}
244 @Override
245 public void fatalError(SAXParseException saxparseexception) throws SAXException {
246 throw saxparseexception;
247 }});
248
249 Document doc = db.parse(new ByteArrayInputStream(elementBuffer.toString().getBytes()));
250 return doc.getDocumentElement();
251 } catch (ParserConfigurationException e) {
252 e.printStackTrace();
253 } catch (SAXException e) {
254 e.printStackTrace();
255 } catch (IOException e) {
256 e.printStackTrace();
257 }
258 return null;
259 }
260
261 private void readElement(StringBuffer buffer, RandomAccessFile raFile) {
262 try {
263 int numRead = 0;
264 boolean startTagClosed = false;
265 int i;
266 while ((i = raFile.read()) != -1) {
267 numRead++;
268 char c = (char)i;
269 buffer.append(c);
270 if (c == '"') {
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) {
277 break; // found "</"
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) == '/') {
282 break; // found "/>"
283 } else if (startTagClosed) {
284 break; // found "<...>...</...>"
285 } else {
286 startTagClosed = true; // found "<...>"
287 }
288 }
289 }
290 return;
291 } catch (IOException e) {
292 return;
293 }
294 }
295
296 private void readQuote(StringBuffer buffer, RandomAccessFile raFile, char eq) {
297 try {
298 int i;
299 while ((i = raFile.read()) != -1) {
300 char c = (char)i;
301 buffer.append(c);
302 if (c == eq) {
303 break; // found matching end-quote
304 }
305 }
306 return;
307 } catch (IOException e) {
308 return;
309 }
310 }
311
312 private void readComment(StringBuffer buffer, RandomAccessFile raFile) {
313 try {
314 int numRead = 0;
315 int i;
316 while ((i = raFile.read()) != -1) {
317 numRead++;
318 char c = (char)i;
319 buffer.append(c);
320 if (c == '>' && numRead >= 2 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("--")) { //$NON-NLS-1$
321 break; // found "-->"
322 }
323 }
324 return;
325 } catch (IOException e) {
326 return;
327 }
328 }
329
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$
338 } else {
339 buffer.append(separator);
340 }
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$
346 } else {
347 buffer.append(element.getNodeName());
348 buffer.append(" [ "); //$NON-NLS-1$
349 parseElement(element, buffer);
350 buffer.append(" ]"); //$NON-NLS-1$
351 }
352 } else if (node.getNodeType() == Node.TEXT_NODE) {
353 if (node.getNodeValue().trim().length() != 0) {
354 buffer.append(node.getNodeValue().trim());
355 }
356 }
357 }
358 return buffer;
359 }
360
361 public InputElement getRecordInputElement(InputElement inputElement) {
362 if (inputElement.logEntry) {
363 return inputElement;
364 } else if (inputElement.childElements != null) {
365 for (InputElement childInputElement : inputElement.childElements) {
366 InputElement recordInputElement = getRecordInputElement(childInputElement);
367 if (recordInputElement != null) {
368 return recordInputElement;
369 }
370 }
371 }
372 return null;
373 }
374
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);
379 return event;
380 }
381
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);
385 }
386 if (inputElement.attributes != null) {
387 for (InputAttribute attribute : inputElement.attributes) {
388 event.parseInput(element.getAttribute(attribute.attributeName), attribute.inputName, attribute.inputAction, attribute.inputFormat);
389 }
390 }
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);
399 break;
400 }
401 }
402 }
403 }
404 }
405 return;
406 }
407
408 public CustomTraceDefinition getDefinition() {
409 return fDefinition;
410 }
411 }
This page took 0.041228 seconds and 6 git commands to generate.