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