001: /**
002: *
003: * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: **/package groovy.util;
018:
019: import groovy.xml.QName;
020:
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.Iterator;
024:
025: /**
026: * A List implementation which is returned by queries on a {@link Node}
027: * which provides some XPath like helper methods for GPath.
028: */
029: public class NodeList extends ArrayList {
030:
031: public NodeList() {
032: }
033:
034: public NodeList(Collection collection) {
035: super (collection);
036: }
037:
038: public NodeList(int size) {
039: super (size);
040: }
041:
042: /**
043: * Provides lookup of elements by non-namespaced name.
044: *
045: * @return the nodes of interest which match name
046: * @param name the name or shortcut key for nodes of interest
047: */
048: public NodeList getAt(String name) {
049: NodeList answer = new NodeList();
050: for (Iterator iter = iterator(); iter.hasNext();) {
051: Object child = iter.next();
052: if (child instanceof Node) {
053: Node childNode = (Node) child;
054: Object temp = childNode.get(name);
055: if (temp instanceof Collection) {
056: answer.addAll((Collection) temp);
057: } else {
058: answer.add(temp);
059: }
060: }
061: }
062: return answer;
063: }
064:
065: /**
066: * Provides lookup of elements by QName.
067: *
068: * @return the nodes of interest which match name
069: * @param name the name or shortcut key for nodes of interest
070: */
071: public NodeList getAt(QName name) {
072: NodeList answer = new NodeList();
073: for (Iterator iter = iterator(); iter.hasNext();) {
074: Object child = iter.next();
075: if (child instanceof Node) {
076: Node childNode = (Node) child;
077: NodeList temp = childNode.getAt(name);
078: answer.addAll(temp);
079: }
080: }
081: return answer;
082: }
083:
084: /**
085: * Returns the text value of all of the elements in the collection.
086: *
087: * @return the text value of all the elements in the collection or null
088: */
089: public String text() {
090: String previousText = null;
091: StringBuffer buffer = null;
092: for (Iterator iter = this .iterator(); iter.hasNext();) {
093: Object child = iter.next();
094: String text = null;
095: if (child instanceof String) {
096: text = (String) child;
097: } else if (child instanceof Node) {
098: text = ((Node) child).text();
099: }
100: if (text != null) {
101: if (previousText == null) {
102: previousText = text;
103: } else {
104: if (buffer == null) {
105: buffer = new StringBuffer();
106: buffer.append(previousText);
107: }
108: buffer.append(text);
109: }
110: }
111: }
112: if (buffer != null) {
113: return buffer.toString();
114: }
115: if (previousText != null) {
116: return previousText;
117: }
118: return "";
119: }
120: }
|