Internalize some more TMF APIs
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / parsers / custom / CustomTxtTraceDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.ui.parsers.custom;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.File;
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;
22 import java.util.Map;
23 import java.util.regex.Pattern;
24 import java.util.regex.PatternSyntaxException;
25
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;
37
38 import org.eclipse.linuxtools.internal.tmf.ui.Messages;
39 import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
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;
49
50 public class CustomTxtTraceDefinition extends CustomTraceDefinition {
51
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 TmfUiPlugin.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_TXT_TRACE_DEFINITIONS_FILE_NAME).toString();
55
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;
69
70 public List<InputLine> inputs;
71
72 public CustomTxtTraceDefinition() {
73 this("", new ArrayList<InputLine>(0), new ArrayList<OutputColumn>(0), ""); //$NON-NLS-1$ //$NON-NLS-2$
74 };
75
76 public CustomTxtTraceDefinition(String logtype, List<InputLine> inputs, List<OutputColumn> outputs, String timeStampOutputFormat) {
77 this.definitionName = logtype;
78 this.inputs = inputs;
79 this.outputs = outputs;
80 this.timeStampOutputFormat = timeStampOutputFormat;
81 }
82
83 public static class InputLine {
84 public List<InputData> columns;
85 public Cardinality cardinality;
86 private String regex;
87 private Pattern pattern;
88 public InputLine parentInput;
89 public int level;
90 public InputLine nextInput;
91 public List<InputLine> childrenInputs;
92
93 public InputLine() {};
94
95 public InputLine(Cardinality cardinality, String regex, List<InputData> columns) {
96 this.cardinality = cardinality;
97 this.regex = regex;
98 this.columns = columns;
99 }
100
101 public void setRegex(String regex) {
102 this.regex = regex;
103 this.pattern = null;
104 }
105
106 public String getRegex() {
107 return regex;
108 }
109
110 public Pattern getPattern() throws PatternSyntaxException {
111 if (pattern == null) {
112 pattern = Pattern.compile(regex);
113 }
114 return pattern;
115 }
116
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;
123 }
124 childrenInputs.add(input);
125 input.parentInput = this;
126 input.level = this.level + 1;
127 }
128
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;
134 nextInput = input;
135 input.nextInput = next;
136 }
137 input.parentInput = this.parentInput;
138 input.level = this.level;
139 }
140
141 public void moveUp() {
142 if (parentInput != null) {
143 int index = parentInput.childrenInputs.indexOf(this);
144 if (index > 0) {
145 parentInput.childrenInputs.add(index - 1 , parentInput.childrenInputs.remove(index));
146 parentInput.childrenInputs.get(index).nextInput = nextInput;
147 nextInput = parentInput.childrenInputs.get(index);
148 }
149 }
150 }
151
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;
159 }
160 }
161 }
162
163 public void addColumn(InputData column) {
164 if (columns == null) {
165 columns = new ArrayList<InputData>(1);
166 }
167 columns.add(column);
168 }
169
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) {
176 return nextInputs;
177 }
178 next = next.nextInput;
179 }
180 if (parentInput != null && parentInput.level > 0) {
181 int parentCount = countMap.get(parentInput);
182 if (parentCount < parentInput.getMaxCount()) {
183 nextInputs.add(parentInput);
184 }
185 if (parentCount < parentInput.getMinCount()) {
186 return nextInputs;
187 }
188 nextInputs.addAll(parentInput.getNextInputs(countMap));
189 }
190 return nextInputs;
191 }
192
193 public int getMinCount() {
194 return cardinality.min;
195 }
196
197 public int getMaxCount() {
198 return cardinality.max;
199 }
200
201 @Override
202 public String toString() {
203 return regex + " " + cardinality; //$NON-NLS-1$
204 }
205
206 }
207
208 public static class InputData {
209 public String name;
210 public int action;
211 public String format;
212
213 public InputData() {};
214
215 public InputData(String name, int action, String format) {
216 this.name = name;
217 this.action = action;
218 this.format = format;
219 }
220
221 public InputData(String name, int action) {
222 this.name = name;
223 this.action = action;
224 }
225 }
226
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);
233
234 private int min;
235 private int max;
236
237 public Cardinality(int min, int max) {
238 this.min = min;
239 this.max = max;
240 }
241
242 @Override
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$
245 }
246
247 /* (non-Javadoc)
248 * @see java.lang.Object#hashCode()
249 */
250 @Override
251 public int hashCode() {
252 final int prime = 31;
253 int result = 1;
254 result = prime * result + max;
255 result = prime * result + min;
256 return result;
257 }
258
259 /* (non-Javadoc)
260 * @see java.lang.Object#equals(java.lang.Object)
261 */
262 @Override
263 public boolean equals(Object obj) {
264 if (this == obj) {
265 return true;
266 }
267 if (obj == null) {
268 return false;
269 }
270 if (!(obj instanceof Cardinality)) return false;
271 Cardinality other = (Cardinality) obj;
272 return (this.min == other.min && this.max == other.max);
273 }
274 }
275
276 @Override
277 public void save() {
278 save(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME);
279 }
280
281 @Override
282 public void save(String path) {
283 try {
284 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
285 DocumentBuilder db = dbf.newDocumentBuilder();
286
287 // The following allows xml parsing without access to the dtd
288 EntityResolver resolver = new EntityResolver () {
289 @Override
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);
294 }
295 };
296 db.setEntityResolver(resolver);
297
298 // The following catches xml parsing exceptions
299 db.setErrorHandler(new ErrorHandler(){
300 @Override
301 public void error(SAXParseException saxparseexception) throws SAXException {}
302 @Override
303 public void warning(SAXParseException saxparseexception) throws SAXException {}
304 @Override
305 public void fatalError(SAXParseException saxparseexception) throws SAXException {
306 throw saxparseexception;
307 }});
308
309 Document doc = null;
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)) {
314 return;
315 }
316 } else {
317 doc = db.newDocument();
318 Node node = doc.createElement(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT);
319 doc.appendChild(node);
320 }
321
322 Element root = doc.getDocumentElement();
323
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);
331 }
332 }
333 Element definitionElement = doc.createElement(DEFINITION_ELEMENT);
334 root.appendChild(definitionElement);
335 definitionElement.setAttribute(NAME_ATTRIBUTE, definitionName);
336
337 Element formatElement = doc.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT);
338 definitionElement.appendChild(formatElement);
339 formatElement.appendChild(doc.createTextNode(timeStampOutputFormat));
340
341 if (inputs != null) {
342 for (InputLine inputLine : inputs) {
343 definitionElement.appendChild(createInputLineElement(inputLine, doc));
344 }
345 }
346
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);
352 }
353 }
354
355 Transformer transformer = TransformerFactory.newInstance().newTransformer();
356 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
357
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();
363
364 FileWriter writer = new FileWriter(file);
365 writer.write(xmlString);
366 writer.close();
367 } catch (ParserConfigurationException e) {
368 e.printStackTrace();
369 } catch (TransformerConfigurationException e) {
370 e.printStackTrace();
371 } catch (TransformerFactoryConfigurationError e) {
372 e.printStackTrace();
373 } catch (TransformerException e) {
374 e.printStackTrace();
375 } catch (IOException e) {
376 e.printStackTrace();
377 } catch (SAXException e) {
378 e.printStackTrace();
379 }
380 }
381
382 private Element createInputLineElement(InputLine inputLine, Document doc) {
383 Element inputLineElement = doc.createElement(INPUT_LINE_ELEMENT);
384
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));
389
390 Element regexElement = doc.createElement(REGEX_ELEMENT);
391 inputLineElement.appendChild(regexElement);
392 regexElement.appendChild(doc.createTextNode(inputLine.regex));
393
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);
402 }
403 }
404 }
405
406 if (inputLine.childrenInputs != null) {
407 for (InputLine childInputLine : inputLine.childrenInputs) {
408 inputLineElement.appendChild(createInputLineElement(childInputLine, doc));
409 }
410 }
411
412 return inputLineElement;
413 }
414
415 public static CustomTxtTraceDefinition[] loadAll() {
416 return loadAll(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME);
417 }
418
419 public static CustomTxtTraceDefinition[] loadAll(String path) {
420 try {
421 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
422 DocumentBuilder db = dbf.newDocumentBuilder();
423
424 // The following allows xml parsing without access to the dtd
425 EntityResolver resolver = new EntityResolver () {
426 @Override
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);
431 }
432 };
433 db.setEntityResolver(resolver);
434
435 // The following catches xml parsing exceptions
436 db.setErrorHandler(new ErrorHandler(){
437 @Override
438 public void error(SAXParseException saxparseexception) throws SAXException {}
439 @Override
440 public void warning(SAXParseException saxparseexception) throws SAXException {}
441 @Override
442 public void fatalError(SAXParseException saxparseexception) throws SAXException {
443 throw saxparseexception;
444 }});
445
446 File file = new File(path);
447 if (!file.canRead()) {
448 return new CustomTxtTraceDefinition[0];
449 }
450 Document doc = db.parse(file);
451
452 Element root = doc.getDocumentElement();
453 if (! root.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT)) {
454 return new CustomTxtTraceDefinition[0];
455 }
456
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);
463 if (def != null) {
464 defList.add(def);
465 }
466 }
467 }
468 return defList.toArray(new CustomTxtTraceDefinition[0]);
469 } catch (ParserConfigurationException e) {
470 e.printStackTrace();
471 } catch (SAXException e) {
472 e.printStackTrace();
473 } catch (IOException e) {
474 e.printStackTrace();
475 }
476 return new CustomTxtTraceDefinition[0];
477 }
478
479 public static CustomTxtTraceDefinition load(String definitionName) {
480 try {
481 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
482 DocumentBuilder db = dbf.newDocumentBuilder();
483
484 // The following allows xml parsing without access to the dtd
485 EntityResolver resolver = new EntityResolver () {
486 @Override
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);
491 }
492 };
493 db.setEntityResolver(resolver);
494
495 // The following catches xml parsing exceptions
496 db.setErrorHandler(new ErrorHandler(){
497 @Override
498 public void error(SAXParseException saxparseexception) throws SAXException {}
499 @Override
500 public void warning(SAXParseException saxparseexception) throws SAXException {}
501 @Override
502 public void fatalError(SAXParseException saxparseexception) throws SAXException {
503 throw saxparseexception;
504 }});
505
506 File file = new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME);
507 Document doc = db.parse(file);
508
509 Element root = doc.getDocumentElement();
510 if (! root.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT)) {
511 return null;
512 }
513
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);
521 }
522 }
523 } catch (ParserConfigurationException e) {
524 e.printStackTrace();
525 } catch (SAXException e) {
526 e.printStackTrace();
527 } catch (IOException e) {
528 e.printStackTrace();
529 }
530 return null;
531 }
532
533 public static CustomTxtTraceDefinition extractDefinition(Element definitionElement) {
534 CustomTxtTraceDefinition def = new CustomTxtTraceDefinition();
535
536 def.definitionName = definitionElement.getAttribute(NAME_ATTRIBUTE);
537 if (def.definitionName == null) return null;
538
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);
550 }
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);
556 }
557 }
558 return def;
559 }
560
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;
569 try {
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) {
574 return null;
575 }
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);
591 }
592 }
593 }
594 return inputLine;
595 }
596
597 public static void delete(String definitionName) {
598 try {
599 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
600 DocumentBuilder db = dbf.newDocumentBuilder();
601
602 // The following allows xml parsing without access to the dtd
603 EntityResolver resolver = new EntityResolver () {
604 @Override
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);
609 }
610 };
611 db.setEntityResolver(resolver);
612
613 // The following catches xml parsing exceptions
614 db.setErrorHandler(new ErrorHandler(){
615 @Override
616 public void error(SAXParseException saxparseexception) throws SAXException {}
617 @Override
618 public void warning(SAXParseException saxparseexception) throws SAXException {}
619 @Override
620 public void fatalError(SAXParseException saxparseexception) throws SAXException {
621 throw saxparseexception;
622 }});
623
624 File file = new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME);
625 Document doc = db.parse(file);
626
627 Element root = doc.getDocumentElement();
628 if (! root.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT)) {
629 return;
630 }
631
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);
639 }
640 }
641
642 Transformer transformer = TransformerFactory.newInstance().newTransformer();
643 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
644
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();
650
651 FileWriter writer = new FileWriter(file);
652 writer.write(xmlString);
653 writer.close();
654 } catch (ParserConfigurationException e) {
655 e.printStackTrace();
656 } catch (SAXException e) {
657 e.printStackTrace();
658 } catch (IOException e) {
659 e.printStackTrace();
660 } catch (TransformerConfigurationException e) {
661 e.printStackTrace();
662 } catch (TransformerFactoryConfigurationError e) {
663 e.printStackTrace();
664 } catch (TransformerException e) {
665 e.printStackTrace();
666 }
667 }
668 }
This page took 0.046051 seconds and 5 git commands to generate.