1 /*******************************************************************************
2 * Copyright (c) 2010 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
.linuxtools
.tmf
.core
.filter
.xml
;
17 import java
.util
.Stack
;
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
;
36 * The SAX Content Handler
39 * @author Yuriy Vashchuk
40 * @author Patrick Tasse
42 public class TmfFilterContentHandler
extends DefaultHandler
{
44 private ITmfFilterTreeNode fRoot
= null;
45 private Stack
<ITmfFilterTreeNode
> fFilterTreeStack
= null;
48 * The default constructor
50 public TmfFilterContentHandler() {
52 fFilterTreeStack
= new Stack
<ITmfFilterTreeNode
>();
58 * @return The builded tree
60 public ITmfFilterTreeNode
getTree() {
66 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
69 public void startElement(String uri
, String localName
, String qName
, Attributes atts
) throws SAXException
{
70 ITmfFilterTreeNode node
= null;
72 if (localName
.equalsIgnoreCase(TmfFilterRootNode
.NODE_NAME
)) {
74 node
= new TmfFilterRootNode();
76 } else if (localName
.equals(TmfFilterNode
.NODE_NAME
)) {
78 node
= new TmfFilterNode(atts
.getValue(TmfFilterNode
.NAME_ATTR
));
80 } else if (localName
.equals(TmfFilterEventTypeNode
.NODE_NAME
)) {
82 node
= new TmfFilterEventTypeNode(null);
83 ((TmfFilterEventTypeNode
) node
).setEventType(atts
.getValue(TmfFilterEventTypeNode
.TYPE_ATTR
));
84 ((TmfFilterEventTypeNode
) node
).setName(atts
.getValue(TmfFilterEventTypeNode
.NAME_ATTR
));
86 } else if (localName
.equals(TmfFilterAndNode
.NODE_NAME
)) {
88 node
= new TmfFilterAndNode(null);
89 String value
= atts
.getValue(TmfFilterAndNode
.NOT_ATTR
);
90 if (value
!= null && value
.equalsIgnoreCase(Boolean
.TRUE
.toString())) {
91 ((TmfFilterAndNode
) node
).setNot(true);
94 } else if (localName
.equals(TmfFilterOrNode
.NODE_NAME
)) {
96 node
= new TmfFilterOrNode(null);
97 String value
= atts
.getValue(TmfFilterOrNode
.NOT_ATTR
);
98 if (value
!= null && value
.equalsIgnoreCase(Boolean
.TRUE
.toString())) {
99 ((TmfFilterOrNode
) node
).setNot(true);
102 } else if (localName
.equals(TmfFilterContainsNode
.NODE_NAME
)) {
104 node
= new TmfFilterContainsNode(null);
105 String value
= atts
.getValue(TmfFilterContainsNode
.NOT_ATTR
);
106 if (value
!= null && value
.equalsIgnoreCase(Boolean
.TRUE
.toString())) {
107 ((TmfFilterContainsNode
) node
).setNot(true);
109 ((TmfFilterContainsNode
) node
).setField(atts
.getValue(TmfFilterContainsNode
.FIELD_ATTR
));
110 ((TmfFilterContainsNode
) node
).setValue(atts
.getValue(TmfFilterContainsNode
.VALUE_ATTR
));
111 value
= atts
.getValue(TmfFilterContainsNode
.IGNORECASE_ATTR
);
112 if (value
!= null && value
.equalsIgnoreCase(Boolean
.TRUE
.toString())) {
113 ((TmfFilterContainsNode
) node
).setIgnoreCase(true);
116 } else if (localName
.equals(TmfFilterEqualsNode
.NODE_NAME
)) {
118 node
= new TmfFilterEqualsNode(null);
119 String value
= atts
.getValue(TmfFilterEqualsNode
.NOT_ATTR
);
120 if (value
!= null && value
.equalsIgnoreCase(Boolean
.TRUE
.toString())) {
121 ((TmfFilterEqualsNode
) node
).setNot(true);
123 ((TmfFilterEqualsNode
) node
).setField(atts
.getValue(TmfFilterEqualsNode
.FIELD_ATTR
));
124 ((TmfFilterEqualsNode
) node
).setValue(atts
.getValue(TmfFilterEqualsNode
.VALUE_ATTR
));
125 value
= atts
.getValue(TmfFilterEqualsNode
.IGNORECASE_ATTR
);
126 if (value
!= null && value
.equalsIgnoreCase(Boolean
.TRUE
.toString())) {
127 ((TmfFilterEqualsNode
) node
).setIgnoreCase(true);
130 } else if (localName
.equals(TmfFilterMatchesNode
.NODE_NAME
)) {
132 node
= new TmfFilterMatchesNode(null);
133 String value
= atts
.getValue(TmfFilterMatchesNode
.NOT_ATTR
);
134 if (value
!= null && value
.equalsIgnoreCase(Boolean
.TRUE
.toString())) {
135 ((TmfFilterMatchesNode
) node
).setNot(true);
137 ((TmfFilterMatchesNode
) node
).setField(atts
.getValue(TmfFilterMatchesNode
.FIELD_ATTR
));
138 ((TmfFilterMatchesNode
) node
).setRegex(atts
.getValue(TmfFilterMatchesNode
.REGEX_ATTR
));
140 } else if (localName
.equals(TmfFilterCompareNode
.NODE_NAME
)) {
142 node
= new TmfFilterCompareNode(null);
143 String value
= atts
.getValue(TmfFilterCompareNode
.NOT_ATTR
);
144 if (value
!= null && value
.equalsIgnoreCase(Boolean
.TRUE
.toString())) {
145 ((TmfFilterCompareNode
) node
).setNot(true);
147 ((TmfFilterCompareNode
) node
).setField(atts
.getValue(TmfFilterCompareNode
.FIELD_ATTR
));
148 value
= atts
.getValue(TmfFilterCompareNode
.TYPE_ATTR
);
150 ((TmfFilterCompareNode
) node
).setType(Type
.valueOf(value
));
152 value
= atts
.getValue(TmfFilterCompareNode
.RESULT_ATTR
);
154 if (value
.equals(Integer
.toString(-1))) {
155 ((TmfFilterCompareNode
) node
).setResult(-1);
156 } else if (value
.equals(Integer
.toString(1))) {
157 ((TmfFilterCompareNode
) node
).setResult(1);
159 ((TmfFilterCompareNode
) node
).setResult(0);
162 ((TmfFilterCompareNode
) node
).setValue(atts
.getValue(TmfFilterCompareNode
.VALUE_ATTR
));
166 fFilterTreeStack
.push(node
);
171 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
174 public void endElement(String uri
, String localName
, String qName
) throws SAXException
{
175 ITmfFilterTreeNode node
= fFilterTreeStack
.pop();
177 if (fFilterTreeStack
.isEmpty()) {
179 } else if (fFilterTreeStack
.lastElement() instanceof TmfFilterTreeNode
&&
180 node
instanceof TmfFilterTreeNode
) {
181 fFilterTreeStack
.lastElement().addChild(node
);
This page took 0.045527 seconds and 5 git commands to generate.