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