1 28 29 package net.n3.nanoxml; 30 31 import java.io.Serializable ; 32 import java.util.Enumeration ; 33 import java.util.Properties ; 34 import java.util.Vector ; 35 36 44 public class XMLElement implements Serializable 45 { 46 47 50 static final long serialVersionUID = -2383376380548624920L; 51 52 55 public static final int NO_LINE = -1; 56 57 60 private Properties attributes; 61 62 65 private Vector children; 66 67 70 private String name; 71 72 75 private String content; 76 77 80 private String systemID; 81 82 85 private int lineNr; 86 87 90 public XMLElement() 91 { 92 this(null, null, NO_LINE); 93 } 94 95 100 public XMLElement(String name) 101 { 102 this(name, null, NO_LINE); 103 } 104 105 112 public XMLElement(String name, String systemID, int lineNr) 113 { 114 this.attributes = new Properties (); 115 this.children = new Vector (8); 116 this.name = name; 117 this.content = null; 118 this.lineNr = lineNr; 119 this.systemID = systemID; 120 } 121 122 125 protected void finalize() throws Throwable 126 { 127 this.attributes = null; 128 this.children = null; 129 this.name = null; 130 this.content = null; 131 this.systemID = null; 132 super.finalize(); 133 } 134 135 140 public String getName() 141 { 142 return this.name; 143 } 144 145 150 public void setName(String name) 151 { 152 if (name == null) { throw new IllegalArgumentException ("name must not be null"); } 153 154 this.name = name; 155 } 156 157 162 public void addChild(XMLElement child) 163 { 164 if (child == null) { throw new IllegalArgumentException ("child must not be null"); } 165 166 if ((child.getName() == null) && (!this.children.isEmpty())) 167 { 168 XMLElement lastChild = (XMLElement) this.children.lastElement(); 169 170 if (lastChild.getName() == null) 171 { 172 lastChild.setContent(lastChild.getContent() + child.getContent()); 173 return; 174 } 175 } 176 177 this.children.addElement(child); 178 } 179 180 185 public void removeChild(XMLElement child) 186 { 187 if (child == null) { throw new IllegalArgumentException ("child must not be null"); } 188 189 this.children.removeElement(child); 190 } 191 192 197 public void removeChildAtIndex(int index) 198 { 199 this.children.removeElementAt(index); 200 } 201 202 207 public Enumeration enumerateChildren() 208 { 209 return this.children.elements(); 210 } 211 212 217 public boolean isLeaf() 218 { 219 return this.children.isEmpty(); 220 } 221 222 227 public boolean hasChildren() 228 { 229 return (!this.children.isEmpty()); 230 } 231 232 237 public int getChildrenCount() 238 { 239 return this.children.size(); 240 } 241 242 247 public Vector getChildren() 248 { 249 return this.children; 250 } 251 252 259 public XMLElement getChildAtIndex(int index) throws ArrayIndexOutOfBoundsException 260 { 261 return (XMLElement) this.children.elementAt(index); 262 } 263 264 271 public XMLElement getFirstChildNamed(String name) 272 { 273 Enumeration enumeration = this.children.elements(); 274 275 while (enumeration.hasMoreElements()) 276 { 277 XMLElement child = (XMLElement) enumeration.nextElement(); 278 String cName = child.getName(); 279 280 if (cName != null && cName.equals(name)) { return child; } 281 } 282 283 return null; 284 } 285 286 293 public Vector getChildrenNamed(String name) 294 { 295 Vector result = new Vector (this.children.size()); 296 Enumeration enumeration = this.children.elements(); 297 298 while (enumeration.hasMoreElements()) 299 { 300 XMLElement child = (XMLElement) enumeration.nextElement(); 301 String cName = child.getName(); 302 303 if (cName != null && cName.equals(name)) 304 { 305 result.addElement(child); 306 } 307 } 308 309 return result; 310 } 311 312 319 public String getAttribute(String name) 320 { 321 return this.getAttribute(name, null); 322 } 323 324 332 public String getAttribute(String name, String defaultValue) 333 { 334 return this.attributes.getProperty(name, defaultValue); 335 } 336 337 343 public void setAttribute(String name, String value) 344 { 345 this.attributes.put(name, value); 346 } 347 348 353 public void removeAttribute(String name) 354 { 355 this.attributes.remove(name); 356 } 357 358 363 public Enumeration enumerateAttributeNames() 364 { 365 return this.attributes.keys(); 366 } 367 368 373 public boolean hasAttribute(String name) 374 { 375 return this.attributes.containsKey(name); 376 } 377 378 383 public Properties getAttributes() 384 { 385 return this.attributes; 386 } 387 388 395 public String getSystemID() 396 { 397 return this.systemID; 398 } 399 400 408 public int getLineNr() 409 { 410 return this.lineNr; 411 } 412 413 420 public String getContent() 421 { 422 return this.content; 423 } 424 425 431 public void setContent(String content) 432 { 433 this.content = content; 434 } 435 436 } 437 | Popular Tags |