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