1 46 package groovy.util; 47 48 import java.io.PrintWriter ; 49 import java.util.ArrayList ; 50 import java.util.Collection ; 51 import java.util.Collections ; 52 import java.util.Iterator ; 53 import java.util.List ; 54 import java.util.Map ; 55 56 import org.codehaus.groovy.runtime.InvokerHelper; 57 58 69 public class Node { 70 71 private Node parent; 72 private Object name; 73 private Map attributes; 74 private Object value; 75 76 public Node(Node parent, Object name) { 77 this(parent, name, Collections.EMPTY_MAP, Collections.EMPTY_LIST); 78 } 79 80 public Node(Node parent, Object name, Object value) { 81 this(parent, name, Collections.EMPTY_MAP, value); 82 } 83 84 public Node(Node parent, Object name, Map attributes) { 85 this(parent, name, attributes, Collections.EMPTY_LIST); 86 } 87 88 public Node(Node parent, Object name, Map attributes, Object value) { 89 this.parent = parent; 90 this.name = name; 91 this.attributes = attributes; 92 this.value = value; 93 94 if (parent != null) { 95 Object parentValue = parent.value(); 96 List parentList = null; 97 if (parentValue instanceof List ) { 98 parentList = (List ) parentValue; 99 } 100 else { 101 parentList = new ArrayList (); 102 parentList.add(parentValue); 103 parent.setValue(parentList); 104 } 105 parentList.add(this); 106 } 107 } 108 109 public String text() { 110 if (value instanceof String ) { 111 return (String ) value; 112 } 113 else if (value instanceof Collection ) { 114 Collection coll = (Collection ) value; 115 String previousText = null; 116 StringBuffer buffer = null; 117 for (Iterator iter = coll.iterator(); iter.hasNext();) { 118 Object child = iter.next(); 119 if (child instanceof String ) { 120 String childText = (String ) child; 121 if (previousText == null) { 122 previousText = childText; 123 } 124 else { 125 if (buffer == null) { 126 buffer = new StringBuffer (); 127 buffer.append(previousText); 128 } 129 buffer.append(childText); 130 } 131 } 132 } 133 if (buffer != null) { 134 return buffer.toString(); 135 } 136 else { 137 if (previousText != null) { 138 return previousText; 139 } 140 } 141 } 142 return ""; 143 } 144 145 public Iterator iterator() { 146 return children().iterator(); 147 } 148 149 public List children() { 150 if (value == null) { 151 return Collections.EMPTY_LIST; 152 } 153 else if (value instanceof List ) { 154 return (List ) value; 155 } 156 else { 157 return Collections.singletonList(value); 159 } 160 } 161 162 public Map attributes() { 163 return attributes; 164 } 165 166 public Object attribute(Object key) { 167 return (attributes != null) ? attributes.get(key) : null; 168 } 169 170 public Object name() { 171 return name; 172 } 173 174 public Object value() { 175 return value; 176 } 177 178 public void setValue(Object value) { 179 this.value = value; 180 } 181 182 public Node parent() { 183 return parent; 184 } 185 186 public Object get(String key) { 187 if (key.charAt(0) == '@') { 188 String attributeName = key.substring(1); 189 return attributes().get(attributeName); 190 } 191 else { 192 List answer = new ArrayList (); 194 for (Iterator iter = children().iterator(); iter.hasNext();) { 195 Object child = iter.next(); 196 if (child instanceof Node) { 197 Node childNode = (Node) child; 198 if (key.equals(childNode.name())) { 199 answer.add(childNode); 200 } 201 } 202 } 203 return answer; 204 } 205 } 206 207 211 212 213 217 public List depthFirst() { 218 List answer = new ArrayList (); 219 answer.add(this); 220 answer.addAll(depthFirstRest()); 221 return answer; 222 } 223 224 private List depthFirstRest() { 225 List answer = new ArrayList (); 226 for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext(); ) { 227 Object child = iter.next(); 228 if (child instanceof Node) { 229 Node childNode = (Node) child; 230 List children = childNode.depthFirstRest(); 231 answer.add(childNode); 232 answer.addAll(children); 233 } 234 } 235 return answer; 236 } 237 238 242 public List breadthFirst() { 243 List answer = new ArrayList (); 244 answer.add(this); 245 answer.addAll(breadthFirstRest()); 246 return answer; 247 } 248 249 private List breadthFirstRest() { 250 List answer = new ArrayList (); 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 answer.add(childNode); 256 } 257 } 258 List copy = new ArrayList (answer); 259 for (Iterator iter = copy.iterator(); iter.hasNext(); ) { 260 Node childNode = (Node) iter.next(); 261 List children = childNode.breadthFirstRest(); 262 answer.addAll(children); 263 } 264 return answer; 265 } 266 267 public String toString() { 268 return name + "[attributes=" + attributes + "; value=" + value + "]"; 269 } 270 271 public void print(PrintWriter out) { 272 new NodePrinter(out).print(this); 273 } 274 } 275 | Popular Tags |