1 17 18 package org.apache.jasper.xmlparser; 19 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 25 26 39 40 public class TreeNode { 41 42 43 45 46 51 public TreeNode(String name) { 52 53 this(name, null); 54 55 } 56 57 58 64 public TreeNode(String name, TreeNode parent) { 65 66 super(); 67 this.name = name; 68 this.parent = parent; 69 if (this.parent != null) 70 this.parent.addChild(this); 71 72 } 73 74 75 77 78 82 protected HashMap attributes = null; 83 84 85 88 protected String body = null; 89 90 91 94 protected ArrayList children = null; 95 96 97 100 protected String name = null; 101 102 103 106 protected TreeNode parent = null; 107 108 109 111 112 119 public void addAttribute(String name, String value) { 120 121 if (attributes == null) 122 attributes = new HashMap (); 123 attributes.put(name, value); 124 125 } 126 127 128 133 public void addChild(TreeNode node) { 134 135 if (children == null) 136 children = new ArrayList (); 137 children.add(node); 138 139 } 140 141 142 148 public String findAttribute(String name) { 149 150 if (attributes == null) 151 return (null); 152 else 153 return ((String ) attributes.get(name)); 154 155 } 156 157 158 162 public Iterator findAttributes() { 163 164 if (attributes == null) 165 return (Collections.EMPTY_LIST.iterator()); 166 else 167 return (attributes.keySet().iterator()); 168 169 } 170 171 172 178 public TreeNode findChild(String name) { 179 180 if (children == null) 181 return (null); 182 Iterator items = children.iterator(); 183 while (items.hasNext()) { 184 TreeNode item = (TreeNode) items.next(); 185 if (name.equals(item.getName())) 186 return (item); 187 } 188 return (null); 189 190 } 191 192 193 197 public Iterator findChildren() { 198 199 if (children == null) 200 return (Collections.EMPTY_LIST.iterator()); 201 else 202 return (children.iterator()); 203 204 } 205 206 207 214 public Iterator findChildren(String name) { 215 216 if (children == null) 217 return (Collections.EMPTY_LIST.iterator()); 218 219 ArrayList results = new ArrayList (); 220 Iterator items = children.iterator(); 221 while (items.hasNext()) { 222 TreeNode item = (TreeNode) items.next(); 223 if (name.equals(item.getName())) 224 results.add(item); 225 } 226 return (results.iterator()); 227 228 } 229 230 231 234 public String getBody() { 235 236 return (this.body); 237 238 } 239 240 241 244 public String getName() { 245 246 return (this.name); 247 248 } 249 250 251 256 public void removeAttribute(String name) { 257 258 if (attributes != null) 259 attributes.remove(name); 260 261 } 262 263 264 269 public void removeNode(TreeNode node) { 270 271 if (children != null) 272 children.remove(node); 273 274 } 275 276 277 282 public void setBody(String body) { 283 284 this.body = body; 285 286 } 287 288 289 292 public String toString() { 293 294 StringBuffer sb = new StringBuffer (); 295 toString(sb, 0, this); 296 return (sb.toString()); 297 298 } 299 300 301 303 304 312 protected void toString(StringBuffer sb, int indent, 313 TreeNode node) { 314 315 int indent2 = indent + 2; 316 317 for (int i = 0; i < indent; i++) 319 sb.append(' '); 320 sb.append('<'); 321 sb.append(node.getName()); 322 Iterator names = node.findAttributes(); 323 while (names.hasNext()) { 324 sb.append(' '); 325 String name = (String ) names.next(); 326 sb.append(name); 327 sb.append("=\""); 328 String value = node.findAttribute(name); 329 sb.append(value); 330 sb.append("\""); 331 } 332 sb.append(">\n"); 333 334 String body = node.getBody(); 336 if ((body != null) && (body.length() > 0)) { 337 for (int i = 0; i < indent2; i++) 338 sb.append(' '); 339 sb.append(body); 340 sb.append("\n"); 341 } 342 343 Iterator children = node.findChildren(); 345 while (children.hasNext()) { 346 TreeNode child = (TreeNode) children.next(); 347 toString(sb, indent2, child); 348 } 349 350 for (int i = 0; i < indent; i++) 352 sb.append(' '); 353 sb.append("</"); 354 sb.append(node.getName()); 355 sb.append(">\n"); 356 357 } 358 359 360 } 361 | Popular Tags |