1 9 package javolution.xml; 10 11 import j2me.lang.CharSequence; 12 import javolution.lang.Text; 13 import javolution.lang.TextBuilder; 14 import javolution.lang.TypeFormat; 15 import javolution.realtime.ObjectFactory; 16 import javolution.util.FastComparator; 17 import javolution.util.FastList; 18 import javolution.util.FastMap; 19 import javolution.util.FastTable; 20 import javolution.xml.sax.Attributes; 21 import javolution.xml.sax.AttributesImpl; 22 23 38 public final class XmlElement { 39 40 43 static final ObjectFactory FACTORY = new ObjectFactory() { 44 protected Object create() { 45 return new XmlElement(); 46 } 47 }; 48 49 52 Class _objectClass; 53 54 57 XmlElement _parent; 58 59 62 XmlFormat _format; 63 64 67 Object _object; 68 69 72 CharSequence _idValue; 73 74 77 AttributesImpl _attributes = new AttributesImpl(); 78 79 82 CharSequence _name; 83 84 87 final FastList _content = new FastList(); 88 89 92 final FastMap _nameToChild 93 = new FastMap().setKeyComparator(FastComparator.LEXICAL); 94 95 98 private FastTable _pool = new FastTable(); 99 100 103 private int _poolIndex; 104 105 108 XmlElement() { 109 } 110 111 118 public Object object() throws XmlException { 119 if (_object == null) { 120 try { 121 _object = _objectClass.newInstance(); 122 } catch (IllegalAccessException e2) { 123 throw new XmlException(_objectClass 124 + " default constructor inaccessible"); 125 } catch (InstantiationException e3) { 126 throw new XmlException(_objectClass 127 + " default constructor throws an exception"); 128 } 129 } 130 return (Object) _object; 131 } 132 133 142 public FastList getContent() { 143 return _content; 144 } 145 146 150 159 public void add(String name, Object obj) { 160 if (obj == null) return; 161 _nameToChild.put(name, obj); 162 } 163 164 176 public TextBuilder newAttribute(String name) { 177 TextBuilder value = newTextBuilder(); 178 setAttribute(name, value); 179 return value; 180 } 181 182 189 public void setAttribute(String name, CharSequence value) { 190 if (value == null) 191 return; 192 CharSequence csqName = toCharSeq(name); 193 _attributes.addAttribute(Text.EMPTY, csqName, Text.EMPTY, csqName, "CDATA", value); 194 } 195 196 197 204 public void setAttribute(String name, String value) { 205 if (value == null) 206 return; 207 CharSequence csqName = toCharSeq(name); 208 CharSequence csqValue = toCharSeq(value); 209 _attributes.addAttribute(Text.EMPTY, csqName, Text.EMPTY, csqName, "CDATA", csqValue); 210 } 211 212 219 public void setAttribute(String name, boolean value) { 220 newAttribute(name).append(value); 221 } 222 223 230 public void setAttribute(String name, int value) { 231 newAttribute(name).append(value); 232 } 233 234 241 public void setAttribute(String name, long value) { 242 newAttribute(name).append(value); 243 } 244 245 256 257 268 269 273 279 void add(CharSequence name, Object obj) { 280 TextBuilder tb = newTextBuilder(); 281 tb.append(name); 282 _nameToChild.put(tb, obj); 283 } 284 285 290 public XmlElement getParent() { 291 return _parent; 292 } 293 294 301 public Class objectClass() { 302 return _objectClass; 303 } 304 305 312 public Object get(String name) { 313 return (Object)_nameToChild.get(name); 314 } 315 316 321 public Attributes getAttributes() { 322 return _attributes; 323 } 324 325 332 public CharSequence getAttribute(String name) { 333 return _attributes.getValue(name); 334 } 335 336 343 public boolean isAttribute(String name) { 344 return _attributes.getIndex(name) >= 0; 345 } 346 347 355 public CharSequence getAttribute(String name, CharSequence defaultValue) { 356 CharSequence value = _attributes.getValue(name); 357 return (value != null) ? value : defaultValue; 358 } 359 360 368 public String getAttribute(String name, String defaultValue) { 369 CharSequence value = _attributes.getValue(name); 370 return (value != null) ? value.toString() : defaultValue; 371 } 372 373 381 public boolean getAttribute(String name, boolean defaultValue) { 382 CharSequence value = _attributes.getValue(name); 383 return (value != null) ? TypeFormat.parseBoolean(value) : defaultValue; 384 } 385 386 395 public int getAttribute(String name, int defaultValue) { 396 CharSequence value = _attributes.getValue(name); 397 return (value != null) ? TypeFormat.parseInt(value) : defaultValue; 398 } 399 400 409 public long getAttribute(String name, long defaultValue) { 410 CharSequence value = _attributes.getValue(name); 411 return (value != null) ? TypeFormat.parseLong(value) : defaultValue; 412 } 413 414 428 429 442 443 446 void reset() { 447 _object = null; 448 _format = null; 449 _objectClass = null; 450 _idValue = null; 451 _name = null; 452 _content.clear(); 453 _nameToChild.clear(); 454 _attributes.reset(); 455 _poolIndex = 0; 456 } 457 458 464 private CharSequence toCharSeq(Object str) { 465 if (str instanceof CharSequence) 466 return (CharSequence) str; 467 TextBuilder tb = newTextBuilder(); 468 tb.append(str); 469 return tb; 470 } 471 472 475 private TextBuilder newTextBuilder() { 476 if (_poolIndex >= _pool.size()) { 477 _pool.addLast(TextBuilder.newInstance().moveHeap()); 478 } 479 TextBuilder tb = (TextBuilder) _pool.get(_poolIndex++); 480 tb.reset(); 481 return tb; 482 } 483 484 488 boolean isRecursion() { 489 for (XmlElement xml = _parent; xml != null; xml = xml._parent) { 490 if (xml._object == _object) return true; 491 } 492 return false; 493 } 494 495 502 final Class classFor(CharSequence tagName) { 503 return ((_parent != null) && (_parent._format != null)) ? 504 _parent._format.classFor(tagName) : null; 505 } 506 } | Popular Tags |