1 16 17 package com.icesoft.jasper.xmlparser; 18 19 import java.util.ArrayList ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 24 25 37 38 public class TreeNode { 39 40 42 43 48 public TreeNode(String name) { 49 50 this(name, null); 51 52 } 53 54 55 61 public TreeNode(String name, TreeNode parent) { 62 63 super(); 64 this.name = name; 65 this.parent = parent; 66 if (this.parent != null) 67 this.parent.addChild(this); 68 69 } 70 71 73 74 78 protected HashMap attributes = null; 79 80 81 84 protected String body = null; 85 86 87 90 protected ArrayList children = null; 91 92 93 96 protected String name = null; 97 98 99 102 protected TreeNode parent = null; 103 104 106 107 114 public void addAttribute(String name, String value) { 115 116 if (attributes == null) 117 attributes = new HashMap (); 118 attributes.put(name, value); 119 120 } 121 122 123 128 public void addChild(TreeNode node) { 129 130 if (children == null) 131 children = new ArrayList (); 132 children.add(node); 133 134 } 135 136 137 143 public String findAttribute(String name) { 144 145 if (attributes == null) 146 return (null); 147 else 148 return ((String ) attributes.get(name)); 149 150 } 151 152 153 157 public Iterator findAttributes() { 158 159 if (attributes == null) 160 return (Collections.EMPTY_LIST.iterator()); 161 else 162 return (attributes.keySet().iterator()); 163 164 } 165 166 167 173 public TreeNode findChild(String name) { 174 175 if (children == null) 176 return (null); 177 Iterator items = children.iterator(); 178 while (items.hasNext()) { 179 TreeNode item = (TreeNode) items.next(); 180 if (name.equals(item.getName())) 181 return (item); 182 } 183 return (null); 184 185 } 186 187 188 192 public Iterator findChildren() { 193 194 if (children == null) 195 return (Collections.EMPTY_LIST.iterator()); 196 else 197 return (children.iterator()); 198 199 } 200 201 202 208 public Iterator findChildren(String name) { 209 210 if (children == null) 211 return (Collections.EMPTY_LIST.iterator()); 212 213 ArrayList results = new ArrayList (); 214 Iterator items = children.iterator(); 215 while (items.hasNext()) { 216 TreeNode item = (TreeNode) items.next(); 217 if (name.equals(item.getName())) 218 results.add(item); 219 } 220 return (results.iterator()); 221 222 } 223 224 225 228 public String getBody() { 229 230 return (this.body); 231 232 } 233 234 235 238 public String getName() { 239 240 return (this.name); 241 242 } 243 244 245 250 public void removeAttribute(String name) { 251 252 if (attributes != null) 253 attributes.remove(name); 254 255 } 256 257 258 263 public void removeNode(TreeNode node) { 264 265 if (children != null) 266 children.remove(node); 267 268 } 269 270 271 276 public void setBody(String body) { 277 278 this.body = body; 279 280 } 281 282 283 286 public String toString() { 287 288 StringBuffer sb = new StringBuffer (); 289 toString(sb, 0, this); 290 return (sb.toString()); 291 292 } 293 294 296 297 305 protected void toString(StringBuffer sb, int indent, 306 TreeNode node) { 307 308 int indent2 = indent + 2; 309 310 for (int i = 0; i < indent; i++) 312 sb.append(' '); 313 sb.append('<'); 314 sb.append(node.getName()); 315 Iterator names = node.findAttributes(); 316 while (names.hasNext()) { 317 sb.append(' '); 318 String name = (String ) names.next(); 319 sb.append(name); 320 sb.append("=\""); 321 String value = node.findAttribute(name); 322 sb.append(value); 323 sb.append("\""); 324 } 325 sb.append(">\n"); 326 327 String body = node.getBody(); 329 if ((body != null) && (body.length() > 0)) { 330 for (int i = 0; i < indent2; i++) 331 sb.append(' '); 332 sb.append(body); 333 sb.append("\n"); 334 } 335 336 Iterator children = node.findChildren(); 338 while (children.hasNext()) { 339 TreeNode child = (TreeNode) children.next(); 340 toString(sb, indent2, child); 341 } 342 343 for (int i = 0; i < indent; i++) 345 sb.append(' '); 346 sb.append("</"); 347 sb.append(node.getName()); 348 sb.append(">\n"); 349 350 } 351 352 353 } 354 | Popular Tags |