001 /* 002 $Id: Node.java,v 1.12 2005/09/27 12:32:51 hmeling Exp $ 003 004 Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved. 005 006 Redistribution and use of this software and associated documentation 007 ("Software"), with or without modification, are permitted provided 008 that the following conditions are met: 009 010 1. Redistributions of source code must retain copyright 011 statements and notices. Redistributions must also contain a 012 copy of this document. 013 014 2. Redistributions in binary form must reproduce the 015 above copyright notice, this list of conditions and the 016 following disclaimer in the documentation and/or other 017 materials provided with the distribution. 018 019 3. The name "groovy" must not be used to endorse or promote 020 products derived from this Software without prior written 021 permission of The Codehaus. For written permission, 022 please contact info@codehaus.org. 023 024 4. Products derived from this Software may not be called "groovy" 025 nor may "groovy" appear in their names without prior written 026 permission of The Codehaus. "groovy" is a registered 027 trademark of The Codehaus. 028 029 5. Due credit should be given to The Codehaus - 030 http://groovy.codehaus.org/ 031 032 THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS 033 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 034 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 035 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 036 THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 037 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 038 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 039 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 040 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 041 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 042 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 043 OF THE POSSIBILITY OF SUCH DAMAGE. 044 045 */ 046 package groovy.util; 047 048 import org.codehaus.groovy.runtime.InvokerHelper; 049 050 import groovy.xml.QName; 051 052 import java.io.PrintWriter; 053 import java.util.Collection; 054 import java.util.Collections; 055 import java.util.Iterator; 056 import java.util.List; 057 import java.util.Map; 058 059 /** 060 * Represents an arbitrary tree node which can be used for structured metadata which can be any arbitrary XML-like tree. 061 * A node can have a name, a value and an optional Map of attributes. 062 * Typically the name is a String and a value is either a String or a List of other Nodes. 063 * Though the types are extensible to provide a flexible structure. 064 * e.g. you could use a QName as the name which includes a namespace URI and a local name. Or a JMX ObjectName etc. 065 * So this class can represent metadata like {foo a=1 b="abc"} or nested metadata like {foo a=1 b="123" { bar x=12 text="hello" }} 066 * 067 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a> 068 * @version $Revision: 1.12 $ 069 */ 070 public class Node implements java.io.Serializable { 071 072 private static final long serialVersionUID = 4121134753270542643L; 073 private Node parent; 074 private Object name; 075 private Map attributes; 076 private Object value; 077 078 public Node(Node parent, Object name) { 079 this(parent, name, Collections.EMPTY_MAP, Collections.EMPTY_LIST); 080 } 081 082 public Node(Node parent, Object name, Object value) { 083 this(parent, name, Collections.EMPTY_MAP, value); 084 } 085 086 public Node(Node parent, Object name, Map attributes) { 087 this(parent, name, attributes, Collections.EMPTY_LIST); 088 } 089 090 public Node(Node parent, Object name, Map attributes, Object value) { 091 this.parent = parent; 092 this.name = name; 093 this.attributes = attributes; 094 this.value = value; 095 096 if (parent != null) { 097 Object parentValue = parent.value(); 098 List parentList = null; 099 if (parentValue instanceof List) { 100 parentList = (List) parentValue; 101 } 102 else { 103 parentList = new NodeList(); 104 parentList.add(parentValue); 105 parent.setValue(parentList); 106 } 107 parentList.add(this); 108 } 109 } 110 111 public String text() { 112 if (value instanceof String) { 113 return (String) value; 114 } 115 else if (value instanceof Collection) { 116 Collection coll = (Collection) value; 117 String previousText = null; 118 StringBuffer buffer = null; 119 for (Iterator iter = coll.iterator(); iter.hasNext();) { 120 Object child = iter.next(); 121 if (child instanceof String) { 122 String childText = (String) child; 123 if (previousText == null) { 124 previousText = childText; 125 } 126 else { 127 if (buffer == null) { 128 buffer = new StringBuffer(); 129 buffer.append(previousText); 130 } 131 buffer.append(childText); 132 } 133 } 134 } 135 if (buffer != null) { 136 return buffer.toString(); 137 } 138 else { 139 if (previousText != null) { 140 return previousText; 141 } 142 } 143 } 144 return ""; 145 } 146 147 148 public Iterator iterator() { 149 return children().iterator(); 150 } 151 152 public List children() { 153 if (value == null) { 154 return Collections.EMPTY_LIST; 155 } 156 else if (value instanceof List) { 157 return (List) value; 158 } 159 else { 160 // we're probably just a String 161 return Collections.singletonList(value); 162 } 163 } 164 165 public Map attributes() { 166 return attributes; 167 } 168 169 public Object attribute(Object key) { 170 return (attributes != null) ? attributes.get(key) : null; 171 } 172 173 public Object name() { 174 return name; 175 } 176 177 public Object value() { 178 return value; 179 } 180 181 public void setValue(Object value) { 182 this.value = value; 183 } 184 185 public Node parent() { 186 return parent; 187 } 188 189 /** 190 * Provides lookup of elements by non-namespaced name 191 */ 192 public Object get(String key) { 193 if (key.charAt(0) == '@') { 194 String attributeName = key.substring(1); 195 return attributes().get(attributeName); 196 } 197 else { 198 // iterate through list looking for node with name 'key' 199 List answer = new NodeList(); 200 for (Iterator iter = children().iterator(); iter.hasNext();) { 201 Object child = iter.next(); 202 if (child instanceof Node) { 203 Node childNode = (Node) child; 204 Object childNodeName = childNode.name(); 205 if (childNodeName != null && childNodeName.equals(key)) { 206 answer.add(childNode); 207 } 208 } 209 } 210 return answer; 211 } 212 } 213 214 /** 215 * Provides lookup of elements by QName 216 */ 217 public NodeList getAt(QName name) { 218 NodeList answer = new NodeList(); 219 for (Iterator iter = children().iterator(); iter.hasNext();) { 220 Object child = iter.next(); 221 if (child instanceof Node) { 222 Node childNode = (Node) child; 223 Object childNodeName = childNode.name(); 224 if (childNodeName != null && childNodeName.equals(name)) { 225 answer.add(childNode); 226 } 227 } 228 } 229 return answer; 230 } 231 232 // public Object get(int idx) { 233 // return children().get(idx); 234 // } 235 236 237 238 /** 239 * Provide a collection of all the nodes in the tree 240 * using a depth first traversal 241 */ 242 public List depthFirst() { 243 List answer = new NodeList(); 244 answer.add(this); 245 answer.addAll(depthFirstRest()); 246 return answer; 247 } 248 249 private List depthFirstRest() { 250 List answer = new NodeList(); 251 for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext(); ) { 252 Object child = iter.next(); 253 if (child instanceof Node) { 254 Node childNode = (Node) child; 255 List children = childNode.depthFirstRest(); 256 answer.add(childNode); 257 answer.addAll(children); 258 } 259 } 260 return answer; 261 } 262 263 /** 264 * Provide a collection of all the nodes in the tree 265 * using a bredth first traversal 266 */ 267 public List breadthFirst() { 268 List answer = new NodeList(); 269 answer.add(this); 270 answer.addAll(breadthFirstRest()); 271 return answer; 272 } 273 274 private List breadthFirstRest() { 275 List answer = new NodeList(); 276 for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext(); ) { 277 Object child = iter.next(); 278 if (child instanceof Node) { 279 Node childNode = (Node) child; 280 answer.add(childNode); 281 } 282 } 283 List copy = new NodeList(answer); 284 for (Iterator iter = copy.iterator(); iter.hasNext(); ) { 285 Node childNode = (Node) iter.next(); 286 List children = childNode.breadthFirstRest(); 287 answer.addAll(children); 288 } 289 return answer; 290 } 291 292 public String toString() { 293 return name + "[attributes=" + attributes + "; value=" + value + "]"; 294 } 295 296 public void print(PrintWriter out) { 297 new NodePrinter(out).print(this); 298 } 299 }