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
.filter
.xml
;
17 import java
.util
.Stack
;
19 import org
.eclipse
.linuxtools
.tmf
.filter
.model
.ITmfFilterTreeNode
;
20 import org
.eclipse
.linuxtools
.tmf
.filter
.model
.TmfFilterAndNode
;
21 import org
.eclipse
.linuxtools
.tmf
.filter
.model
.TmfFilterCompareNode
;
22 import org
.eclipse
.linuxtools
.tmf
.filter
.model
.TmfFilterCompareNode
.Type
;
23 import org
.eclipse
.linuxtools
.tmf
.filter
.model
.TmfFilterContainsNode
;
24 import org
.eclipse
.linuxtools
.tmf
.filter
.model
.TmfFilterEqualsNode
;
25 import org
.eclipse
.linuxtools
.tmf
.filter
.model
.TmfFilterEventTypeNode
;
26 import org
.eclipse
.linuxtools
.tmf
.filter
.model
.TmfFilterMatchesNode
;
27 import org
.eclipse
.linuxtools
.tmf
.filter
.model
.TmfFilterNode
;
28 import org
.eclipse
.linuxtools
.tmf
.filter
.model
.TmfFilterOrNode
;
29 import org
.eclipse
.linuxtools
.tmf
.filter
.model
.TmfFilterRootNode
;
30 import org
.eclipse
.linuxtools
.tmf
.filter
.model
.TmfFilterTreeNode
;
31 import org
.xml
.sax
.Attributes
;
32 import org
.xml
.sax
.SAXException
;
33 import org
.xml
.sax
.helpers
.DefaultHandler
;
36 * <b><u>FilterContentHandler</u></b>
38 * The Filter SAX Content Handler
41 public class TmfFilterContentHandler
extends DefaultHandler
{
43 private ITmfFilterTreeNode fRoot
= null;
44 private Stack
<ITmfFilterTreeNode
> fFilterTreeStack
= null;
47 * The default constructor
49 public TmfFilterContentHandler() {
51 fFilterTreeStack
= new Stack
<ITmfFilterTreeNode
>();
57 * @return The builded tree
59 public ITmfFilterTreeNode
getTree() {
65 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
68 public void startElement(String uri
, String localName
, String qName
, Attributes atts
) throws SAXException
{
69 ITmfFilterTreeNode node
= null;
71 if (localName
.equalsIgnoreCase(TmfFilterRootNode
.NODE_NAME
)) {
73 node
= new TmfFilterRootNode();
75 } else if (localName
.equals(TmfFilterNode
.NODE_NAME
)) {
77 node
= new TmfFilterNode(atts
.getValue(TmfFilterNode
.NAME_ATTR
));
79 } else if (localName
.equals(TmfFilterEventTypeNode
.NODE_NAME
)) {
81 node
= new TmfFilterEventTypeNode(null);
82 ((TmfFilterEventTypeNode
) node
).setEventType(atts
.getValue(TmfFilterEventTypeNode
.TYPE_ATTR
));
83 ((TmfFilterEventTypeNode
) node
).setName(atts
.getValue(TmfFilterEventTypeNode
.NAME_ATTR
));
85 } else if (localName
.equals(TmfFilterAndNode
.NODE_NAME
)) {
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);
93 } else if (localName
.equals(TmfFilterOrNode
.NODE_NAME
)) {
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);
101 } else if (localName
.equals(TmfFilterContainsNode
.NODE_NAME
)) {
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);
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);
115 } else if (localName
.equals(TmfFilterEqualsNode
.NODE_NAME
)) {
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);
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);
129 } else if (localName
.equals(TmfFilterMatchesNode
.NODE_NAME
)) {
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);
136 ((TmfFilterMatchesNode
) node
).setField(atts
.getValue(TmfFilterMatchesNode
.FIELD_ATTR
));
137 ((TmfFilterMatchesNode
) node
).setRegex(atts
.getValue(TmfFilterMatchesNode
.REGEX_ATTR
));
139 } else if (localName
.equals(TmfFilterCompareNode
.NODE_NAME
)) {
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);
146 ((TmfFilterCompareNode
) node
).setField(atts
.getValue(TmfFilterCompareNode
.FIELD_ATTR
));
147 value
= atts
.getValue(TmfFilterCompareNode
.TYPE_ATTR
);
149 ((TmfFilterCompareNode
) node
).setType(Type
.valueOf(value
));
151 value
= atts
.getValue(TmfFilterCompareNode
.RESULT_ATTR
);
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);
158 ((TmfFilterCompareNode
) node
).setResult(0);
161 ((TmfFilterCompareNode
) node
).setValue(atts
.getValue(TmfFilterCompareNode
.VALUE_ATTR
));
165 fFilterTreeStack
.push(node
);
170 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
173 public void endElement(String uri
, String localName
, String qName
) throws SAXException
{
174 ITmfFilterTreeNode node
= fFilterTreeStack
.pop();
176 if (fFilterTreeStack
.isEmpty()) {
178 } else if (fFilterTreeStack
.lastElement() instanceof TmfFilterTreeNode
&&
179 node
instanceof TmfFilterTreeNode
) {
180 fFilterTreeStack
.lastElement().addChild(node
);
This page took 0.034875 seconds and 5 git commands to generate.