1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.parsers
.custom
;
15 import java
.io
.ByteArrayInputStream
;
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
;
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
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.TmfUiPlugin
;
36 import org
.w3c
.dom
.Document
;
37 import org
.w3c
.dom
.Element
;
38 import org
.w3c
.dom
.Node
;
39 import org
.w3c
.dom
.NodeList
;
40 import org
.xml
.sax
.EntityResolver
;
41 import org
.xml
.sax
.ErrorHandler
;
42 import org
.xml
.sax
.InputSource
;
43 import org
.xml
.sax
.SAXException
;
44 import org
.xml
.sax
.SAXParseException
;
46 public class CustomXmlTraceDefinition
extends CustomTraceDefinition
{
48 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
= "custom_xml_parsers.xml"; //$NON-NLS-1$
49 protected static final String CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
=
50 TmfUiPlugin
.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
52 public static final String TAG_IGNORE
= Messages
.CustomXmlTraceDefinition_ignoreTag
;
54 private static final String CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
= Messages
.CustomXmlTraceDefinition_definitionRootElement
;
55 private static final String DEFINITION_ELEMENT
= Messages
.CustomXmlTraceDefinition_definition
;
56 private static final String NAME_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_name
;
57 private static final String LOG_ENTRY_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_logEntry
;
58 private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT
= Messages
.CustomXmlTraceDefinition_timestampOutputFormat
;
59 private static final String INPUT_ELEMENT_ELEMENT
= Messages
.CustomXmlTraceDefinition_inputElement
;
60 private static final String ATTRIBUTE_ELEMENT
= Messages
.CustomXmlTraceDefinition_attribute
;
61 private static final String INPUT_DATA_ELEMENT
= Messages
.CustomXmlTraceDefinition_inputData
;
62 private static final String ACTION_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_action
;
63 private static final String FORMAT_ATTRIBUTE
= Messages
.CustomXmlTraceDefinition_format
;
64 private static final String OUTPUT_COLUMN_ELEMENT
= Messages
.CustomXmlTraceDefinition_outputColumn
;
66 public InputElement rootInputElement
;
68 public CustomXmlTraceDefinition() {
69 this("", null, new ArrayList
<OutputColumn
>(), ""); //$NON-NLS-1$ //$NON-NLS-2$
72 public CustomXmlTraceDefinition(String logtype
, InputElement rootElement
, List
<OutputColumn
> outputs
, String timeStampOutputFormat
) {
73 this.definitionName
= logtype
;
74 this.rootInputElement
= rootElement
;
75 this.outputs
= outputs
;
76 this.timeStampOutputFormat
= timeStampOutputFormat
;
79 public static class InputElement
{
80 public String elementName
;
81 public boolean logEntry
;
82 public String inputName
;
83 public int inputAction
;
84 public String inputFormat
;
85 public List
<InputAttribute
> attributes
;
86 public InputElement parentElement
;
87 public InputElement nextElement
;
88 public List
<InputElement
> childElements
;
90 public InputElement() {};
92 public InputElement(String elementName
, boolean logEntry
, String inputName
, int inputAction
, String inputFormat
, List
<InputAttribute
> attributes
) {
93 this.elementName
= elementName
;
94 this.logEntry
= logEntry
;
95 this.inputName
= inputName
;
96 this.inputAction
= inputAction
;
97 this.inputFormat
= inputFormat
;
98 this.attributes
= attributes
;
101 public void addAttribute(InputAttribute attribute
) {
102 if (attributes
== null) {
103 attributes
= new ArrayList
<InputAttribute
>(1);
105 attributes
.add(attribute
);
108 public void addChild(InputElement input
) {
109 if (childElements
== null) {
110 childElements
= new ArrayList
<InputElement
>(1);
111 } else if (childElements
.size() > 0) {
112 InputElement last
= childElements
.get(childElements
.size() - 1);
113 last
.nextElement
= input
;
115 childElements
.add(input
);
116 input
.parentElement
= this;
119 public void addNext(InputElement input
) {
120 if (parentElement
!= null) {
121 int index
= parentElement
.childElements
.indexOf(this);
122 parentElement
.childElements
.add(index
+ 1, input
);
123 InputElement next
= nextElement
;
125 input
.nextElement
= next
;
127 input
.parentElement
= this.parentElement
;
130 public void moveUp() {
131 if (parentElement
!= null) {
132 int index
= parentElement
.childElements
.indexOf(this);
134 parentElement
.childElements
.add(index
- 1 , parentElement
.childElements
.remove(index
));
135 parentElement
.childElements
.get(index
).nextElement
= nextElement
;
136 nextElement
= parentElement
.childElements
.get(index
);
141 public void moveDown() {
142 if (parentElement
!= null) {
143 int index
= parentElement
.childElements
.indexOf(this);
144 if (index
< parentElement
.childElements
.size() - 1) {
145 parentElement
.childElements
.add(index
+ 1 , parentElement
.childElements
.remove(index
));
146 nextElement
= parentElement
.childElements
.get(index
).nextElement
;
147 parentElement
.childElements
.get(index
).nextElement
= this;
154 public static class InputAttribute
{
155 public String attributeName
;
156 public String inputName
;
157 public int inputAction
;
158 public String inputFormat
;
160 public InputAttribute() {};
162 public InputAttribute(String attributeName
, String inputName
, int inputAction
, String inputFormat
) {
163 this.attributeName
= attributeName
;
164 this.inputName
= inputName
;
165 this.inputAction
= inputAction
;
166 this.inputFormat
= inputFormat
;
172 save(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
176 public void save(String path
) {
178 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
179 DocumentBuilder db
= dbf
.newDocumentBuilder();
181 // The following allows xml parsing without access to the dtd
182 EntityResolver resolver
= new EntityResolver () {
184 public InputSource
resolveEntity (String publicId
, String systemId
) {
185 String empty
= ""; //$NON-NLS-1$
186 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
187 return new InputSource(bais
);
190 db
.setEntityResolver(resolver
);
192 // The following catches xml parsing exceptions
193 db
.setErrorHandler(new ErrorHandler(){
195 public void error(SAXParseException saxparseexception
) throws SAXException
{}
197 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
199 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
200 throw saxparseexception
;
204 File file
= new File(path
);
205 if (file
.canRead()) {
206 doc
= db
.parse(file
);
207 if (! doc
.getDocumentElement().getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
211 doc
= db
.newDocument();
212 Node node
= doc
.createElement(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
);
213 doc
.appendChild(node
);
216 Element root
= doc
.getDocumentElement();
218 NodeList nodeList
= root
.getChildNodes();
219 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
220 Node node
= nodeList
.item(i
);
221 if (node
instanceof Element
&&
222 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
223 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
224 root
.removeChild(node
);
227 Element definitionElement
= doc
.createElement(DEFINITION_ELEMENT
);
228 root
.appendChild(definitionElement
);
229 definitionElement
.setAttribute(NAME_ATTRIBUTE
, definitionName
);
231 Element formatElement
= doc
.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
);
232 definitionElement
.appendChild(formatElement
);
233 formatElement
.appendChild(doc
.createTextNode(timeStampOutputFormat
));
235 if (rootInputElement
!= null) {
236 definitionElement
.appendChild(createInputElementElement(rootInputElement
, doc
));
239 if (outputs
!= null) {
240 for (OutputColumn output
: outputs
) {
241 Element outputColumnElement
= doc
.createElement(OUTPUT_COLUMN_ELEMENT
);
242 definitionElement
.appendChild(outputColumnElement
);
243 outputColumnElement
.setAttribute(NAME_ATTRIBUTE
, output
.name
);
247 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
248 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
250 //initialize StreamResult with File object to save to file
251 StreamResult result
= new StreamResult(new StringWriter());
252 DOMSource source
= new DOMSource(doc
);
253 transformer
.transform(source
, result
);
254 String xmlString
= result
.getWriter().toString();
256 FileWriter writer
= new FileWriter(file
);
257 writer
.write(xmlString
);
259 } catch (ParserConfigurationException e
) {
261 } catch (TransformerConfigurationException e
) {
263 } catch (TransformerFactoryConfigurationError e
) {
265 } catch (TransformerException e
) {
267 } catch (IOException e
) {
269 } catch (SAXException e
) {
274 private Element
createInputElementElement(InputElement inputElement
, Document doc
) {
275 Element inputElementElement
= doc
.createElement(INPUT_ELEMENT_ELEMENT
);
276 inputElementElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.elementName
);
278 if (inputElement
.logEntry
) {
279 inputElementElement
.setAttribute(LOG_ENTRY_ATTRIBUTE
, Boolean
.toString(inputElement
.logEntry
));
282 if (inputElement
.parentElement
!= null) {
283 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
284 inputElementElement
.appendChild(inputDataElement
);
285 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.inputName
);
286 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(inputElement
.inputAction
));
287 if (inputElement
.inputFormat
!= null) {
288 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, inputElement
.inputFormat
);
292 if (inputElement
.attributes
!= null) {
293 for (InputAttribute attribute
: inputElement
.attributes
) {
294 Element inputAttributeElement
= doc
.createElement(ATTRIBUTE_ELEMENT
);
295 inputElementElement
.appendChild(inputAttributeElement
);
296 inputAttributeElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.attributeName
);
297 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
298 inputAttributeElement
.appendChild(inputDataElement
);
299 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, attribute
.inputName
);
300 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(attribute
.inputAction
));
301 if (attribute
.inputFormat
!= null) {
302 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, attribute
.inputFormat
);
307 if (inputElement
.childElements
!= null) {
308 for (InputElement childInputElement
: inputElement
.childElements
) {
309 inputElementElement
.appendChild(createInputElementElement(childInputElement
, doc
));
313 return inputElementElement
;
316 public static CustomXmlTraceDefinition
[] loadAll() {
317 return loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
320 public static CustomXmlTraceDefinition
[] loadAll(String path
) {
322 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
323 DocumentBuilder db
= dbf
.newDocumentBuilder();
325 // The following allows xml parsing without access to the dtd
326 EntityResolver resolver
= new EntityResolver () {
328 public InputSource
resolveEntity (String publicId
, String systemId
) {
329 String empty
= ""; //$NON-NLS-1$
330 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
331 return new InputSource(bais
);
334 db
.setEntityResolver(resolver
);
336 // The following catches xml parsing exceptions
337 db
.setErrorHandler(new ErrorHandler(){
339 public void error(SAXParseException saxparseexception
) throws SAXException
{}
341 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
343 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
344 throw saxparseexception
;
347 File file
= new File(path
);
348 if (!file
.canRead()) {
349 return new CustomXmlTraceDefinition
[0];
351 Document doc
= db
.parse(file
);
353 Element root
= doc
.getDocumentElement();
354 if (! root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
355 return new CustomXmlTraceDefinition
[0];
358 ArrayList
<CustomXmlTraceDefinition
> defList
= new ArrayList
<CustomXmlTraceDefinition
>();
359 NodeList nodeList
= root
.getChildNodes();
360 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
361 Node node
= nodeList
.item(i
);
362 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
363 CustomXmlTraceDefinition def
= extractDefinition((Element
) node
);
369 return defList
.toArray(new CustomXmlTraceDefinition
[0]);
370 } catch (ParserConfigurationException e
) {
372 } catch (SAXException e
) {
374 } catch (IOException e
) {
377 return new CustomXmlTraceDefinition
[0];
380 public static CustomXmlTraceDefinition
load(String definitionName
) {
382 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
383 DocumentBuilder db
= dbf
.newDocumentBuilder();
385 // The following allows xml parsing without access to the dtd
386 EntityResolver resolver
= new EntityResolver () {
388 public InputSource
resolveEntity (String publicId
, String systemId
) {
389 String empty
= ""; //$NON-NLS-1$
390 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
391 return new InputSource(bais
);
394 db
.setEntityResolver(resolver
);
396 // The following catches xml parsing exceptions
397 db
.setErrorHandler(new ErrorHandler(){
399 public void error(SAXParseException saxparseexception
) throws SAXException
{}
401 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
403 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
404 throw saxparseexception
;
407 File file
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
408 Document doc
= db
.parse(file
);
410 Element root
= doc
.getDocumentElement();
411 if (! root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
415 NodeList nodeList
= root
.getChildNodes();
416 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
417 Node node
= nodeList
.item(i
);
418 if (node
instanceof Element
&&
419 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
420 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
421 return extractDefinition((Element
) node
);
424 } catch (ParserConfigurationException e
) {
426 } catch (SAXException e
) {
428 } catch (IOException e
) {
434 public static CustomXmlTraceDefinition
extractDefinition(Element definitionElement
) {
435 CustomXmlTraceDefinition def
= new CustomXmlTraceDefinition();
437 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
438 if (def
.definitionName
== null) return null;
440 NodeList nodeList
= definitionElement
.getChildNodes();
441 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
442 Node node
= nodeList
.item(i
);
443 String nodeName
= node
.getNodeName();
444 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
445 Element formatElement
= (Element
) node
;
446 def
.timeStampOutputFormat
= formatElement
.getTextContent();
447 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
448 InputElement inputElement
= extractInputElement((Element
) node
);
449 if (inputElement
!= null) {
450 if (def
.rootInputElement
== null) {
451 def
.rootInputElement
= inputElement
;
456 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
457 Element outputColumnElement
= (Element
) node
;
458 OutputColumn outputColumn
= new OutputColumn();
459 outputColumn
.name
= outputColumnElement
.getAttribute(NAME_ATTRIBUTE
);
460 def
.outputs
.add(outputColumn
);
466 private static InputElement
extractInputElement(Element inputElementElement
) {
467 InputElement inputElement
= new InputElement();
468 inputElement
.elementName
= inputElementElement
.getAttribute(NAME_ATTRIBUTE
);
469 inputElement
.logEntry
= (Boolean
.toString(true).equals(inputElementElement
.getAttribute(LOG_ENTRY_ATTRIBUTE
))) ?
true : false;
470 NodeList nodeList
= inputElementElement
.getChildNodes();
471 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
472 Node node
= nodeList
.item(i
);
473 String nodeName
= node
.getNodeName();
474 if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
475 Element inputDataElement
= (Element
) node
;
476 inputElement
.inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
477 inputElement
.inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
478 inputElement
.inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
479 } else if (nodeName
.equals(ATTRIBUTE_ELEMENT
)) {
480 Element attributeElement
= (Element
) node
;
481 InputAttribute attribute
= new InputAttribute();
482 attribute
.attributeName
= attributeElement
.getAttribute(NAME_ATTRIBUTE
);
483 NodeList attributeNodeList
= attributeElement
.getChildNodes();
484 for (int j
= 0; j
< attributeNodeList
.getLength(); j
++) {
485 Node attributeNode
= attributeNodeList
.item(j
);
486 String attributeNodeName
= attributeNode
.getNodeName();
487 if (attributeNodeName
.equals(INPUT_DATA_ELEMENT
)) {
488 Element inputDataElement
= (Element
) attributeNode
;
489 attribute
.inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
490 attribute
.inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
491 attribute
.inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
494 inputElement
.addAttribute(attribute
);
495 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
496 Element childInputElementElement
= (Element
) node
;
497 InputElement childInputElement
= extractInputElement(childInputElementElement
);
498 if (childInputElement
!= null) {
499 inputElement
.addChild(childInputElement
);
506 public static void delete(String definitionName
) {
508 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
509 DocumentBuilder db
= dbf
.newDocumentBuilder();
511 // The following allows xml parsing without access to the dtd
512 EntityResolver resolver
= new EntityResolver () {
514 public InputSource
resolveEntity (String publicId
, String systemId
) {
515 String empty
= ""; //$NON-NLS-1$
516 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
517 return new InputSource(bais
);
520 db
.setEntityResolver(resolver
);
522 // The following catches xml parsing exceptions
523 db
.setErrorHandler(new ErrorHandler(){
525 public void error(SAXParseException saxparseexception
) throws SAXException
{}
527 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
529 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
530 throw saxparseexception
;
533 File file
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
534 Document doc
= db
.parse(file
);
536 Element root
= doc
.getDocumentElement();
537 if (! root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
541 NodeList nodeList
= root
.getChildNodes();
542 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
543 Node node
= nodeList
.item(i
);
544 if (node
instanceof Element
&&
545 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
546 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
547 root
.removeChild(node
);
551 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
552 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
554 //initialize StreamResult with File object to save to file
555 StreamResult result
= new StreamResult(new StringWriter());
556 DOMSource source
= new DOMSource(doc
);
557 transformer
.transform(source
, result
);
558 String xmlString
= result
.getWriter().toString();
560 FileWriter writer
= new FileWriter(file
);
561 writer
.write(xmlString
);
563 } catch (ParserConfigurationException e
) {
565 } catch (SAXException e
) {
567 } catch (IOException e
) {
569 } catch (TransformerConfigurationException e
) {
571 } catch (TransformerFactoryConfigurationError e
) {
573 } catch (TransformerException e
) {