Commit | Line | Data |
---|---|---|
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 | 15 | package org.eclipse.tracecompass.tmf.core.filter.xml; |
be222f56 PT |
16 | |
17 | import java.util.Stack; | |
18 | ||
ec34bf48 PT |
19 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; |
20 | import org.eclipse.tracecompass.tmf.core.event.aspect.TmfEventFieldAspect; | |
2bdf0193 AM |
21 | import org.eclipse.tracecompass.tmf.core.filter.model.ITmfFilterTreeNode; |
22 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterAndNode; | |
ec34bf48 | 23 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterAspectNode; |
2bdf0193 | 24 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterCompareNode; |
ec34bf48 | 25 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterCompareNode.Type; |
2bdf0193 AM |
26 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterContainsNode; |
27 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterEqualsNode; | |
2bdf0193 AM |
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; | |
2b0005f0 | 32 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterTraceTypeNode; |
2bdf0193 | 33 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterTreeNode; |
2b0005f0 PT |
34 | import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType; |
35 | import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper; | |
be222f56 PT |
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 | |
11252342 | 42 | * |
be222f56 PT |
43 | * @version 1.0 |
44 | * @author Yuriy Vashchuk | |
45 | * @author Patrick Tasse | |
46 | */ | |
47 | public 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); | |
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)); | |
d5efe032 AF |
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 | } | |
ec34bf48 | 126 | createEventAspect((TmfFilterAspectNode) node, atts); |
d5efe032 AF |
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 | } | |
ec34bf48 | 140 | createEventAspect((TmfFilterAspectNode) node, atts); |
d5efe032 AF |
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 | ||
ec34bf48 | 147 | } else if (localName.equals(TmfFilterMatchesNode.NODE_NAME)) { |
d5efe032 | 148 | |
ec34bf48 | 149 | node = new TmfFilterMatchesNode(null); |
d5efe032 AF |
150 | String value = atts.getValue(TmfFilterMatchesNode.NOT_ATTR); |
151 | if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) { | |
152 | ((TmfFilterMatchesNode) node).setNot(true); | |
153 | } | |
ec34bf48 | 154 | createEventAspect((TmfFilterAspectNode) node, atts); |
d5efe032 AF |
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 | } | |
ec34bf48 | 164 | createEventAspect((TmfFilterAspectNode) node, atts); |
d5efe032 AF |
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 | ||
2b0005f0 PT |
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 | ||
d5efe032 AF |
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 | } | |
be222f56 | 228 | |
ec34bf48 PT |
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); | |
40dfafb3 PT |
237 | if (eventAspect instanceof TmfEventFieldAspect) { |
238 | String field = atts.getValue(TmfFilterAspectNode.FIELD_ATTR); | |
239 | if (field != null && !field.isEmpty()) { | |
240 | node.setEventAspect(((TmfEventFieldAspect) eventAspect).forField(field)); | |
241 | } | |
242 | } | |
ec34bf48 PT |
243 | break; |
244 | } | |
245 | } | |
ec34bf48 PT |
246 | } else if (traceTypeId != null && name != null) { |
247 | TraceTypeHelper helper = TmfTraceType.getTraceType(traceTypeId); | |
248 | if (helper != null) { | |
249 | for (ITmfEventAspect eventAspect : helper.getTrace().getEventAspects()) { | |
250 | if (eventAspect.getName().equals(name)) { | |
251 | node.setEventAspect(eventAspect); | |
252 | node.setTraceTypeId(traceTypeId); | |
40dfafb3 PT |
253 | if (eventAspect instanceof TmfEventFieldAspect) { |
254 | String field = atts.getValue(TmfFilterAspectNode.FIELD_ATTR); | |
255 | if (field != null && !field.isEmpty()) { | |
256 | node.setEventAspect(((TmfEventFieldAspect) eventAspect).forField(field)); | |
257 | } | |
258 | } | |
ec34bf48 PT |
259 | break; |
260 | } | |
261 | } | |
262 | } | |
263 | } else { | |
264 | // Backward compatibility with field-based filters | |
265 | String field = atts.getValue(FIELD_ATTR); | |
266 | if (field != null) { | |
267 | if (field.equals(EVENT_FIELD_TIMESTAMP)) { | |
268 | node.setEventAspect(ITmfEventAspect.BaseAspects.TIMESTAMP); | |
269 | node.setTraceTypeId(TmfFilterAspectNode.BASE_ASPECT_ID); | |
270 | } else if (field.equals(EVENT_FIELD_TYPE)) { | |
271 | node.setEventAspect(ITmfEventAspect.BaseAspects.EVENT_TYPE); | |
272 | node.setTraceTypeId(TmfFilterAspectNode.BASE_ASPECT_ID); | |
273 | } else if (field.equals(EVENT_FIELD_CONTENT)) { | |
274 | node.setEventAspect(ITmfEventAspect.BaseAspects.CONTENTS); | |
275 | node.setTraceTypeId(TmfFilterAspectNode.BASE_ASPECT_ID); | |
276 | } else { | |
40dfafb3 PT |
277 | node.setEventAspect(ITmfEventAspect.BaseAspects.CONTENTS.forField(field)); |
278 | node.setTraceTypeId(TmfFilterAspectNode.BASE_ASPECT_ID); | |
ec34bf48 PT |
279 | } |
280 | } | |
281 | } | |
282 | } | |
be222f56 | 283 | } |