KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > SearchElement


1 package org.enhydra.xml;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.w3c.dom.DOMException JavaDoc;
7 import org.w3c.dom.Document JavaDoc;
8 import org.w3c.dom.Element JavaDoc;
9 import org.w3c.dom.Node JavaDoc;
10 import org.w3c.dom.NodeList JavaDoc;
11
12 /**
13  * @author Tweety
14  *
15  * A class representing a node in a meta-data tree, which implements
16  * the <a HREF="../../../../api/org/w3c/dom/Element.html">
17  *
18  * <p> Namespaces are ignored in this implementation. The terms "tag
19  * name" and "node name" are always considered to be synonymous.
20  *
21  * @version 1.0
22  */

23 public class SearchElement extends HashMapElement {
24
25     public static String JavaDoc TAG_SEPARATOR = "/";
26     public static String JavaDoc ATTR_SEPARATOR = "@";
27     public static String JavaDoc EQUAL_SEPARATOR = "=";
28
29     /**
30      * Constructs an empty <code>SearchElement</code>.
31      */

32     public SearchElement() {
33     }
34
35     /**
36      * Constructs an <code>SearchElement</code> with a given document owner and node name.
37      *
38      * @param ownerDoc the document owner of the node, as a <code>Document</code>.
39      * @param name the name of the node, as a <code>String</code>.
40      */

41     public SearchElement(Document JavaDoc ownerDoc, String JavaDoc name) {
42         super(ownerDoc, name);
43     }
44
45     /**
46      * Constructs an <code>SearchElement</code> with a given <code>Node</code>.
47      *
48      * @param node <code>Node</code>.
49      */

50     public SearchElement(Node JavaDoc node) {
51         super(node);
52     }
53
54     /**
55      * Constructs an <code>SearchElement</code> with a given <code>SearchElement</code>.
56      *
57      * @param node <code>SearchElement</code>.
58      */

59     public SearchElement(SearchElement node) {
60         super((HashMapElement) node);
61     }
62
63     /**
64      * Returns new <code>SearchElement</code> instance from a given <code>Node</code>.
65      *
66      * @param node <code>Node</code>.
67      */

68     protected Node JavaDoc newElementInstance(Node JavaDoc node) {
69         return new SearchElement(node);
70     }
71
72   /**
73      * Creates new instance of <code>SearchElement</code> from a given document
74      * as a <code>Document</code>.
75      *
76      * @param document document.
77      *
78      * @return new <code>SearchElement</code> node as a root of the <code>Document</code>.
79      */

80     public static Element JavaDoc newInstance(Document JavaDoc document) {
81         Node JavaDoc root = document.getDocumentElement();
82         return new SearchElement(root);
83     }
84
85     /**
86      * Returns a list of elements in the subtree of this node, with the given tag name.
87      *
88      * @param namePath relative path to the </code>Element</code> (through children).
89      *
90      * @return list of elements in the subtree of this node, with the given tag name.
91      */

92     public NodeList JavaDoc getSubElementsByTagName(String JavaDoc namePath) {
93         List JavaDoc list = new ArrayList JavaDoc();
94         getSubElementsByTagName(namePath, list);
95         return new NodeListImpl(list);
96     }
97
98     /*
99      * Recursively fullfills the <code>list</code> with all the nodes on the given path.
100      */

101     private void getSubElementsByTagName(String JavaDoc name, List JavaDoc list) {
102         String JavaDoc[] keys = name.split(this.TAG_SEPARATOR, 2);
103         if (keys.length == 1) {
104             List JavaDoc l = (List JavaDoc) this.children.get(name);
105             if (l != null)
106                 list.addAll(l);
107             return;
108         }
109         NodeList JavaDoc tagChildren = this.getChildrenByTagName(keys[0]);
110         if (tagChildren != null)
111             for (int i = 0; i < tagChildren.getLength(); i++)
112                 ((SearchElement) tagChildren.item(i)).getSubElementsByTagName(
113                     keys[1],
114                     list);
115     }
116
117     /**
118      * Returns a list of <code>Element</code>s in the subtree of this node,
119      * which contain attribute with the given name and value.
120      *
121      * @param attrPath relative path to the attribute name.
122      * @param attrValue attribute value.
123      *
124      * @return list of <code>Element</code>s in the subtree of this node,
125      * which contain attribute with the given name and value.
126      */

127     public NodeList JavaDoc getSubElementsByAttrValue(String JavaDoc attrPath, String JavaDoc attrValue) {
128         String JavaDoc[] keys = attrPath.split(this.ATTR_SEPARATOR, 2);
129         if (keys.length != 2)
130             throw new DOMException JavaDoc(
131                 DOMException.INVALID_ACCESS_ERR,
132                 "Parameter not supported");
133         List JavaDoc list = new ArrayList JavaDoc();
134         getSubElementsByAttrValue(keys[0], keys[1], attrValue, list);
135         return new NodeListImpl(list);
136     }
137
138
139     /*
140      * Recursively fullfills the <code>list</code> with all the nodes in the given path.
141      */

142     private void getSubElementsByAttrValue(String JavaDoc tagName, String JavaDoc attrName, String JavaDoc attrValue, List JavaDoc list) {
143         String JavaDoc[] keys = tagName.split(this.TAG_SEPARATOR, 2);
144         if (keys.length == 1) {
145             List JavaDoc fList = (List JavaDoc) this.children.get(tagName);
146             if (fList != null) {
147                 for (int i = 0; i < fList.size(); i++) {
148                     Element JavaDoc elm = (Element JavaDoc) fList.get(i);
149                     String JavaDoc val = (String JavaDoc) elm.getAttribute(attrName);
150                     if (val != null)
151                         if (val.equals(attrValue))
152                             list.add(elm);
153                 }
154             }
155             return;
156         }
157         NodeList JavaDoc tagChildren = this.getChildrenByTagName(keys[0]);
158         if (tagChildren != null) {
159             for (int i = 0; i < tagChildren.getLength(); i++)
160                 ((SearchElement) tagChildren.item(i)).getSubElementsByAttrValue(
161                     keys[1],
162                     attrName,
163                     attrValue,
164                     list);
165         }
166     }
167
168
169     /**
170      * Returns a list of <code>Element</code>s in the subtree of this node,
171      * with the given tag name and value.
172      *
173      * @param tagPath relative path to the tag name.
174      * @param tagValue <code>Element</code> value.
175      *
176      * @return list of <code>Element</code>s in the subtree of this node,
177      * with the given tag name and value.
178      */

179     public NodeList JavaDoc getSubElementsByTagText(String JavaDoc tagPath, String JavaDoc tagValue) {
180         List JavaDoc list = new ArrayList JavaDoc();
181         getSubElementsByTagText(tagPath, tagValue, list);
182         return new NodeListImpl(list);
183     }
184
185
186     /*
187      * Recursively fullfills the <code>list</code> with all the nodes in the given path.
188      */

189     private void getSubElementsByTagText(
190         String JavaDoc tagName,
191         String JavaDoc tagValue,
192         List JavaDoc list) {
193         String JavaDoc[] keys = tagName.split(this.TAG_SEPARATOR, 2);
194         if (keys.length == 1) {
195             List JavaDoc fList = (List JavaDoc) this.children.get(tagName);
196             if (fList != null) {
197                 for (int i = 0; i < fList.size(); i++) {
198                     HashMapElement elm = (HashMapElement) fList.get(i);
199                     String JavaDoc val = (String JavaDoc) elm.getText();
200                     if (val != null)
201                         if (val.equals(tagValue))
202                             list.add(elm);
203                 }
204             }
205             return;
206         }
207         NodeList JavaDoc tagChildren = this.getChildrenByTagName(keys[0]);
208         if (tagChildren != null) {
209             for (int i = 0; i < tagChildren.getLength(); i++)
210                 ((SearchElement) tagChildren.item(i)).getSubElementsByTagText(
211                     keys[1],
212                     tagValue,
213                     list);
214         }
215     }
216
217
218     /**
219      * Returns a list of <code>Element</code>s in the subtree of this node,
220      * that satisfy the given condition.
221      *
222      * @param condition condition.
223      *
224      * @return list of <code>Element</code>s in the subtree of this node,
225      * that satisfy the given condition.
226      */

227     public NodeList JavaDoc getSubElementsByCondition(String JavaDoc condition) {
228         if (!condition.matches("([^@=]*)(@?[^@=/]*=[^@=/]*)"))
229             throw new DOMException JavaDoc(
230                 DOMException.INVALID_ACCESS_ERR,
231                 "Parameter not supported");
232         String JavaDoc[] keys = condition.split(this.EQUAL_SEPARATOR, 2);
233         String JavaDoc namePath = keys[0];
234         if (namePath.indexOf(ATTR_SEPARATOR) != -1)
235             return getSubElementsByAttrValue(namePath, keys[1]);
236         else
237             return getSubElementsByTagText(namePath, keys[1]);
238     }
239
240
241     /**
242      * Returns the first <code>Element</code> in the subtree of this node,
243      * that satisfy the given condition.
244      *
245      * @param condition condition.
246      *
247      * @return the first <code>Element</code> in the subtree of this node,
248      * that satisfy the given condition.
249      */

250     public Element JavaDoc getFirstSubElementsByCondition(String JavaDoc condition) {
251         NodeList JavaDoc nodes = getSubElementsByCondition(condition);
252         if (nodes != null && nodes.getLength() > 0)
253             return (Element JavaDoc) nodes.item(0);
254         return null;
255     }
256
257
258     /**
259      * Returns the first <code>Element</code> in the subtree of this node,
260      * with the given tag name.
261      *
262      * @param namePath relative path to the <code>Element</code>.
263      *
264      * @return the first <code>Element</code> in the subtree of this node,
265      * with the given tag name.
266      */

267     public Element JavaDoc getFirstSubElementByTagName(String JavaDoc namePath) {
268         NodeList JavaDoc nodes = getSubElementsByTagName(namePath);
269         if (nodes != null && nodes.getLength() > 0)
270             return (Element JavaDoc) nodes.item(0);
271         return null;
272     }
273
274
275     /**
276      * Return the text of the <code>Element</code> found on the given path.
277      *
278      * @param namePath relative path to the <code>Element</code> node.
279      *
280      * @return text of the <code>Element</code> found on the given path.
281      */

282     public String JavaDoc getText(String JavaDoc namePath) {
283         NodeList JavaDoc nodes = this.getSubElementsByTagName(namePath);
284         if (nodes != null && nodes.getLength() > 0)
285             return ((SearchElement) nodes.item(0)).getText();
286         return null;
287     }
288
289
290     /**
291      * Sets the given text to the <code>Element</code> found on the given path.
292      *
293      * @param namePath relative path to the <code>Element</code> node.
294      * @param text new text.
295      */

296     public void setText(String JavaDoc namePath, String JavaDoc text) {
297         NodeList JavaDoc nodes = this.getSubElementsByTagName(namePath);
298         if (nodes != null && nodes.getLength() > 0)
299              ((SearchElement) nodes.item(0)).setText(text);
300     }
301
302
303     /**
304      * Sets the value of an attribute found on the given path.
305      *
306      * @param namePath relative path to the attribute.
307      * @param text new value.
308      */

309     public void setAttr(String JavaDoc namePath, String JavaDoc value) {
310         if (!namePath.matches("([^@]*)(@[^@/]*)"))
311             throw new DOMException JavaDoc(
312                 DOMException.INVALID_ACCESS_ERR,
313                 "Parameter not supported");
314         String JavaDoc[] keys = namePath.split(this.ATTR_SEPARATOR, 2);
315
316         NodeList JavaDoc nodes = this.getSubElementsByTagName(keys[0]);
317         if (nodes != null && nodes.getLength() > 0)
318              ((SearchElement) nodes.item(0)).setAttribute(keys[1], value);
319     }
320
321
322 }
323
Popular Tags