1 /***
2 *
3 * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package groovy.util;
19
20 import groovy.xml.QName;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Iterator;
25
26 /***
27 * A List implementation which is returned by queries on a {@link Node}
28 * which provides some XPath like helper methods for GPath.
29 */
30 public class NodeList extends ArrayList {
31
32 public NodeList() {
33 }
34
35 public NodeList(Collection collection) {
36 super(collection);
37 }
38
39 public NodeList(int size) {
40 super(size);
41 }
42
43 /***
44 * Provides lookup of elements by non-namespaced name.
45 *
46 * @return the nodes of interest which match name
47 * @param name the name or shortcut key for nodes of interest
48 */
49 public NodeList getAt(String name) {
50 NodeList answer = new NodeList();
51 for (Iterator iter = iterator(); iter.hasNext();) {
52 Object child = iter.next();
53 if (child instanceof Node) {
54 Node childNode = (Node) child;
55 Object temp = childNode.get(name);
56 if (temp instanceof Collection) {
57 answer.addAll((Collection) temp);
58 }
59 else {
60 answer.add(temp);
61 }
62 }
63 }
64 return answer;
65 }
66
67 /***
68 * Provides lookup of elements by QName.
69 *
70 * @return the nodes of interest which match name
71 * @param name the name or shortcut key for nodes of interest
72 */
73 public NodeList getAt(QName name) {
74 NodeList answer = new NodeList();
75 for (Iterator iter = iterator(); iter.hasNext();) {
76 Object child = iter.next();
77 if (child instanceof Node) {
78 Node childNode = (Node) child;
79 NodeList temp = childNode.getAt(name);
80 answer.addAll(temp);
81 }
82 }
83 return answer;
84 }
85
86 /***
87 * Returns the text value of all of the elements in the collection.
88 *
89 * @return the text value of all the elements in the collection or null
90 */
91 public String text() {
92 String previousText = null;
93 StringBuffer buffer = null;
94 for (Iterator iter = this.iterator(); iter.hasNext();) {
95 Object child = iter.next();
96 String text = null;
97 if (child instanceof String) {
98 text = (String) child;
99 }
100 else if (child instanceof Node) {
101 text = ((Node) child).text();
102 }
103 if (text != null) {
104 if (previousText == null) {
105 previousText = text;
106 }
107 else {
108 if (buffer == null) {
109 buffer = new StringBuffer();
110 buffer.append(previousText);
111 }
112 buffer.append(text);
113 }
114 }
115 }
116 if (buffer != null) {
117 return buffer.toString();
118 }
119 if (previousText != null) {
120 return previousText;
121 }
122 return "";
123 }
124 }