1 9 package javolution.xml.sax; 10 11 import j2me.lang.CharSequence; 12 import j2me.lang.UnsupportedOperationException; 13 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.Reader ; 17 18 import javolution.lang.Reflection; 19 import javolution.lang.Reusable; 20 import javolution.text.CharArray; 21 import javolution.xml.stream.XMLStreamConstants; 22 import javolution.xml.stream.XMLStreamException; 23 import javolution.xml.stream.XMLStreamReaderImpl; 24 25 import org.xml.sax.DTDHandler ; 26 import org.xml.sax.EntityResolver ; 27 import org.xml.sax.ErrorHandler ; 28 import org.xml.sax.InputSource ; 29 import org.xml.sax.SAXException ; 30 import org.xml.sax.SAXNotRecognizedException ; 31 import org.xml.sax.SAXNotSupportedException ; 32 33 53 public class XMLReaderImpl implements XMLReader, Reusable { 54 55 58 private static DefaultHandler DEFAULT_HANDLER = new DefaultHandler(); 59 60 63 private ContentHandler _contentHandler; 64 65 68 private ErrorHandler _errorHandler; 69 70 73 private final XMLStreamReaderImpl _xmlReader = new XMLStreamReaderImpl(); 74 75 78 public XMLReaderImpl() { 79 setContentHandler(DEFAULT_HANDLER); 81 setErrorHandler(DEFAULT_HANDLER); 82 } 83 84 95 public void parse(InputStream in) throws IOException , SAXException { 96 try { 97 _xmlReader.setInput(in); 98 parseAll(); 99 } catch (XMLStreamException e) { 100 if (e.getNestedException() instanceof IOException ) 101 throw (IOException )e.getNestedException(); 102 throw new SAXException (e); 103 } finally { 104 _xmlReader.reset(); 105 } 106 } 107 108 119 public void parse(InputStream in, String encoding) throws IOException , SAXException { 120 try { 121 _xmlReader.setInput(in, encoding); 122 parseAll(); 123 } catch (XMLStreamException e) { 124 if (e.getNestedException() instanceof IOException ) 125 throw (IOException )e.getNestedException(); 126 throw new SAXException (e); 127 } finally { 128 _xmlReader.reset(); 129 } 130 } 131 132 144 public void parse(Reader reader) throws IOException , SAXException { 145 try { 146 _xmlReader.setInput(reader); 147 parseAll(); 148 } catch (XMLStreamException e) { 149 if (e.getNestedException() instanceof IOException ) 150 throw (IOException )e.getNestedException(); 151 throw new SAXException (e); 152 } finally { 153 _xmlReader.reset(); 154 } 155 } 156 157 public void parse(InputSource input) throws IOException , SAXException { 159 Reader reader = input.getCharacterStream(); 160 if (reader != null) { 161 parse(reader); 162 } else { 163 InputStream inStream = input.getByteStream(); 164 if (inStream != null) { 165 parse(inStream, input.getEncoding()); 166 } else { 167 parse(input.getSystemId()); 168 } 169 } 170 } 171 172 public void parse(String systemId) throws IOException , SAXException { 174 InputStream inStream; 175 try { 176 Object url = NEW_URL.newInstance(systemId); 177 inStream = (InputStream ) OPEN_STREAM.invoke(url); 178 } catch (Exception urlException) { try { 180 inStream = (InputStream ) NEW_FILE_INPUT_STREAM.newInstance(systemId); 181 } catch (Exception fileException) { 182 throw new UnsupportedOperationException ("Cannot parse " 183 + systemId); 184 } 185 } 186 parse(inStream); 187 } 188 189 private static final Reflection.Constructor NEW_URL = Reflection 190 .getConstructor("java.net.URL(j2me.lang.String)"); 191 192 private static final Reflection.Method OPEN_STREAM = Reflection 193 .getMethod("java.net.URL.openStream()"); 194 195 private static final Reflection.Constructor NEW_FILE_INPUT_STREAM = Reflection 196 .getConstructor("j2me.io.FileInputStream(j2me.lang.String)"); 197 198 public void setContentHandler(ContentHandler handler) { 200 if (handler != null) { 201 _contentHandler = handler; 202 } else { 203 throw new NullPointerException (); 204 } 205 } 206 207 public ContentHandler getContentHandler() { 209 return (_contentHandler == DEFAULT_HANDLER) ? null : _contentHandler; 210 } 211 212 public void setErrorHandler(ErrorHandler handler) { 214 if (handler != null) { 215 _errorHandler = handler; 216 } else { 217 throw new NullPointerException (); 218 } 219 } 220 221 public ErrorHandler getErrorHandler() { 223 return (_errorHandler == DEFAULT_HANDLER) ? null : _errorHandler; 224 } 225 226 public boolean getFeature(String name) throws SAXNotRecognizedException , 228 SAXNotSupportedException { 229 if (name.equals("http://xml.org/sax/features/namespaces")) { 230 return true; 231 } else if (name 232 .equals("http://xml.org/sax/features/namespace-prefixes")) { 233 return true; 234 } else { 235 throw new SAXNotRecognizedException ("Feature " + name 236 + " not recognized"); 237 } 238 } 239 240 public void setFeature(String name, boolean value) 241 throws SAXNotRecognizedException , SAXNotSupportedException { 242 if (name.equals("http://xml.org/sax/features/namespaces") 243 || name 244 .equals("http://xml.org/sax/features/namespace-prefixes")) { 245 return; } else { 247 throw new SAXNotRecognizedException ("Feature " + name 248 + " not recognized"); 249 } 250 } 251 252 public Object getProperty(String name) throws SAXNotRecognizedException , 253 SAXNotSupportedException { 254 throw new SAXNotRecognizedException ("Property " + name 255 + " not recognized"); 256 } 257 258 public void setProperty(String name, Object value) 259 throws SAXNotRecognizedException , SAXNotSupportedException { 260 throw new SAXNotRecognizedException ("Property " + name 261 + " not recognized"); 262 } 263 264 public void setEntityResolver(EntityResolver resolver) { 265 _entityResolver = resolver; 266 } 267 268 private EntityResolver _entityResolver; 269 270 public EntityResolver getEntityResolver() { 271 return _entityResolver; 272 } 273 274 public void setDTDHandler(DTDHandler handler) { 275 _dtdHandler = handler; 276 } 277 278 private DTDHandler _dtdHandler; 279 280 public DTDHandler getDTDHandler() { 281 return _dtdHandler; 282 } 283 284 public void reset() { 286 setContentHandler(DEFAULT_HANDLER); 287 setErrorHandler(DEFAULT_HANDLER); 288 _xmlReader.reset(); 289 } 290 291 299 private void parseAll() throws XMLStreamException, SAXException { 300 int eventType = _xmlReader.getEventType(); 301 if (eventType != XMLStreamConstants.START_DOCUMENT) 302 throw new SAXException ("Currently parsing"); 303 _contentHandler.startDocument(); 304 305 boolean doContinue = true; 306 while (doContinue) { 307 CharArray uri, localName, qName, prefix, text; 308 switch (_xmlReader.next()) { 309 case XMLStreamConstants.START_ELEMENT: 310 311 for (int i = 0, count = _xmlReader.getNamespaceCount(); i < count; i++) { 313 prefix = _xmlReader.getNamespacePrefix(i); 314 prefix = (prefix == null) ? NO_CHAR : prefix; uri = _xmlReader.getNamespaceURI(i); 316 _contentHandler.startPrefixMapping(prefix, uri); 317 } 318 319 uri = _xmlReader.getNamespaceURI(); 321 uri = (uri == null) ? NO_CHAR : uri; 322 localName = _xmlReader.getLocalName(); 323 qName = _xmlReader.getQName(); 324 _contentHandler.startElement(uri, localName, qName, _xmlReader 325 .getAttributes()); 326 break; 327 328 case XMLStreamConstants.END_ELEMENT: 329 330 uri = _xmlReader.getNamespaceURI(); 332 uri = (uri == null) ? NO_CHAR : uri; 333 localName = _xmlReader.getLocalName(); 334 qName = _xmlReader.getQName(); 335 _contentHandler.endElement(uri, localName, qName); 336 337 for (int i = 0, count = _xmlReader.getNamespaceCount(); i < count; i++) { 339 prefix = _xmlReader.getNamespacePrefix(i); 340 prefix = (prefix == null) ? NO_CHAR : prefix; _contentHandler.endPrefixMapping(prefix); 342 } 343 break; 344 345 case XMLStreamConstants.CDATA: 346 case XMLStreamConstants.CHARACTERS: 347 text = _xmlReader.getText(); 348 _contentHandler.characters(text.array(), text.offset(), text 349 .length()); 350 break; 351 352 case XMLStreamConstants.SPACE: 353 text = _xmlReader.getText(); 354 _contentHandler.ignorableWhitespace(text.array(), 355 text.offset(), text.length()); 356 break; 357 358 case XMLStreamConstants.PROCESSING_INSTRUCTION: 359 _contentHandler.processingInstruction( 360 _xmlReader.getPITarget(), _xmlReader.getPIData()); 361 break; 362 363 case XMLStreamConstants.COMMENT: 364 break; 366 367 case XMLStreamConstants.END_DOCUMENT: 368 doContinue = false; 369 _xmlReader.close(); 370 break; 371 372 } 373 } 374 } 375 376 private static final CharArray NO_CHAR = new CharArray(""); 377 } | Popular Tags |