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
.Messages
;
39 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
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
;
50 public class CustomTxtTraceDefinition
extends CustomTraceDefinition
{
52 protected static final String CUSTOM_TXT_TRACE_DEFINITIONS_FILE_NAME
= "custom_txt_parsers.xml"; //$NON-NLS-1$
53 protected static final String CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
=
54 Activator
.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_TXT_TRACE_DEFINITIONS_FILE_NAME
).toString();
56 private static final String CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
= Messages
.CustomTxtTraceDefinition_definitionRootElement
;
57 private static final String DEFINITION_ELEMENT
= Messages
.CustomTxtTraceDefinition_definition
;
58 private static final String NAME_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_name
;
59 private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT
= Messages
.CustomTxtTraceDefinition_timestampOutputFormat
;
60 private static final String INPUT_LINE_ELEMENT
= Messages
.CustomTxtTraceDefinition_inputLine
;
61 private static final String CARDINALITY_ELEMENT
= Messages
.CustomTxtTraceDefinition_cardinality
;
62 private static final String MIN_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_min
;
63 private static final String MAX_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_max
;
64 private static final String REGEX_ELEMENT
= Messages
.CustomTxtTraceDefinition_regEx
;
65 private static final String INPUT_DATA_ELEMENT
= Messages
.CustomTxtTraceDefinition_inputData
;
66 private static final String ACTION_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_action
;
67 private static final String FORMAT_ATTRIBUTE
= Messages
.CustomTxtTraceDefinition_format
;
68 private static final String OUTPUT_COLUMN_ELEMENT
= Messages
.CustomTxtTraceDefinition_outputColumn
;
70 public List
<InputLine
> inputs
;
72 public CustomTxtTraceDefinition() {
73 this("", new ArrayList
<InputLine
>(0), new ArrayList
<OutputColumn
>(0), ""); //$NON-NLS-1$ //$NON-NLS-2$
76 public CustomTxtTraceDefinition(String logtype
, List
<InputLine
> inputs
, List
<OutputColumn
> outputs
, String timeStampOutputFormat
) {
77 this.definitionName
= logtype
;
79 this.outputs
= outputs
;
80 this.timeStampOutputFormat
= timeStampOutputFormat
;
83 public static class InputLine
{
84 public List
<InputData
> columns
;
85 public Cardinality cardinality
;
87 private Pattern pattern
;
88 public InputLine parentInput
;
90 public InputLine nextInput
;
91 public List
<InputLine
> childrenInputs
;
93 public InputLine() {};
95 public InputLine(Cardinality cardinality
, String regex
, List
<InputData
> columns
) {
96 this.cardinality
= cardinality
;
98 this.columns
= columns
;
101 public void setRegex(String regex
) {
106 public String
getRegex() {
110 public Pattern
getPattern() throws PatternSyntaxException
{
111 if (pattern
== null) {
112 pattern
= Pattern
.compile(regex
);
117 public void addChild(InputLine input
) {
118 if (childrenInputs
== null) {
119 childrenInputs
= new ArrayList
<InputLine
>(1);
120 } else if (childrenInputs
.size() > 0) {
121 InputLine last
= childrenInputs
.get(childrenInputs
.size() - 1);
122 last
.nextInput
= input
;
124 childrenInputs
.add(input
);
125 input
.parentInput
= this;
126 input
.level
= this.level
+ 1;
129 public void addNext(InputLine input
) {
130 if (parentInput
!= null) {
131 int index
= parentInput
.childrenInputs
.indexOf(this);
132 parentInput
.childrenInputs
.add(index
+ 1, input
);
133 InputLine next
= nextInput
;
135 input
.nextInput
= next
;
137 input
.parentInput
= this.parentInput
;
138 input
.level
= this.level
;
141 public void moveUp() {
142 if (parentInput
!= null) {
143 int index
= parentInput
.childrenInputs
.indexOf(this);
145 parentInput
.childrenInputs
.add(index
- 1 , parentInput
.childrenInputs
.remove(index
));
146 parentInput
.childrenInputs
.get(index
).nextInput
= nextInput
;
147 nextInput
= parentInput
.childrenInputs
.get(index
);
152 public void moveDown() {
153 if (parentInput
!= null) {
154 int index
= parentInput
.childrenInputs
.indexOf(this);
155 if (index
< parentInput
.childrenInputs
.size() - 1) {
156 parentInput
.childrenInputs
.add(index
+ 1 , parentInput
.childrenInputs
.remove(index
));
157 nextInput
= parentInput
.childrenInputs
.get(index
).nextInput
;
158 parentInput
.childrenInputs
.get(index
).nextInput
= this;
163 public void addColumn(InputData column
) {
164 if (columns
== null) {
165 columns
= new ArrayList
<InputData
>(1);
170 public List
<InputLine
> getNextInputs(Map
<InputLine
, Integer
> countMap
) {
171 List
<InputLine
> nextInputs
= new ArrayList
<InputLine
>();
172 InputLine next
= nextInput
;
173 while (next
!= null) {
174 nextInputs
.add(next
);
175 if (next
.cardinality
.min
> 0) {
178 next
= next
.nextInput
;
180 if (parentInput
!= null && parentInput
.level
> 0) {
181 int parentCount
= countMap
.get(parentInput
);
182 if (parentCount
< parentInput
.getMaxCount()) {
183 nextInputs
.add(parentInput
);
185 if (parentCount
< parentInput
.getMinCount()) {
188 nextInputs
.addAll(parentInput
.getNextInputs(countMap
));
193 public int getMinCount() {
194 return cardinality
.min
;
197 public int getMaxCount() {
198 return cardinality
.max
;
202 public String
toString() {
203 return regex
+ " " + cardinality
; //$NON-NLS-1$
208 public static class InputData
{
211 public String format
;
213 public InputData() {};
215 public InputData(String name
, int action
, String format
) {
217 this.action
= action
;
218 this.format
= format
;
221 public InputData(String name
, int action
) {
223 this.action
= action
;
227 public static class Cardinality
{
228 public final static int INF
= Integer
.MAX_VALUE
;
229 public final static Cardinality ONE
= new Cardinality(1, 1);
230 public final static Cardinality ONE_OR_MORE
= new Cardinality(1, INF
);
231 public final static Cardinality ZERO_OR_ONE
= new Cardinality(0, 1);
232 public final static Cardinality ZERO_OR_MORE
= new Cardinality(0, INF
);
237 public Cardinality(int min
, int max
) {
243 public String
toString() {
244 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$
248 * @see java.lang.Object#hashCode()
251 public int hashCode() {
252 final int prime
= 31;
254 result
= prime
* result
+ max
;
255 result
= prime
* result
+ min
;
260 * @see java.lang.Object#equals(java.lang.Object)
263 public boolean equals(Object obj
) {
270 if (!(obj
instanceof Cardinality
)) return false;
271 Cardinality other
= (Cardinality
) obj
;
272 return (this.min
== other
.min
&& this.max
== other
.max
);
278 save(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
282 public void save(String path
) {
284 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
285 DocumentBuilder db
= dbf
.newDocumentBuilder();
287 // The following allows xml parsing without access to the dtd
288 EntityResolver resolver
= new EntityResolver () {
290 public InputSource
resolveEntity (String publicId
, String systemId
) {
291 String empty
= ""; //$NON-NLS-1$
292 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
293 return new InputSource(bais
);
296 db
.setEntityResolver(resolver
);
298 // The following catches xml parsing exceptions
299 db
.setErrorHandler(new ErrorHandler(){
301 public void error(SAXParseException saxparseexception
) throws SAXException
{}
303 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
305 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
306 throw saxparseexception
;
310 File file
= new File(path
);
311 if (file
.canRead()) {
312 doc
= db
.parse(file
);
313 if (! doc
.getDocumentElement().getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
317 doc
= db
.newDocument();
318 Node node
= doc
.createElement(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
);
319 doc
.appendChild(node
);
322 Element root
= doc
.getDocumentElement();
324 NodeList nodeList
= root
.getChildNodes();
325 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
326 Node node
= nodeList
.item(i
);
327 if (node
instanceof Element
&&
328 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
329 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
330 root
.removeChild(node
);
333 Element definitionElement
= doc
.createElement(DEFINITION_ELEMENT
);
334 root
.appendChild(definitionElement
);
335 definitionElement
.setAttribute(NAME_ATTRIBUTE
, definitionName
);
337 Element formatElement
= doc
.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
);
338 definitionElement
.appendChild(formatElement
);
339 formatElement
.appendChild(doc
.createTextNode(timeStampOutputFormat
));
341 if (inputs
!= null) {
342 for (InputLine inputLine
: inputs
) {
343 definitionElement
.appendChild(createInputLineElement(inputLine
, doc
));
347 if (outputs
!= null) {
348 for (OutputColumn output
: outputs
) {
349 Element outputColumnElement
= doc
.createElement(OUTPUT_COLUMN_ELEMENT
);
350 definitionElement
.appendChild(outputColumnElement
);
351 outputColumnElement
.setAttribute(NAME_ATTRIBUTE
, output
.name
);
355 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
356 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
358 //initialize StreamResult with File object to save to file
359 StreamResult result
= new StreamResult(new StringWriter());
360 DOMSource source
= new DOMSource(doc
);
361 transformer
.transform(source
, result
);
362 String xmlString
= result
.getWriter().toString();
364 FileWriter writer
= new FileWriter(file
);
365 writer
.write(xmlString
);
367 } catch (ParserConfigurationException e
) {
368 Activator
.getDefault().logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
369 } catch (TransformerConfigurationException e
) {
370 Activator
.getDefault().logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
371 } catch (TransformerFactoryConfigurationError e
) {
372 Activator
.getDefault().logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
373 } catch (TransformerException e
) {
374 Activator
.getDefault().logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
375 } catch (IOException e
) {
376 Activator
.getDefault().logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
377 } catch (SAXException e
) {
378 Activator
.getDefault().logError("Error saving CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
382 private Element
createInputLineElement(InputLine inputLine
, Document doc
) {
383 Element inputLineElement
= doc
.createElement(INPUT_LINE_ELEMENT
);
385 Element cardinalityElement
= doc
.createElement(CARDINALITY_ELEMENT
);
386 inputLineElement
.appendChild(cardinalityElement
);
387 cardinalityElement
.setAttribute(MIN_ATTRIBUTE
, Integer
.toString(inputLine
.cardinality
.min
));
388 cardinalityElement
.setAttribute(MAX_ATTRIBUTE
, Integer
.toString(inputLine
.cardinality
.max
));
390 Element regexElement
= doc
.createElement(REGEX_ELEMENT
);
391 inputLineElement
.appendChild(regexElement
);
392 regexElement
.appendChild(doc
.createTextNode(inputLine
.regex
));
394 if (inputLine
.columns
!= null) {
395 for (InputData inputData
: inputLine
.columns
) {
396 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
397 inputLineElement
.appendChild(inputDataElement
);
398 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, inputData
.name
);
399 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(inputData
.action
));
400 if (inputData
.format
!= null) {
401 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, inputData
.format
);
406 if (inputLine
.childrenInputs
!= null) {
407 for (InputLine childInputLine
: inputLine
.childrenInputs
) {
408 inputLineElement
.appendChild(createInputLineElement(childInputLine
, doc
));
412 return inputLineElement
;
415 public static CustomTxtTraceDefinition
[] loadAll() {
416 return loadAll(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
419 public static CustomTxtTraceDefinition
[] loadAll(String path
) {
421 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
422 DocumentBuilder db
= dbf
.newDocumentBuilder();
424 // The following allows xml parsing without access to the dtd
425 EntityResolver resolver
= new EntityResolver () {
427 public InputSource
resolveEntity (String publicId
, String systemId
) {
428 String empty
= ""; //$NON-NLS-1$
429 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
430 return new InputSource(bais
);
433 db
.setEntityResolver(resolver
);
435 // The following catches xml parsing exceptions
436 db
.setErrorHandler(new ErrorHandler(){
438 public void error(SAXParseException saxparseexception
) throws SAXException
{}
440 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
442 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
443 throw saxparseexception
;
446 File file
= new File(path
);
447 if (!file
.canRead()) {
448 return new CustomTxtTraceDefinition
[0];
450 Document doc
= db
.parse(file
);
452 Element root
= doc
.getDocumentElement();
453 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
454 return new CustomTxtTraceDefinition
[0];
457 ArrayList
<CustomTxtTraceDefinition
> defList
= new ArrayList
<CustomTxtTraceDefinition
>();
458 NodeList nodeList
= root
.getChildNodes();
459 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
460 Node node
= nodeList
.item(i
);
461 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
462 CustomTxtTraceDefinition def
= extractDefinition((Element
) node
);
468 return defList
.toArray(new CustomTxtTraceDefinition
[0]);
469 } catch (ParserConfigurationException e
) {
470 Activator
.getDefault().logError("Error loading all in CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
471 } catch (SAXException e
) {
472 Activator
.getDefault().logError("Error loading all in CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
473 } catch (IOException e
) {
474 Activator
.getDefault().logError("Error loading all in CustomTxtTraceDefinition: path=" + path
, e
); //$NON-NLS-1$
476 return new CustomTxtTraceDefinition
[0];
479 public static CustomTxtTraceDefinition
load(String definitionName
) {
481 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
482 DocumentBuilder db
= dbf
.newDocumentBuilder();
484 // The following allows xml parsing without access to the dtd
485 EntityResolver resolver
= new EntityResolver () {
487 public InputSource
resolveEntity (String publicId
, String systemId
) {
488 String empty
= ""; //$NON-NLS-1$
489 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
490 return new InputSource(bais
);
493 db
.setEntityResolver(resolver
);
495 // The following catches xml parsing exceptions
496 db
.setErrorHandler(new ErrorHandler(){
498 public void error(SAXParseException saxparseexception
) throws SAXException
{}
500 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
502 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
503 throw saxparseexception
;
506 File file
= new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
507 Document doc
= db
.parse(file
);
509 Element root
= doc
.getDocumentElement();
510 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
514 NodeList nodeList
= root
.getChildNodes();
515 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
516 Node node
= nodeList
.item(i
);
517 if (node
instanceof Element
&&
518 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
519 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
520 return extractDefinition((Element
) node
);
523 } catch (ParserConfigurationException e
) {
524 Activator
.getDefault().logError("Error loading CustomTxtTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
525 } catch (SAXException e
) {
526 Activator
.getDefault().logError("Error loading CustomTxtTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
527 } catch (IOException e
) {
528 Activator
.getDefault().logError("Error loading CustomTxtTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
533 public static CustomTxtTraceDefinition
extractDefinition(Element definitionElement
) {
534 CustomTxtTraceDefinition def
= new CustomTxtTraceDefinition();
536 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
537 if (def
.definitionName
== null) return null;
539 NodeList nodeList
= definitionElement
.getChildNodes();
540 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
541 Node node
= nodeList
.item(i
);
542 String nodeName
= node
.getNodeName();
543 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
544 Element formatElement
= (Element
) node
;
545 def
.timeStampOutputFormat
= formatElement
.getTextContent();
546 } else if (nodeName
.equals(INPUT_LINE_ELEMENT
)) {
547 InputLine inputLine
= extractInputLine((Element
) node
);
548 if (inputLine
!= null) {
549 def
.inputs
.add(inputLine
);
551 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
552 Element outputColumnElement
= (Element
) node
;
553 OutputColumn outputColumn
= new OutputColumn();
554 outputColumn
.name
= outputColumnElement
.getAttribute(NAME_ATTRIBUTE
);
555 def
.outputs
.add(outputColumn
);
561 private static InputLine
extractInputLine(Element inputLineElement
) {
562 InputLine inputLine
= new InputLine();
563 NodeList nodeList
= inputLineElement
.getChildNodes();
564 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
565 Node node
= nodeList
.item(i
);
566 String nodeName
= node
.getNodeName();
567 if (nodeName
.equals(CARDINALITY_ELEMENT
)) {
568 Element cardinalityElement
= (Element
) node
;
570 int min
= Integer
.parseInt(cardinalityElement
.getAttribute(MIN_ATTRIBUTE
));
571 int max
= Integer
.parseInt(cardinalityElement
.getAttribute(MAX_ATTRIBUTE
));
572 inputLine
.cardinality
= new Cardinality(min
, max
);
573 } catch (NumberFormatException e
) {
576 } else if (nodeName
.equals(REGEX_ELEMENT
)) {
577 Element regexElement
= (Element
) node
;
578 inputLine
.regex
= regexElement
.getTextContent();
579 } else if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
580 Element inputDataElement
= (Element
) node
;
581 InputData inputData
= new InputData();
582 inputData
.name
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
583 inputData
.action
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
584 inputData
.format
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
585 inputLine
.addColumn(inputData
);
586 } else if (nodeName
.equals(INPUT_LINE_ELEMENT
)) {
587 Element childInputLineElement
= (Element
) node
;
588 InputLine childInputLine
= extractInputLine(childInputLineElement
);
589 if (childInputLine
!= null) {
590 inputLine
.addChild(childInputLine
);
597 public static void delete(String definitionName
) {
599 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
600 DocumentBuilder db
= dbf
.newDocumentBuilder();
602 // The following allows xml parsing without access to the dtd
603 EntityResolver resolver
= new EntityResolver () {
605 public InputSource
resolveEntity (String publicId
, String systemId
) {
606 String empty
= ""; //$NON-NLS-1$
607 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
608 return new InputSource(bais
);
611 db
.setEntityResolver(resolver
);
613 // The following catches xml parsing exceptions
614 db
.setErrorHandler(new ErrorHandler(){
616 public void error(SAXParseException saxparseexception
) throws SAXException
{}
618 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
620 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
621 throw saxparseexception
;
624 File file
= new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
625 Document doc
= db
.parse(file
);
627 Element root
= doc
.getDocumentElement();
628 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
632 NodeList nodeList
= root
.getChildNodes();
633 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
634 Node node
= nodeList
.item(i
);
635 if (node
instanceof Element
&&
636 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
637 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
638 root
.removeChild(node
);
642 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
643 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes"); //$NON-NLS-1$
645 //initialize StreamResult with File object to save to file
646 StreamResult result
= new StreamResult(new StringWriter());
647 DOMSource source
= new DOMSource(doc
);
648 transformer
.transform(source
, result
);
649 String xmlString
= result
.getWriter().toString();
651 FileWriter writer
= new FileWriter(file
);
652 writer
.write(xmlString
);
654 } catch (ParserConfigurationException e
) {
655 Activator
.getDefault().logError("Error deleting CustomTxtTraceDefinition: definitionName=" + definitionName
, e
); //$NON-NLS-1$
656 } catch (SAXException e
) {
657 Activator
.getDefault().logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$
658 } catch (IOException e
) {
659 Activator
.getDefault().logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$
660 } catch (TransformerConfigurationException e
) {
661 Activator
.getDefault().logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$
662 } catch (TransformerFactoryConfigurationError e
) {
663 Activator
.getDefault().logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$
664 } catch (TransformerException e
) {
665 Activator
.getDefault().logError("Error deleting CustomTxtTraceDefinition: definitionName= " + definitionName
, e
); //$NON-NLS-1$