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 ((TmfFilterTraceTypeNode
) node
).setTraceTypeId(traceTypeId
);
97 TraceTypeHelper helper
= TmfTraceType
.getTraceType(traceTypeId
);
99 ((TmfFilterTraceTypeNode
) node
).setTraceClass(helper
.getTraceClass());
101 ((TmfFilterTraceTypeNode
) node
).setName(atts
.getValue(TmfFilterTraceTypeNode
.NAME_ATTR
));
103 } else if (localName
.equals(TmfFilterAndNode
.NODE_NAME
)) {
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);
111 } else if (localName
.equals(TmfFilterOrNode
.NODE_NAME
)) {
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);
119 } else if (localName
.equals(TmfFilterContainsNode
.NODE_NAME
)) {
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);
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);
133 } else if (localName
.equals(TmfFilterEqualsNode
.NODE_NAME
)) {
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);
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);
147 } else if (localName
.equals(TmfFilterMatchesNode
.NODE_NAME
)) {
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);
154 createEventAspect((TmfFilterAspectNode
) node
, atts
);
155 ((TmfFilterMatchesNode
) node
).setRegex(atts
.getValue(TmfFilterMatchesNode
.REGEX_ATTR
));
157 } else if (localName
.equals(TmfFilterCompareNode
.NODE_NAME
)) {
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);
164 createEventAspect((TmfFilterAspectNode
) node
, atts
);
165 value
= atts
.getValue(TmfFilterCompareNode
.TYPE_ATTR
);
167 ((TmfFilterCompareNode
) node
).setType(Type
.valueOf(value
));
169 value
= atts
.getValue(TmfFilterCompareNode
.RESULT_ATTR
);
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);
176 ((TmfFilterCompareNode
) node
).setResult(0);
179 ((TmfFilterCompareNode
) node
).setValue(atts
.getValue(TmfFilterCompareNode
.VALUE_ATTR
));
181 // Backward compatibility with event type filter node
182 } else if (localName
.equals(EVENTTYPE_NODE_NAME
)) {
184 node
= new TmfFilterTraceTypeNode(null);
185 String label
= atts
.getValue(NAME_ATTR
);
187 // Backward compatibility with renamed LTTng Kernel Trace
188 if (label
.equals(LTTNG_KERNEL_TRACE
)) {
189 label
= LINUX_KERNEL_TRACE
;
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();
204 if (helper
!= null) {
205 ((TmfFilterTraceTypeNode
) node
).setTraceTypeId(helper
.getTraceTypeId());
206 ((TmfFilterTraceTypeNode
) node
).setTraceClass(helper
.getTraceClass());
208 ((TmfFilterTraceTypeNode
) node
).setName(label
);
213 fFilterTreeStack
.push(node
);
217 public void endElement(String uri
, String localName
, String qName
) throws SAXException
{
218 ITmfFilterTreeNode node
= fFilterTreeStack
.pop();
220 if (fFilterTreeStack
.isEmpty()) {
222 } else if (fFilterTreeStack
.lastElement() instanceof TmfFilterTreeNode
&&
223 node
instanceof TmfFilterTreeNode
) {
224 fFilterTreeStack
.lastElement().addChild(node
);
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
);
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
);
256 // Backward compatibility with field-based filters
257 String field
= atts
.getValue(FIELD_ATTR
);
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
);
269 ITmfEventAspect eventAspect
= new TmfEventFieldAspect(field
, field
);
270 node
.setEventAspect(eventAspect
);
271 node
.setTraceTypeId(TmfFilterAspectNode
.EVENT_FIELD_ASPECT_ID
);