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 **/ 018 package groovy.util; 019 020 import groovy.xml.QName; 021 022 import java.util.ArrayList; 023 import java.util.Collection; 024 import java.util.Iterator; 025 026 /** 027 * A List implementation which is returned by queries on a {@link Node} which provides some XPath like helper methods for GPath 028 * 029 * @version $Revision$ 030 */ 031 public class NodeList extends ArrayList { 032 033 public NodeList() { 034 } 035 036 public NodeList(Collection collection) { 037 super(collection); 038 } 039 040 public NodeList(int size) { 041 super(size); 042 } 043 044 /** 045 * Provides lookup of elements by non-namespaced name 046 */ 047 public NodeList getAt(String name) { 048 NodeList answer = new NodeList(); 049 for (Iterator iter = iterator(); iter.hasNext();) { 050 Object child = iter.next(); 051 if (child instanceof Node) { 052 Node childNode = (Node) child; 053 Object temp = childNode.get(name); 054 if (temp instanceof Collection) { 055 answer.addAll((Collection) temp); 056 } 057 else { 058 answer.add(temp); 059 } 060 } 061 } 062 return answer; 063 } 064 065 /** 066 * Provides lookup of elements by QName 067 */ 068 public NodeList getAt(QName name) { 069 NodeList answer = new NodeList(); 070 for (Iterator iter = iterator(); iter.hasNext();) { 071 Object child = iter.next(); 072 if (child instanceof Node) { 073 Node childNode = (Node) child; 074 NodeList temp = childNode.getAt(name); 075 answer.addAll(temp); 076 } 077 } 078 return answer; 079 } 080 081 /** 082 * Returns the text value of all of the elements in the collection 083 * 084 * @return the text value of all the elements in the collection or null 085 */ 086 public String text() { 087 String previousText = null; 088 StringBuffer buffer = null; 089 for (Iterator iter = this.iterator(); iter.hasNext();) { 090 Object child = iter.next(); 091 String text = null; 092 if (child instanceof String) { 093 text = (String) child; 094 } 095 else if (child instanceof Node) { 096 text = ((Node) child).text(); 097 } 098 if (text != null) { 099 if (previousText == null) { 100 previousText = text; 101 } 102 else { 103 if (buffer == null) { 104 buffer = new StringBuffer(); 105 buffer.append(previousText); 106 } 107 buffer.append(text); 108 } 109 } 110 } 111 if (buffer != null) { 112 return buffer.toString(); 113 } 114 else { 115 if (previousText != null) { 116 return previousText; 117 } 118 } 119 return ""; 120 } 121 }