1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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
.core
.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 java
.util
.regex
.Pattern
;
24 import java
.util
.regex
.PatternSyntaxException
;
26 import javax
.xml
.parsers
.DocumentBuilder
;
27 import javax
.xml
.parsers
.DocumentBuilderFactory
;
28 import javax
.xml
.parsers
.ParserConfigurationException
;
29 import javax
.xml
.transform
.OutputKeys
;
30 import javax
.xml
.transform
.Transformer
;
31 import javax
.xml
.transform
.TransformerConfigurationException
;
32 import javax
.xml
.transform
.TransformerException
;
33 import javax
.xml
.transform
.TransformerFactory
;
34 import javax
.xml
.transform
.TransformerFactoryConfigurationError
;
35 import javax
.xml
.transform
.dom
.DOMSource
;
36 import javax
.xml
.transform
.stream
.StreamResult
;
38 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TmfTraceType
;
40 import org
.w3c
.dom
.Document
;
41 import org
.w3c
.dom
.Element
;
42 import org
.w3c
.dom
.Node
;
43 import org
.w3c
.dom
.NodeList
;
44 import org
.xml
.sax
.EntityResolver
;
45 import org
.xml
.sax
.ErrorHandler
;
46 import org
.xml
.sax
.InputSource
;
47 import org
.xml
.sax
.SAXException
;
48 import org
.xml
.sax
.SAXParseException
;
51 * Trace definition for custom text traces.
53 * @author Patrick Tassé
56 public class CustomTxtTraceDefinition
extends CustomTraceDefinition
{
59 public List
<InputLine
> inputs
;
61 /** File name of the definition file */
62 protected static final String CUSTOM_TXT_TRACE_DEFINITIONS_FILE_NAME
= "custom_txt_parsers.xml"; //$NON-NLS-1$
64 /** Path of the definition file */
65 protected static final String CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
=
66 Activator
.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_TXT_TRACE_DEFINITIONS_FILE_NAME
).toString();
68 private static final String CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
= Messages
.CustomTxtTraceDefinition_definitionRootElement
;
69 private static final String DEFINITION_ELEMENT
= Messages
.CustomTxtTraceDefinition_definition
;
70 private static final String NAME_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_name
;
71 private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT
= Messages
.CustomTxtTraceDefinition_timestampOutputFormat
;
72 private static final String INPUT_LINE_ELEMENT
= Messages
.CustomTxtTraceDefinition_inputLine
;
73 private static final String CARDINALITY_ELEMENT
= Messages
.CustomTxtTraceDefinition_cardinality
;
74 private static final String MIN_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_min
;
75 private static final String MAX_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_max
;
76 private static final String REGEX_ELEMENT
= Messages
.CustomTxtTraceDefinition_regEx
;
77 private static final String INPUT_DATA_ELEMENT
= Messages
.CustomTxtTraceDefinition_inputData
;
78 private static final String ACTION_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_action
;
79 private static final String FORMAT_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_format
;
80 private static final String OUTPUT_COLUMN_ELEMENT
= Messages
.CustomTxtTraceDefinition_outputColumn
;
83 * Default constructor.
85 public CustomTxtTraceDefinition() {
86 this("", new ArrayList
<InputLine
>(0), new ArrayList
<OutputColumn
>(0), ""); //$NON-NLS-1$ //$NON-NLS-2$
93 * Name of the trace type
97 * List of output columns
98 * @param timeStampOutputFormat
99 * The timestamp format to use
101 public CustomTxtTraceDefinition(String logtype
, List
<InputLine
> inputs
,
102 List
<OutputColumn
> outputs
, String timeStampOutputFormat
) {
103 this.definitionName
= logtype
;
104 this.inputs
= inputs
;
105 this.outputs
= outputs
;
106 this.timeStampOutputFormat
= timeStampOutputFormat
;
110 * Wrapper to store a line of the log file
112 public static class InputLine
{
114 /** Data columns of this line */
115 public List
<InputData
> columns
;
117 /** Cardinality of this line (see {@link Cardinality}) */
118 public Cardinality cardinality
;
121 public InputLine parentInput
;
123 /** Level of this line */
126 /** Next input line in the file */
127 public InputLine nextInput
;
129 /** Children of this line (if one "event" spans many lines) */
130 public List
<InputLine
> childrenInputs
;
132 private String regex
;
133 private Pattern pattern
;
136 * Default (empty) constructor.
138 public InputLine() {}
143 * @param cardinality Cardinality of this line.
145 * @param columns Columns to use
147 public InputLine(Cardinality cardinality
, String regex
, List
<InputData
> columns
) {
148 this.cardinality
= cardinality
;
150 this.columns
= columns
;
154 * Set the regex of this input line
159 public void setRegex(String regex
) {
165 * Get the current regex
167 * @return The current regex
169 public String
getRegex() {
174 * Get the Pattern object of this line's regex
176 * @return The Pattern
177 * @throws PatternSyntaxException
178 * If the regex does not parse correctly
180 public Pattern
getPattern() throws PatternSyntaxException
{
181 if (pattern
== null) {
182 pattern
= Pattern
.compile(regex
);
188 * Add a child line to this line.
191 * The child input line
193 public void addChild(InputLine input
) {
194 if (childrenInputs
== null) {
195 childrenInputs
= new ArrayList
<>(1);
196 } else if (childrenInputs
.size() > 0) {
197 InputLine last
= childrenInputs
.get(childrenInputs
.size() - 1);
198 last
.nextInput
= input
;
200 childrenInputs
.add(input
);
201 input
.parentInput
= this;
202 input
.level
= this.level
+ 1;
206 * Set the next input line.
209 * The next input line
211 public void addNext(InputLine input
) {
212 if (parentInput
!= null) {
213 int index
= parentInput
.childrenInputs
.indexOf(this);
214 parentInput
.childrenInputs
.add(index
+ 1, input
);
215 InputLine next
= nextInput
;
217 input
.nextInput
= next
;
219 input
.parentInput
= this.parentInput
;
220 input
.level
= this.level
;
224 * Move this line up in its parent's children.
226 public void moveUp() {
227 if (parentInput
!= null) {
228 int index
= parentInput
.childrenInputs
.indexOf(this);
230 parentInput
.childrenInputs
.add(index
- 1 , parentInput
.childrenInputs
.remove(index
));
231 parentInput
.childrenInputs
.get(index
).nextInput
= nextInput
;
232 nextInput
= parentInput
.childrenInputs
.get(index
);
238 * Move this line down in its parent's children.
240 public void moveDown() {
241 if (parentInput
!= null) {
242 int index
= parentInput
.childrenInputs
.indexOf(this);
243 if (index
< parentInput
.childrenInputs
.size() - 1) {
244 parentInput
.childrenInputs
.add(index
+ 1 , parentInput
.childrenInputs
.remove(index
));
245 nextInput
= parentInput
.childrenInputs
.get(index
).nextInput
;
246 parentInput
.childrenInputs
.get(index
).nextInput
= this;
252 * Add a data column to this line
257 public void addColumn(InputData column
) {
258 if (columns
== null) {
259 columns
= new ArrayList
<>(1);
265 * Get the next input lines.
268 * The map of line "sets".
269 * @return The next list of lines.
271 public List
<InputLine
> getNextInputs(Map
<InputLine
, Integer
> countMap
) {
272 List
<InputLine
> nextInputs
= new ArrayList
<>();
273 InputLine next
= nextInput
;
274 while (next
!= null) {
275 nextInputs
.add(next
);
276 if (next
.cardinality
.min
> 0) {
279 next
= next
.nextInput
;
281 if (parentInput
!= null && parentInput
.level
> 0) {
282 int parentCount
= countMap
.get(parentInput
);
283 if (parentCount
< parentInput
.getMaxCount()) {
284 nextInputs
.add(parentInput
);
286 if (parentCount
< parentInput
.getMinCount()) {
289 nextInputs
.addAll(parentInput
.getNextInputs(countMap
));
295 * Get the minimum possible amount of entries.
297 * @return The minimum
299 public int getMinCount() {
300 return cardinality
.min
;
304 * Get the maximum possible amount of entries.
306 * @return The maximum
308 public int getMaxCount() {
309 return cardinality
.max
;
313 public String
toString() {
314 return regex
+ " " + cardinality
; //$NON-NLS-1$
319 * Data column for input lines.
321 public static class InputData
{
323 /** Name of this column */
330 public String format
;
333 * Default (empty) constructor
335 public InputData() {}
341 * @param action Action
342 * @param format Format
344 public InputData(String name
, int action
, String format
) {
346 this.action
= action
;
347 this.format
= format
;
351 * Constructor with default format
354 * @param action Action
356 public InputData(String name
, int action
) {
358 this.action
= action
;
363 * Input line cardinality
365 public static class Cardinality
{
367 /** Representation of infinity */
368 public final static int INF
= Integer
.MAX_VALUE
;
370 /** Preset for [1, 1] */
371 public final static Cardinality ONE
= new Cardinality(1, 1);
373 /** Preset for [1, inf] */
374 public final static Cardinality ONE_OR_MORE
= new Cardinality(1, INF
);
376 /** Preset for [0, 1] */
377 public final static Cardinality ZERO_OR_ONE
= new Cardinality(0, 1);
379 /** Preset for [0, inf] */
380 public final static Cardinality ZERO_OR_MORE
= new Cardinality(0, INF
);
382 private final int min
;
383 private final int max
;
393 public Cardinality(int min
, int max
) {
399 public String
toString() {
400 return "(" + (min
>= 0 ? min
: "?") + "," + (max
== INF ?
"\u221E" : (max
>= 0 ? max
: "?")) + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
404 public int hashCode() {
405 final int prime
= 31;
407 result
= prime
* result
+ max
;
408 result
= prime
* result
+ min
;
413 public boolean equals(Object obj
) {
420 if (!(obj
instanceof Cardinality
)) {
423 Cardinality other
= (Cardinality
) obj
;
424 return (this.min
== other
.min
&& this.max
== other
.max
);
430 save(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
434 public void save(String path
) {
436 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
437 DocumentBuilder db
= dbf
.newDocumentBuilder();
439 // The following allows xml parsing without access to the dtd
440 EntityResolver resolver
= new EntityResolver() {
442 public InputSource
resolveEntity(String publicId
, String systemId
) {
443 String empty
= ""; //$NON-NLS-1$
444 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
445 return new InputSource(bais
);
448 db
.setEntityResolver(resolver
);
450 // The following catches xml parsing exceptions
451 db
.setErrorHandler(new ErrorHandler() {
453 public void error(SAXParseException saxparseexception
) throws SAXException
{}
456 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
459 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
460 throw saxparseexception
;
465 File file
= new File(path
);
466 if (file
.canRead()) {
467 doc
= db
.parse(file
);
468 if (! doc
.getDocumentElement().getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
472 doc
= db
.newDocument();
473 Node node
= doc
.createElement(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
);
474 doc
.appendChild(node
);
477 Element root
= doc
.getDocumentElement();
479 NodeList nodeList
= root
.getChildNodes();
480 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
481 Node node
= nodeList
.item(i
);
482 if (node
instanceof Element
&&
483 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
484 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
485 root
.removeChild(node
);
488 Element definitionElement
= doc
.createElement(DEFINITION_ELEMENT
);
489 root
.appendChild(definitionElement
);
490 definitionElement
.setAttribute(NAME_ATTRIBUTE
, definitionName
);
492 Element formatElement
= doc
.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
);
493 definitionElement
.appendChild(formatElement
);
494 formatElement
.appendChild(doc
.createTextNode(timeStampOutputFormat
));
496 if (inputs
!= null) {
497 for (InputLine inputLine
: inputs
) {
498 definitionElement
.appendChild(createInputLineElement(inputLine
, doc
));
502 if (outputs
!= null) {
503 for (OutputColumn output
: outputs
) {
504 Element outputColumnElement
= doc
.createElement(OUTPUT_COLUMN_ELEMENT
);
505 definitionElement
.appendChild(outputColumnElement
);
506 outputColumnElement
.setAttribute(NAME_ATTRIBUTE
, output
.name
);
510 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
511 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
513 //initialize StreamResult with File object to save to file
514 StreamResult result
= new StreamResult(new StringWriter());
515 DOMSource source
= new DOMSource(doc
);
516 transformer
.transform(source
, result
);
517 String xmlString
= result
.getWriter().toString();
519 try (FileWriter writer
= new FileWriter(file
);) {
520 writer
.write(xmlString
);
523 TmfTraceType
.getInstance().addCustomTraceType(TmfTraceType
.CUSTOM_TXT_CATEGORY
, definitionName
);
525 } catch (ParserConfigurationException e
) {
526 Activator
.logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
527 } catch (TransformerConfigurationException e
) {
528 Activator
.logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
529 } catch (TransformerFactoryConfigurationError e
) {
530 Activator
.logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
531 } catch (TransformerException e
) {
532 Activator
.logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
533 } catch (IOException e
) {
534 Activator
.logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
535 } catch (SAXException e
) {
536 Activator
.logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
540 private Element
createInputLineElement(InputLine inputLine
, Document doc
) {
541 Element inputLineElement
= doc
.createElement(INPUT_LINE_ELEMENT
);
543 Element cardinalityElement
= doc
.createElement(CARDINALITY_ELEMENT
);
544 inputLineElement
.appendChild(cardinalityElement
);
545 cardinalityElement
.setAttribute(MIN_ATTRIBUTE
, Integer
.toString(inputLine
.cardinality
.min
));
546 cardinalityElement
.setAttribute(MAX_ATTRIBUTE
, Integer
.toString(inputLine
.cardinality
.max
));
548 Element regexElement
= doc
.createElement(REGEX_ELEMENT
);
549 inputLineElement
.appendChild(regexElement
);
550 regexElement
.appendChild(doc
.createTextNode(inputLine
.regex
));
552 if (inputLine
.columns
!= null) {
553 for (InputData inputData
: inputLine
.columns
) {
554 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
555 inputLineElement
.appendChild(inputDataElement
);
556 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, inputData
.name
);
557 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(inputData
.action
));
558 if (inputData
.format
!= null) {
559 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, inputData
.format
);
564 if (inputLine
.childrenInputs
!= null) {
565 for (InputLine childInputLine
: inputLine
.childrenInputs
) {
566 inputLineElement
.appendChild(createInputLineElement(childInputLine
, doc
));
570 return inputLineElement
;
574 * Load the default text trace definitions file.
576 * @return The loaded trace definitions
578 public static CustomTxtTraceDefinition
[] loadAll() {
579 return loadAll(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
583 * Load a specific text trace definition file.
586 * The path to the file to load
587 * @return The loaded trace definitions
589 public static CustomTxtTraceDefinition
[] loadAll(String path
) {
591 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
592 DocumentBuilder db
= dbf
.newDocumentBuilder();
594 // The following allows xml parsing without access to the dtd
595 EntityResolver resolver
= new EntityResolver() {
597 public InputSource
resolveEntity(String publicId
, String systemId
) {
598 String empty
= ""; //$NON-NLS-1$
599 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
600 return new InputSource(bais
);
603 db
.setEntityResolver(resolver
);
605 // The following catches xml parsing exceptions
606 db
.setErrorHandler(new ErrorHandler() {
608 public void error(SAXParseException saxparseexception
) throws SAXException
{}
611 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
614 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
615 throw saxparseexception
;
619 File file
= new File(path
);
620 if (!file
.canRead()) {
621 return new CustomTxtTraceDefinition
[0];
623 Document doc
= db
.parse(file
);
625 Element root
= doc
.getDocumentElement();
626 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
627 return new CustomTxtTraceDefinition
[0];
630 ArrayList
<CustomTxtTraceDefinition
> defList
= new ArrayList
<>();
631 NodeList nodeList
= root
.getChildNodes();
632 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
633 Node node
= nodeList
.item(i
);
634 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
635 CustomTxtTraceDefinition def
= extractDefinition((Element
) node
);
641 return defList
.toArray(new CustomTxtTraceDefinition
[0]);
642 } catch (ParserConfigurationException e
) {
643 Activator
.logError("Error loading all in CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
644 } catch (SAXException e
) {
645 Activator
.logError("Error loading all in CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
646 } catch (IOException e
) {
647 Activator
.logError("Error loading all in CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
649 return new CustomTxtTraceDefinition
[0];
653 * Load a single definition.
655 * @param definitionName
656 * Name of the definition to load
657 * @return The loaded trace definition
659 public static CustomTxtTraceDefinition
load(String definitionName
) {
661 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
662 DocumentBuilder db
= dbf
.newDocumentBuilder();
664 // The following allows xml parsing without access to the dtd
665 EntityResolver resolver
= new EntityResolver() {
667 public InputSource
resolveEntity(String publicId
, String systemId
) {
668 String empty
= ""; //$NON-NLS-1$
669 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
670 return new InputSource(bais
);
673 db
.setEntityResolver(resolver
);
675 // The following catches xml parsing exceptions
676 db
.setErrorHandler(new ErrorHandler() {
678 public void error(SAXParseException saxparseexception
) throws SAXException
{}
681 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
684 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
685 throw saxparseexception
;
689 File file
= new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
690 Document doc
= db
.parse(file
);
692 Element root
= doc
.getDocumentElement();
693 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
697 NodeList nodeList
= root
.getChildNodes();
698 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
699 Node node
= nodeList
.item(i
);
700 if (node
instanceof Element
&&
701 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
702 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
703 return extractDefinition((Element
) node
);
706 } catch (ParserConfigurationException e
) {
707 Activator
.logError("Error loading CustomTxtTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
708 } catch (SAXException e
) {
709 Activator
.logError("Error loading CustomTxtTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
710 } catch (IOException e
) {
711 Activator
.logError("Error loading CustomTxtTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
717 * Get the definition from a definition element.
719 * @param definitionElement
720 * The Element to extract from
721 * @return The loaded trace definition
723 public static CustomTxtTraceDefinition
extractDefinition(Element definitionElement
) {
724 CustomTxtTraceDefinition def
= new CustomTxtTraceDefinition();
726 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
727 if (def
.definitionName
== null) {
731 NodeList nodeList
= definitionElement
.getChildNodes();
732 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
733 Node node
= nodeList
.item(i
);
734 String nodeName
= node
.getNodeName();
735 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
736 Element formatElement
= (Element
) node
;
737 def
.timeStampOutputFormat
= formatElement
.getTextContent();
738 } else if (nodeName
.equals(INPUT_LINE_ELEMENT
)) {
739 InputLine inputLine
= extractInputLine((Element
) node
);
740 if (inputLine
!= null) {
741 def
.inputs
.add(inputLine
);
743 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
744 Element outputColumnElement
= (Element
) node
;
745 OutputColumn outputColumn
= new OutputColumn();
746 outputColumn
.name
= outputColumnElement
.getAttribute(NAME_ATTRIBUTE
);
747 def
.outputs
.add(outputColumn
);
753 private static InputLine
extractInputLine(Element inputLineElement
) {
754 InputLine inputLine
= new InputLine();
755 NodeList nodeList
= inputLineElement
.getChildNodes();
756 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
757 Node node
= nodeList
.item(i
);
758 String nodeName
= node
.getNodeName();
759 if (nodeName
.equals(CARDINALITY_ELEMENT
)) {
760 Element cardinalityElement
= (Element
) node
;
762 int min
= Integer
.parseInt(cardinalityElement
.getAttribute(MIN_ATTRIBUTE
));
763 int max
= Integer
.parseInt(cardinalityElement
.getAttribute(MAX_ATTRIBUTE
));
764 inputLine
.cardinality
= new Cardinality(min
, max
);
765 } catch (NumberFormatException e
) {
768 } else if (nodeName
.equals(REGEX_ELEMENT
)) {
769 Element regexElement
= (Element
) node
;
770 inputLine
.regex
= regexElement
.getTextContent();
771 } else if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
772 Element inputDataElement
= (Element
) node
;
773 InputData inputData
= new InputData();
774 inputData
.name
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
775 inputData
.action
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
776 inputData
.format
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
777 inputLine
.addColumn(inputData
);
778 } else if (nodeName
.equals(INPUT_LINE_ELEMENT
)) {
779 Element childInputLineElement
= (Element
) node
;
780 InputLine childInputLine
= extractInputLine(childInputLineElement
);
781 if (childInputLine
!= null) {
782 inputLine
.addChild(childInputLine
);
790 * Delete a definition from the currently loaded ones.
792 * @param definitionName
793 * The name of the definition to delete
795 public static void delete(String definitionName
) {
797 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
798 DocumentBuilder db
= dbf
.newDocumentBuilder();
800 // The following allows xml parsing without access to the dtd
801 EntityResolver resolver
= new EntityResolver() {
803 public InputSource
resolveEntity(String publicId
, String systemId
) {
804 String empty
= ""; //$NON-NLS-1$
805 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
806 return new InputSource(bais
);
809 db
.setEntityResolver(resolver
);
811 // The following catches xml parsing exceptions
812 db
.setErrorHandler(new ErrorHandler() {
814 public void error(SAXParseException saxparseexception
) throws SAXException
{}
817 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
820 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
821 throw saxparseexception
;
825 File file
= new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
826 Document doc
= db
.parse(file
);
828 Element root
= doc
.getDocumentElement();
829 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
833 NodeList nodeList
= root
.getChildNodes();
834 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
835 Node node
= nodeList
.item(i
);
836 if (node
instanceof Element
&&
837 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
838 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
839 root
.removeChild(node
);
843 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
844 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
846 //initialize StreamResult with File object to save to file
847 StreamResult result
= new StreamResult(new StringWriter());
848 DOMSource source
= new DOMSource(doc
);
849 transformer
.transform(source
, result
);
850 String xmlString
= result
.getWriter().toString();
852 try (FileWriter writer
= new FileWriter(file
);) {
853 writer
.write(xmlString
);
856 TmfTraceType
.getInstance().removeCustomTraceType(TmfTraceType
.CUSTOM_TXT_CATEGORY
, definitionName
);
858 } catch (ParserConfigurationException e
) {
859 Activator
.logError("Error deleting CustomTxtTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
860 } catch (SAXException e
) {
861 Activator
.logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$
862 } catch (IOException e
) {
863 Activator
.logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$
864 } catch (TransformerConfigurationException e
) {
865 Activator
.logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$
866 } catch (TransformerFactoryConfigurationError e
) {
867 Activator
.logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$
868 } catch (TransformerException e
) {
869 Activator
.logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$