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 * Yuriy Vashchuk (yvashchuk@gmail.com) - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.filter
.xml
;
16 import java
.io
.IOException
;
18 import javax
.xml
.parsers
.DocumentBuilder
;
19 import javax
.xml
.parsers
.DocumentBuilderFactory
;
20 import javax
.xml
.parsers
.ParserConfigurationException
;
21 import javax
.xml
.transform
.Transformer
;
22 import javax
.xml
.transform
.TransformerConfigurationException
;
23 import javax
.xml
.transform
.TransformerException
;
24 import javax
.xml
.transform
.TransformerFactory
;
25 import javax
.xml
.transform
.dom
.DOMSource
;
26 import javax
.xml
.transform
.stream
.StreamResult
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.ITmfFilterTreeNode
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterAndNode
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterCompareNode
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterContainsNode
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterEqualsNode
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterEventTypeNode
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterMatchesNode
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterNode
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterOrNode
;
37 import org
.w3c
.dom
.Document
;
38 import org
.w3c
.dom
.Element
;
41 * The SAX based XML writer
44 * @author Yuriy Vashchuk
45 * @author Patrick Tasse
47 public class TmfFilterXMLWriter
{
49 private Document document
= null;
52 * The XMLParser constructor
54 * @param root The tree root
55 * @throws ParserConfigurationException
57 public TmfFilterXMLWriter(final ITmfFilterTreeNode root
) throws IOException
, ParserConfigurationException
{
58 DocumentBuilderFactory documentBuilderFactory
= DocumentBuilderFactory
.newInstance();
59 DocumentBuilder documentBuilder
= documentBuilderFactory
.newDocumentBuilder();
60 document
= documentBuilder
.newDocument();
62 Element rootElement
= document
.createElement(root
.getNodeName());
63 document
.appendChild(rootElement
);
65 for (ITmfFilterTreeNode node
: root
.getChildren()) {
66 buildXMLTree(document
, node
, rootElement
);
71 * The Tree to XML parser
73 * @param document The XML document
74 * @param treenode The node to write
75 * @param parentElement The XML element of the parent
77 public static void buildXMLTree(final Document document
, final ITmfFilterTreeNode treenode
, Element parentElement
) {
78 Element element
= document
.createElement(treenode
.getNodeName());
80 if (treenode
instanceof TmfFilterNode
) {
82 TmfFilterNode node
= (TmfFilterNode
) treenode
;
83 element
.setAttribute(TmfFilterNode
.NAME_ATTR
, node
.getFilterName());
85 } else if (treenode
instanceof TmfFilterEventTypeNode
) {
87 TmfFilterEventTypeNode node
= (TmfFilterEventTypeNode
) treenode
;
88 element
.setAttribute(TmfFilterEventTypeNode
.TYPE_ATTR
, node
.getEventType());
89 element
.setAttribute(TmfFilterEventTypeNode
.NAME_ATTR
, node
.getName());
91 } else if (treenode
instanceof TmfFilterAndNode
) {
93 TmfFilterAndNode node
= (TmfFilterAndNode
) treenode
;
94 element
.setAttribute(TmfFilterAndNode
.NOT_ATTR
, Boolean
.toString(node
.isNot()));
96 } else if (treenode
instanceof TmfFilterOrNode
) {
98 TmfFilterOrNode node
= (TmfFilterOrNode
) treenode
;
99 element
.setAttribute(TmfFilterOrNode
.NOT_ATTR
, Boolean
.toString(node
.isNot()));
101 } else if (treenode
instanceof TmfFilterContainsNode
) {
103 TmfFilterContainsNode node
= (TmfFilterContainsNode
) treenode
;
104 element
.setAttribute(TmfFilterContainsNode
.NOT_ATTR
, Boolean
.toString(node
.isNot()));
105 element
.setAttribute(TmfFilterContainsNode
.FIELD_ATTR
, node
.getField());
106 element
.setAttribute(TmfFilterContainsNode
.VALUE_ATTR
, node
.getValue());
107 element
.setAttribute(TmfFilterContainsNode
.IGNORECASE_ATTR
, Boolean
.toString(node
.isIgnoreCase()));
109 } else if (treenode
instanceof TmfFilterEqualsNode
) {
111 TmfFilterEqualsNode node
= (TmfFilterEqualsNode
) treenode
;
112 element
.setAttribute(TmfFilterEqualsNode
.NOT_ATTR
, Boolean
.toString(node
.isNot()));
113 element
.setAttribute(TmfFilterEqualsNode
.FIELD_ATTR
, node
.getField());
114 element
.setAttribute(TmfFilterEqualsNode
.VALUE_ATTR
, node
.getValue());
115 element
.setAttribute(TmfFilterEqualsNode
.IGNORECASE_ATTR
, Boolean
.toString(node
.isIgnoreCase()));
117 } else if (treenode
instanceof TmfFilterMatchesNode
) {
119 TmfFilterMatchesNode node
= (TmfFilterMatchesNode
) treenode
;
120 element
.setAttribute(TmfFilterMatchesNode
.NOT_ATTR
, Boolean
.toString(node
.isNot()));
121 element
.setAttribute(TmfFilterMatchesNode
.FIELD_ATTR
, node
.getField());
122 element
.setAttribute(TmfFilterMatchesNode
.REGEX_ATTR
, node
.getRegex());
124 } else if (treenode
instanceof TmfFilterCompareNode
) {
126 TmfFilterCompareNode node
= (TmfFilterCompareNode
) treenode
;
127 element
.setAttribute(TmfFilterCompareNode
.NOT_ATTR
, Boolean
.toString(node
.isNot()));
128 element
.setAttribute(TmfFilterCompareNode
.FIELD_ATTR
, node
.getField());
129 element
.setAttribute(TmfFilterCompareNode
.RESULT_ATTR
, Integer
.toString(node
.getResult()));
130 element
.setAttribute(TmfFilterCompareNode
.TYPE_ATTR
, node
.getType().toString());
131 element
.setAttribute(TmfFilterCompareNode
.VALUE_ATTR
, node
.getValue());
135 parentElement
.appendChild(element
);
137 for (int i
= 0; i
< treenode
.getChildrenCount(); i
++) {
138 buildXMLTree(document
, treenode
.getChild(i
), element
);
145 * @param uri The new Filter XML path
147 public void saveTree(final String uri
) {
148 TransformerFactory transformerFactory
= TransformerFactory
.newInstance();
151 Transformer transformer
= transformerFactory
.newTransformer();
152 DOMSource source
= new DOMSource(document
);
153 StreamResult result
= new StreamResult(new File(uri
));
154 transformer
.transform(source
, result
);
155 } catch (TransformerConfigurationException e
) {
157 } catch (TransformerException e
) {