dc159844fc3a3a79698a030329c5e6dd58246d7f
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / filter / xml / TmfFilterContentHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 Ericsson
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
11 * based on http://smeric.developpez.com/java/cours/xml/sax/
12 * Patrick Tasse - Refactoring
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.core.filter.xml;
16
17 import java.util.Stack;
18
19 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
20 import org.eclipse.tracecompass.tmf.core.event.aspect.TmfEventFieldAspect;
21 import org.eclipse.tracecompass.tmf.core.filter.model.ITmfFilterTreeNode;
22 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterAndNode;
23 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterAspectNode;
24 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterCompareNode;
25 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterCompareNode.Type;
26 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterContainsNode;
27 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterEqualsNode;
28 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterMatchesNode;
29 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterNode;
30 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterOrNode;
31 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterRootNode;
32 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterTraceTypeNode;
33 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterTreeNode;
34 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
35 import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
36 import org.xml.sax.Attributes;
37 import org.xml.sax.SAXException;
38 import org.xml.sax.helpers.DefaultHandler;
39
40 /**
41 * The SAX Content Handler
42 *
43 * @version 1.0
44 * @author Yuriy Vashchuk
45 * @author Patrick Tasse
46 */
47 public class TmfFilterContentHandler extends DefaultHandler {
48
49 // Backward compatibility strings
50 private static final String EVENTTYPE_NODE_NAME = "EVENTTYPE"; //$NON-NLS-1$
51 private static final String NAME_ATTR = "name"; //$NON-NLS-1$
52 private static final String LTTNG_KERNEL_TRACE = "Common Trace Format : LTTng Kernel Trace"; //$NON-NLS-1$
53 private static final String LINUX_KERNEL_TRACE = "Common Trace Format : Linux Kernel Trace"; //$NON-NLS-1$
54 private static final String FIELD_ATTR = "field"; //$NON-NLS-1$
55 private static final String EVENT_FIELD_TIMESTAMP = ":timestamp:"; //$NON-NLS-1$
56 private static final String EVENT_FIELD_TYPE = ":type:"; //$NON-NLS-1$
57 private static final String EVENT_FIELD_CONTENT = ":content:"; //$NON-NLS-1$
58
59 private ITmfFilterTreeNode fRoot = null;
60 private Stack<ITmfFilterTreeNode> fFilterTreeStack = null;
61
62 /**
63 * The default constructor
64 */
65 public TmfFilterContentHandler() {
66 super();
67 fFilterTreeStack = new Stack<>();
68 }
69
70 /**
71 * Getter of tree
72 *
73 * @return The builded tree
74 */
75 public ITmfFilterTreeNode getTree() {
76 return fRoot;
77 }
78
79
80 @Override
81 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
82 ITmfFilterTreeNode node = null;
83
84 if (localName.equalsIgnoreCase(TmfFilterRootNode.NODE_NAME)) {
85
86 node = new TmfFilterRootNode();
87
88 } else if (localName.equals(TmfFilterNode.NODE_NAME)) {
89
90 node = new TmfFilterNode(atts.getValue(TmfFilterNode.NAME_ATTR));
91
92 } else if (localName.equals(TmfFilterTraceTypeNode.NODE_NAME)) {
93
94 node = new TmfFilterTraceTypeNode(null);
95 String traceTypeId = atts.getValue(TmfFilterTraceTypeNode.TYPE_ATTR);
96 traceTypeId = TmfTraceType.buildCompatibilityTraceTypeId(traceTypeId);
97 ((TmfFilterTraceTypeNode) node).setTraceTypeId(traceTypeId);
98 TraceTypeHelper helper = TmfTraceType.getTraceType(traceTypeId);
99 if (helper != null) {
100 ((TmfFilterTraceTypeNode) node).setTraceClass(helper.getTraceClass());
101 }
102 ((TmfFilterTraceTypeNode) node).setName(atts.getValue(TmfFilterTraceTypeNode.NAME_ATTR));
103
104 } else if (localName.equals(TmfFilterAndNode.NODE_NAME)) {
105
106 node = new TmfFilterAndNode(null);
107 String value = atts.getValue(TmfFilterAndNode.NOT_ATTR);
108 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
109 ((TmfFilterAndNode) node).setNot(true);
110 }
111
112 } else if (localName.equals(TmfFilterOrNode.NODE_NAME)) {
113
114 node = new TmfFilterOrNode(null);
115 String value = atts.getValue(TmfFilterOrNode.NOT_ATTR);
116 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
117 ((TmfFilterOrNode) node).setNot(true);
118 }
119
120 } else if (localName.equals(TmfFilterContainsNode.NODE_NAME)) {
121
122 node = new TmfFilterContainsNode(null);
123 String value = atts.getValue(TmfFilterContainsNode.NOT_ATTR);
124 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
125 ((TmfFilterContainsNode) node).setNot(true);
126 }
127 createEventAspect((TmfFilterAspectNode) node, atts);
128 ((TmfFilterContainsNode) node).setValue(atts.getValue(TmfFilterContainsNode.VALUE_ATTR));
129 value = atts.getValue(TmfFilterContainsNode.IGNORECASE_ATTR);
130 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
131 ((TmfFilterContainsNode) node).setIgnoreCase(true);
132 }
133
134 } else if (localName.equals(TmfFilterEqualsNode.NODE_NAME)) {
135
136 node = new TmfFilterEqualsNode(null);
137 String value = atts.getValue(TmfFilterEqualsNode.NOT_ATTR);
138 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
139 ((TmfFilterEqualsNode) node).setNot(true);
140 }
141 createEventAspect((TmfFilterAspectNode) node, atts);
142 ((TmfFilterEqualsNode) node).setValue(atts.getValue(TmfFilterEqualsNode.VALUE_ATTR));
143 value = atts.getValue(TmfFilterEqualsNode.IGNORECASE_ATTR);
144 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
145 ((TmfFilterEqualsNode) node).setIgnoreCase(true);
146 }
147
148 } else if (localName.equals(TmfFilterMatchesNode.NODE_NAME)) {
149
150 node = new TmfFilterMatchesNode(null);
151 String value = atts.getValue(TmfFilterMatchesNode.NOT_ATTR);
152 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
153 ((TmfFilterMatchesNode) node).setNot(true);
154 }
155 createEventAspect((TmfFilterAspectNode) node, atts);
156 ((TmfFilterMatchesNode) node).setRegex(atts.getValue(TmfFilterMatchesNode.REGEX_ATTR));
157
158 } else if (localName.equals(TmfFilterCompareNode.NODE_NAME)) {
159
160 node = new TmfFilterCompareNode(null);
161 String value = atts.getValue(TmfFilterCompareNode.NOT_ATTR);
162 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
163 ((TmfFilterCompareNode) node).setNot(true);
164 }
165 createEventAspect((TmfFilterAspectNode) node, atts);
166 value = atts.getValue(TmfFilterCompareNode.TYPE_ATTR);
167 if (value != null) {
168 ((TmfFilterCompareNode) node).setType(Type.valueOf(value));
169 }
170 value = atts.getValue(TmfFilterCompareNode.RESULT_ATTR);
171 if (value != null) {
172 if (value.equals(Integer.toString(-1))) {
173 ((TmfFilterCompareNode) node).setResult(-1);
174 } else if (value.equals(Integer.toString(1))) {
175 ((TmfFilterCompareNode) node).setResult(1);
176 } else {
177 ((TmfFilterCompareNode) node).setResult(0);
178 }
179 }
180 ((TmfFilterCompareNode) node).setValue(atts.getValue(TmfFilterCompareNode.VALUE_ATTR));
181
182 // Backward compatibility with event type filter node
183 } else if (localName.equals(EVENTTYPE_NODE_NAME)) {
184
185 node = new TmfFilterTraceTypeNode(null);
186 String label = atts.getValue(NAME_ATTR);
187 if (label != null) {
188 // Backward compatibility with renamed LTTng Kernel Trace
189 if (label.equals(LTTNG_KERNEL_TRACE)) {
190 label = LINUX_KERNEL_TRACE;
191 }
192
193 String traceTypeId = TmfTraceType.getTraceTypeId(label);
194 TraceTypeHelper helper = TmfTraceType.getTraceType(traceTypeId);
195 if (helper == null) {
196 // Backward compatibility with category-less custom trace types
197 for (TraceTypeHelper h : TmfTraceType.getTraceTypeHelpers()) {
198 if (h.getName().equals(label)) {
199 label = h.getLabel();
200 helper = h;
201 break;
202 }
203 }
204 }
205 if (helper != null) {
206 ((TmfFilterTraceTypeNode) node).setTraceTypeId(helper.getTraceTypeId());
207 ((TmfFilterTraceTypeNode) node).setTraceClass(helper.getTraceClass());
208 }
209 ((TmfFilterTraceTypeNode) node).setName(label);
210 }
211
212 }
213
214 fFilterTreeStack.push(node);
215 }
216
217 @Override
218 public void endElement(String uri, String localName, String qName) throws SAXException {
219 ITmfFilterTreeNode node = fFilterTreeStack.pop();
220
221 if (fFilterTreeStack.isEmpty()) {
222 fRoot = node;
223 } else if (fFilterTreeStack.lastElement() instanceof TmfFilterTreeNode &&
224 node instanceof TmfFilterTreeNode) {
225 fFilterTreeStack.lastElement().addChild(node);
226 }
227
228 }
229
230 private static void createEventAspect(TmfFilterAspectNode node, Attributes atts) {
231 String traceTypeId = atts.getValue(TmfFilterAspectNode.TRACE_TYPE_ID_ATTR);
232 traceTypeId = TmfTraceType.buildCompatibilityTraceTypeId(traceTypeId);
233 String name = atts.getValue(TmfFilterAspectNode.EVENT_ASPECT_ATTR);
234 if (TmfFilterAspectNode.BASE_ASPECT_ID.equals(traceTypeId)) {
235 for (ITmfEventAspect eventAspect : ITmfEventAspect.BASE_ASPECTS) {
236 if (eventAspect.getName().equals(name)) {
237 node.setEventAspect(eventAspect);
238 node.setTraceTypeId(traceTypeId);
239 if (eventAspect instanceof TmfEventFieldAspect) {
240 String field = atts.getValue(TmfFilterAspectNode.FIELD_ATTR);
241 if (field != null && !field.isEmpty()) {
242 node.setEventAspect(((TmfEventFieldAspect) eventAspect).forField(field));
243 }
244 }
245 break;
246 }
247 }
248 } else if (traceTypeId != null && name != null) {
249 TraceTypeHelper helper = TmfTraceType.getTraceType(traceTypeId);
250 if (helper != null) {
251 for (ITmfEventAspect eventAspect : helper.getTrace().getEventAspects()) {
252 if (eventAspect.getName().equals(name)) {
253 node.setEventAspect(eventAspect);
254 node.setTraceTypeId(traceTypeId);
255 if (eventAspect instanceof TmfEventFieldAspect) {
256 String field = atts.getValue(TmfFilterAspectNode.FIELD_ATTR);
257 if (field != null && !field.isEmpty()) {
258 node.setEventAspect(((TmfEventFieldAspect) eventAspect).forField(field));
259 }
260 }
261 break;
262 }
263 }
264 }
265 } else {
266 // Backward compatibility with field-based filters
267 String field = atts.getValue(FIELD_ATTR);
268 if (field != null) {
269 if (field.equals(EVENT_FIELD_TIMESTAMP)) {
270 node.setEventAspect(ITmfEventAspect.BaseAspects.TIMESTAMP);
271 node.setTraceTypeId(TmfFilterAspectNode.BASE_ASPECT_ID);
272 } else if (field.equals(EVENT_FIELD_TYPE)) {
273 node.setEventAspect(ITmfEventAspect.BaseAspects.EVENT_TYPE);
274 node.setTraceTypeId(TmfFilterAspectNode.BASE_ASPECT_ID);
275 } else if (field.equals(EVENT_FIELD_CONTENT)) {
276 node.setEventAspect(ITmfEventAspect.BaseAspects.CONTENTS);
277 node.setTraceTypeId(TmfFilterAspectNode.BASE_ASPECT_ID);
278 } else {
279 node.setEventAspect(ITmfEventAspect.BaseAspects.CONTENTS.forField(field));
280 node.setTraceTypeId(TmfFilterAspectNode.BASE_ASPECT_ID);
281 }
282 }
283 }
284 }
285 }
This page took 0.065917 seconds and 4 git commands to generate.