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
.internal
.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
.internal
.tmf
.ui
.Messages
;
36 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.TmfUiPlugin
;
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
;
47 public class CustomXmlTraceDefinition
extends CustomTraceDefinition
{
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 TmfUiPlugin
.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_XML_TRACE_DEFINITIONS_FILE_NAME
).toString();
53 public static final String TAG_IGNORE
= Messages
.CustomXmlTraceDefinition_ignoreTag
;
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
;
67 public InputElement rootInputElement
;
69 public CustomXmlTraceDefinition() {
70 this("", null, new ArrayList
<OutputColumn
>(), ""); //$NON-NLS-1$ //$NON-NLS-2$
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
;
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
;
91 public InputElement() {};
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
;
102 public void addAttribute(InputAttribute attribute
) {
103 if (attributes
== null) {
104 attributes
= new ArrayList
<InputAttribute
>(1);
106 attributes
.add(attribute
);
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
;
116 childElements
.add(input
);
117 input
.parentElement
= this;
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
;
126 input
.nextElement
= next
;
128 input
.parentElement
= this.parentElement
;
131 public void moveUp() {
132 if (parentElement
!= null) {
133 int index
= parentElement
.childElements
.indexOf(this);
135 parentElement
.childElements
.add(index
- 1 , parentElement
.childElements
.remove(index
));
136 parentElement
.childElements
.get(index
).nextElement
= nextElement
;
137 nextElement
= parentElement
.childElements
.get(index
);
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;
155 public static class InputAttribute
{
156 public String attributeName
;
157 public String inputName
;
158 public int inputAction
;
159 public String inputFormat
;
161 public InputAttribute() {};
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
;
173 save(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
177 public void save(String path
) {
179 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
180 DocumentBuilder db
= dbf
.newDocumentBuilder();
182 // The following allows xml parsing without access to the dtd
183 EntityResolver resolver
= new EntityResolver () {
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
);
191 db
.setEntityResolver(resolver
);
193 // The following catches xml parsing exceptions
194 db
.setErrorHandler(new ErrorHandler(){
196 public void error(SAXParseException saxparseexception
) throws SAXException
{}
198 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
200 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
201 throw saxparseexception
;
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
)) {
212 doc
= db
.newDocument();
213 Node node
= doc
.createElement(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
);
214 doc
.appendChild(node
);
217 Element root
= doc
.getDocumentElement();
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
);
228 Element definitionElement
= doc
.createElement(DEFINITION_ELEMENT
);
229 root
.appendChild(definitionElement
);
230 definitionElement
.setAttribute(NAME_ATTRIBUTE
, definitionName
);
232 Element formatElement
= doc
.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
);
233 definitionElement
.appendChild(formatElement
);
234 formatElement
.appendChild(doc
.createTextNode(timeStampOutputFormat
));
236 if (rootInputElement
!= null) {
237 definitionElement
.appendChild(createInputElementElement(rootInputElement
, doc
));
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
);
248 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
249 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
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();
257 FileWriter writer
= new FileWriter(file
);
258 writer
.write(xmlString
);
260 } catch (ParserConfigurationException e
) {
262 } catch (TransformerConfigurationException e
) {
264 } catch (TransformerFactoryConfigurationError e
) {
266 } catch (TransformerException e
) {
268 } catch (IOException e
) {
270 } catch (SAXException e
) {
275 private Element
createInputElementElement(InputElement inputElement
, Document doc
) {
276 Element inputElementElement
= doc
.createElement(INPUT_ELEMENT_ELEMENT
);
277 inputElementElement
.setAttribute(NAME_ATTRIBUTE
, inputElement
.elementName
);
279 if (inputElement
.logEntry
) {
280 inputElementElement
.setAttribute(LOG_ENTRY_ATTRIBUTE
, Boolean
.toString(inputElement
.logEntry
));
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
);
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
);
308 if (inputElement
.childElements
!= null) {
309 for (InputElement childInputElement
: inputElement
.childElements
) {
310 inputElementElement
.appendChild(createInputElementElement(childInputElement
, doc
));
314 return inputElementElement
;
317 public static CustomXmlTraceDefinition
[] loadAll() {
318 return loadAll(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
321 public static CustomXmlTraceDefinition
[] loadAll(String path
) {
323 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
324 DocumentBuilder db
= dbf
.newDocumentBuilder();
326 // The following allows xml parsing without access to the dtd
327 EntityResolver resolver
= new EntityResolver () {
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
);
335 db
.setEntityResolver(resolver
);
337 // The following catches xml parsing exceptions
338 db
.setErrorHandler(new ErrorHandler(){
340 public void error(SAXParseException saxparseexception
) throws SAXException
{}
342 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
344 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
345 throw saxparseexception
;
348 File file
= new File(path
);
349 if (!file
.canRead()) {
350 return new CustomXmlTraceDefinition
[0];
352 Document doc
= db
.parse(file
);
354 Element root
= doc
.getDocumentElement();
355 if (! root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
356 return new CustomXmlTraceDefinition
[0];
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
);
370 return defList
.toArray(new CustomXmlTraceDefinition
[0]);
371 } catch (ParserConfigurationException e
) {
373 } catch (SAXException e
) {
375 } catch (IOException e
) {
378 return new CustomXmlTraceDefinition
[0];
381 public static CustomXmlTraceDefinition
load(String definitionName
) {
383 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
384 DocumentBuilder db
= dbf
.newDocumentBuilder();
386 // The following allows xml parsing without access to the dtd
387 EntityResolver resolver
= new EntityResolver () {
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
);
395 db
.setEntityResolver(resolver
);
397 // The following catches xml parsing exceptions
398 db
.setErrorHandler(new ErrorHandler(){
400 public void error(SAXParseException saxparseexception
) throws SAXException
{}
402 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
404 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
405 throw saxparseexception
;
408 File file
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
409 Document doc
= db
.parse(file
);
411 Element root
= doc
.getDocumentElement();
412 if (! root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
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
);
425 } catch (ParserConfigurationException e
) {
427 } catch (SAXException e
) {
429 } catch (IOException e
) {
435 public static CustomXmlTraceDefinition
extractDefinition(Element definitionElement
) {
436 CustomXmlTraceDefinition def
= new CustomXmlTraceDefinition();
438 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
439 if (def
.definitionName
== null) return null;
441 NodeList nodeList
= definitionElement
.getChildNodes();
442 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
443 Node node
= nodeList
.item(i
);
444 String nodeName
= node
.getNodeName();
445 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
446 Element formatElement
= (Element
) node
;
447 def
.timeStampOutputFormat
= formatElement
.getTextContent();
448 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
449 InputElement inputElement
= extractInputElement((Element
) node
);
450 if (inputElement
!= null) {
451 if (def
.rootInputElement
== null) {
452 def
.rootInputElement
= inputElement
;
457 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
458 Element outputColumnElement
= (Element
) node
;
459 OutputColumn outputColumn
= new OutputColumn();
460 outputColumn
.name
= outputColumnElement
.getAttribute(NAME_ATTRIBUTE
);
461 def
.outputs
.add(outputColumn
);
467 private static InputElement
extractInputElement(Element inputElementElement
) {
468 InputElement inputElement
= new InputElement();
469 inputElement
.elementName
= inputElementElement
.getAttribute(NAME_ATTRIBUTE
);
470 inputElement
.logEntry
= (Boolean
.toString(true).equals(inputElementElement
.getAttribute(LOG_ENTRY_ATTRIBUTE
))) ?
true : false;
471 NodeList nodeList
= inputElementElement
.getChildNodes();
472 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
473 Node node
= nodeList
.item(i
);
474 String nodeName
= node
.getNodeName();
475 if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
476 Element inputDataElement
= (Element
) node
;
477 inputElement
.inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
478 inputElement
.inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
479 inputElement
.inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
480 } else if (nodeName
.equals(ATTRIBUTE_ELEMENT
)) {
481 Element attributeElement
= (Element
) node
;
482 InputAttribute attribute
= new InputAttribute();
483 attribute
.attributeName
= attributeElement
.getAttribute(NAME_ATTRIBUTE
);
484 NodeList attributeNodeList
= attributeElement
.getChildNodes();
485 for (int j
= 0; j
< attributeNodeList
.getLength(); j
++) {
486 Node attributeNode
= attributeNodeList
.item(j
);
487 String attributeNodeName
= attributeNode
.getNodeName();
488 if (attributeNodeName
.equals(INPUT_DATA_ELEMENT
)) {
489 Element inputDataElement
= (Element
) attributeNode
;
490 attribute
.inputName
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
491 attribute
.inputAction
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
492 attribute
.inputFormat
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
495 inputElement
.addAttribute(attribute
);
496 } else if (nodeName
.equals(INPUT_ELEMENT_ELEMENT
)) {
497 Element childInputElementElement
= (Element
) node
;
498 InputElement childInputElement
= extractInputElement(childInputElementElement
);
499 if (childInputElement
!= null) {
500 inputElement
.addChild(childInputElement
);
507 public static void delete(String definitionName
) {
509 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
510 DocumentBuilder db
= dbf
.newDocumentBuilder();
512 // The following allows xml parsing without access to the dtd
513 EntityResolver resolver
= new EntityResolver () {
515 public InputSource
resolveEntity (String publicId
, String systemId
) {
516 String empty
= ""; //$NON-NLS-1$
517 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
518 return new InputSource(bais
);
521 db
.setEntityResolver(resolver
);
523 // The following catches xml parsing exceptions
524 db
.setErrorHandler(new ErrorHandler(){
526 public void error(SAXParseException saxparseexception
) throws SAXException
{}
528 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
530 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
531 throw saxparseexception
;
534 File file
= new File(CUSTOM_XML_TRACE_DEFINITIONS_PATH_NAME
);
535 Document doc
= db
.parse(file
);
537 Element root
= doc
.getDocumentElement();
538 if (! root
.getNodeName().equals(CUSTOM_XML_TRACE_DEFINITION_ROOT_ELEMENT
)) {
542 NodeList nodeList
= root
.getChildNodes();
543 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
544 Node node
= nodeList
.item(i
);
545 if (node
instanceof Element
&&
546 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
547 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
548 root
.removeChild(node
);
552 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
553 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
555 //initialize StreamResult with File object to save to file
556 StreamResult result
= new StreamResult(new StringWriter());
557 DOMSource source
= new DOMSource(doc
);
558 transformer
.transform(source
, result
);
559 String xmlString
= result
.getWriter().toString();
561 FileWriter writer
= new FileWriter(file
);
562 writer
.write(xmlString
);
564 } catch (ParserConfigurationException e
) {
566 } catch (SAXException e
) {
568 } catch (IOException e
) {
570 } catch (TransformerConfigurationException e
) {
572 } catch (TransformerFactoryConfigurationError e
) {
574 } catch (TransformerException e
) {