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 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
.ui
.Activator
;
39 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Messages
;
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é
55 public class CustomTxtTraceDefinition
extends CustomTraceDefinition
{
58 public List
<InputLine
> inputs
;
60 /** File name of the definition file */
61 protected static final String CUSTOM_TXT_TRACE_DEFINITIONS_FILE_NAME
= "custom_txt_parsers.xml"; //$NON-NLS-1$
63 /** Path of the definition file */
64 protected static final String CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
=
65 Activator
.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_TXT_TRACE_DEFINITIONS_FILE_NAME
).toString();
67 private static final String CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
= Messages
.CustomTxtTraceDefinition_definitionRootElement
;
68 private static final String DEFINITION_ELEMENT
= Messages
.CustomTxtTraceDefinition_definition
;
69 private static final String NAME_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_name
;
70 private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT
= Messages
.CustomTxtTraceDefinition_timestampOutputFormat
;
71 private static final String INPUT_LINE_ELEMENT
= Messages
.CustomTxtTraceDefinition_inputLine
;
72 private static final String CARDINALITY_ELEMENT
= Messages
.CustomTxtTraceDefinition_cardinality
;
73 private static final String MIN_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_min
;
74 private static final String MAX_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_max
;
75 private static final String REGEX_ELEMENT
= Messages
.CustomTxtTraceDefinition_regEx
;
76 private static final String INPUT_DATA_ELEMENT
= Messages
.CustomTxtTraceDefinition_inputData
;
77 private static final String ACTION_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_action
;
78 private static final String FORMAT_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_format
;
79 private static final String OUTPUT_COLUMN_ELEMENT
= Messages
.CustomTxtTraceDefinition_outputColumn
;
82 * Default constructor.
84 public CustomTxtTraceDefinition() {
85 this("", new ArrayList
<InputLine
>(0), new ArrayList
<OutputColumn
>(0), ""); //$NON-NLS-1$ //$NON-NLS-2$
92 * Name of the trace type
96 * List of output columns
97 * @param timeStampOutputFormat
98 * The timestamp format to use
100 public CustomTxtTraceDefinition(String logtype
, List
<InputLine
> inputs
,
101 List
<OutputColumn
> outputs
, String timeStampOutputFormat
) {
102 this.definitionName
= logtype
;
103 this.inputs
= inputs
;
104 this.outputs
= outputs
;
105 this.timeStampOutputFormat
= timeStampOutputFormat
;
109 * Wrapper to store a line of the log file
111 public static class InputLine
{
113 /** Data columns of this line */
114 public List
<InputData
> columns
;
116 /** Cardinality of this line (see {@link Cardinality}) */
117 public Cardinality cardinality
;
120 public InputLine parentInput
;
122 /** Level of this line */
125 /** Next input line in the file */
126 public InputLine nextInput
;
128 /** Children of this line (if one "event" spans many lines) */
129 public List
<InputLine
> childrenInputs
;
131 private String regex
;
132 private Pattern pattern
;
135 * Default (empty) constructor.
137 public InputLine() {}
142 * @param cardinality Cardinality of this line.
144 * @param columns Columns to use
146 public InputLine(Cardinality cardinality
, String regex
, List
<InputData
> columns
) {
147 this.cardinality
= cardinality
;
149 this.columns
= columns
;
153 * Set the regex of this input line
158 public void setRegex(String regex
) {
164 * Get the current regex
166 * @return The current regex
168 public String
getRegex() {
173 * Get the Pattern object of this line's regex
175 * @return The Pattern
176 * @throws PatternSyntaxException
177 * If the regex does not parse correctly
179 public Pattern
getPattern() throws PatternSyntaxException
{
180 if (pattern
== null) {
181 pattern
= Pattern
.compile(regex
);
187 * Add a child line to this line.
190 * The child input line
192 public void addChild(InputLine input
) {
193 if (childrenInputs
== null) {
194 childrenInputs
= new ArrayList
<InputLine
>(1);
195 } else if (childrenInputs
.size() > 0) {
196 InputLine last
= childrenInputs
.get(childrenInputs
.size() - 1);
197 last
.nextInput
= input
;
199 childrenInputs
.add(input
);
200 input
.parentInput
= this;
201 input
.level
= this.level
+ 1;
205 * Set the next input line.
208 * The next input line
210 public void addNext(InputLine input
) {
211 if (parentInput
!= null) {
212 int index
= parentInput
.childrenInputs
.indexOf(this);
213 parentInput
.childrenInputs
.add(index
+ 1, input
);
214 InputLine next
= nextInput
;
216 input
.nextInput
= next
;
218 input
.parentInput
= this.parentInput
;
219 input
.level
= this.level
;
223 * Move this line up in its parent's children.
225 public void moveUp() {
226 if (parentInput
!= null) {
227 int index
= parentInput
.childrenInputs
.indexOf(this);
229 parentInput
.childrenInputs
.add(index
- 1 , parentInput
.childrenInputs
.remove(index
));
230 parentInput
.childrenInputs
.get(index
).nextInput
= nextInput
;
231 nextInput
= parentInput
.childrenInputs
.get(index
);
237 * Move this line down in its parent's children.
239 public void moveDown() {
240 if (parentInput
!= null) {
241 int index
= parentInput
.childrenInputs
.indexOf(this);
242 if (index
< parentInput
.childrenInputs
.size() - 1) {
243 parentInput
.childrenInputs
.add(index
+ 1 , parentInput
.childrenInputs
.remove(index
));
244 nextInput
= parentInput
.childrenInputs
.get(index
).nextInput
;
245 parentInput
.childrenInputs
.get(index
).nextInput
= this;
251 * Add a data column to this line
256 public void addColumn(InputData column
) {
257 if (columns
== null) {
258 columns
= new ArrayList
<InputData
>(1);
264 * Get the next input lines.
267 * The map of line "sets".
268 * @return The next list of lines.
270 public List
<InputLine
> getNextInputs(Map
<InputLine
, Integer
> countMap
) {
271 List
<InputLine
> nextInputs
= new ArrayList
<InputLine
>();
272 InputLine next
= nextInput
;
273 while (next
!= null) {
274 nextInputs
.add(next
);
275 if (next
.cardinality
.min
> 0) {
278 next
= next
.nextInput
;
280 if (parentInput
!= null && parentInput
.level
> 0) {
281 int parentCount
= countMap
.get(parentInput
);
282 if (parentCount
< parentInput
.getMaxCount()) {
283 nextInputs
.add(parentInput
);
285 if (parentCount
< parentInput
.getMinCount()) {
288 nextInputs
.addAll(parentInput
.getNextInputs(countMap
));
294 * Get the minimum possible amount of entries.
296 * @return The minimum
298 public int getMinCount() {
299 return cardinality
.min
;
303 * Get the maximum possible amount of entries.
305 * @return The maximum
307 public int getMaxCount() {
308 return cardinality
.max
;
312 public String
toString() {
313 return regex
+ " " + cardinality
; //$NON-NLS-1$
318 * Data column for input lines.
320 public static class InputData
{
322 /** Name of this column */
329 public String format
;
332 * Default (empty) constructor
334 public InputData() {}
340 * @param action Action
341 * @param format Format
343 public InputData(String name
, int action
, String format
) {
345 this.action
= action
;
346 this.format
= format
;
350 * Constructor with default format
353 * @param action Action
355 public InputData(String name
, int action
) {
357 this.action
= action
;
362 * Input line cardinality
364 public static class Cardinality
{
366 /** Representation of infinity */
367 public final static int INF
= Integer
.MAX_VALUE
;
369 /** Preset for [1, 1] */
370 public final static Cardinality ONE
= new Cardinality(1, 1);
372 /** Preset for [1, inf] */
373 public final static Cardinality ONE_OR_MORE
= new Cardinality(1, INF
);
375 /** Preset for [0, 1] */
376 public final static Cardinality ZERO_OR_ONE
= new Cardinality(0, 1);
378 /** Preset for [0, inf] */
379 public final static Cardinality ZERO_OR_MORE
= new Cardinality(0, INF
);
381 private final int min
;
382 private final int max
;
392 public Cardinality(int min
, int max
) {
398 public String
toString() {
399 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$
403 public int hashCode() {
404 final int prime
= 31;
406 result
= prime
* result
+ max
;
407 result
= prime
* result
+ min
;
412 public boolean equals(Object obj
) {
419 if (!(obj
instanceof Cardinality
)) {
422 Cardinality other
= (Cardinality
) obj
;
423 return (this.min
== other
.min
&& this.max
== other
.max
);
429 save(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
433 public void save(String path
) {
435 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
436 DocumentBuilder db
= dbf
.newDocumentBuilder();
438 // The following allows xml parsing without access to the dtd
439 EntityResolver resolver
= new EntityResolver() {
441 public InputSource
resolveEntity(String publicId
, String systemId
) {
442 String empty
= ""; //$NON-NLS-1$
443 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
444 return new InputSource(bais
);
447 db
.setEntityResolver(resolver
);
449 // The following catches xml parsing exceptions
450 db
.setErrorHandler(new ErrorHandler() {
452 public void error(SAXParseException saxparseexception
) throws SAXException
{}
455 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
458 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
459 throw saxparseexception
;
464 File file
= new File(path
);
465 if (file
.canRead()) {
466 doc
= db
.parse(file
);
467 if (! doc
.getDocumentElement().getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
471 doc
= db
.newDocument();
472 Node node
= doc
.createElement(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
);
473 doc
.appendChild(node
);
476 Element root
= doc
.getDocumentElement();
478 NodeList nodeList
= root
.getChildNodes();
479 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
480 Node node
= nodeList
.item(i
);
481 if (node
instanceof Element
&&
482 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
483 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
484 root
.removeChild(node
);
487 Element definitionElement
= doc
.createElement(DEFINITION_ELEMENT
);
488 root
.appendChild(definitionElement
);
489 definitionElement
.setAttribute(NAME_ATTRIBUTE
, definitionName
);
491 Element formatElement
= doc
.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
);
492 definitionElement
.appendChild(formatElement
);
493 formatElement
.appendChild(doc
.createTextNode(timeStampOutputFormat
));
495 if (inputs
!= null) {
496 for (InputLine inputLine
: inputs
) {
497 definitionElement
.appendChild(createInputLineElement(inputLine
, doc
));
501 if (outputs
!= null) {
502 for (OutputColumn output
: outputs
) {
503 Element outputColumnElement
= doc
.createElement(OUTPUT_COLUMN_ELEMENT
);
504 definitionElement
.appendChild(outputColumnElement
);
505 outputColumnElement
.setAttribute(NAME_ATTRIBUTE
, output
.name
);
509 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
510 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
512 //initialize StreamResult with File object to save to file
513 StreamResult result
= new StreamResult(new StringWriter());
514 DOMSource source
= new DOMSource(doc
);
515 transformer
.transform(source
, result
);
516 String xmlString
= result
.getWriter().toString();
518 FileWriter writer
= new FileWriter(file
);
519 writer
.write(xmlString
);
521 } catch (ParserConfigurationException e
) {
522 Activator
.getDefault().logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
523 } catch (TransformerConfigurationException e
) {
524 Activator
.getDefault().logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
525 } catch (TransformerFactoryConfigurationError e
) {
526 Activator
.getDefault().logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
527 } catch (TransformerException e
) {
528 Activator
.getDefault().logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
529 } catch (IOException e
) {
530 Activator
.getDefault().logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
531 } catch (SAXException e
) {
532 Activator
.getDefault().logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
536 private Element
createInputLineElement(InputLine inputLine
, Document doc
) {
537 Element inputLineElement
= doc
.createElement(INPUT_LINE_ELEMENT
);
539 Element cardinalityElement
= doc
.createElement(CARDINALITY_ELEMENT
);
540 inputLineElement
.appendChild(cardinalityElement
);
541 cardinalityElement
.setAttribute(MIN_ATTRIBUTE
, Integer
.toString(inputLine
.cardinality
.min
));
542 cardinalityElement
.setAttribute(MAX_ATTRIBUTE
, Integer
.toString(inputLine
.cardinality
.max
));
544 Element regexElement
= doc
.createElement(REGEX_ELEMENT
);
545 inputLineElement
.appendChild(regexElement
);
546 regexElement
.appendChild(doc
.createTextNode(inputLine
.regex
));
548 if (inputLine
.columns
!= null) {
549 for (InputData inputData
: inputLine
.columns
) {
550 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
551 inputLineElement
.appendChild(inputDataElement
);
552 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, inputData
.name
);
553 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(inputData
.action
));
554 if (inputData
.format
!= null) {
555 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, inputData
.format
);
560 if (inputLine
.childrenInputs
!= null) {
561 for (InputLine childInputLine
: inputLine
.childrenInputs
) {
562 inputLineElement
.appendChild(createInputLineElement(childInputLine
, doc
));
566 return inputLineElement
;
570 * Load the default text trace definitions file.
572 * @return The loaded trace definitions
574 public static CustomTxtTraceDefinition
[] loadAll() {
575 return loadAll(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
579 * Load a specific text trace definition file.
582 * The path to the file to load
583 * @return The loaded trace definitions
585 public static CustomTxtTraceDefinition
[] loadAll(String path
) {
587 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
588 DocumentBuilder db
= dbf
.newDocumentBuilder();
590 // The following allows xml parsing without access to the dtd
591 EntityResolver resolver
= new EntityResolver() {
593 public InputSource
resolveEntity(String publicId
, String systemId
) {
594 String empty
= ""; //$NON-NLS-1$
595 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
596 return new InputSource(bais
);
599 db
.setEntityResolver(resolver
);
601 // The following catches xml parsing exceptions
602 db
.setErrorHandler(new ErrorHandler() {
604 public void error(SAXParseException saxparseexception
) throws SAXException
{}
607 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
610 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
611 throw saxparseexception
;
615 File file
= new File(path
);
616 if (!file
.canRead()) {
617 return new CustomTxtTraceDefinition
[0];
619 Document doc
= db
.parse(file
);
621 Element root
= doc
.getDocumentElement();
622 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
623 return new CustomTxtTraceDefinition
[0];
626 ArrayList
<CustomTxtTraceDefinition
> defList
= new ArrayList
<CustomTxtTraceDefinition
>();
627 NodeList nodeList
= root
.getChildNodes();
628 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
629 Node node
= nodeList
.item(i
);
630 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
631 CustomTxtTraceDefinition def
= extractDefinition((Element
) node
);
637 return defList
.toArray(new CustomTxtTraceDefinition
[0]);
638 } catch (ParserConfigurationException e
) {
639 Activator
.getDefault().logError("Error loading all in CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
640 } catch (SAXException e
) {
641 Activator
.getDefault().logError("Error loading all in CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
642 } catch (IOException e
) {
643 Activator
.getDefault().logError("Error loading all in CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
645 return new CustomTxtTraceDefinition
[0];
649 * Load a single definition.
651 * @param definitionName
652 * Name of the definition to load
653 * @return The loaded trace definition
655 public static CustomTxtTraceDefinition
load(String definitionName
) {
657 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
658 DocumentBuilder db
= dbf
.newDocumentBuilder();
660 // The following allows xml parsing without access to the dtd
661 EntityResolver resolver
= new EntityResolver() {
663 public InputSource
resolveEntity(String publicId
, String systemId
) {
664 String empty
= ""; //$NON-NLS-1$
665 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
666 return new InputSource(bais
);
669 db
.setEntityResolver(resolver
);
671 // The following catches xml parsing exceptions
672 db
.setErrorHandler(new ErrorHandler() {
674 public void error(SAXParseException saxparseexception
) throws SAXException
{}
677 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
680 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
681 throw saxparseexception
;
685 File file
= new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
686 Document doc
= db
.parse(file
);
688 Element root
= doc
.getDocumentElement();
689 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
693 NodeList nodeList
= root
.getChildNodes();
694 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
695 Node node
= nodeList
.item(i
);
696 if (node
instanceof Element
&&
697 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
698 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
699 return extractDefinition((Element
) node
);
702 } catch (ParserConfigurationException e
) {
703 Activator
.getDefault().logError("Error loading CustomTxtTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
704 } catch (SAXException e
) {
705 Activator
.getDefault().logError("Error loading CustomTxtTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
706 } catch (IOException e
) {
707 Activator
.getDefault().logError("Error loading CustomTxtTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
713 * Get the definition from a definition element.
715 * @param definitionElement
716 * The Element to extract from
717 * @return The loaded trace definition
719 public static CustomTxtTraceDefinition
extractDefinition(Element definitionElement
) {
720 CustomTxtTraceDefinition def
= new CustomTxtTraceDefinition();
722 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
723 if (def
.definitionName
== null) {
727 NodeList nodeList
= definitionElement
.getChildNodes();
728 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
729 Node node
= nodeList
.item(i
);
730 String nodeName
= node
.getNodeName();
731 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
732 Element formatElement
= (Element
) node
;
733 def
.timeStampOutputFormat
= formatElement
.getTextContent();
734 } else if (nodeName
.equals(INPUT_LINE_ELEMENT
)) {
735 InputLine inputLine
= extractInputLine((Element
) node
);
736 if (inputLine
!= null) {
737 def
.inputs
.add(inputLine
);
739 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
740 Element outputColumnElement
= (Element
) node
;
741 OutputColumn outputColumn
= new OutputColumn();
742 outputColumn
.name
= outputColumnElement
.getAttribute(NAME_ATTRIBUTE
);
743 def
.outputs
.add(outputColumn
);
749 private static InputLine
extractInputLine(Element inputLineElement
) {
750 InputLine inputLine
= new InputLine();
751 NodeList nodeList
= inputLineElement
.getChildNodes();
752 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
753 Node node
= nodeList
.item(i
);
754 String nodeName
= node
.getNodeName();
755 if (nodeName
.equals(CARDINALITY_ELEMENT
)) {
756 Element cardinalityElement
= (Element
) node
;
758 int min
= Integer
.parseInt(cardinalityElement
.getAttribute(MIN_ATTRIBUTE
));
759 int max
= Integer
.parseInt(cardinalityElement
.getAttribute(MAX_ATTRIBUTE
));
760 inputLine
.cardinality
= new Cardinality(min
, max
);
761 } catch (NumberFormatException e
) {
764 } else if (nodeName
.equals(REGEX_ELEMENT
)) {
765 Element regexElement
= (Element
) node
;
766 inputLine
.regex
= regexElement
.getTextContent();
767 } else if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
768 Element inputDataElement
= (Element
) node
;
769 InputData inputData
= new InputData();
770 inputData
.name
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
771 inputData
.action
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
772 inputData
.format
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
773 inputLine
.addColumn(inputData
);
774 } else if (nodeName
.equals(INPUT_LINE_ELEMENT
)) {
775 Element childInputLineElement
= (Element
) node
;
776 InputLine childInputLine
= extractInputLine(childInputLineElement
);
777 if (childInputLine
!= null) {
778 inputLine
.addChild(childInputLine
);
786 * Delete a definition from the currently loaded ones.
788 * @param definitionName
789 * The name of the definition to delete
791 public static void delete(String definitionName
) {
793 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
794 DocumentBuilder db
= dbf
.newDocumentBuilder();
796 // The following allows xml parsing without access to the dtd
797 EntityResolver resolver
= new EntityResolver() {
799 public InputSource
resolveEntity(String publicId
, String systemId
) {
800 String empty
= ""; //$NON-NLS-1$
801 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
802 return new InputSource(bais
);
805 db
.setEntityResolver(resolver
);
807 // The following catches xml parsing exceptions
808 db
.setErrorHandler(new ErrorHandler() {
810 public void error(SAXParseException saxparseexception
) throws SAXException
{}
813 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
816 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
817 throw saxparseexception
;
821 File file
= new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
822 Document doc
= db
.parse(file
);
824 Element root
= doc
.getDocumentElement();
825 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
829 NodeList nodeList
= root
.getChildNodes();
830 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
831 Node node
= nodeList
.item(i
);
832 if (node
instanceof Element
&&
833 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
834 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
835 root
.removeChild(node
);
839 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
840 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
842 //initialize StreamResult with File object to save to file
843 StreamResult result
= new StreamResult(new StringWriter());
844 DOMSource source
= new DOMSource(doc
);
845 transformer
.transform(source
, result
);
846 String xmlString
= result
.getWriter().toString();
848 FileWriter writer
= new FileWriter(file
);
849 writer
.write(xmlString
);
851 } catch (ParserConfigurationException e
) {
852 Activator
.getDefault().logError("Error deleting CustomTxtTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
853 } catch (SAXException e
) {
854 Activator
.getDefault().logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$
855 } catch (IOException e
) {
856 Activator
.getDefault().logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$
857 } catch (TransformerConfigurationException e
) {
858 Activator
.getDefault().logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$
859 } catch (TransformerFactoryConfigurationError e
) {
860 Activator
.getDefault().logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$
861 } catch (TransformerException e
) {
862 Activator
.getDefault().logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$