Copyright header update, 2015 edition
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / filter / xml / TmfFilterContentHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 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.filter.model.ITmfFilterTreeNode;
20 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterAndNode;
21 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterCompareNode;
22 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterContainsNode;
23 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterEqualsNode;
24 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterEventTypeNode;
25 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterMatchesFieldNode;
26 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterMatchesNode;
27 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterNode;
28 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterOrNode;
29 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterRootNode;
30 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterTreeNode;
31 import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterCompareNode.Type;
32 import org.xml.sax.Attributes;
33 import org.xml.sax.SAXException;
34 import org.xml.sax.helpers.DefaultHandler;
35
36 /**
37 * The SAX Content Handler
38 *
39 * @version 1.0
40 * @author Yuriy Vashchuk
41 * @author Patrick Tasse
42 */
43 public class TmfFilterContentHandler extends DefaultHandler {
44
45 private ITmfFilterTreeNode fRoot = null;
46 private Stack<ITmfFilterTreeNode> fFilterTreeStack = null;
47
48 /**
49 * The default constructor
50 */
51 public TmfFilterContentHandler() {
52 super();
53 fFilterTreeStack = new Stack<>();
54 }
55
56 /**
57 * Getter of tree
58 *
59 * @return The builded tree
60 */
61 public ITmfFilterTreeNode getTree() {
62 return fRoot;
63 }
64
65
66 @Override
67 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
68 ITmfFilterTreeNode node = null;
69
70 if (localName.equalsIgnoreCase(TmfFilterRootNode.NODE_NAME)) {
71
72 node = new TmfFilterRootNode();
73
74 } else if (localName.equals(TmfFilterNode.NODE_NAME)) {
75
76 node = new TmfFilterNode(atts.getValue(TmfFilterNode.NAME_ATTR));
77
78 } else if (localName.equals(TmfFilterEventTypeNode.NODE_NAME)) {
79
80 node = new TmfFilterEventTypeNode(null);
81 ((TmfFilterEventTypeNode) node).setEventType(atts.getValue(TmfFilterEventTypeNode.TYPE_ATTR));
82 ((TmfFilterEventTypeNode) node).setName(atts.getValue(TmfFilterEventTypeNode.NAME_ATTR));
83
84 } else if (localName.equals(TmfFilterAndNode.NODE_NAME)) {
85
86 node = new TmfFilterAndNode(null);
87 String value = atts.getValue(TmfFilterAndNode.NOT_ATTR);
88 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
89 ((TmfFilterAndNode) node).setNot(true);
90 }
91
92 } else if (localName.equals(TmfFilterOrNode.NODE_NAME)) {
93
94 node = new TmfFilterOrNode(null);
95 String value = atts.getValue(TmfFilterOrNode.NOT_ATTR);
96 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
97 ((TmfFilterOrNode) node).setNot(true);
98 }
99
100 } else if (localName.equals(TmfFilterContainsNode.NODE_NAME)) {
101
102 node = new TmfFilterContainsNode(null);
103 String value = atts.getValue(TmfFilterContainsNode.NOT_ATTR);
104 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
105 ((TmfFilterContainsNode) node).setNot(true);
106 }
107 ((TmfFilterContainsNode) node).setField(atts.getValue(TmfFilterContainsNode.FIELD_ATTR));
108 ((TmfFilterContainsNode) node).setValue(atts.getValue(TmfFilterContainsNode.VALUE_ATTR));
109 value = atts.getValue(TmfFilterContainsNode.IGNORECASE_ATTR);
110 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
111 ((TmfFilterContainsNode) node).setIgnoreCase(true);
112 }
113
114 } else if (localName.equals(TmfFilterEqualsNode.NODE_NAME)) {
115
116 node = new TmfFilterEqualsNode(null);
117 String value = atts.getValue(TmfFilterEqualsNode.NOT_ATTR);
118 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
119 ((TmfFilterEqualsNode) node).setNot(true);
120 }
121 ((TmfFilterEqualsNode) node).setField(atts.getValue(TmfFilterEqualsNode.FIELD_ATTR));
122 ((TmfFilterEqualsNode) node).setValue(atts.getValue(TmfFilterEqualsNode.VALUE_ATTR));
123 value = atts.getValue(TmfFilterEqualsNode.IGNORECASE_ATTR);
124 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
125 ((TmfFilterEqualsNode) node).setIgnoreCase(true);
126 }
127
128 } else if (localName.equals(TmfFilterMatchesFieldNode.NODE_NAME)) {
129
130 node = new TmfFilterMatchesFieldNode(null);
131 String value = atts.getValue(TmfFilterMatchesNode.NOT_ATTR);
132 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
133 ((TmfFilterMatchesNode) node).setNot(true);
134 }
135 ((TmfFilterMatchesFieldNode) node).setField(atts.getValue(TmfFilterMatchesFieldNode.FIELD_ATTR));
136 ((TmfFilterMatchesNode) node).setRegex(atts.getValue(TmfFilterMatchesNode.REGEX_ATTR));
137
138 } else if (localName.equals(TmfFilterCompareNode.NODE_NAME)) {
139
140 node = new TmfFilterCompareNode(null);
141 String value = atts.getValue(TmfFilterCompareNode.NOT_ATTR);
142 if (value != null && value.equalsIgnoreCase(Boolean.TRUE.toString())) {
143 ((TmfFilterCompareNode) node).setNot(true);
144 }
145 ((TmfFilterCompareNode) node).setField(atts.getValue(TmfFilterCompareNode.FIELD_ATTR));
146 value = atts.getValue(TmfFilterCompareNode.TYPE_ATTR);
147 if (value != null) {
148 ((TmfFilterCompareNode) node).setType(Type.valueOf(value));
149 }
150 value = atts.getValue(TmfFilterCompareNode.RESULT_ATTR);
151 if (value != null) {
152 if (value.equals(Integer.toString(-1))) {
153 ((TmfFilterCompareNode) node).setResult(-1);
154 } else if (value.equals(Integer.toString(1))) {
155 ((TmfFilterCompareNode) node).setResult(1);
156 } else {
157 ((TmfFilterCompareNode) node).setResult(0);
158 }
159 }
160 ((TmfFilterCompareNode) node).setValue(atts.getValue(TmfFilterCompareNode.VALUE_ATTR));
161
162 }
163
164 fFilterTreeStack.push(node);
165 }
166
167 @Override
168 public void endElement(String uri, String localName, String qName) throws SAXException {
169 ITmfFilterTreeNode node = fFilterTreeStack.pop();
170
171 if (fFilterTreeStack.isEmpty()) {
172 fRoot = node;
173 } else if (fFilterTreeStack.lastElement() instanceof TmfFilterTreeNode &&
174 node instanceof TmfFilterTreeNode) {
175 fFilterTreeStack.lastElement().addChild(node);
176 }
177
178 }
179
180 }
This page took 0.060821 seconds and 6 git commands to generate.