Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
2b0005f0 | 2 | * Copyright (c) 2010, 2015 Ericsson |
be222f56 PT |
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 | * Yuriy Vashchuk (yvashchuk@gmail.com) - Initial API and implementation | |
2b0005f0 | 11 | * Patrick Tasse - Update filter nodes |
be222f56 PT |
12 | *******************************************************************************/ |
13 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.core.filter.xml; |
be222f56 PT |
15 | |
16 | import java.io.File; | |
17 | ||
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; | |
27 | ||
40dfafb3 | 28 | import org.eclipse.tracecompass.tmf.core.event.aspect.TmfEventFieldAspect; |
2bdf0193 AM |
29 | import org.eclipse.tracecompass.tmf.core.filter.model.ITmfFilterTreeNode; |
30 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterAndNode; | |
ec34bf48 | 31 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterAspectNode; |
2bdf0193 AM |
32 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterCompareNode; |
33 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterContainsNode; | |
34 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterEqualsNode; | |
2bdf0193 AM |
35 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterMatchesNode; |
36 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterNode; | |
37 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterOrNode; | |
2b0005f0 | 38 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterTraceTypeNode; |
be222f56 PT |
39 | import org.w3c.dom.Document; |
40 | import org.w3c.dom.Element; | |
41 | ||
42 | /** | |
43 | * The SAX based XML writer | |
44 | * | |
45 | * @version 1.0 | |
46 | * @author Yuriy Vashchuk | |
47 | * @author Patrick Tasse | |
48 | */ | |
49 | public class TmfFilterXMLWriter { | |
50 | ||
d5efe032 | 51 | private Document document = null; |
be222f56 | 52 | |
d5efe032 AF |
53 | /** |
54 | * The XMLParser constructor | |
55 | * | |
be222f56 | 56 | * @param root The tree root |
d5efe032 | 57 | * @throws ParserConfigurationException if a DocumentBuilder |
be222f56 | 58 | * cannot be created which satisfies the configuration requested. |
d5efe032 AF |
59 | */ |
60 | public TmfFilterXMLWriter(final ITmfFilterTreeNode root) throws ParserConfigurationException { | |
61 | DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); | |
62 | DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); | |
63 | document = documentBuilder.newDocument(); | |
64 | ||
65 | Element rootElement = document.createElement(root.getNodeName()); | |
66 | document.appendChild(rootElement); | |
67 | ||
68 | for (ITmfFilterTreeNode node : root.getChildren()) { | |
69 | buildXMLTree(document, node, rootElement); | |
70 | } | |
71 | } | |
72 | ||
73 | /** | |
74 | * The Tree to XML parser | |
75 | * | |
76 | * @param document The XML document | |
77 | * @param treenode The node to write | |
78 | * @param parentElement The XML element of the parent | |
79 | */ | |
80 | public static void buildXMLTree(final Document document, final ITmfFilterTreeNode treenode, Element parentElement) { | |
81 | Element element = document.createElement(treenode.getNodeName()); | |
82 | ||
83 | if (treenode instanceof TmfFilterNode) { | |
84 | ||
85 | TmfFilterNode node = (TmfFilterNode) treenode; | |
86 | element.setAttribute(TmfFilterNode.NAME_ATTR, node.getFilterName()); | |
87 | ||
2b0005f0 | 88 | } else if (treenode instanceof TmfFilterTraceTypeNode) { |
d5efe032 | 89 | |
2b0005f0 PT |
90 | TmfFilterTraceTypeNode node = (TmfFilterTraceTypeNode) treenode; |
91 | element.setAttribute(TmfFilterTraceTypeNode.TYPE_ATTR, node.getTraceTypeId()); | |
92 | element.setAttribute(TmfFilterTraceTypeNode.NAME_ATTR, node.getName()); | |
d5efe032 AF |
93 | |
94 | } else if (treenode instanceof TmfFilterAndNode) { | |
95 | ||
96 | TmfFilterAndNode node = (TmfFilterAndNode) treenode; | |
97 | element.setAttribute(TmfFilterAndNode.NOT_ATTR, Boolean.toString(node.isNot())); | |
98 | ||
99 | } else if (treenode instanceof TmfFilterOrNode) { | |
100 | ||
101 | TmfFilterOrNode node = (TmfFilterOrNode) treenode; | |
102 | element.setAttribute(TmfFilterOrNode.NOT_ATTR, Boolean.toString(node.isNot())); | |
103 | ||
104 | } else if (treenode instanceof TmfFilterContainsNode) { | |
105 | ||
106 | TmfFilterContainsNode node = (TmfFilterContainsNode) treenode; | |
107 | element.setAttribute(TmfFilterContainsNode.NOT_ATTR, Boolean.toString(node.isNot())); | |
40dfafb3 | 108 | setAspectAttributes(element, node); |
d5efe032 AF |
109 | element.setAttribute(TmfFilterContainsNode.VALUE_ATTR, node.getValue()); |
110 | element.setAttribute(TmfFilterContainsNode.IGNORECASE_ATTR, Boolean.toString(node.isIgnoreCase())); | |
111 | ||
112 | } else if (treenode instanceof TmfFilterEqualsNode) { | |
113 | ||
114 | TmfFilterEqualsNode node = (TmfFilterEqualsNode) treenode; | |
115 | element.setAttribute(TmfFilterEqualsNode.NOT_ATTR, Boolean.toString(node.isNot())); | |
40dfafb3 | 116 | setAspectAttributes(element, node); |
d5efe032 AF |
117 | element.setAttribute(TmfFilterEqualsNode.VALUE_ATTR, node.getValue()); |
118 | element.setAttribute(TmfFilterEqualsNode.IGNORECASE_ATTR, Boolean.toString(node.isIgnoreCase())); | |
119 | ||
ec34bf48 | 120 | } else if (treenode instanceof TmfFilterMatchesNode) { |
d5efe032 | 121 | |
ec34bf48 | 122 | TmfFilterMatchesNode node = (TmfFilterMatchesNode) treenode; |
d5efe032 | 123 | element.setAttribute(TmfFilterMatchesNode.NOT_ATTR, Boolean.toString(node.isNot())); |
40dfafb3 | 124 | setAspectAttributes(element, node); |
d5efe032 AF |
125 | element.setAttribute(TmfFilterMatchesNode.REGEX_ATTR, node.getRegex()); |
126 | ||
127 | } else if (treenode instanceof TmfFilterCompareNode) { | |
128 | ||
129 | TmfFilterCompareNode node = (TmfFilterCompareNode) treenode; | |
130 | element.setAttribute(TmfFilterCompareNode.NOT_ATTR, Boolean.toString(node.isNot())); | |
40dfafb3 | 131 | setAspectAttributes(element, node); |
d5efe032 AF |
132 | element.setAttribute(TmfFilterCompareNode.RESULT_ATTR, Integer.toString(node.getResult())); |
133 | element.setAttribute(TmfFilterCompareNode.TYPE_ATTR, node.getType().toString()); | |
134 | element.setAttribute(TmfFilterCompareNode.VALUE_ATTR, node.getValue()); | |
135 | ||
136 | } | |
137 | ||
138 | parentElement.appendChild(element); | |
139 | ||
140 | for (int i = 0; i < treenode.getChildrenCount(); i++) { | |
141 | buildXMLTree(document, treenode.getChild(i), element); | |
142 | } | |
143 | } | |
144 | ||
40dfafb3 PT |
145 | private static void setAspectAttributes(Element element, TmfFilterAspectNode node) { |
146 | if (node.getEventAspect() != null) { | |
147 | element.setAttribute(TmfFilterAspectNode.EVENT_ASPECT_ATTR, node.getEventAspect().getName()); | |
148 | element.setAttribute(TmfFilterAspectNode.TRACE_TYPE_ID_ATTR, node.getTraceTypeId()); | |
149 | if (node.getEventAspect() instanceof TmfEventFieldAspect) { | |
150 | TmfEventFieldAspect aspect = (TmfEventFieldAspect) node.getEventAspect(); | |
151 | if (aspect.getFieldPath() != null) { | |
152 | element.setAttribute(TmfFilterAspectNode.FIELD_ATTR, aspect.getFieldPath()); | |
153 | } | |
154 | } | |
155 | } | |
156 | } | |
157 | ||
d5efe032 AF |
158 | /** |
159 | * Save the tree | |
160 | * | |
161 | * @param uri The new Filter XML path | |
162 | */ | |
163 | public void saveTree(final String uri) { | |
164 | TransformerFactory transformerFactory = TransformerFactory.newInstance(); | |
165 | ||
166 | try { | |
167 | Transformer transformer = transformerFactory.newTransformer(); | |
168 | DOMSource source = new DOMSource(document); | |
169 | StreamResult result = new StreamResult(new File(uri)); | |
170 | transformer.transform(source, result); | |
171 | } catch (TransformerConfigurationException e) { | |
172 | e.printStackTrace(); | |
173 | } catch (TransformerException e) { | |
174 | e.printStackTrace(); | |
175 | } | |
176 | } | |
be222f56 PT |
177 | |
178 | } |