Commit | Line | Data |
---|---|---|
46ab8ce3 FC |
1 | /*******************************************************************************\r |
2 | * Copyright (c) 2010 Ericsson\r | |
9b749023 | 3 | *\r |
46ab8ce3 FC |
4 | * All rights reserved. This program and the accompanying materials are\r |
5 | * made available under the terms of the Eclipse Public License v1.0 which\r | |
6 | * accompanies this distribution, and is available at\r | |
7 | * http://www.eclipse.org/legal/epl-v10.html\r | |
9b749023 | 8 | *\r |
46ab8ce3 FC |
9 | * Contributors:\r |
10 | * Yuriy Vashchuk (yvashchuk@gmail.com) - Initial API and implementation\r | |
11 | *******************************************************************************/\r | |
12 | \r | |
6c13869b | 13 | package org.eclipse.linuxtools.tmf.core.filter.xml;\r |
46ab8ce3 FC |
14 | \r |
15 | import java.io.File;\r | |
46ab8ce3 FC |
16 | \r |
17 | import javax.xml.parsers.DocumentBuilder;\r | |
18 | import javax.xml.parsers.DocumentBuilderFactory;\r | |
19 | import javax.xml.parsers.ParserConfigurationException;\r | |
20 | import javax.xml.transform.Transformer;\r | |
21 | import javax.xml.transform.TransformerConfigurationException;\r | |
22 | import javax.xml.transform.TransformerException;\r | |
23 | import javax.xml.transform.TransformerFactory;\r | |
24 | import javax.xml.transform.dom.DOMSource;\r | |
25 | import javax.xml.transform.stream.StreamResult;\r | |
26 | \r | |
6c13869b FC |
27 | import org.eclipse.linuxtools.tmf.core.filter.model.ITmfFilterTreeNode;\r |
28 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterAndNode;\r | |
29 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterCompareNode;\r | |
30 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterContainsNode;\r | |
31 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterEqualsNode;\r | |
32 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterEventTypeNode;\r | |
33 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterMatchesNode;\r | |
34 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterNode;\r | |
35 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterOrNode;\r | |
46ab8ce3 FC |
36 | import org.w3c.dom.Document;\r |
37 | import org.w3c.dom.Element;\r | |
38 | \r | |
39 | /**\r | |
d37aaa7f | 40 | * The SAX based XML writer\r |
9b749023 | 41 | *\r |
d37aaa7f FC |
42 | * @version 1.0\r |
43 | * @author Yuriy Vashchuk\r | |
44 | * @author Patrick Tasse\r | |
46ab8ce3 FC |
45 | */\r |
46 | public class TmfFilterXMLWriter {\r | |
9b749023 | 47 | \r |
46ab8ce3 | 48 | private Document document = null;\r |
9b749023 | 49 | \r |
46ab8ce3 FC |
50 | /**\r |
51 | * The XMLParser constructor\r | |
9b749023 | 52 | *\r |
0d9a6d76 | 53 | * @param root The tree root\r |
0283f7ff FC |
54 | * @throws ParserConfigurationException if a DocumentBuilder\r |
55 | * cannot be created which satisfies the configuration requested.\r | |
9b749023 AM |
56 | */\r |
57 | public TmfFilterXMLWriter(final ITmfFilterTreeNode root) throws ParserConfigurationException {\r | |
46ab8ce3 FC |
58 | DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();\r |
59 | DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();\r | |
60 | document = documentBuilder.newDocument();\r | |
9b749023 | 61 | \r |
46ab8ce3 FC |
62 | Element rootElement = document.createElement(root.getNodeName());\r |
63 | document.appendChild(rootElement);\r | |
64 | \r | |
65 | for (ITmfFilterTreeNode node : root.getChildren()) {\r | |
66 | buildXMLTree(document, node, rootElement);\r | |
67 | }\r | |
68 | }\r | |
69 | \r | |
70 | /**\r | |
71 | * The Tree to XML parser\r | |
9b749023 | 72 | *\r |
46ab8ce3 FC |
73 | * @param document The XML document\r |
74 | * @param treenode The node to write\r | |
75 | * @param parentElement The XML element of the parent\r | |
9b749023 | 76 | */\r |
46ab8ce3 FC |
77 | public static void buildXMLTree(final Document document, final ITmfFilterTreeNode treenode, Element parentElement) {\r |
78 | Element element = document.createElement(treenode.getNodeName());\r | |
9b749023 | 79 | \r |
46ab8ce3 | 80 | if (treenode instanceof TmfFilterNode) {\r |
9b749023 | 81 | \r |
46ab8ce3 FC |
82 | TmfFilterNode node = (TmfFilterNode) treenode;\r |
83 | element.setAttribute(TmfFilterNode.NAME_ATTR, node.getFilterName());\r | |
9b749023 | 84 | \r |
46ab8ce3 | 85 | } else if (treenode instanceof TmfFilterEventTypeNode) {\r |
9b749023 | 86 | \r |
46ab8ce3 FC |
87 | TmfFilterEventTypeNode node = (TmfFilterEventTypeNode) treenode;\r |
88 | element.setAttribute(TmfFilterEventTypeNode.TYPE_ATTR, node.getEventType());\r | |
89 | element.setAttribute(TmfFilterEventTypeNode.NAME_ATTR, node.getName());\r | |
9b749023 | 90 | \r |
46ab8ce3 | 91 | } else if (treenode instanceof TmfFilterAndNode) {\r |
9b749023 | 92 | \r |
46ab8ce3 FC |
93 | TmfFilterAndNode node = (TmfFilterAndNode) treenode;\r |
94 | element.setAttribute(TmfFilterAndNode.NOT_ATTR, Boolean.toString(node.isNot()));\r | |
9b749023 | 95 | \r |
46ab8ce3 | 96 | } else if (treenode instanceof TmfFilterOrNode) {\r |
9b749023 | 97 | \r |
46ab8ce3 FC |
98 | TmfFilterOrNode node = (TmfFilterOrNode) treenode;\r |
99 | element.setAttribute(TmfFilterOrNode.NOT_ATTR, Boolean.toString(node.isNot()));\r | |
9b749023 | 100 | \r |
46ab8ce3 | 101 | } else if (treenode instanceof TmfFilterContainsNode) {\r |
9b749023 | 102 | \r |
46ab8ce3 FC |
103 | TmfFilterContainsNode node = (TmfFilterContainsNode) treenode;\r |
104 | element.setAttribute(TmfFilterContainsNode.NOT_ATTR, Boolean.toString(node.isNot()));\r | |
105 | element.setAttribute(TmfFilterContainsNode.FIELD_ATTR, node.getField());\r | |
106 | element.setAttribute(TmfFilterContainsNode.VALUE_ATTR, node.getValue());\r | |
107 | element.setAttribute(TmfFilterContainsNode.IGNORECASE_ATTR, Boolean.toString(node.isIgnoreCase()));\r | |
9b749023 | 108 | \r |
46ab8ce3 | 109 | } else if (treenode instanceof TmfFilterEqualsNode) {\r |
9b749023 | 110 | \r |
46ab8ce3 FC |
111 | TmfFilterEqualsNode node = (TmfFilterEqualsNode) treenode;\r |
112 | element.setAttribute(TmfFilterEqualsNode.NOT_ATTR, Boolean.toString(node.isNot()));\r | |
113 | element.setAttribute(TmfFilterEqualsNode.FIELD_ATTR, node.getField());\r | |
114 | element.setAttribute(TmfFilterEqualsNode.VALUE_ATTR, node.getValue());\r | |
115 | element.setAttribute(TmfFilterEqualsNode.IGNORECASE_ATTR, Boolean.toString(node.isIgnoreCase()));\r | |
9b749023 | 116 | \r |
46ab8ce3 | 117 | } else if (treenode instanceof TmfFilterMatchesNode) {\r |
9b749023 | 118 | \r |
46ab8ce3 FC |
119 | TmfFilterMatchesNode node = (TmfFilterMatchesNode) treenode;\r |
120 | element.setAttribute(TmfFilterMatchesNode.NOT_ATTR, Boolean.toString(node.isNot()));\r | |
121 | element.setAttribute(TmfFilterMatchesNode.FIELD_ATTR, node.getField());\r | |
122 | element.setAttribute(TmfFilterMatchesNode.REGEX_ATTR, node.getRegex());\r | |
9b749023 | 123 | \r |
46ab8ce3 | 124 | } else if (treenode instanceof TmfFilterCompareNode) {\r |
9b749023 | 125 | \r |
46ab8ce3 FC |
126 | TmfFilterCompareNode node = (TmfFilterCompareNode) treenode;\r |
127 | element.setAttribute(TmfFilterCompareNode.NOT_ATTR, Boolean.toString(node.isNot()));\r | |
128 | element.setAttribute(TmfFilterCompareNode.FIELD_ATTR, node.getField());\r | |
129 | element.setAttribute(TmfFilterCompareNode.RESULT_ATTR, Integer.toString(node.getResult()));\r | |
130 | element.setAttribute(TmfFilterCompareNode.TYPE_ATTR, node.getType().toString());\r | |
131 | element.setAttribute(TmfFilterCompareNode.VALUE_ATTR, node.getValue());\r | |
9b749023 | 132 | \r |
46ab8ce3 | 133 | }\r |
9b749023 | 134 | \r |
46ab8ce3 | 135 | parentElement.appendChild(element);\r |
9b749023 | 136 | \r |
46ab8ce3 FC |
137 | for (int i = 0; i < treenode.getChildrenCount(); i++) {\r |
138 | buildXMLTree(document, treenode.getChild(i), element);\r | |
139 | }\r | |
140 | }\r | |
9b749023 | 141 | \r |
46ab8ce3 | 142 | /**\r |
9b749023 AM |
143 | * Save the tree\r |
144 | *\r | |
46ab8ce3 | 145 | * @param uri The new Filter XML path\r |
9b749023 | 146 | */\r |
46ab8ce3 FC |
147 | public void saveTree(final String uri) {\r |
148 | TransformerFactory transformerFactory = TransformerFactory.newInstance();\r | |
9b749023 | 149 | \r |
46ab8ce3 FC |
150 | try {\r |
151 | Transformer transformer = transformerFactory.newTransformer();\r | |
152 | DOMSource source = new DOMSource(document);\r | |
153 | StreamResult result = new StreamResult(new File(uri));\r | |
154 | transformer.transform(source, result);\r | |
155 | } catch (TransformerConfigurationException e) {\r | |
156 | e.printStackTrace();\r | |
157 | } catch (TransformerException e) {\r | |
158 | e.printStackTrace();\r | |
159 | }\r | |
160 | }\r | |
9b749023 | 161 | \r |
46ab8ce3 | 162 | }\r |