KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > configuration > XmlElement


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.configuration;
17
18 import org.w3c.dom.Document JavaDoc;
19 import org.w3c.dom.Element JavaDoc;
20 import org.w3c.dom.Node JavaDoc;
21 import org.w3c.dom.NodeList JavaDoc;
22 import scriptella.expression.PropertiesSubstitutor;
23
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28
29
30 /**
31  * Represents XML element
32  *
33  * @author Fyodor Kupolov
34  * @version 1.0
35  */

36 public class XmlElement {
37     private Element JavaDoc element;
38     private URL JavaDoc documentUrl;
39     private PropertiesSubstitutor substitutor;
40
41     public XmlElement(Element JavaDoc element, URL JavaDoc documentUrl, PropertiesSubstitutor substitutor) {
42         this.element = element;
43         this.documentUrl = documentUrl;
44         this.substitutor = substitutor;
45     }
46
47     public XmlElement(Element JavaDoc element, XmlElement parent) {
48         this(element, parent.documentUrl, parent.substitutor);
49     }
50
51     public String JavaDoc getTagName() {
52         return element.getTagName();
53     }
54
55     public Element JavaDoc getElement() {
56         return element;
57     }
58
59     public URL JavaDoc getDocumentUrl() {
60         return documentUrl;
61     }
62
63     protected List JavaDoc<XmlElement> getChildren() {
64         return asList(element.getChildNodes());
65     }
66
67     public String JavaDoc getXPath() {
68         List JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc>();
69         Node JavaDoc cur = element;
70         StringBuilder JavaDoc tmp = new StringBuilder JavaDoc();
71
72         while (!(cur instanceof Document JavaDoc)) {
73             int pos = 1;
74             Node JavaDoc sib = cur;
75             final String JavaDoc curTagName = ((Element JavaDoc) cur).getTagName();
76
77             while (sib != null) {
78                 sib = sib.getPreviousSibling();
79
80                 if ((sib != null) && sib instanceof Element JavaDoc) {
81                     Element JavaDoc ee = (Element JavaDoc) sib;
82
83                     if (curTagName.equals(ee.getTagName())) {
84                         pos++;
85                     }
86                 }
87             }
88
89             tmp.setLength(0);
90             tmp.append(curTagName);
91             cur = cur.getParentNode();
92             if (!(cur instanceof Document JavaDoc)) {
93                 tmp.append('[');
94                 tmp.append(pos);
95                 tmp.append(']');
96             }
97             l.add(tmp.toString());
98
99         }
100
101         StringBuilder JavaDoc res = new StringBuilder JavaDoc(100);
102
103         for (int i = l.size() - 1; i >= 0; i--) {
104             res.append('/');
105             res.append(l.get(i));
106         }
107
108         return res.toString();
109     }
110
111     public List JavaDoc<XmlElement> getChildren(final String JavaDoc name) {
112         List JavaDoc<XmlElement> res = new ArrayList JavaDoc<XmlElement>();
113         Node JavaDoc node = element.getFirstChild();
114
115         while (node != null) {
116             if (node instanceof Element JavaDoc) {
117                 if (name.equals(((Element JavaDoc) node).getTagName())) {
118                     res.add(new XmlElement((Element JavaDoc) node, this));
119                 }
120             }
121
122             node = node.getNextSibling();
123         }
124
125         return res;
126     }
127
128     public List JavaDoc<XmlElement> getChildren(final Set JavaDoc<String JavaDoc> names) {
129         List JavaDoc<XmlElement> res = new ArrayList JavaDoc<XmlElement>();
130         Node JavaDoc node = element.getFirstChild();
131
132         while (node != null) {
133             if (node instanceof Element JavaDoc) {
134                 if (names.contains(((Element JavaDoc) node).getTagName())) {
135                     res.add(new XmlElement((Element JavaDoc) node, this));
136                 }
137             }
138
139             node = node.getNextSibling();
140         }
141
142         return res;
143     }
144
145     public XmlElement getChild(final String JavaDoc name) {
146         Node JavaDoc node = element.getFirstChild();
147
148         while (node != null) {
149             if (node instanceof Element JavaDoc) {
150                 if (name.equals(((Element JavaDoc) node).getTagName())) {
151                     return new XmlElement((Element JavaDoc) node, this);
152                 }
153             }
154
155             node = node.getNextSibling();
156         }
157
158         return null;
159     }
160
161     protected List JavaDoc<XmlElement> asList(final NodeList JavaDoc list) {
162         List JavaDoc<XmlElement> result = new ArrayList JavaDoc<XmlElement>();
163         Node JavaDoc node = element.getFirstChild();
164
165         while (node != null) {
166             if (node instanceof Element JavaDoc) {
167                 result.add(new XmlElement((Element JavaDoc) node, this));
168             }
169
170             node = node.getNextSibling();
171         }
172
173         return result;
174     }
175
176     /**
177      * Gets the value of attribute.
178      * <p>Additionally property {@link #expandProperties(String) expansion} is performed.
179      * @param attribute attribute name.
180      * @return value of the attribute or null if attribute is absent or has empty value.
181      */

182     public String JavaDoc getAttribute(final String JavaDoc attribute) {
183         final String JavaDoc a = element.getAttribute(attribute);
184
185         return ((a != null) && (a.length() == 0)) ? null : expandProperties(a);
186     }
187
188     /**
189      * Returns the value of boolean attribute.
190      * @param attribute attribute name.
191      * @param defaultValue default value to use if attribute value unspecified.
192      * @see #getAttribute(String)
193      */

194     protected boolean getBooleanAttribute(final String JavaDoc attribute,
195                                           final boolean defaultValue) {
196         final String JavaDoc a = getAttribute(attribute);
197
198         if (a == null) {
199             return defaultValue;
200         }
201
202         if ("true".equalsIgnoreCase(a) || "1".equalsIgnoreCase(a) ||
203                 "on".equalsIgnoreCase(a) || "yes".equalsIgnoreCase(a)) {
204             return true;
205         }
206
207         if ("false".equalsIgnoreCase(a) || "0".equalsIgnoreCase(a) ||
208                 "off".equalsIgnoreCase(a) || "no".equalsIgnoreCase(a)) {
209             return false;
210         }
211
212         throw new ConfigurationException("Unrecognized value '" + a +
213                 "' of boolean attribute " + attribute +
214                 ". Valid values: yes/no, true/false, 1/0, on/off", this);
215     }
216
217     /**
218      * Expands properties in a string.
219      * @param s string to expand properties.
220      * @return string with substituted properties.
221      */

222     public String JavaDoc expandProperties(final String JavaDoc s) {
223         return substitutor.substitute(s);
224     }
225
226 }
227
Popular Tags