1 9 package javolution.xml; 10 11 import j2me.util.List; 12 import j2me.lang.CharSequence; 13 import javolution.lang.Reusable; 14 import javolution.lang.Text; 15 import javolution.lang.TextBuilder; 16 import javolution.util.FastComparator; 17 import javolution.util.FastMap; 18 import javolution.util.FastTable; 19 import javolution.xml.sax.Attributes; 20 import javolution.xml.sax.AttributesImpl; 21 import javolution.xml.sax.ContentHandler; 22 import org.xml.sax.ErrorHandler; 23 import org.xml.sax.Locator; 24 import org.xml.sax.SAXException; 25 import org.xml.sax.SAXParseException; 26 27 41 public final class ConstructorHandler implements ContentHandler, ErrorHandler, 42 Reusable { 43 44 47 private int _level; 48 49 52 private Object _root; 53 54 57 private final FastTable _stack = new FastTable(); 58 59 62 private final FastMap _idToObject = new FastMap() 63 .setKeyComparator(FastComparator.LEXICAL); 64 65 68 ConstructorHandler() { 69 _stack.addLast(new XmlElement()); 70 } 71 72 77 public Object getRoot() { 78 return _root; 79 } 80 81 87 public void startDocument() throws SAXException { 88 _level = 0; 89 _root = null; 90 } 91 92 98 public void endDocument() throws SAXException { 99 List roots = ((XmlElement) _stack.get(0)).getContent(); 100 if (roots.size() > 0) { 101 _root = roots.get(0); 102 } 103 for (int i = 0; i <= _level;) { 105 ((XmlElement) _stack.get(i++)).reset(); 106 } 107 } 108 109 119 public void startElement(CharSequence uri, CharSequence localName, 120 CharSequence qName, Attributes attrs) throws SAXException { 121 122 if (++_level >= _stack.size()) { 123 XmlElement tmp = (XmlElement) XmlElement.FACTORY.newObject(); 124 tmp._parent = (XmlElement) _stack.get(_level - 1); 125 _stack.addLast(tmp); 126 } 127 XmlElement xml = (XmlElement) _stack.get(_level); 128 xml._name = localName; 129 130 final AttributesImpl attributes = (AttributesImpl) attrs; 131 Class objectClass = xml.classFor(localName); 132 if (objectClass == null) { 133 int i = attributes.getIndex("j:class"); 135 if (i >= 0) { objectClass = XmlFormat.classFor("", attributes.getValue(i)); 137 } else { objectClass = XmlFormat.classFor(uri, localName); 139 xml._name = null; 140 } 141 } 142 final XmlFormat xmlFormat = XmlFormat.getInstance(objectClass); 143 final int attLength = attributes.getLength(); 144 145 if (xmlFormat._idRef != null) { 147 int j = attributes.getIndex(xmlFormat._idRef); 148 if (j >= 0) { final CharSequence idValue = attributes.getValue(j); 150 xml._object = _idToObject.get(idValue); 151 if (xml._object != null) 152 return; if (!xmlFormat._idRef.equals(xmlFormat._idName)) 154 throw new SAXException("Referenced object (" + idValue 155 + ") not found"); 156 } 158 } 159 160 xml._attributes = attributes; 162 if (xmlFormat._idName != null) { 163 xml._idValue = attributes.getValue(xmlFormat._idName); 164 } 165 xml._format = xmlFormat; 166 xml._objectClass = objectClass; 167 xml._object = xmlFormat.preallocate(xml); 168 169 if ((xml._object != null) && (xml._idValue != null)) { 171 _idToObject.put(newId().append(xml._idValue), xml._object); 172 } 173 } 174 175 176 185 public void endElement(CharSequence uri, CharSequence localName, 186 CharSequence qName) throws SAXException { 187 XmlElement xml = ((XmlElement) _stack.get(_level)); 188 if (xml._format != null) { xml._object = xml._format.parse(xml); 190 if (xml._idValue != null) { 191 _idToObject.put(newId().append(xml._idValue), xml._object); 192 } 193 } 194 195 if (xml._name == null) { ((XmlElement) _stack.get(--_level))._content.addLast(xml._object); 198 } else { ((XmlElement) _stack.get(--_level)).add(xml._name, xml._object); 200 } 201 202 xml.reset(); 204 } 205 206 215 public void characters(char ch[], int start, int length) 216 throws SAXException { 217 CharacterData charData = CharacterData.valueOf(Text.valueOf(ch, start, 218 length)); 219 ((XmlElement) _stack.get(_level))._content.addLast(charData); 220 } 221 222 public void setDocumentLocator(Locator locator) { 224 } 225 226 public void startPrefixMapping(CharSequence prefix, CharSequence uri) 228 throws SAXException { 229 } 230 231 public void endPrefixMapping(CharSequence prefix) throws SAXException { 233 } 234 235 public void ignorableWhitespace(char ch[], int start, int length) 237 throws SAXException { 238 } 239 240 public void processingInstruction(CharSequence target, CharSequence data) 242 throws SAXException { 243 } 244 245 public void skippedEntity(CharSequence name) throws SAXException { 247 } 248 249 257 public void warning(SAXParseException e) throws SAXException { 258 System.err.println("XML Parsing Warning: " + e); 259 } 260 261 269 public void error(SAXParseException e) throws SAXException { 270 throw e; 271 } 272 273 280 public void fatalError(SAXParseException e) throws SAXException { 281 throw e; 282 } 283 284 public void reset() { 286 _idPool.addAll(_idToObject.keySet()); 288 _idToObject.clear(); 289 _root = null; 290 } 291 292 297 private TextBuilder newId() { 298 if (_idPool.isEmpty()) 299 return (TextBuilder) TextBuilder.newInstance().moveHeap(); 300 TextBuilder tb = (TextBuilder) _idPool.removeLast(); 301 tb.reset(); 302 return tb; 303 } 304 305 private FastTable _idPool = new FastTable(); } | Popular Tags |