tmf: Support aspect-based filtering in FilterViewer
[deliverable/tracecompass.git] / 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 ((TmfFilterTraceTypeNode) node).setTraceTypeId(traceTypeId);
97 TraceTypeHelper helper = TmfTraceType.getTraceType(traceTypeId);
98 if (helper != null) {
99 ((TmfFilterTraceTypeNode) node).setTraceClass(helper.getTraceClass());
100 }
101 ((TmfFilterTraceTypeNode) node).setName(atts.getValue(TmfFilterTraceTypeNode.NAME_ATTR));
102
103 } else if (localName.equals(TmfFilterAndNode.NODE_NAME)) {
104
105 node = new TmfFilterAndNode(null);
106 String value = atts.getValue(TmfFilterAndNode.NOT_ATTR);
107 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
108 ((TmfFilterAndNode) node).setNot(true);
109 }
110
111 } else if (localName.equals(TmfFilterOrNode.NODE_NAME)) {
112
113 node = new TmfFilterOrNode(null);
114 String value = atts.getValue(TmfFilterOrNode.NOT_ATTR);
115 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
116 ((TmfFilterOrNode) node).setNot(true);
117 }
118
119 } else if (localName.equals(TmfFilterContainsNode.NODE_NAME)) {
120
121 node = new TmfFilterContainsNode(null);
122 String value = atts.getValue(TmfFilterContainsNode.NOT_ATTR);
123 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
124 ((TmfFilterContainsNode) node).setNot(true);
125 }
126 createEventAspect((TmfFilterAspectNode) node, atts);
127 ((TmfFilterContainsNode) node).setValue(atts.getValue(TmfFilterContainsNode.VALUE_ATTR));
128 value = atts.getValue(TmfFilterContainsNode.IGNORECASE_ATTR);
129 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
130 ((TmfFilterContainsNode) node).setIgnoreCase(true);
131 }
132
133 } else if (localName.equals(TmfFilterEqualsNode.NODE_NAME)) {
134
135 node = new TmfFilterEqualsNode(null);
136 String value = atts.getValue(TmfFilterEqualsNode.NOT_ATTR);
137 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
138 ((TmfFilterEqualsNode) node).setNot(true);
139 }
140 createEventAspect((TmfFilterAspectNode) node, atts);
141 ((TmfFilterEqualsNode) node).setValue(atts.getValue(TmfFilterEqualsNode.VALUE_ATTR));
142 value = atts.getValue(TmfFilterEqualsNode.IGNORECASE_ATTR);
143 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
144 ((TmfFilterEqualsNode) node).setIgnoreCase(true);
145 }
146
147 } else if (localName.equals(TmfFilterMatchesNode.NODE_NAME)) {
148
149 node = new TmfFilterMatchesNode(null);
150 String value = atts.getValue(TmfFilterMatchesNode.NOT_ATTR);
151 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
152 ((TmfFilterMatchesNode) node).setNot(true);
153 }
154 createEventAspect((TmfFilterAspectNode) node, atts);
155 ((TmfFilterMatchesNode) node).setRegex(atts.getValue(TmfFilterMatchesNode.REGEX_ATTR));
156
157 } else if (localName.equals(TmfFilterCompareNode.NODE_NAME)) {
158
159 node = new TmfFilterCompareNode(null);
160 String value = atts.getValue(TmfFilterCompareNode.NOT_ATTR);
161 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
162 ((TmfFilterCompareNode) node).setNot(true);
163 }
164 createEventAspect((TmfFilterAspectNode) node, atts);
165 value = atts.getValue(TmfFilterCompareNode.TYPE_ATTR);
166 if (value != null) {
167 ((TmfFilterCompareNode) node).setType(Type.valueOf(value));
168 }
169 value = atts.getValue(TmfFilterCompareNode.RESULT_ATTR);
170 if (value != null) {
171 if (value.equals(Integer.toString(-1))) {
172 ((TmfFilterCompareNode) node).setResult(-1);
173 } else if (value.equals(Integer.toString(1))) {
174 ((TmfFilterCompareNode) node).setResult(1);
175 } else {
176 ((TmfFilterCompareNode) node).setResult(0);
177 }
178 }
179 ((TmfFilterCompareNode) node).setValue(atts.getValue(TmfFilterCompareNode.VALUE_ATTR));
180
181 // Backward compatibility with event type filter node
182 } else if (localName.equals(EVENTTYPE_NODE_NAME)) {
183
184 node = new TmfFilterTraceTypeNode(null);
185 String label = atts.getValue(NAME_ATTR);
186 if (label != null) {
187 // Backward compatibility with renamed LTTng Kernel Trace
188 if (label.equals(LTTNG_KERNEL_TRACE)) {
189 label = LINUX_KERNEL_TRACE;
190 }
191
192 String traceTypeId = TmfTraceType.getTraceTypeId(label);
193 TraceTypeHelper helper = TmfTraceType.getTraceType(traceTypeId);
194 if (helper == null) {
195 // Backward compatibility with category-less custom trace types
196 for (TraceTypeHelper h : TmfTraceType.getTraceTypeHelpers()) {
197 if (h.getName().equals(label)) {
198 label = h.getLabel();
199 helper = h;
200 break;
201 }
202 }
203 }
204 if (helper != null) {
205 ((TmfFilterTraceTypeNode) node).setTraceTypeId(helper.getTraceTypeId());
206 ((TmfFilterTraceTypeNode) node).setTraceClass(helper.getTraceClass());
207 }
208 ((TmfFilterTraceTypeNode) node).setName(label);
209 }
210
211 }
212
213 fFilterTreeStack.push(node);
214 }
215
216 @Override
217 public void endElement(String uri, String localName, String qName) throws SAXException {
218 ITmfFilterTreeNode node = fFilterTreeStack.pop();
219
220 if (fFilterTreeStack.isEmpty()) {
221 fRoot = node;
222 } else if (fFilterTreeStack.lastElement() instanceof TmfFilterTreeNode &&
223 node instanceof TmfFilterTreeNode) {
224 fFilterTreeStack.lastElement().addChild(node);
225 }
226
227 }
228
229 private static void createEventAspect(TmfFilterAspectNode node, Attributes atts) {
230 String traceTypeId = atts.getValue(TmfFilterAspectNode.TRACE_TYPE_ID_ATTR);
231 String name = atts.getValue(TmfFilterAspectNode.EVENT_ASPECT_ATTR);
232 if (TmfFilterAspectNode.BASE_ASPECT_ID.equals(traceTypeId)) {
233 for (ITmfEventAspect eventAspect : ITmfEventAspect.BASE_ASPECTS) {
234 if (eventAspect.getName().equals(name)) {
235 node.setEventAspect(eventAspect);
236 node.setTraceTypeId(traceTypeId);
237 break;
238 }
239 }
240 } else if (TmfFilterAspectNode.EVENT_FIELD_ASPECT_ID.equals(traceTypeId) && name != null) {
241 ITmfEventAspect eventAspect = new TmfEventFieldAspect(name, name);
242 node.setEventAspect(eventAspect);
243 node.setTraceTypeId(traceTypeId);
244 } else if (traceTypeId != null && name != null) {
245 TraceTypeHelper helper = TmfTraceType.getTraceType(traceTypeId);
246 if (helper != null) {
247 for (ITmfEventAspect eventAspect : helper.getTrace().getEventAspects()) {
248 if (eventAspect.getName().equals(name)) {
249 node.setEventAspect(eventAspect);
250 node.setTraceTypeId(traceTypeId);
251 break;
252 }
253 }
254 }
255 } else {
256 // Backward compatibility with field-based filters
257 String field = atts.getValue(FIELD_ATTR);
258 if (field != null) {
259 if (field.equals(EVENT_FIELD_TIMESTAMP)) {
260 node.setEventAspect(ITmfEventAspect.BaseAspects.TIMESTAMP);
261 node.setTraceTypeId(TmfFilterAspectNode.BASE_ASPECT_ID);
262 } else if (field.equals(EVENT_FIELD_TYPE)) {
263 node.setEventAspect(ITmfEventAspect.BaseAspects.EVENT_TYPE);
264 node.setTraceTypeId(TmfFilterAspectNode.BASE_ASPECT_ID);
265 } else if (field.equals(EVENT_FIELD_CONTENT)) {
266 node.setEventAspect(ITmfEventAspect.BaseAspects.CONTENTS);
267 node.setTraceTypeId(TmfFilterAspectNode.BASE_ASPECT_ID);
268 } else {
269 ITmfEventAspect eventAspect = new TmfEventFieldAspect(field, field);
270 node.setEventAspect(eventAspect);
271 node.setTraceTypeId(TmfFilterAspectNode.EVENT_FIELD_ASPECT_ID);
272 }
273 }
274 }
275 }
276 }
This page took 0.056592 seconds and 6 git commands to generate.