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