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
.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
.tmf
.ui
.TmfUiPlugin
;
39 import org
.w3c
.dom
.Document
;
40 import org
.w3c
.dom
.Element
;
41 import org
.w3c
.dom
.Node
;
42 import org
.w3c
.dom
.NodeList
;
43 import org
.xml
.sax
.EntityResolver
;
44 import org
.xml
.sax
.ErrorHandler
;
45 import org
.xml
.sax
.InputSource
;
46 import org
.xml
.sax
.SAXException
;
47 import org
.xml
.sax
.SAXParseException
;
49 public class CustomTxtTraceDefinition
extends CustomTraceDefinition
{
51 protected static final String CUSTOM_TXT_TRACE_DEFINITIONS_FILE_NAME
= "custom_txt_parsers.xml";
52 protected static final String CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
=
53 TmfUiPlugin
.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_TXT_TRACE_DEFINITIONS_FILE_NAME
).toString();
55 private static final String CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
= "CustomTxtTraceDefinitionList";
56 private static final String DEFINITION_ELEMENT
= "Definition";
57 private static final String NAME_ATTRIBUTE
= "name";
58 private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT
= "TimeStampOutputFormat";
59 private static final String INPUT_LINE_ELEMENT
= "InputLine";
60 private static final String CARDINALITY_ELEMENT
= "Cardinality";
61 private static final String MIN_ATTRIBUTE
= "min";
62 private static final String MAX_ATTRIBUTE
= "max";
63 private static final String REGEX_ELEMENT
= "RegEx";
64 private static final String INPUT_DATA_ELEMENT
= "InputData";
65 private static final String ACTION_ATTRIBUTE
= "action";
66 private static final String FORMAT_ATTRIBUTE
= "format";
67 private static final String OUTPUT_COLUMN_ELEMENT
= "OutputColumn";
69 public List
<InputLine
> inputs
;
71 public CustomTxtTraceDefinition() {
72 this("", new ArrayList
<InputLine
>(0), new ArrayList
<OutputColumn
>(0), "");
75 public CustomTxtTraceDefinition(String logtype
, List
<InputLine
> inputs
, List
<OutputColumn
> outputs
, String timeStampOutputFormat
) {
76 this.definitionName
= logtype
;
78 this.outputs
= outputs
;
79 this.timeStampOutputFormat
= timeStampOutputFormat
;
82 public static class InputLine
{
83 public List
<InputData
> columns
;
84 public Cardinality cardinality
;
86 private Pattern pattern
;
87 public InputLine parentInput
;
89 public InputLine nextInput
;
90 public List
<InputLine
> childrenInputs
;
92 public InputLine() {};
94 public InputLine(Cardinality cardinality
, String regex
, List
<InputData
> columns
) {
95 this.cardinality
= cardinality
;
97 this.columns
= columns
;
100 public void setRegex(String regex
) {
105 public String
getRegex() {
109 public Pattern
getPattern() throws PatternSyntaxException
{
110 if (pattern
== null) {
111 pattern
= Pattern
.compile(regex
);
116 public void addChild(InputLine input
) {
117 if (childrenInputs
== null) {
118 childrenInputs
= new ArrayList
<InputLine
>(1);
119 } else if (childrenInputs
.size() > 0) {
120 InputLine last
= childrenInputs
.get(childrenInputs
.size() - 1);
121 last
.nextInput
= input
;
123 childrenInputs
.add(input
);
124 input
.parentInput
= this;
125 input
.level
= this.level
+ 1;
128 public void addNext(InputLine input
) {
129 if (parentInput
!= null) {
130 int index
= parentInput
.childrenInputs
.indexOf(this);
131 parentInput
.childrenInputs
.add(index
+ 1, input
);
132 InputLine next
= nextInput
;
134 input
.nextInput
= next
;
136 input
.parentInput
= this.parentInput
;
137 input
.level
= this.level
;
140 public void moveUp() {
141 if (parentInput
!= null) {
142 int index
= parentInput
.childrenInputs
.indexOf(this);
144 parentInput
.childrenInputs
.add(index
- 1 , parentInput
.childrenInputs
.remove(index
));
145 parentInput
.childrenInputs
.get(index
).nextInput
= nextInput
;
146 nextInput
= parentInput
.childrenInputs
.get(index
);
151 public void moveDown() {
152 if (parentInput
!= null) {
153 int index
= parentInput
.childrenInputs
.indexOf(this);
154 if (index
< parentInput
.childrenInputs
.size() - 1) {
155 parentInput
.childrenInputs
.add(index
+ 1 , parentInput
.childrenInputs
.remove(index
));
156 nextInput
= parentInput
.childrenInputs
.get(index
).nextInput
;
157 parentInput
.childrenInputs
.get(index
).nextInput
= this;
162 public void addColumn(InputData column
) {
163 if (columns
== null) {
164 columns
= new ArrayList
<InputData
>(1);
169 public List
<InputLine
> getNextInputs(Map
<InputLine
, Integer
> countMap
) {
170 List
<InputLine
> nextInputs
= new ArrayList
<InputLine
>();
171 InputLine next
= nextInput
;
172 while (next
!= null) {
173 nextInputs
.add(next
);
174 if (next
.cardinality
.min
> 0) {
177 next
= next
.nextInput
;
179 if (parentInput
!= null && parentInput
.level
> 0) {
180 int parentCount
= countMap
.get(parentInput
);
181 if (parentCount
< parentInput
.getMaxCount()) {
182 nextInputs
.add(parentInput
);
184 if (parentCount
< parentInput
.getMinCount()) {
187 nextInputs
.addAll(parentInput
.getNextInputs(countMap
));
192 public int getMinCount() {
193 return cardinality
.min
;
196 public int getMaxCount() {
197 return cardinality
.max
;
201 public String
toString() {
202 return regex
+ " " + cardinality
;
207 public static class InputData
{
210 public String format
;
212 public InputData() {};
214 public InputData(String name
, int action
, String format
) {
216 this.action
= action
;
217 this.format
= format
;
220 public InputData(String name
, int action
) {
222 this.action
= action
;
226 public static class Cardinality
{
227 public final static int INF
= Integer
.MAX_VALUE
;
228 public final static Cardinality ONE
= new Cardinality(1, 1);
229 public final static Cardinality ONE_OR_MORE
= new Cardinality(1, INF
);
230 public final static Cardinality ZERO_OR_ONE
= new Cardinality(0, 1);
231 public final static Cardinality ZERO_OR_MORE
= new Cardinality(0, INF
);
236 public Cardinality(int min
, int max
) {
242 public String
toString() {
243 return "(" + (min
>= 0 ? min
: "?") + "," + (max
== INF ?
"\u221E" : (max
>= 0 ? max
: "?")) + ")";
247 public boolean equals(Object obj
) {
248 if (!(obj
instanceof Cardinality
)) return false;
249 Cardinality other
= (Cardinality
) obj
;
250 return (this.min
== other
.min
&& this.max
== other
.max
);
256 save(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
260 public void save(String path
) {
262 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
263 DocumentBuilder db
= dbf
.newDocumentBuilder();
265 // The following allows xml parsing without access to the dtd
266 EntityResolver resolver
= new EntityResolver () {
267 public InputSource
resolveEntity (String publicId
, String systemId
) {
269 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
270 return new InputSource(bais
);
273 db
.setEntityResolver(resolver
);
275 // The following catches xml parsing exceptions
276 db
.setErrorHandler(new ErrorHandler(){
277 public void error(SAXParseException saxparseexception
) throws SAXException
{}
278 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
279 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
280 throw saxparseexception
;
284 File file
= new File(path
);
285 if (file
.canRead()) {
286 doc
= db
.parse(file
);
287 if (! doc
.getDocumentElement().getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
291 doc
= db
.newDocument();
292 Node node
= doc
.createElement(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
);
293 doc
.appendChild(node
);
296 Element root
= doc
.getDocumentElement();
298 NodeList nodeList
= root
.getChildNodes();
299 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
300 Node node
= nodeList
.item(i
);
301 if (node
instanceof Element
&&
302 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
303 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
304 root
.removeChild(node
);
307 Element definitionElement
= doc
.createElement(DEFINITION_ELEMENT
);
308 root
.appendChild(definitionElement
);
309 definitionElement
.setAttribute(NAME_ATTRIBUTE
, definitionName
);
311 Element formatElement
= doc
.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
);
312 definitionElement
.appendChild(formatElement
);
313 formatElement
.appendChild(doc
.createTextNode(timeStampOutputFormat
));
315 if (inputs
!= null) {
316 for (InputLine inputLine
: inputs
) {
317 definitionElement
.appendChild(createInputLineElement(inputLine
, doc
));
321 if (outputs
!= null) {
322 for (OutputColumn output
: outputs
) {
323 Element outputColumnElement
= doc
.createElement(OUTPUT_COLUMN_ELEMENT
);
324 definitionElement
.appendChild(outputColumnElement
);
325 outputColumnElement
.setAttribute(NAME_ATTRIBUTE
, output
.name
);
329 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
330 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes");
332 //initialize StreamResult with File object to save to file
333 StreamResult result
= new StreamResult(new StringWriter());
334 DOMSource source
= new DOMSource(doc
);
335 transformer
.transform(source
, result
);
336 String xmlString
= result
.getWriter().toString();
338 FileWriter writer
= new FileWriter(file
);
339 writer
.write(xmlString
);
341 } catch (ParserConfigurationException e
) {
343 } catch (TransformerConfigurationException e
) {
345 } catch (TransformerFactoryConfigurationError e
) {
347 } catch (TransformerException e
) {
349 } catch (IOException e
) {
351 } catch (SAXException e
) {
356 private Element
createInputLineElement(InputLine inputLine
, Document doc
) {
357 Element inputLineElement
= doc
.createElement(INPUT_LINE_ELEMENT
);
359 Element cardinalityElement
= doc
.createElement(CARDINALITY_ELEMENT
);
360 inputLineElement
.appendChild(cardinalityElement
);
361 cardinalityElement
.setAttribute(MIN_ATTRIBUTE
, Integer
.toString(inputLine
.cardinality
.min
));
362 cardinalityElement
.setAttribute(MAX_ATTRIBUTE
, Integer
.toString(inputLine
.cardinality
.max
));
364 Element regexElement
= doc
.createElement(REGEX_ELEMENT
);
365 inputLineElement
.appendChild(regexElement
);
366 regexElement
.appendChild(doc
.createTextNode(inputLine
.regex
));
368 if (inputLine
.columns
!= null) {
369 for (InputData inputData
: inputLine
.columns
) {
370 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
371 inputLineElement
.appendChild(inputDataElement
);
372 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, inputData
.name
);
373 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(inputData
.action
));
374 if (inputData
.format
!= null) {
375 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, inputData
.format
);
380 if (inputLine
.childrenInputs
!= null) {
381 for (InputLine childInputLine
: inputLine
.childrenInputs
) {
382 inputLineElement
.appendChild(createInputLineElement(childInputLine
, doc
));
386 return inputLineElement
;
389 public static CustomTxtTraceDefinition
[] loadAll() {
390 return loadAll(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
393 public static CustomTxtTraceDefinition
[] loadAll(String path
) {
395 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
396 DocumentBuilder db
= dbf
.newDocumentBuilder();
398 // The following allows xml parsing without access to the dtd
399 EntityResolver resolver
= new EntityResolver () {
400 public InputSource
resolveEntity (String publicId
, String systemId
) {
402 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
403 return new InputSource(bais
);
406 db
.setEntityResolver(resolver
);
408 // The following catches xml parsing exceptions
409 db
.setErrorHandler(new ErrorHandler(){
410 public void error(SAXParseException saxparseexception
) throws SAXException
{}
411 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
412 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
413 throw saxparseexception
;
416 File file
= new File(path
);
417 if (!file
.canRead()) {
418 return new CustomTxtTraceDefinition
[0];
420 Document doc
= db
.parse(file
);
422 Element root
= doc
.getDocumentElement();
423 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
424 return new CustomTxtTraceDefinition
[0];
427 ArrayList
<CustomTxtTraceDefinition
> defList
= new ArrayList
<CustomTxtTraceDefinition
>();
428 NodeList nodeList
= root
.getChildNodes();
429 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
430 Node node
= nodeList
.item(i
);
431 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
432 CustomTxtTraceDefinition def
= extractDefinition((Element
) node
);
438 return defList
.toArray(new CustomTxtTraceDefinition
[0]);
439 } catch (ParserConfigurationException e
) {
441 } catch (SAXException e
) {
443 } catch (IOException e
) {
446 return new CustomTxtTraceDefinition
[0];
449 public static CustomTxtTraceDefinition
load(String definitionName
) {
451 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
452 DocumentBuilder db
= dbf
.newDocumentBuilder();
454 // The following allows xml parsing without access to the dtd
455 EntityResolver resolver
= new EntityResolver () {
456 public InputSource
resolveEntity (String publicId
, String systemId
) {
458 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
459 return new InputSource(bais
);
462 db
.setEntityResolver(resolver
);
464 // The following catches xml parsing exceptions
465 db
.setErrorHandler(new ErrorHandler(){
466 public void error(SAXParseException saxparseexception
) throws SAXException
{}
467 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
468 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
469 throw saxparseexception
;
472 File file
= new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
473 Document doc
= db
.parse(file
);
475 Element root
= doc
.getDocumentElement();
476 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
480 NodeList nodeList
= root
.getChildNodes();
481 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
482 Node node
= nodeList
.item(i
);
483 if (node
instanceof Element
&&
484 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
485 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
486 return extractDefinition((Element
) node
);
489 } catch (ParserConfigurationException e
) {
491 } catch (SAXException e
) {
493 } catch (IOException e
) {
499 public static CustomTxtTraceDefinition
extractDefinition(Element definitionElement
) {
500 CustomTxtTraceDefinition def
= new CustomTxtTraceDefinition();
502 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
503 if (def
.definitionName
== null) return null;
505 NodeList nodeList
= definitionElement
.getChildNodes();
506 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
507 Node node
= nodeList
.item(i
);
508 String nodeName
= node
.getNodeName();
509 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
510 Element formatElement
= (Element
) node
;
511 def
.timeStampOutputFormat
= formatElement
.getTextContent();
512 } else if (nodeName
.equals(INPUT_LINE_ELEMENT
)) {
513 InputLine inputLine
= extractInputLine((Element
) node
);
514 if (inputLine
!= null) {
515 def
.inputs
.add(inputLine
);
517 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
518 Element outputColumnElement
= (Element
) node
;
519 OutputColumn outputColumn
= new OutputColumn();
520 outputColumn
.name
= outputColumnElement
.getAttribute(NAME_ATTRIBUTE
);
521 def
.outputs
.add(outputColumn
);
527 private static InputLine
extractInputLine(Element inputLineElement
) {
528 InputLine inputLine
= new InputLine();
529 NodeList nodeList
= inputLineElement
.getChildNodes();
530 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
531 Node node
= nodeList
.item(i
);
532 String nodeName
= node
.getNodeName();
533 if (nodeName
.equals(CARDINALITY_ELEMENT
)) {
534 Element cardinalityElement
= (Element
) node
;
536 int min
= Integer
.parseInt(cardinalityElement
.getAttribute(MIN_ATTRIBUTE
));
537 int max
= Integer
.parseInt(cardinalityElement
.getAttribute(MAX_ATTRIBUTE
));
538 inputLine
.cardinality
= new Cardinality(min
, max
);
539 } catch (NumberFormatException e
) {
542 } else if (nodeName
.equals(REGEX_ELEMENT
)) {
543 Element regexElement
= (Element
) node
;
544 inputLine
.regex
= regexElement
.getTextContent();
545 } else if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
546 Element inputDataElement
= (Element
) node
;
547 InputData inputData
= new InputData();
548 inputData
.name
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
549 inputData
.action
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
550 inputData
.format
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
551 inputLine
.addColumn(inputData
);
552 } else if (nodeName
.equals(INPUT_LINE_ELEMENT
)) {
553 Element childInputLineElement
= (Element
) node
;
554 InputLine childInputLine
= extractInputLine(childInputLineElement
);
555 if (childInputLine
!= null) {
556 inputLine
.addChild(childInputLine
);
563 public static void delete(String definitionName
) {
565 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
566 DocumentBuilder db
= dbf
.newDocumentBuilder();
568 // The following allows xml parsing without access to the dtd
569 EntityResolver resolver
= new EntityResolver () {
570 public InputSource
resolveEntity (String publicId
, String systemId
) {
572 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
573 return new InputSource(bais
);
576 db
.setEntityResolver(resolver
);
578 // The following catches xml parsing exceptions
579 db
.setErrorHandler(new ErrorHandler(){
580 public void error(SAXParseException saxparseexception
) throws SAXException
{}
581 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
582 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
583 throw saxparseexception
;
586 File file
= new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
587 Document doc
= db
.parse(file
);
589 Element root
= doc
.getDocumentElement();
590 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
594 NodeList nodeList
= root
.getChildNodes();
595 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
596 Node node
= nodeList
.item(i
);
597 if (node
instanceof Element
&&
598 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
599 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
600 root
.removeChild(node
);
604 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
605 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes");
607 //initialize StreamResult with File object to save to file
608 StreamResult result
= new StreamResult(new StringWriter());
609 DOMSource source
= new DOMSource(doc
);
610 transformer
.transform(source
, result
);
611 String xmlString
= result
.getWriter().toString();
613 FileWriter writer
= new FileWriter(file
);
614 writer
.write(xmlString
);
616 } catch (ParserConfigurationException e
) {
618 } catch (SAXException e
) {
620 } catch (IOException e
) {
622 } catch (TransformerConfigurationException e
) {
624 } catch (TransformerFactoryConfigurationError e
) {
626 } catch (TransformerException e
) {