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.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 | |
35 | import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;\r | |
7995b722 | 36 | import org.eclipse.linuxtools.tmf.ui.internal.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 FC |
50 | protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME =\r |
51 | TmfUiPlugin.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME).toString();\r | |
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 | |
c3c5c786 FC |
66 | \r |
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 |
c3c5c786 FC |
71 | };\r |
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 | |
90 | \r | |
91 | public InputElement() {};\r | |
92 | \r | |
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 | |
101 | \r | |
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 | |
160 | \r | |
161 | public InputAttribute() {};\r | |
162 | \r | |
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 | |
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 | |
181 | \r | |
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 | |
203 | \r | |
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 | |
218 | \r | |
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 | |
231 | \r | |
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 | |
247 | \r | |
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 | |
256 | \r | |
257 | FileWriter writer = new FileWriter(file);\r | |
258 | writer.write(xmlString);\r | |
259 | writer.close();\r | |
260 | } catch (ParserConfigurationException e) {\r | |
261 | e.printStackTrace();\r | |
262 | } catch (TransformerConfigurationException e) {\r | |
263 | e.printStackTrace();\r | |
264 | } catch (TransformerFactoryConfigurationError e) {\r | |
265 | e.printStackTrace();\r | |
266 | } catch (TransformerException e) {\r | |
267 | e.printStackTrace();\r | |
268 | } catch (IOException e) {\r | |
269 | e.printStackTrace();\r | |
270 | } catch (SAXException e) {\r | |
271 | e.printStackTrace();\r | |
272 | }\r | |
273 | }\r | |
274 | \r | |
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 | |
278 | \r | |
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 | |
307 | \r | |
308 | if (inputElement.childElements != null) {\r | |
309 | for (InputElement childInputElement : inputElement.childElements) {\r | |
310 | inputElementElement.appendChild(createInputElementElement(childInputElement, doc));\r | |
311 | }\r | |
312 | }\r | |
313 | \r | |
314 | return inputElementElement;\r | |
315 | }\r | |
316 | \r | |
317 | public static CustomXmlTraceDefinition[] loadAll() {\r | |
318 | return loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);\r | |
319 | }\r | |
320 | \r | |
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 | |
372 | e.printStackTrace();\r | |
373 | } catch (SAXException e) {\r | |
374 | e.printStackTrace();\r | |
375 | } catch (IOException e) {\r | |
376 | e.printStackTrace();\r | |
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 | |
426 | e.printStackTrace();\r | |
427 | } catch (SAXException e) {\r | |
428 | e.printStackTrace();\r | |
429 | } catch (IOException e) {\r | |
430 | e.printStackTrace();\r | |
431 | }\r | |
432 | return null;\r | |
433 | }\r | |
434 | \r | |
435 | public static CustomXmlTraceDefinition extractDefinition(Element definitionElement) {\r | |
436 | CustomXmlTraceDefinition def = new CustomXmlTraceDefinition();\r | |
437 | \r | |
438 | def.definitionName = definitionElement.getAttribute(NAME_ATTRIBUTE);\r | |
439 | if (def.definitionName == null) return null;\r | |
440 | \r | |
441 | NodeList nodeList = definitionElement.getChildNodes();\r | |
442 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
443 | Node node = nodeList.item(i);\r | |
444 | String nodeName = node.getNodeName();\r | |
445 | if (nodeName.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT)) {\r | |
446 | Element formatElement = (Element) node;\r | |
447 | def.timeStampOutputFormat = formatElement.getTextContent();\r | |
448 | } else if (nodeName.equals(INPUT_ELEMENT_ELEMENT)) {\r | |
449 | InputElement inputElement = extractInputElement((Element) node);\r | |
450 | if (inputElement != null) {\r | |
451 | if (def.rootInputElement == null) {\r | |
452 | def.rootInputElement = inputElement;\r | |
453 | } else {\r | |
454 | return null;\r | |
455 | }\r | |
456 | }\r | |
457 | } else if (nodeName.equals(OUTPUT_COLUMN_ELEMENT)) {\r | |
458 | Element outputColumnElement = (Element) node;\r | |
459 | OutputColumn outputColumn = new OutputColumn();\r | |
460 | outputColumn.name = outputColumnElement.getAttribute(NAME_ATTRIBUTE);\r | |
461 | def.outputs.add(outputColumn);\r | |
462 | }\r | |
463 | }\r | |
464 | return def;\r | |
465 | }\r | |
466 | \r | |
467 | private static InputElement extractInputElement(Element inputElementElement) {\r | |
468 | InputElement inputElement = new InputElement();\r | |
469 | inputElement.elementName = inputElementElement.getAttribute(NAME_ATTRIBUTE);\r | |
470 | inputElement.logEntry = (Boolean.toString(true).equals(inputElementElement.getAttribute(LOG_ENTRY_ATTRIBUTE))) ? true : false;\r | |
471 | NodeList nodeList = inputElementElement.getChildNodes();\r | |
472 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
473 | Node node = nodeList.item(i);\r | |
474 | String nodeName = node.getNodeName();\r | |
475 | if (nodeName.equals(INPUT_DATA_ELEMENT)) {\r | |
476 | Element inputDataElement = (Element) node;\r | |
477 | inputElement.inputName = inputDataElement.getAttribute(NAME_ATTRIBUTE);\r | |
478 | inputElement.inputAction = Integer.parseInt(inputDataElement.getAttribute(ACTION_ATTRIBUTE));\r | |
479 | inputElement.inputFormat = inputDataElement.getAttribute(FORMAT_ATTRIBUTE);\r | |
480 | } else if (nodeName.equals(ATTRIBUTE_ELEMENT)) {\r | |
481 | Element attributeElement = (Element) node;\r | |
482 | InputAttribute attribute = new InputAttribute();\r | |
483 | attribute.attributeName = attributeElement.getAttribute(NAME_ATTRIBUTE);\r | |
484 | NodeList attributeNodeList = attributeElement.getChildNodes();\r | |
485 | for (int j = 0; j < attributeNodeList.getLength(); j++) {\r | |
486 | Node attributeNode = attributeNodeList.item(j);\r | |
487 | String attributeNodeName = attributeNode.getNodeName();\r | |
488 | if (attributeNodeName.equals(INPUT_DATA_ELEMENT)) {\r | |
489 | Element inputDataElement = (Element) attributeNode;\r | |
490 | attribute.inputName = inputDataElement.getAttribute(NAME_ATTRIBUTE);\r | |
491 | attribute.inputAction = Integer.parseInt(inputDataElement.getAttribute(ACTION_ATTRIBUTE));\r | |
492 | attribute.inputFormat = inputDataElement.getAttribute(FORMAT_ATTRIBUTE);\r | |
493 | }\r | |
494 | }\r | |
495 | inputElement.addAttribute(attribute);\r | |
496 | } else if (nodeName.equals(INPUT_ELEMENT_ELEMENT)) {\r | |
497 | Element childInputElementElement = (Element) node;\r | |
498 | InputElement childInputElement = extractInputElement(childInputElementElement);\r | |
499 | if (childInputElement != null) {\r | |
500 | inputElement.addChild(childInputElement);\r | |
501 | }\r | |
502 | }\r | |
503 | }\r | |
504 | return inputElement;\r | |
505 | }\r | |
506 | \r | |
507 | public static void delete(String definitionName) {\r | |
508 | try {\r | |
509 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r | |
510 | DocumentBuilder db = dbf.newDocumentBuilder();\r | |
511 | \r | |
512 | // The following allows xml parsing without access to the dtd\r | |
513 | EntityResolver resolver = new EntityResolver () {\r | |
d4011df2 FC |
514 | @Override\r |
515 | public InputSource resolveEntity (String publicId, String systemId) {\r | |
3b38ea61 | 516 | String empty = ""; //$NON-NLS-1$\r |
c3c5c786 FC |
517 | ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());\r |
518 | return new InputSource(bais);\r | |
519 | }\r | |
520 | };\r | |
521 | db.setEntityResolver(resolver);\r | |
522 | \r | |
523 | // The following catches xml parsing exceptions\r | |
524 | db.setErrorHandler(new ErrorHandler(){\r | |
d4011df2 FC |
525 | @Override\r |
526 | public void error(SAXParseException saxparseexception) throws SAXException {}\r | |
527 | @Override\r | |
528 | public void warning(SAXParseException saxparseexception) throws SAXException {}\r | |
529 | @Override\r | |
530 | public void fatalError(SAXParseException saxparseexception) throws SAXException {\r | |
c3c5c786 FC |
531 | throw saxparseexception;\r |
532 | }});\r | |
533 | \r | |
534 | File file = new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME);\r | |
535 | Document doc = db.parse(file);\r | |
536 | \r | |
537 | Element root = doc.getDocumentElement();\r | |
538 | if (! root.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT)) {\r | |
539 | return;\r | |
540 | }\r | |
541 | \r | |
542 | NodeList nodeList = root.getChildNodes();\r | |
543 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
544 | Node node = nodeList.item(i);\r | |
545 | if (node instanceof Element &&\r | |
546 | node.getNodeName().equals(DEFINITION_ELEMENT) &&\r | |
547 | definitionName.equals(((Element) node).getAttribute(NAME_ATTRIBUTE))) {\r | |
548 | root.removeChild(node);\r | |
549 | }\r | |
550 | }\r | |
551 | \r | |
552 | Transformer transformer = TransformerFactory.newInstance().newTransformer();\r | |
3b38ea61 | 553 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$\r |
c3c5c786 FC |
554 | \r |
555 | //initialize StreamResult with File object to save to file\r | |
556 | StreamResult result = new StreamResult(new StringWriter());\r | |
557 | DOMSource source = new DOMSource(doc);\r | |
558 | transformer.transform(source, result);\r | |
559 | String xmlString = result.getWriter().toString();\r | |
560 | \r | |
561 | FileWriter writer = new FileWriter(file);\r | |
562 | writer.write(xmlString);\r | |
563 | writer.close();\r | |
564 | } catch (ParserConfigurationException e) {\r | |
565 | e.printStackTrace();\r | |
566 | } catch (SAXException e) {\r | |
567 | e.printStackTrace();\r | |
568 | } catch (IOException e) {\r | |
569 | e.printStackTrace();\r | |
570 | } catch (TransformerConfigurationException e) {\r | |
571 | e.printStackTrace();\r | |
572 | } catch (TransformerFactoryConfigurationError e) {\r | |
573 | e.printStackTrace();\r | |
574 | } catch (TransformerException e) {\r | |
575 | e.printStackTrace();\r | |
576 | }\r | |
577 | }\r | |
578 | }\r |