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
);
255 save(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
258 public void save(String path
) {
260 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
261 DocumentBuilder db
= dbf
.newDocumentBuilder();
263 // The following allows xml parsing without access to the dtd
264 EntityResolver resolver
= new EntityResolver () {
265 public InputSource
resolveEntity (String publicId
, String systemId
) {
267 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
268 return new InputSource(bais
);
271 db
.setEntityResolver(resolver
);
273 // The following catches xml parsing exceptions
274 db
.setErrorHandler(new ErrorHandler(){
275 public void error(SAXParseException saxparseexception
) throws SAXException
{}
276 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
277 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
278 throw saxparseexception
;
282 File file
= new File(path
);
283 if (file
.canRead()) {
284 doc
= db
.parse(file
);
285 if (! doc
.getDocumentElement().getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
289 doc
= db
.newDocument();
290 Node node
= doc
.createElement(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
);
291 doc
.appendChild(node
);
294 Element root
= doc
.getDocumentElement();
296 NodeList nodeList
= root
.getChildNodes();
297 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
298 Node node
= nodeList
.item(i
);
299 if (node
instanceof Element
&&
300 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
301 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
302 root
.removeChild(node
);
305 Element definitionElement
= doc
.createElement(DEFINITION_ELEMENT
);
306 root
.appendChild(definitionElement
);
307 definitionElement
.setAttribute(NAME_ATTRIBUTE
, definitionName
);
309 Element formatElement
= doc
.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
);
310 definitionElement
.appendChild(formatElement
);
311 formatElement
.appendChild(doc
.createTextNode(timeStampOutputFormat
));
313 if (inputs
!= null) {
314 for (InputLine inputLine
: inputs
) {
315 definitionElement
.appendChild(createInputLineElement(inputLine
, doc
));
319 if (outputs
!= null) {
320 for (OutputColumn output
: outputs
) {
321 Element outputColumnElement
= doc
.createElement(OUTPUT_COLUMN_ELEMENT
);
322 definitionElement
.appendChild(outputColumnElement
);
323 outputColumnElement
.setAttribute(NAME_ATTRIBUTE
, output
.name
);
327 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
328 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes");
330 //initialize StreamResult with File object to save to file
331 StreamResult result
= new StreamResult(new StringWriter());
332 DOMSource source
= new DOMSource(doc
);
333 transformer
.transform(source
, result
);
334 String xmlString
= result
.getWriter().toString();
336 FileWriter writer
= new FileWriter(file
);
337 writer
.write(xmlString
);
339 } catch (ParserConfigurationException e
) {
341 } catch (TransformerConfigurationException e
) {
343 } catch (TransformerFactoryConfigurationError e
) {
345 } catch (TransformerException e
) {
347 } catch (IOException e
) {
349 } catch (SAXException e
) {
354 private Element
createInputLineElement(InputLine inputLine
, Document doc
) {
355 Element inputLineElement
= doc
.createElement(INPUT_LINE_ELEMENT
);
357 Element cardinalityElement
= doc
.createElement(CARDINALITY_ELEMENT
);
358 inputLineElement
.appendChild(cardinalityElement
);
359 cardinalityElement
.setAttribute(MIN_ATTRIBUTE
, Integer
.toString(inputLine
.cardinality
.min
));
360 cardinalityElement
.setAttribute(MAX_ATTRIBUTE
, Integer
.toString(inputLine
.cardinality
.max
));
362 Element regexElement
= doc
.createElement(REGEX_ELEMENT
);
363 inputLineElement
.appendChild(regexElement
);
364 regexElement
.appendChild(doc
.createTextNode(inputLine
.regex
));
366 if (inputLine
.columns
!= null) {
367 for (InputData inputData
: inputLine
.columns
) {
368 Element inputDataElement
= doc
.createElement(INPUT_DATA_ELEMENT
);
369 inputLineElement
.appendChild(inputDataElement
);
370 inputDataElement
.setAttribute(NAME_ATTRIBUTE
, inputData
.name
);
371 inputDataElement
.setAttribute(ACTION_ATTRIBUTE
, Integer
.toString(inputData
.action
));
372 if (inputData
.format
!= null) {
373 inputDataElement
.setAttribute(FORMAT_ATTRIBUTE
, inputData
.format
);
378 if (inputLine
.childrenInputs
!= null) {
379 for (InputLine childInputLine
: inputLine
.childrenInputs
) {
380 inputLineElement
.appendChild(createInputLineElement(childInputLine
, doc
));
384 return inputLineElement
;
387 public static CustomTxtTraceDefinition
[] loadAll() {
388 return loadAll(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
391 public static CustomTxtTraceDefinition
[] loadAll(String path
) {
393 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
394 DocumentBuilder db
= dbf
.newDocumentBuilder();
396 // The following allows xml parsing without access to the dtd
397 EntityResolver resolver
= new EntityResolver () {
398 public InputSource
resolveEntity (String publicId
, String systemId
) {
400 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
401 return new InputSource(bais
);
404 db
.setEntityResolver(resolver
);
406 // The following catches xml parsing exceptions
407 db
.setErrorHandler(new ErrorHandler(){
408 public void error(SAXParseException saxparseexception
) throws SAXException
{}
409 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
410 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
411 throw saxparseexception
;
414 File file
= new File(path
);
415 if (!file
.canRead()) {
416 return new CustomTxtTraceDefinition
[0];
418 Document doc
= db
.parse(file
);
420 Element root
= doc
.getDocumentElement();
421 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
422 return new CustomTxtTraceDefinition
[0];
425 ArrayList
<CustomTxtTraceDefinition
> defList
= new ArrayList
<CustomTxtTraceDefinition
>();
426 NodeList nodeList
= root
.getChildNodes();
427 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
428 Node node
= nodeList
.item(i
);
429 if (node
instanceof Element
&& node
.getNodeName().equals(DEFINITION_ELEMENT
)) {
430 CustomTxtTraceDefinition def
= extractDefinition((Element
) node
);
436 return defList
.toArray(new CustomTxtTraceDefinition
[0]);
437 } catch (ParserConfigurationException e
) {
439 } catch (SAXException e
) {
441 } catch (IOException e
) {
444 return new CustomTxtTraceDefinition
[0];
447 public static CustomTxtTraceDefinition
load(String definitionName
) {
449 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
450 DocumentBuilder db
= dbf
.newDocumentBuilder();
452 // The following allows xml parsing without access to the dtd
453 EntityResolver resolver
= new EntityResolver () {
454 public InputSource
resolveEntity (String publicId
, String systemId
) {
456 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
457 return new InputSource(bais
);
460 db
.setEntityResolver(resolver
);
462 // The following catches xml parsing exceptions
463 db
.setErrorHandler(new ErrorHandler(){
464 public void error(SAXParseException saxparseexception
) throws SAXException
{}
465 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
466 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
467 throw saxparseexception
;
470 File file
= new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
471 Document doc
= db
.parse(file
);
473 Element root
= doc
.getDocumentElement();
474 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
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 return extractDefinition((Element
) node
);
487 } catch (ParserConfigurationException e
) {
489 } catch (SAXException e
) {
491 } catch (IOException e
) {
497 public static CustomTxtTraceDefinition
extractDefinition(Element definitionElement
) {
498 CustomTxtTraceDefinition def
= new CustomTxtTraceDefinition();
500 def
.definitionName
= definitionElement
.getAttribute(NAME_ATTRIBUTE
);
501 if (def
.definitionName
== null) return null;
503 NodeList nodeList
= definitionElement
.getChildNodes();
504 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
505 Node node
= nodeList
.item(i
);
506 String nodeName
= node
.getNodeName();
507 if (nodeName
.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT
)) {
508 Element formatElement
= (Element
) node
;
509 def
.timeStampOutputFormat
= formatElement
.getTextContent();
510 } else if (nodeName
.equals(INPUT_LINE_ELEMENT
)) {
511 InputLine inputLine
= extractInputLine((Element
) node
);
512 if (inputLine
!= null) {
513 def
.inputs
.add(inputLine
);
515 } else if (nodeName
.equals(OUTPUT_COLUMN_ELEMENT
)) {
516 Element outputColumnElement
= (Element
) node
;
517 OutputColumn outputColumn
= new OutputColumn();
518 outputColumn
.name
= outputColumnElement
.getAttribute(NAME_ATTRIBUTE
);
519 def
.outputs
.add(outputColumn
);
525 private static InputLine
extractInputLine(Element inputLineElement
) {
526 InputLine inputLine
= new InputLine();
527 NodeList nodeList
= inputLineElement
.getChildNodes();
528 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
529 Node node
= nodeList
.item(i
);
530 String nodeName
= node
.getNodeName();
531 if (nodeName
.equals(CARDINALITY_ELEMENT
)) {
532 Element cardinalityElement
= (Element
) node
;
534 int min
= Integer
.parseInt(cardinalityElement
.getAttribute(MIN_ATTRIBUTE
));
535 int max
= Integer
.parseInt(cardinalityElement
.getAttribute(MAX_ATTRIBUTE
));
536 inputLine
.cardinality
= new Cardinality(min
, max
);
537 } catch (NumberFormatException e
) {
540 } else if (nodeName
.equals(REGEX_ELEMENT
)) {
541 Element regexElement
= (Element
) node
;
542 inputLine
.regex
= regexElement
.getTextContent();
543 } else if (nodeName
.equals(INPUT_DATA_ELEMENT
)) {
544 Element inputDataElement
= (Element
) node
;
545 InputData inputData
= new InputData();
546 inputData
.name
= inputDataElement
.getAttribute(NAME_ATTRIBUTE
);
547 inputData
.action
= Integer
.parseInt(inputDataElement
.getAttribute(ACTION_ATTRIBUTE
));
548 inputData
.format
= inputDataElement
.getAttribute(FORMAT_ATTRIBUTE
);
549 inputLine
.addColumn(inputData
);
550 } else if (nodeName
.equals(INPUT_LINE_ELEMENT
)) {
551 Element childInputLineElement
= (Element
) node
;
552 InputLine childInputLine
= extractInputLine(childInputLineElement
);
553 if (childInputLine
!= null) {
554 inputLine
.addChild(childInputLine
);
561 public static void delete(String definitionName
) {
563 DocumentBuilderFactory dbf
= DocumentBuilderFactory
.newInstance();
564 DocumentBuilder db
= dbf
.newDocumentBuilder();
566 // The following allows xml parsing without access to the dtd
567 EntityResolver resolver
= new EntityResolver () {
568 public InputSource
resolveEntity (String publicId
, String systemId
) {
570 ByteArrayInputStream bais
= new ByteArrayInputStream(empty
.getBytes());
571 return new InputSource(bais
);
574 db
.setEntityResolver(resolver
);
576 // The following catches xml parsing exceptions
577 db
.setErrorHandler(new ErrorHandler(){
578 public void error(SAXParseException saxparseexception
) throws SAXException
{}
579 public void warning(SAXParseException saxparseexception
) throws SAXException
{}
580 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
581 throw saxparseexception
;
584 File file
= new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME
);
585 Document doc
= db
.parse(file
);
587 Element root
= doc
.getDocumentElement();
588 if (! root
.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT
)) {
592 NodeList nodeList
= root
.getChildNodes();
593 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
594 Node node
= nodeList
.item(i
);
595 if (node
instanceof Element
&&
596 node
.getNodeName().equals(DEFINITION_ELEMENT
) &&
597 definitionName
.equals(((Element
) node
).getAttribute(NAME_ATTRIBUTE
))) {
598 root
.removeChild(node
);
602 Transformer transformer
= TransformerFactory
.newInstance().newTransformer();
603 transformer
.setOutputProperty(OutputKeys
.INDENT
, "yes");
605 //initialize StreamResult with File object to save to file
606 StreamResult result
= new StreamResult(new StringWriter());
607 DOMSource source
= new DOMSource(doc
);
608 transformer
.transform(source
, result
);
609 String xmlString
= result
.getWriter().toString();
611 FileWriter writer
= new FileWriter(file
);
612 writer
.write(xmlString
);
614 } catch (ParserConfigurationException e
) {
616 } catch (SAXException e
) {
618 } catch (IOException e
) {
620 } catch (TransformerConfigurationException e
) {
622 } catch (TransformerFactoryConfigurationError e
) {
624 } catch (TransformerException e
) {