Commit | Line | Data |
---|---|---|
c3c5c786 FC |
1 | /*******************************************************************************\r |
2 | * Copyright (c) 2010 Ericsson\r | |
abbdd66a | 3 | *\r |
c3c5c786 FC |
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 | |
abbdd66a | 8 | *\r |
c3c5c786 FC |
9 | * Contributors:\r |
10 | * Patrick Tasse - Initial API and implementation\r | |
11 | *******************************************************************************/\r | |
12 | \r | |
d34665f9 | 13 | package org.eclipse.linuxtools.internal.tmf.ui.parsers.custom;\r |
c3c5c786 FC |
14 | \r |
15 | import java.io.ByteArrayInputStream;\r | |
16 | import java.io.File;\r | |
17 | import java.io.FileWriter;\r | |
18 | import java.io.IOException;\r | |
19 | import java.io.StringWriter;\r | |
20 | import java.util.ArrayList;\r | |
21 | import java.util.List;\r | |
22 | \r | |
23 | import javax.xml.parsers.DocumentBuilder;\r | |
24 | import javax.xml.parsers.DocumentBuilderFactory;\r | |
25 | import javax.xml.parsers.ParserConfigurationException;\r | |
26 | import javax.xml.transform.OutputKeys;\r | |
27 | import javax.xml.transform.Transformer;\r | |
28 | import javax.xml.transform.TransformerConfigurationException;\r | |
29 | import javax.xml.transform.TransformerException;\r | |
30 | import javax.xml.transform.TransformerFactory;\r | |
31 | import javax.xml.transform.TransformerFactoryConfigurationError;\r | |
32 | import javax.xml.transform.dom.DOMSource;\r | |
33 | import javax.xml.transform.stream.StreamResult;\r | |
34 | \r | |
8fd82db5 | 35 | import org.eclipse.linuxtools.internal.tmf.ui.Activator;\r |
abbdd66a | 36 | import org.eclipse.linuxtools.internal.tmf.ui.Messages;\r |
c3c5c786 FC |
37 | import org.w3c.dom.Document;\r |
38 | import org.w3c.dom.Element;\r | |
39 | import org.w3c.dom.Node;\r | |
40 | import org.w3c.dom.NodeList;\r | |
41 | import org.xml.sax.EntityResolver;\r | |
42 | import org.xml.sax.ErrorHandler;\r | |
43 | import org.xml.sax.InputSource;\r | |
44 | import org.xml.sax.SAXException;\r | |
45 | import org.xml.sax.SAXParseException;\r | |
46 | \r | |
47 | public class CustomXmlTraceDefinition extends CustomTraceDefinition {\r | |
48 | \r | |
3b38ea61 | 49 | protected static final String CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME = "custom_xml_parsers.xml"; //$NON-NLS-1$\r |
c3c5c786 | 50 | protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME =\r |
8fd82db5 | 51 | Activator.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME).toString();\r |
c3c5c786 | 52 | \r |
3b38ea61 FC |
53 | public static final String TAG_IGNORE = Messages.CustomXmlTraceDefinition_ignoreTag;\r |
54 | \r | |
55 | private static final String CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT = Messages.CustomXmlTraceDefinition_definitionRootElement;\r | |
56 | private static final String DEFINITION_ELEMENT = Messages.CustomXmlTraceDefinition_definition;\r | |
57 | private static final String NAME_ATTRIBUTE = Messages.CustomXmlTraceDefinition_name;\r | |
58 | private static final String LOG_ENTRY_ATTRIBUTE = Messages.CustomXmlTraceDefinition_logEntry;\r | |
59 | private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT = Messages.CustomXmlTraceDefinition_timestampOutputFormat;\r | |
60 | private static final String INPUT_ELEMENT_ELEMENT = Messages.CustomXmlTraceDefinition_inputElement;\r | |
61 | private static final String ATTRIBUTE_ELEMENT = Messages.CustomXmlTraceDefinition_attribute;\r | |
62 | private static final String INPUT_DATA_ELEMENT = Messages.CustomXmlTraceDefinition_inputData;\r | |
63 | private static final String ACTION_ATTRIBUTE = Messages.CustomXmlTraceDefinition_action;\r | |
64 | private static final String FORMAT_ATTRIBUTE = Messages.CustomXmlTraceDefinition_format;\r | |
65 | private static final String OUTPUT_COLUMN_ELEMENT = Messages.CustomXmlTraceDefinition_outputColumn;\r | |
abbdd66a | 66 | \r |
c3c5c786 FC |
67 | public InputElement rootInputElement;\r |
68 | \r | |
69 | public CustomXmlTraceDefinition() {\r | |
3b38ea61 | 70 | this("", null, new ArrayList<OutputColumn>(), ""); //$NON-NLS-1$ //$NON-NLS-2$\r |
abbdd66a | 71 | }\r |
c3c5c786 FC |
72 | \r |
73 | public CustomXmlTraceDefinition(String logtype, InputElement rootElement, List<OutputColumn> outputs, String timeStampOutputFormat) {\r | |
74 | this.definitionName = logtype;\r | |
75 | this.rootInputElement = rootElement;\r | |
76 | this.outputs = outputs;\r | |
77 | this.timeStampOutputFormat = timeStampOutputFormat;\r | |
78 | }\r | |
79 | \r | |
80 | public static class InputElement {\r | |
81 | public String elementName;\r | |
82 | public boolean logEntry;\r | |
83 | public String inputName;\r | |
84 | public int inputAction;\r | |
85 | public String inputFormat;\r | |
86 | public List<InputAttribute> attributes;\r | |
87 | public InputElement parentElement;\r | |
88 | public InputElement nextElement;\r | |
89 | public List<InputElement> childElements;\r | |
abbdd66a AM |
90 | \r |
91 | public InputElement() {}\r | |
92 | \r | |
c3c5c786 FC |
93 | public InputElement(String elementName, boolean logEntry, String inputName, int inputAction, String inputFormat, List<InputAttribute> attributes) {\r |
94 | this.elementName = elementName;\r | |
95 | this.logEntry = logEntry;\r | |
96 | this.inputName = inputName;\r | |
97 | this.inputAction = inputAction;\r | |
98 | this.inputFormat = inputFormat;\r | |
99 | this.attributes = attributes;\r | |
100 | }\r | |
abbdd66a | 101 | \r |
c3c5c786 FC |
102 | public void addAttribute(InputAttribute attribute) {\r |
103 | if (attributes == null) {\r | |
104 | attributes = new ArrayList<InputAttribute>(1);\r | |
105 | }\r | |
106 | attributes.add(attribute);\r | |
107 | }\r | |
108 | \r | |
109 | public void addChild(InputElement input) {\r | |
110 | if (childElements == null) {\r | |
111 | childElements = new ArrayList<InputElement>(1);\r | |
112 | } else if (childElements.size() > 0) {\r | |
113 | InputElement last = childElements.get(childElements.size() - 1);\r | |
114 | last.nextElement = input;\r | |
115 | }\r | |
116 | childElements.add(input);\r | |
117 | input.parentElement = this;\r | |
118 | }\r | |
119 | \r | |
120 | public void addNext(InputElement input) {\r | |
121 | if (parentElement != null) {\r | |
122 | int index = parentElement.childElements.indexOf(this);\r | |
123 | parentElement.childElements.add(index + 1, input);\r | |
124 | InputElement next = nextElement;\r | |
125 | nextElement = input;\r | |
126 | input.nextElement = next;\r | |
127 | }\r | |
128 | input.parentElement = this.parentElement;\r | |
129 | }\r | |
130 | \r | |
131 | public void moveUp() {\r | |
132 | if (parentElement != null) {\r | |
133 | int index = parentElement.childElements.indexOf(this);\r | |
134 | if (index > 0) {\r | |
135 | parentElement.childElements.add(index - 1 , parentElement.childElements.remove(index));\r | |
136 | parentElement.childElements.get(index).nextElement = nextElement;\r | |
137 | nextElement = parentElement.childElements.get(index);\r | |
138 | }\r | |
139 | }\r | |
140 | }\r | |
141 | \r | |
142 | public void moveDown() {\r | |
143 | if (parentElement != null) {\r | |
144 | int index = parentElement.childElements.indexOf(this);\r | |
145 | if (index < parentElement.childElements.size() - 1) {\r | |
146 | parentElement.childElements.add(index + 1 , parentElement.childElements.remove(index));\r | |
147 | nextElement = parentElement.childElements.get(index).nextElement;\r | |
148 | parentElement.childElements.get(index).nextElement = this;\r | |
149 | }\r | |
150 | }\r | |
151 | }\r | |
152 | \r | |
153 | }\r | |
154 | \r | |
155 | public static class InputAttribute {\r | |
156 | public String attributeName;\r | |
157 | public String inputName;\r | |
158 | public int inputAction;\r | |
159 | public String inputFormat;\r | |
abbdd66a AM |
160 | \r |
161 | public InputAttribute() {}\r | |
162 | \r | |
c3c5c786 FC |
163 | public InputAttribute(String attributeName, String inputName, int inputAction, String inputFormat) {\r |
164 | this.attributeName = attributeName;\r | |
165 | this.inputName = inputName;\r | |
166 | this.inputAction = inputAction;\r | |
167 | this.inputFormat = inputFormat;\r | |
168 | }\r | |
169 | }\r | |
170 | \r | |
9ccc6d01 | 171 | @Override\r |
c3c5c786 FC |
172 | public void save() {\r |
173 | save(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);\r | |
174 | }\r | |
abbdd66a | 175 | \r |
9ccc6d01 | 176 | @Override\r |
c3c5c786 FC |
177 | public void save(String path) {\r |
178 | try {\r | |
179 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r | |
180 | DocumentBuilder db = dbf.newDocumentBuilder();\r | |
abbdd66a | 181 | \r |
c3c5c786 FC |
182 | // The following allows xml parsing without access to the dtd\r |
183 | EntityResolver resolver = new EntityResolver () {\r | |
d4011df2 FC |
184 | @Override\r |
185 | public InputSource resolveEntity (String publicId, String systemId) {\r | |
3b38ea61 | 186 | String empty = ""; //$NON-NLS-1$\r |
c3c5c786 FC |
187 | ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());\r |
188 | return new InputSource(bais);\r | |
189 | }\r | |
190 | };\r | |
191 | db.setEntityResolver(resolver);\r | |
192 | \r | |
193 | // The following catches xml parsing exceptions\r | |
194 | db.setErrorHandler(new ErrorHandler(){\r | |
d4011df2 FC |
195 | @Override\r |
196 | public void error(SAXParseException saxparseexception) throws SAXException {}\r | |
197 | @Override\r | |
198 | public void warning(SAXParseException saxparseexception) throws SAXException {}\r | |
199 | @Override\r | |
200 | public void fatalError(SAXParseException saxparseexception) throws SAXException {\r | |
c3c5c786 FC |
201 | throw saxparseexception;\r |
202 | }});\r | |
abbdd66a | 203 | \r |
c3c5c786 FC |
204 | Document doc = null;\r |
205 | File file = new File(path);\r | |
206 | if (file.canRead()) {\r | |
207 | doc = db.parse(file);\r | |
208 | if (! doc.getDocumentElement().getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {\r | |
209 | return;\r | |
210 | }\r | |
211 | } else {\r | |
212 | doc = db.newDocument();\r | |
213 | Node node = doc.createElement(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT);\r | |
214 | doc.appendChild(node);\r | |
215 | }\r | |
216 | \r | |
217 | Element root = doc.getDocumentElement();\r | |
abbdd66a | 218 | \r |
c3c5c786 FC |
219 | NodeList nodeList = root.getChildNodes();\r |
220 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
221 | Node node = nodeList.item(i);\r | |
222 | if (node instanceof Element &&\r | |
223 | node.getNodeName().equals(DEFINITION_ELEMENT) &&\r | |
224 | definitionName.equals(((Element) node).getAttribute(NAME_ATTRIBUTE))) {\r | |
225 | root.removeChild(node);\r | |
226 | }\r | |
227 | }\r | |
228 | Element definitionElement = doc.createElement(DEFINITION_ELEMENT);\r | |
229 | root.appendChild(definitionElement);\r | |
230 | definitionElement.setAttribute(NAME_ATTRIBUTE, definitionName);\r | |
abbdd66a | 231 | \r |
c3c5c786 FC |
232 | Element formatElement = doc.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT);\r |
233 | definitionElement.appendChild(formatElement);\r | |
234 | formatElement.appendChild(doc.createTextNode(timeStampOutputFormat));\r | |
235 | \r | |
236 | if (rootInputElement != null) {\r | |
237 | definitionElement.appendChild(createInputElementElement(rootInputElement, doc));\r | |
238 | }\r | |
239 | \r | |
240 | if (outputs != null) {\r | |
241 | for (OutputColumn output : outputs) {\r | |
242 | Element outputColumnElement = doc.createElement(OUTPUT_COLUMN_ELEMENT);\r | |
243 | definitionElement.appendChild(outputColumnElement);\r | |
244 | outputColumnElement.setAttribute(NAME_ATTRIBUTE, output.name);\r | |
245 | }\r | |
246 | }\r | |
abbdd66a | 247 | \r |
c3c5c786 | 248 | Transformer transformer = TransformerFactory.newInstance().newTransformer();\r |
3b38ea61 | 249 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$\r |
c3c5c786 FC |
250 | \r |
251 | //initialize StreamResult with File object to save to file\r | |
252 | StreamResult result = new StreamResult(new StringWriter());\r | |
253 | DOMSource source = new DOMSource(doc);\r | |
254 | transformer.transform(source, result);\r | |
255 | String xmlString = result.getWriter().toString();\r | |
abbdd66a | 256 | \r |
c3c5c786 FC |
257 | FileWriter writer = new FileWriter(file);\r |
258 | writer.write(xmlString);\r | |
259 | writer.close();\r | |
260 | } catch (ParserConfigurationException e) {\r | |
8fd82db5 | 261 | Activator.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$\r |
c3c5c786 | 262 | } catch (TransformerConfigurationException e) {\r |
8fd82db5 | 263 | Activator.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$\r |
c3c5c786 | 264 | } catch (TransformerFactoryConfigurationError e) {\r |
8fd82db5 | 265 | Activator.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$\r |
c3c5c786 | 266 | } catch (TransformerException e) {\r |
8fd82db5 | 267 | Activator.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$\r |
c3c5c786 | 268 | } catch (IOException e) {\r |
8fd82db5 | 269 | Activator.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$\r |
c3c5c786 | 270 | } catch (SAXException e) {\r |
8fd82db5 | 271 | Activator.getDefault().logError("Error saving CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$\r |
c3c5c786 FC |
272 | }\r |
273 | }\r | |
abbdd66a | 274 | \r |
c3c5c786 FC |
275 | private Element createInputElementElement(InputElement inputElement, Document doc) {\r |
276 | Element inputElementElement = doc.createElement(INPUT_ELEMENT_ELEMENT);\r | |
277 | inputElementElement.setAttribute(NAME_ATTRIBUTE, inputElement.elementName);\r | |
abbdd66a | 278 | \r |
c3c5c786 FC |
279 | if (inputElement.logEntry) {\r |
280 | inputElementElement.setAttribute(LOG_ENTRY_ATTRIBUTE, Boolean.toString(inputElement.logEntry));\r | |
281 | }\r | |
282 | \r | |
283 | if (inputElement.parentElement != null) {\r | |
284 | Element inputDataElement = doc.createElement(INPUT_DATA_ELEMENT);\r | |
285 | inputElementElement.appendChild(inputDataElement);\r | |
286 | inputDataElement.setAttribute(NAME_ATTRIBUTE, inputElement.inputName);\r | |
287 | inputDataElement.setAttribute(ACTION_ATTRIBUTE, Integer.toString(inputElement.inputAction));\r | |
288 | if (inputElement.inputFormat != null) {\r | |
289 | inputDataElement.setAttribute(FORMAT_ATTRIBUTE, inputElement.inputFormat);\r | |
290 | }\r | |
291 | }\r | |
292 | \r | |
293 | if (inputElement.attributes != null) {\r | |
294 | for (InputAttribute attribute : inputElement.attributes) {\r | |
295 | Element inputAttributeElement = doc.createElement(ATTRIBUTE_ELEMENT);\r | |
296 | inputElementElement.appendChild(inputAttributeElement);\r | |
297 | inputAttributeElement.setAttribute(NAME_ATTRIBUTE, attribute.attributeName);\r | |
298 | Element inputDataElement = doc.createElement(INPUT_DATA_ELEMENT);\r | |
299 | inputAttributeElement.appendChild(inputDataElement);\r | |
300 | inputDataElement.setAttribute(NAME_ATTRIBUTE, attribute.inputName);\r | |
301 | inputDataElement.setAttribute(ACTION_ATTRIBUTE, Integer.toString(attribute.inputAction));\r | |
302 | if (attribute.inputFormat != null) {\r | |
303 | inputDataElement.setAttribute(FORMAT_ATTRIBUTE, attribute.inputFormat);\r | |
304 | }\r | |
305 | }\r | |
306 | }\r | |
abbdd66a | 307 | \r |
c3c5c786 FC |
308 | if (inputElement.childElements != null) {\r |
309 | for (InputElement childInputElement : inputElement.childElements) {\r | |
310 | inputElementElement.appendChild(createInputElementElement(childInputElement, doc));\r | |
311 | }\r | |
312 | }\r | |
abbdd66a | 313 | \r |
c3c5c786 FC |
314 | return inputElementElement;\r |
315 | }\r | |
abbdd66a | 316 | \r |
c3c5c786 FC |
317 | public static CustomXmlTraceDefinition[] loadAll() {\r |
318 | return loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);\r | |
319 | }\r | |
abbdd66a | 320 | \r |
c3c5c786 FC |
321 | public static CustomXmlTraceDefinition[] loadAll(String path) {\r |
322 | try {\r | |
323 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r | |
324 | DocumentBuilder db = dbf.newDocumentBuilder();\r | |
325 | \r | |
326 | // The following allows xml parsing without access to the dtd\r | |
327 | EntityResolver resolver = new EntityResolver () {\r | |
d4011df2 FC |
328 | @Override\r |
329 | public InputSource resolveEntity (String publicId, String systemId) {\r | |
3b38ea61 | 330 | String empty = ""; //$NON-NLS-1$\r |
c3c5c786 FC |
331 | ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());\r |
332 | return new InputSource(bais);\r | |
333 | }\r | |
334 | };\r | |
335 | db.setEntityResolver(resolver);\r | |
336 | \r | |
337 | // The following catches xml parsing exceptions\r | |
338 | db.setErrorHandler(new ErrorHandler(){\r | |
d4011df2 FC |
339 | @Override\r |
340 | public void error(SAXParseException saxparseexception) throws SAXException {}\r | |
341 | @Override\r | |
342 | public void warning(SAXParseException saxparseexception) throws SAXException {}\r | |
343 | @Override\r | |
344 | public void fatalError(SAXParseException saxparseexception) throws SAXException {\r | |
c3c5c786 FC |
345 | throw saxparseexception;\r |
346 | }});\r | |
347 | \r | |
348 | File file = new File(path);\r | |
349 | if (!file.canRead()) {\r | |
350 | return new CustomXmlTraceDefinition[0];\r | |
351 | }\r | |
352 | Document doc = db.parse(file);\r | |
353 | \r | |
354 | Element root = doc.getDocumentElement();\r | |
355 | if (! root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {\r | |
356 | return new CustomXmlTraceDefinition[0];\r | |
357 | }\r | |
358 | \r | |
359 | ArrayList<CustomXmlTraceDefinition> defList = new ArrayList<CustomXmlTraceDefinition>();\r | |
360 | NodeList nodeList = root.getChildNodes();\r | |
361 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
362 | Node node = nodeList.item(i);\r | |
363 | if (node instanceof Element && node.getNodeName().equals(DEFINITION_ELEMENT)) {\r | |
364 | CustomXmlTraceDefinition def = extractDefinition((Element) node);\r | |
365 | if (def != null) {\r | |
366 | defList.add(def);\r | |
367 | }\r | |
368 | }\r | |
369 | }\r | |
370 | return defList.toArray(new CustomXmlTraceDefinition[0]);\r | |
371 | } catch (ParserConfigurationException e) {\r | |
8fd82db5 | 372 | Activator.getDefault().logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$\r |
c3c5c786 | 373 | } catch (SAXException e) {\r |
8fd82db5 | 374 | Activator.getDefault().logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$\r |
c3c5c786 | 375 | } catch (IOException e) {\r |
8fd82db5 | 376 | Activator.getDefault().logError("Error loading all in CustomXmlTraceDefinition: path=" + path, e); //$NON-NLS-1$\r |
c3c5c786 FC |
377 | }\r |
378 | return new CustomXmlTraceDefinition[0];\r | |
379 | }\r | |
380 | \r | |
381 | public static CustomXmlTraceDefinition load(String definitionName) {\r | |
382 | try {\r | |
383 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r | |
384 | DocumentBuilder db = dbf.newDocumentBuilder();\r | |
385 | \r | |
386 | // The following allows xml parsing without access to the dtd\r | |
387 | EntityResolver resolver = new EntityResolver () {\r | |
d4011df2 FC |
388 | @Override\r |
389 | public InputSource resolveEntity (String publicId, String systemId) {\r | |
3b38ea61 | 390 | String empty = ""; //$NON-NLS-1$\r |
c3c5c786 FC |
391 | ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());\r |
392 | return new InputSource(bais);\r | |
393 | }\r | |
394 | };\r | |
395 | db.setEntityResolver(resolver);\r | |
396 | \r | |
397 | // The following catches xml parsing exceptions\r | |
398 | db.setErrorHandler(new ErrorHandler(){\r | |
d4011df2 FC |
399 | @Override\r |
400 | public void error(SAXParseException saxparseexception) throws SAXException {}\r | |
401 | @Override\r | |
402 | public void warning(SAXParseException saxparseexception) throws SAXException {}\r | |
403 | @Override\r | |
404 | public void fatalError(SAXParseException saxparseexception) throws SAXException {\r | |
c3c5c786 FC |
405 | throw saxparseexception;\r |
406 | }});\r | |
407 | \r | |
408 | File file = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);\r | |
409 | Document doc = db.parse(file);\r | |
410 | \r | |
411 | Element root = doc.getDocumentElement();\r | |
412 | if (! root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {\r | |
413 | return null;\r | |
414 | }\r | |
415 | \r | |
416 | NodeList nodeList = root.getChildNodes();\r | |
417 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
418 | Node node = nodeList.item(i);\r | |
419 | if (node instanceof Element &&\r | |
420 | node.getNodeName().equals(DEFINITION_ELEMENT) &&\r | |
421 | definitionName.equals(((Element) node).getAttribute(NAME_ATTRIBUTE))) {\r | |
422 | return extractDefinition((Element) node);\r | |
423 | }\r | |
424 | }\r | |
425 | } catch (ParserConfigurationException e) {\r | |
8fd82db5 | 426 | Activator.getDefault().logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$\r |
c3c5c786 | 427 | } catch (SAXException e) {\r |
8fd82db5 | 428 | Activator.getDefault().logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$\r |
c3c5c786 | 429 | } catch (IOException e) {\r |
8fd82db5 | 430 | Activator.getDefault().logError("Error loading CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$\r |
c3c5c786 FC |
431 | }\r |
432 | return null;\r | |
433 | }\r | |
abbdd66a | 434 | \r |
c3c5c786 FC |
435 | public static CustomXmlTraceDefinition extractDefinition(Element definitionElement) {\r |
436 | CustomXmlTraceDefinition def = new CustomXmlTraceDefinition();\r | |
abbdd66a | 437 | \r |
c3c5c786 | 438 | def.definitionName = definitionElement.getAttribute(NAME_ATTRIBUTE);\r |
abbdd66a AM |
439 | if (def.definitionName == null) {\r |
440 | return null;\r | |
441 | }\r | |
442 | \r | |
c3c5c786 FC |
443 | NodeList nodeList = definitionElement.getChildNodes();\r |
444 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
445 | Node node = nodeList.item(i);\r | |
446 | String nodeName = node.getNodeName();\r | |
447 | if (nodeName.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT)) {\r | |
448 | Element formatElement = (Element) node;\r | |
449 | def.timeStampOutputFormat = formatElement.getTextContent();\r | |
450 | } else if (nodeName.equals(INPUT_ELEMENT_ELEMENT)) {\r | |
451 | InputElement inputElement = extractInputElement((Element) node);\r | |
452 | if (inputElement != null) {\r | |
453 | if (def.rootInputElement == null) {\r | |
454 | def.rootInputElement = inputElement;\r | |
455 | } else {\r | |
456 | return null;\r | |
457 | }\r | |
458 | }\r | |
459 | } else if (nodeName.equals(OUTPUT_COLUMN_ELEMENT)) {\r | |
460 | Element outputColumnElement = (Element) node;\r | |
461 | OutputColumn outputColumn = new OutputColumn();\r | |
462 | outputColumn.name = outputColumnElement.getAttribute(NAME_ATTRIBUTE);\r | |
463 | def.outputs.add(outputColumn);\r | |
464 | }\r | |
465 | }\r | |
466 | return def;\r | |
467 | }\r | |
468 | \r | |
469 | private static InputElement extractInputElement(Element inputElementElement) {\r | |
470 | InputElement inputElement = new InputElement();\r | |
471 | inputElement.elementName = inputElementElement.getAttribute(NAME_ATTRIBUTE);\r | |
472 | inputElement.logEntry = (Boolean.toString(true).equals(inputElementElement.getAttribute(LOG_ENTRY_ATTRIBUTE))) ? true : false;\r | |
473 | NodeList nodeList = inputElementElement.getChildNodes();\r | |
474 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
475 | Node node = nodeList.item(i);\r | |
476 | String nodeName = node.getNodeName();\r | |
477 | if (nodeName.equals(INPUT_DATA_ELEMENT)) {\r | |
478 | Element inputDataElement = (Element) node;\r | |
479 | inputElement.inputName = inputDataElement.getAttribute(NAME_ATTRIBUTE);\r | |
480 | inputElement.inputAction = Integer.parseInt(inputDataElement.getAttribute(ACTION_ATTRIBUTE));\r | |
481 | inputElement.inputFormat = inputDataElement.getAttribute(FORMAT_ATTRIBUTE);\r | |
482 | } else if (nodeName.equals(ATTRIBUTE_ELEMENT)) {\r | |
483 | Element attributeElement = (Element) node;\r | |
484 | InputAttribute attribute = new InputAttribute();\r | |
485 | attribute.attributeName = attributeElement.getAttribute(NAME_ATTRIBUTE);\r | |
486 | NodeList attributeNodeList = attributeElement.getChildNodes();\r | |
487 | for (int j = 0; j < attributeNodeList.getLength(); j++) {\r | |
488 | Node attributeNode = attributeNodeList.item(j);\r | |
489 | String attributeNodeName = attributeNode.getNodeName();\r | |
490 | if (attributeNodeName.equals(INPUT_DATA_ELEMENT)) {\r | |
491 | Element inputDataElement = (Element) attributeNode;\r | |
492 | attribute.inputName = inputDataElement.getAttribute(NAME_ATTRIBUTE);\r | |
493 | attribute.inputAction = Integer.parseInt(inputDataElement.getAttribute(ACTION_ATTRIBUTE));\r | |
494 | attribute.inputFormat = inputDataElement.getAttribute(FORMAT_ATTRIBUTE);\r | |
495 | }\r | |
496 | }\r | |
497 | inputElement.addAttribute(attribute);\r | |
498 | } else if (nodeName.equals(INPUT_ELEMENT_ELEMENT)) {\r | |
499 | Element childInputElementElement = (Element) node;\r | |
500 | InputElement childInputElement = extractInputElement(childInputElementElement);\r | |
501 | if (childInputElement != null) {\r | |
502 | inputElement.addChild(childInputElement);\r | |
503 | }\r | |
504 | }\r | |
505 | }\r | |
506 | return inputElement;\r | |
507 | }\r | |
abbdd66a | 508 | \r |
c3c5c786 FC |
509 | public static void delete(String definitionName) {\r |
510 | try {\r | |
511 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r | |
512 | DocumentBuilder db = dbf.newDocumentBuilder();\r | |
513 | \r | |
514 | // The following allows xml parsing without access to the dtd\r | |
515 | EntityResolver resolver = new EntityResolver () {\r | |
d4011df2 FC |
516 | @Override\r |
517 | public InputSource resolveEntity (String publicId, String systemId) {\r | |
3b38ea61 | 518 | String empty = ""; //$NON-NLS-1$\r |
c3c5c786 FC |
519 | ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());\r |
520 | return new InputSource(bais);\r | |
521 | }\r | |
522 | };\r | |
523 | db.setEntityResolver(resolver);\r | |
524 | \r | |
525 | // The following catches xml parsing exceptions\r | |
526 | db.setErrorHandler(new ErrorHandler(){\r | |
d4011df2 FC |
527 | @Override\r |
528 | public void error(SAXParseException saxparseexception) throws SAXException {}\r | |
529 | @Override\r | |
530 | public void warning(SAXParseException saxparseexception) throws SAXException {}\r | |
531 | @Override\r | |
532 | public void fatalError(SAXParseException saxparseexception) throws SAXException {\r | |
c3c5c786 FC |
533 | throw saxparseexception;\r |
534 | }});\r | |
535 | \r | |
536 | File file = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);\r | |
537 | Document doc = db.parse(file);\r | |
538 | \r | |
539 | Element root = doc.getDocumentElement();\r | |
540 | if (! root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {\r | |
541 | return;\r | |
542 | }\r | |
543 | \r | |
544 | NodeList nodeList = root.getChildNodes();\r | |
545 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
546 | Node node = nodeList.item(i);\r | |
547 | if (node instanceof Element &&\r | |
548 | node.getNodeName().equals(DEFINITION_ELEMENT) &&\r | |
549 | definitionName.equals(((Element) node).getAttribute(NAME_ATTRIBUTE))) {\r | |
550 | root.removeChild(node);\r | |
551 | }\r | |
552 | }\r | |
abbdd66a | 553 | \r |
c3c5c786 | 554 | Transformer transformer = TransformerFactory.newInstance().newTransformer();\r |
3b38ea61 | 555 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$\r |
c3c5c786 FC |
556 | \r |
557 | //initialize StreamResult with File object to save to file\r | |
558 | StreamResult result = new StreamResult(new StringWriter());\r | |
559 | DOMSource source = new DOMSource(doc);\r | |
560 | transformer.transform(source, result);\r | |
561 | String xmlString = result.getWriter().toString();\r | |
abbdd66a | 562 | \r |
c3c5c786 FC |
563 | FileWriter writer = new FileWriter(file);\r |
564 | writer.write(xmlString);\r | |
565 | writer.close();\r | |
566 | } catch (ParserConfigurationException e) {\r | |
8fd82db5 | 567 | Activator.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$\r |
c3c5c786 | 568 | } catch (SAXException e) {\r |
8fd82db5 | 569 | Activator.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$\r |
c3c5c786 | 570 | } catch (IOException e) {\r |
8fd82db5 | 571 | Activator.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$\r |
c3c5c786 | 572 | } catch (TransformerConfigurationException e) {\r |
8fd82db5 | 573 | Activator.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$\r |
c3c5c786 | 574 | } catch (TransformerFactoryConfigurationError e) {\r |
8fd82db5 | 575 | Activator.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$\r |
c3c5c786 | 576 | } catch (TransformerException e) {\r |
8fd82db5 | 577 | Activator.getDefault().logError("Error deleteing CustomXmlTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$\r |
c3c5c786 FC |
578 | }\r |
579 | }\r | |
580 | }\r |