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 | |
d4011df2 FC |
100 | @Override\r |
101 | public ITmfTrace createTraceCopy() {\r | |
c3c5c786 FC |
102 | // TODO Auto-generated method stub\r |
103 | return null;\r | |
104 | }\r | |
105 | \r | |
106 | @Override\r | |
107 | public ITmfLocation<?> getCurrentLocation() {\r | |
108 | return new TmfLocation<Object>(null);\r | |
109 | }\r | |
110 | \r | |
111 | @Override\r | |
112 | public synchronized TmfEvent getNextEvent(TmfContext context) {\r | |
113 | ITmfContext savedContext = context.clone();\r | |
114 | TmfEvent event = parseEvent(context);\r | |
115 | if (event != null) {\r | |
116 | updateIndex(savedContext, savedContext.getRank(), event.getTimestamp());\r | |
117 | context.updateRank(1);\r | |
118 | }\r | |
119 | return event;\r | |
120 | }\r | |
121 | \r | |
122 | @Override\r | |
123 | public TmfEvent parseEvent(TmfContext tmfContext) {\r | |
124 | //System.out.println(Thread.currentThread().getName() + ":: " + getName() + " parseEvent(" + tmfContext.getRank() + " @ " + (tmfContext.getLocation().getLocation() == null ? "null" : tmfContext.getLocation()));\r | |
125 | if (!(tmfContext instanceof CustomXmlTraceContext)) {\r | |
126 | return null;\r | |
127 | }\r | |
128 | \r | |
129 | CustomXmlTraceContext context = (CustomXmlTraceContext) tmfContext;\r | |
d7fcacc9 | 130 | if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {\r |
c3c5c786 FC |
131 | return null;\r |
132 | }\r | |
133 | \r | |
134 | synchronized (context.raFile) {\r | |
135 | CustomXmlEvent event = null;\r | |
136 | try {\r | |
137 | if (context.raFile.getFilePointer() != (Long)context.getLocation().getLocation() + 1) {\r | |
138 | context.raFile.seek((Long)context.getLocation().getLocation() + 1); // +1 is for the <\r | |
139 | }\r | |
3b38ea61 | 140 | StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$\r |
c3c5c786 FC |
141 | readElement(elementBuffer, context.raFile);\r |
142 | Element element = parseElementBuffer(elementBuffer);\r | |
143 | \r | |
144 | event = extractEvent(element, fRecordInputElement);\r | |
d7fcacc9 | 145 | ((StringBuffer) event.getContent().getContent()).append(elementBuffer);\r |
c3c5c786 FC |
146 | \r |
147 | String line;\r | |
3b38ea61 | 148 | String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$\r |
c3c5c786 FC |
149 | long rawPos = context.raFile.getFilePointer();\r |
150 | \r | |
d7fcacc9 | 151 | while ((line = context.raFile.getNextLine()) != null) {\r |
c3c5c786 FC |
152 | int idx = line.indexOf(recordElementStart); \r |
153 | if (idx != -1) {\r | |
154 | context.setLocation(new TmfLocation<Long>(rawPos + idx));\r | |
155 | return event;\r | |
156 | }\r | |
157 | rawPos = context.raFile.getFilePointer();\r | |
158 | }\r | |
159 | } catch (IOException e) {\r | |
160 | e.printStackTrace();\r | |
161 | }\r | |
d7fcacc9 | 162 | context.setLocation(NULL_LOCATION);\r |
c3c5c786 FC |
163 | return event;\r |
164 | }\r | |
165 | }\r | |
166 | \r | |
167 | private Element parseElementBuffer(StringBuffer elementBuffer) {\r | |
168 | try {\r | |
169 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r | |
170 | DocumentBuilder db = dbf.newDocumentBuilder();\r | |
171 | \r | |
172 | // The following allows xml parsing without access to the dtd\r | |
173 | EntityResolver resolver = new EntityResolver () {\r | |
d4011df2 FC |
174 | @Override\r |
175 | public InputSource resolveEntity (String publicId, String systemId) {\r | |
3b38ea61 | 176 | String empty = ""; //$NON-NLS-1$\r |
c3c5c786 FC |
177 | ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());\r |
178 | return new InputSource(bais);\r | |
179 | }\r | |
180 | };\r | |
181 | db.setEntityResolver(resolver);\r | |
182 | \r | |
183 | // The following catches xml parsing exceptions\r | |
184 | db.setErrorHandler(new ErrorHandler(){\r | |
d4011df2 FC |
185 | @Override\r |
186 | public void error(SAXParseException saxparseexception) throws SAXException {}\r | |
187 | @Override\r | |
188 | public void warning(SAXParseException saxparseexception) throws SAXException {}\r | |
189 | @Override\r | |
190 | public void fatalError(SAXParseException saxparseexception) throws SAXException {\r | |
c3c5c786 FC |
191 | throw saxparseexception;\r |
192 | }});\r | |
193 | \r | |
194 | Document doc = db.parse(new ByteArrayInputStream(elementBuffer.toString().getBytes()));\r | |
195 | return doc.getDocumentElement();\r | |
196 | } catch (ParserConfigurationException e) {\r | |
197 | e.printStackTrace();\r | |
198 | } catch (SAXException e) {\r | |
199 | e.printStackTrace();\r | |
200 | } catch (IOException e) {\r | |
201 | e.printStackTrace();\r | |
202 | }\r | |
203 | return null;\r | |
204 | }\r | |
205 | \r | |
206 | private void readElement(StringBuffer buffer, RandomAccessFile raFile) {\r | |
207 | try {\r | |
208 | int numRead = 0;\r | |
209 | boolean startTagClosed = false;\r | |
210 | int i;\r | |
211 | while ((i = raFile.read()) != -1) {\r | |
212 | numRead++;\r | |
213 | char c = (char)i;\r | |
214 | buffer.append(c);\r | |
215 | if (c == '"') {\r | |
216 | readQuote(buffer, raFile, '"');\r | |
217 | } else if (c == '\'') {\r | |
218 | readQuote(buffer, raFile, '\'');\r | |
219 | } else if (c == '<') {\r | |
220 | readElement(buffer, raFile);\r | |
221 | } else if (c == '/' && numRead == 1) {\r | |
222 | break; // found "</"\r | |
3b38ea61 | 223 | } else if (c == '-' && numRead == 3 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("!-")) { //$NON-NLS-1$\r |
c3c5c786 FC |
224 | readComment(buffer, raFile); // found "<!--"\r |
225 | } else if (i == '>') {\r | |
226 | if (buffer.charAt(buffer.length() - 2) == '/') {\r | |
227 | break; // found "/>"\r | |
228 | } else if (startTagClosed) {\r | |
229 | break; // found "<...>...</...>"\r | |
230 | } else {\r | |
231 | startTagClosed = true; // found "<...>"\r | |
232 | }\r | |
233 | }\r | |
234 | }\r | |
235 | return;\r | |
236 | } catch (IOException e) {\r | |
237 | return;\r | |
238 | }\r | |
239 | }\r | |
240 | \r | |
241 | private void readQuote(StringBuffer buffer, RandomAccessFile raFile, char eq) {\r | |
242 | try {\r | |
243 | int i;\r | |
244 | while ((i = raFile.read()) != -1) {\r | |
245 | char c = (char)i;\r | |
246 | buffer.append(c);\r | |
247 | if (c == eq) {\r | |
248 | break; // found matching end-quote\r | |
249 | }\r | |
250 | }\r | |
251 | return;\r | |
252 | } catch (IOException e) {\r | |
253 | return;\r | |
254 | }\r | |
255 | }\r | |
256 | \r | |
257 | private void readComment(StringBuffer buffer, RandomAccessFile raFile) {\r | |
258 | try {\r | |
259 | int numRead = 0;\r | |
260 | int i;\r | |
261 | while ((i = raFile.read()) != -1) {\r | |
262 | numRead++;\r | |
263 | char c = (char)i;\r | |
264 | buffer.append(c);\r | |
3b38ea61 | 265 | if (c == '>' && numRead >= 2 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("--")) { //$NON-NLS-1$\r |
c3c5c786 FC |
266 | break; // found "-->"\r |
267 | }\r | |
268 | }\r | |
269 | return;\r | |
270 | } catch (IOException e) {\r | |
271 | return;\r | |
272 | }\r | |
273 | }\r | |
274 | \r | |
275 | public static StringBuffer parseElement(Element parentElement, StringBuffer buffer) {\r | |
276 | NodeList nodeList = parentElement.getChildNodes();\r | |
277 | String separator = null;\r | |
278 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
279 | Node node = nodeList.item(i);\r | |
280 | if (node.getNodeType() == Node.ELEMENT_NODE) {\r | |
281 | if (separator == null) {\r | |
3b38ea61 | 282 | separator = " | "; //$NON-NLS-1$\r |
c3c5c786 FC |
283 | } else {\r |
284 | buffer.append(separator);\r | |
285 | }\r | |
286 | Element element = (Element) node;\r | |
287 | if (element.hasChildNodes() == false) {\r | |
288 | buffer.append(element.getNodeName());\r | |
289 | } else if (element.getChildNodes().getLength() == 1 && element.getFirstChild().getNodeType() == Node.TEXT_NODE) {\r | |
3b38ea61 | 290 | buffer.append(element.getNodeName() + ":" + element.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$\r |
c3c5c786 FC |
291 | } else {\r |
292 | buffer.append(element.getNodeName());\r | |
3b38ea61 | 293 | buffer.append(" [ "); //$NON-NLS-1$\r |
c3c5c786 | 294 | parseElement(element, buffer);\r |
3b38ea61 | 295 | buffer.append(" ]"); //$NON-NLS-1$\r |
c3c5c786 FC |
296 | }\r |
297 | } else if (node.getNodeType() == Node.TEXT_NODE) {\r | |
298 | if (node.getNodeValue().trim().length() != 0) {\r | |
299 | buffer.append(node.getNodeValue().trim());\r | |
300 | }\r | |
301 | }\r | |
302 | }\r | |
303 | return buffer;\r | |
304 | }\r | |
305 | \r | |
306 | public InputElement getRecordInputElement(InputElement inputElement) {\r | |
307 | if (inputElement.logEntry) {\r | |
308 | return inputElement;\r | |
309 | } else if (inputElement.childElements != null) {\r | |
310 | for (InputElement childInputElement : inputElement.childElements) {\r | |
311 | InputElement recordInputElement = getRecordInputElement(childInputElement);\r | |
312 | if (recordInputElement != null) {\r | |
313 | return recordInputElement;\r | |
314 | }\r | |
315 | }\r | |
316 | }\r | |
317 | return null;\r | |
318 | }\r | |
319 | \r | |
320 | public CustomXmlEvent extractEvent(Element element, InputElement inputElement) {\r | |
d7fcacc9 FC |
321 | CustomXmlEvent event = new CustomXmlEvent(fDefinition, TmfTimestamp.Zero, new TmfEventSource(""), fEventType, new TmfEventReference("")); //$NON-NLS-1$ //$NON-NLS-2$\r |
322 | event.setContent(new CustomEventContent(event, new StringBuffer()));\r | |
c3c5c786 FC |
323 | parseElement(element, event, inputElement);\r |
324 | return event;\r | |
325 | }\r | |
326 | \r | |
327 | private void parseElement(Element element, CustomXmlEvent event, InputElement inputElement) {\r | |
328 | if (inputElement.inputName != null && !inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_IGNORE)) {\r | |
329 | event.parseInput(parseElement(element, new StringBuffer()).toString(), inputElement.inputName, inputElement.inputAction, inputElement.inputFormat);\r | |
330 | }\r | |
331 | if (inputElement.attributes != null) {\r | |
332 | for (InputAttribute attribute : inputElement.attributes) {\r | |
333 | event.parseInput(element.getAttribute(attribute.attributeName), attribute.inputName, attribute.inputAction, attribute.inputFormat);\r | |
334 | }\r | |
335 | }\r | |
336 | NodeList childNodes = element.getChildNodes();\r | |
337 | if (inputElement.childElements != null) {\r | |
338 | for (int i = 0; i < childNodes.getLength(); i++) {\r | |
339 | Node node = childNodes.item(i);\r | |
340 | if (node instanceof Element) {\r | |
341 | for (InputElement child : inputElement.childElements) {\r | |
342 | if (node.getNodeName().equals(child.elementName)) {\r | |
343 | parseElement((Element) node, event, child);\r | |
344 | break;\r | |
345 | }\r | |
346 | }\r | |
347 | }\r | |
348 | }\r | |
349 | }\r | |
350 | return;\r | |
351 | }\r | |
352 | \r | |
353 | public CustomTraceDefinition getDefinition() {\r | |
354 | return fDefinition;\r | |
355 | }\r | |
356 | }\r |