analysis: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / filter / xml / TmfFilterContentHandler.java
CommitLineData
be222f56 1/*******************************************************************************
2b0005f0 2 * Copyright (c) 2010, 2015 Ericsson
11252342 3 *
be222f56
PT
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
11252342 8 *
be222f56
PT
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
2bdf0193 15package org.eclipse.tracecompass.tmf.core.filter.xml;
be222f56
PT
16
17import java.util.Stack;
18
ec34bf48
PT
19import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
20import org.eclipse.tracecompass.tmf.core.event.aspect.TmfEventFieldAspect;
2bdf0193
AM
21import org.eclipse.tracecompass.tmf.core.filter.model.ITmfFilterTreeNode;
22import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterAndNode;
ec34bf48 23import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterAspectNode;
2bdf0193 24import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterCompareNode;
ec34bf48 25import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterCompareNode.Type;
2bdf0193
AM
26import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterContainsNode;
27import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterEqualsNode;
2bdf0193
AM
28import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterMatchesNode;
29import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterNode;
30import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterOrNode;
31import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterRootNode;
2b0005f0 32import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterTraceTypeNode;
2bdf0193 33import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterTreeNode;
2b0005f0
PT
34import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
35import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
be222f56
PT
36import org.xml.sax.Attributes;
37import org.xml.sax.SAXException;
38import org.xml.sax.helpers.DefaultHandler;
39
40/**
41 * The SAX Content Handler
11252342 42 *
be222f56
PT
43 * @version 1.0
44 * @author Yuriy Vashchuk
45 * @author Patrick Tasse
46 */
47public class TmfFilterContentHandler extends DefaultHandler {
11252342 48
2b0005f0
PT
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$
ec34bf48
PT
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$
2b0005f0 58
d5efe032
AF
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
2b0005f0 92 } else if (localName.equals(TmfFilterTraceTypeNode.NODE_NAME)) {
d5efe032 93
2b0005f0
PT
94 node = new TmfFilterTraceTypeNode(null);
95 String traceTypeId = atts.getValue(TmfFilterTraceTypeNode.TYPE_ATTR);
2aebf5f3 96 traceTypeId = TmfTraceType.buildCompatibilityTraceTypeId(traceTypeId);
2b0005f0
PT
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));
d5efe032
AF
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 }
ec34bf48 127 createEventAspect((TmfFilterAspectNode) node, atts);
d5efe032
AF
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 }
ec34bf48 141 createEventAspect((TmfFilterAspectNode) node, atts);
d5efe032
AF
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
ec34bf48 148 } else if (localName.equals(TmfFilterMatchesNode.NODE_NAME)) {
d5efe032 149
ec34bf48 150 node = new TmfFilterMatchesNode(null);
d5efe032
AF
151 String value = atts.getValue(TmfFilterMatchesNode.NOT_ATTR);
152 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
153 ((TmfFilterMatchesNode) node).setNot(true);
154 }
ec34bf48 155 createEventAspect((TmfFilterAspectNode) node, atts);
d5efe032
AF
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 }
ec34bf48 165 createEventAspect((TmfFilterAspectNode) node, atts);
d5efe032
AF
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
2b0005f0
PT
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
d5efe032
AF
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 }
be222f56 229
ec34bf48
PT
230 private static void createEventAspect(TmfFilterAspectNode node, Attributes atts) {
231 String traceTypeId = atts.getValue(TmfFilterAspectNode.TRACE_TYPE_ID_ATTR);
2aebf5f3 232 traceTypeId = TmfTraceType.buildCompatibilityTraceTypeId(traceTypeId);
ec34bf48
PT
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);
40dfafb3
PT
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 }
ec34bf48
PT
245 break;
246 }
247 }
ec34bf48
PT
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);
40dfafb3
PT
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 }
ec34bf48
PT
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 {
40dfafb3
PT
279 node.setEventAspect(ITmfEventAspect.BaseAspects.CONTENTS.forField(field));
280 node.setTraceTypeId(TmfFilterAspectNode.BASE_ASPECT_ID);
ec34bf48
PT
281 }
282 }
283 }
284 }
be222f56 285}
This page took 0.075199 seconds and 5 git commands to generate.