1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 Ericsson
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
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 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.core
.filter
.xml
;
17 import java
.util
.Stack
;
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
;
41 * The SAX Content Handler
44 * @author Yuriy Vashchuk
45 * @author Patrick Tasse
47 public class TmfFilterContentHandler
extends DefaultHandler
{
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$
59 private ITmfFilterTreeNode fRoot
= null;
60 private Stack
<ITmfFilterTreeNode
> fFilterTreeStack
= null;
63 * The default constructor
65 public TmfFilterContentHandler() {
67 fFilterTreeStack
= new Stack
<>();
73 * @return The builded tree
75 public ITmfFilterTreeNode
getTree() {
81 public void startElement(String uri
, String localName
, String qName
, Attributes atts
) throws SAXException
{
82 ITmfFilterTreeNode node
= null;
84 if (localName
.equalsIgnoreCase(TmfFilterRootNode
.NODE_NAME
)) {
86 node
= new TmfFilterRootNode();
88 } else if (localName
.equals(TmfFilterNode
.NODE_NAME
)) {
90 node
= new TmfFilterNode(atts
.getValue(TmfFilterNode
.NAME_ATTR
));
92 } else if (localName
.equals(TmfFilterTraceTypeNode
.NODE_NAME
)) {
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
);
100 ((TmfFilterTraceTypeNode
) node
).setTraceClass(helper
.getTraceClass());
102 ((TmfFilterTraceTypeNode
) node
).setName(atts
.getValue(TmfFilterTraceTypeNode
.NAME_ATTR
));
104 } else if (localName
.equals(TmfFilterAndNode
.NODE_NAME
)) {
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);
112 } else if (localName
.equals(TmfFilterOrNode
.NODE_NAME
)) {
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);
120 } else if (localName
.equals(TmfFilterContainsNode
.NODE_NAME
)) {
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);
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);
134 } else if (localName
.equals(TmfFilterEqualsNode
.NODE_NAME
)) {
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);
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);
148 } else if (localName
.equals(TmfFilterMatchesNode
.NODE_NAME
)) {
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);
155 createEventAspect((TmfFilterAspectNode
) node
, atts
);
156 ((TmfFilterMatchesNode
) node
).setRegex(atts
.getValue(TmfFilterMatchesNode
.REGEX_ATTR
));
158 } else if (localName
.equals(TmfFilterCompareNode
.NODE_NAME
)) {
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);
165 createEventAspect((TmfFilterAspectNode
) node
, atts
);
166 value
= atts
.getValue(TmfFilterCompareNode
.TYPE_ATTR
);
168 ((TmfFilterCompareNode
) node
).setType(Type
.valueOf(value
));
170 value
= atts
.getValue(TmfFilterCompareNode
.RESULT_ATTR
);
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);
177 ((TmfFilterCompareNode
) node
).setResult(0);
180 ((TmfFilterCompareNode
) node
).setValue(atts
.getValue(TmfFilterCompareNode
.VALUE_ATTR
));
182 // Backward compatibility with event type filter node
183 } else if (localName
.equals(EVENTTYPE_NODE_NAME
)) {
185 node
= new TmfFilterTraceTypeNode(null);
186 String label
= atts
.getValue(NAME_ATTR
);
188 // Backward compatibility with renamed LTTng Kernel Trace
189 if (label
.equals(LTTNG_KERNEL_TRACE
)) {
190 label
= LINUX_KERNEL_TRACE
;
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();
205 if (helper
!= null) {
206 ((TmfFilterTraceTypeNode
) node
).setTraceTypeId(helper
.getTraceTypeId());
207 ((TmfFilterTraceTypeNode
) node
).setTraceClass(helper
.getTraceClass());
209 ((TmfFilterTraceTypeNode
) node
).setName(label
);
214 fFilterTreeStack
.push(node
);
218 public void endElement(String uri
, String localName
, String qName
) throws SAXException
{
219 ITmfFilterTreeNode node
= fFilterTreeStack
.pop();
221 if (fFilterTreeStack
.isEmpty()) {
223 } else if (fFilterTreeStack
.lastElement() instanceof TmfFilterTreeNode
&&
224 node
instanceof TmfFilterTreeNode
) {
225 fFilterTreeStack
.lastElement().addChild(node
);
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
));
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
));
266 // Backward compatibility with field-based filters
267 String field
= atts
.getValue(FIELD_ATTR
);
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
);
279 node
.setEventAspect(ITmfEventAspect
.BaseAspects
.CONTENTS
.forField(field
));
280 node
.setTraceTypeId(TmfFilterAspectNode
.BASE_ASPECT_ID
);