1 2 3 package org.xmlpull.v1.sax2; 4 5 import java.io.InputStream ; 6 import java.io.InputStreamReader ; 7 import java.io.IOException ; 8 import java.io.Reader ; 9 10 import java.net.URL ; 12 import java.net.MalformedURLException ; 13 14 15 import java.io.FileInputStream ; 17 import java.io.FileNotFoundException ; 18 import java.io.StringReader ; 19 import java.io.StringWriter ; 20 import java.io.UnsupportedEncodingException ; 21 22 import org.xml.sax.Attributes ; 23 import org.xml.sax.DTDHandler ; 24 import org.xml.sax.ContentHandler ; 25 import org.xml.sax.EntityResolver ; 26 import org.xml.sax.ErrorHandler ; 27 import org.xml.sax.InputSource ; 28 import org.xml.sax.Locator ; 29 import org.xml.sax.SAXException ; 30 import org.xml.sax.SAXParseException ; 31 import org.xml.sax.SAXNotRecognizedException ; 32 import org.xml.sax.SAXNotSupportedException ; 33 import org.xml.sax.XMLReader ; 34 import org.xml.sax.helpers.DefaultHandler ; 35 36 import org.xmlpull.v1.XmlPullParser; 37 import org.xmlpull.v1.XmlPullParserException; 38 import org.xmlpull.v1.XmlPullParserFactory; 39 40 47 48 public class Driver implements Locator , XMLReader , Attributes 49 { 50 51 protected static final String DECLARATION_HANDLER_PROPERTY = 52 "http://xml.org/sax/properties/declaration-handler"; 53 54 protected static final String LEXICAL_HANDLER_PROPERTY = 55 "http://xml.org/sax/properties/lexical-handler"; 56 57 protected static final String NAMESPACES_FEATURE = 58 "http://xml.org/sax/features/namespaces"; 59 60 protected static final String NAMESPACE_PREFIXES_FEATURE = 61 "http://xml.org/sax/features/namespace-prefixes"; 62 63 protected static final String VALIDATION_FEATURE = 64 "http://xml.org/sax/features/validation"; 65 66 protected static final String APACHE_SCHEMA_VALIDATION_FEATURE = 67 "http://apache.org/xml/features/validation/schema"; 68 69 protected static final String APACHE_DYNAMIC_VALIDATION_FEATURE = 70 "http://apache.org/xml/features/validation/dynamic"; 71 72 protected ContentHandler contentHandler = new DefaultHandler (); 73 protected ErrorHandler errorHandler = new DefaultHandler ();; 74 75 protected String systemId; 76 77 protected XmlPullParser pp; 78 79 81 83 public Driver() throws XmlPullParserException { 84 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 85 factory.setNamespaceAware(true); 86 pp = factory.newPullParser(); 87 } 88 89 public Driver(XmlPullParser pp) throws XmlPullParserException { 90 this.pp = pp; 91 } 92 93 95 public int getLength() { return pp.getAttributeCount(); } 96 public String getURI(int index) { return pp.getAttributeNamespace(index); } 97 public String getLocalName(int index) { return pp.getAttributeName(index); } 98 public String getQName(int index) { 99 String prefix = pp.getAttributePrefix(index); 100 if(prefix != null) { 101 return prefix+':'+pp.getAttributeName(index); 102 } else { 103 return pp.getAttributeName(index); 104 } 105 } 106 public String getType(int index) { return pp.getAttributeType(index); } 107 public String getValue(int index) { return pp.getAttributeValue(index); } 108 109 public int getIndex(String uri, String localName) { 110 for (int i = 0; i < pp.getAttributeCount(); i++) 111 { 112 if(pp.getAttributeNamespace(i).equals(uri) 113 && pp.getAttributeName(i).equals(localName)) 114 { 115 return i; 116 } 117 118 } 119 return -1; 120 } 121 122 public int getIndex(String qName) { 123 for (int i = 0; i < pp.getAttributeCount(); i++) 124 { 125 if(pp.getAttributeName(i).equals(qName)) 126 { 127 return i; 128 } 129 130 } 131 return -1; 132 } 133 134 public String getType(String uri, String localName) { 135 for (int i = 0; i < pp.getAttributeCount(); i++) 136 { 137 if(pp.getAttributeNamespace(i).equals(uri) 138 && pp.getAttributeName(i).equals(localName)) 139 { 140 return pp.getAttributeType(i); 141 } 142 143 } 144 return null; 145 } 146 public String getType(String qName) { 147 for (int i = 0; i < pp.getAttributeCount(); i++) 148 { 149 if(pp.getAttributeName(i).equals(qName)) 150 { 151 return pp.getAttributeType(i); 152 } 153 154 } 155 return null; 156 } 157 public String getValue(String uri, String localName) { 158 return pp.getAttributeValue(uri, localName); 159 } 160 public String getValue(String qName) { 161 return pp.getAttributeValue(null, qName); 162 } 163 164 166 public String getPublicId() { return null; } 167 public String getSystemId() { return systemId; } 168 public int getLineNumber() { return pp.getLineNumber(); } 169 public int getColumnNumber() { return pp.getColumnNumber(); } 170 171 173 public boolean getFeature(String name) 174 throws SAXNotRecognizedException , SAXNotSupportedException 175 { 176 if(NAMESPACES_FEATURE.equals(name)) { 177 return pp.getFeature(pp.FEATURE_PROCESS_NAMESPACES); 178 } else if(NAMESPACE_PREFIXES_FEATURE.equals(name)) { 179 return pp.getFeature(pp.FEATURE_REPORT_NAMESPACE_ATTRIBUTES); 180 } else if(VALIDATION_FEATURE.equals(name)) { 181 return pp.getFeature(pp.FEATURE_VALIDATION); 182 } else { 187 return pp.getFeature(name); 188 } 190 } 191 192 public void setFeature (String name, boolean value) 193 throws SAXNotRecognizedException , SAXNotSupportedException 194 { 195 try { 196 if(NAMESPACES_FEATURE.equals(name)) { 197 pp.setFeature(pp.FEATURE_PROCESS_NAMESPACES, value); 198 } else if(NAMESPACE_PREFIXES_FEATURE.equals(name)) { 199 if(pp.getFeature(pp.FEATURE_REPORT_NAMESPACE_ATTRIBUTES) != value) { 200 pp.setFeature(pp.FEATURE_REPORT_NAMESPACE_ATTRIBUTES, value); 201 } 202 } else if(VALIDATION_FEATURE.equals(name)) { 203 pp.setFeature(pp.FEATURE_VALIDATION, value); 204 } else { 214 pp.setFeature(name, value); 215 } 217 } catch(XmlPullParserException ex) { 218 throw new SAXNotSupportedException ("problem with setting feature "+name+": "+ex); 219 } 220 } 221 222 public Object getProperty (String name) 223 throws SAXNotRecognizedException , SAXNotSupportedException 224 { 225 if(DECLARATION_HANDLER_PROPERTY.equals(name)) { 226 return null; 227 } else if(LEXICAL_HANDLER_PROPERTY.equals(name)) { 228 return null; 229 } else { 230 return pp.getProperty(name); 231 } 233 } 234 235 public void setProperty (String name, Object value) 236 throws SAXNotRecognizedException , SAXNotSupportedException 237 { 238 if(DECLARATION_HANDLER_PROPERTY.equals(name)) { 240 throw new SAXNotSupportedException ("not supported setting property "+name); } else if(LEXICAL_HANDLER_PROPERTY.equals(name)) { 242 throw new SAXNotSupportedException ("not supported setting property "+name); } else { 244 try { 245 pp.setProperty(name, value); 246 } catch(XmlPullParserException ex) { 247 throw new SAXNotSupportedException ("not supported set property "+name+": "+ ex); 248 } 249 } 251 } 252 253 public void setEntityResolver (EntityResolver resolver) {} 254 255 public EntityResolver getEntityResolver () { return null; } 256 257 public void setDTDHandler (DTDHandler handler) {} 258 259 public DTDHandler getDTDHandler () { return null; } 260 261 public void setContentHandler (ContentHandler handler) 262 { 263 this.contentHandler = handler; 264 } 265 266 public ContentHandler getContentHandler() { return contentHandler; } 267 268 public void setErrorHandler(ErrorHandler handler) { 269 this.errorHandler = handler; 270 } 271 272 public ErrorHandler getErrorHandler() { return errorHandler; } 273 274 public void parse(InputSource source) throws SAXException , IOException 275 { 276 277 systemId = source.getSystemId(); 278 contentHandler.setDocumentLocator(this); 279 280 Reader reader = source.getCharacterStream(); 281 try { 282 if (reader == null) { 283 InputStream stream = source.getByteStream(); 284 String encoding = source.getEncoding(); 285 286 if (stream == null) { 287 systemId = source.getSystemId(); 288 if(systemId == null) { 289 SAXParseException saxException = new SAXParseException ( 290 "null source systemId" , this); 291 errorHandler.fatalError(saxException); 292 return; 293 } 294 try { 295 URL url = new URL (systemId); 296 stream = url.openStream(); 297 } catch (MalformedURLException nue) { 298 try { 299 stream = new FileInputStream (systemId); 300 } catch (FileNotFoundException fnfe) { 301 SAXParseException saxException = new SAXParseException ( 302 "could not open file with systemId "+systemId, this, fnfe); 303 errorHandler.fatalError(saxException); 304 return; 305 } 306 } 307 } 308 pp.setInput(stream, encoding); 309 } else { 310 pp.setInput(reader); 311 } 312 } catch (XmlPullParserException ex) { 313 SAXParseException saxException = new SAXParseException ( 314 "parsing initialization error: "+ex, this, ex); 315 errorHandler.fatalError(saxException); 317 return; 318 } 319 320 try { 322 contentHandler.startDocument(); 323 pp.next(); 325 if(pp.getEventType() != pp.START_TAG) { 327 SAXParseException saxException = new SAXParseException ( 328 "expected start tag not"+pp.getPositionDescription(), this); 329 errorHandler.fatalError(saxException); 331 return; 332 } 333 } catch (XmlPullParserException ex) { 334 SAXParseException saxException = new SAXParseException ( 335 "parsing initialization error: "+ex, this, ex); 336 errorHandler.fatalError(saxException); 338 return; 339 } 340 341 343 parseSubTree(pp); 344 345 347 contentHandler.endDocument(); 348 } 349 350 public void parse(String systemId) throws SAXException , IOException { 351 parse(new InputSource (systemId)); 352 } 353 354 355 public void parseSubTree(XmlPullParser pp) throws SAXException , IOException { 356 this.pp = pp; 357 boolean namespaceAware = pp.getFeature(pp.FEATURE_PROCESS_NAMESPACES); 358 try { 359 if(pp.getEventType() != pp.START_TAG) { 360 throw new SAXException ( 361 "start tag must be read before skiping subtree"+pp.getPositionDescription()); 362 } 363 int[] holderForStartAndLength = new int[2]; 364 StringBuffer rawName = new StringBuffer (); 365 String prefix = null; 366 String name = null; 367 int level = pp.getDepth() - 1; 368 int type = pp.START_TAG; 369 370 LOOP: 371 do { 372 switch(type) { 373 case XmlPullParser.START_TAG: 374 if(namespaceAware) { 375 int depth = pp.getDepth() - 1; 376 int countPrev = 377 (level > depth) ? pp.getNamespaceCount(depth) : 0; 378 int count = pp.getNamespaceCount(depth + 1); 380 for (int i = countPrev; i < count; i++) 381 { 382 contentHandler.startPrefixMapping( 383 pp.getNamespacePrefix(i), 384 pp.getNamespaceUri(i) 385 ); 386 } 387 name = pp.getName(); 388 prefix = pp.getPrefix(); 389 if(prefix != null) { 390 rawName.setLength(0); 391 rawName.append(prefix); 392 rawName.append(':'); 393 rawName.append(name); 394 } 395 startElement(pp.getNamespace(), 396 name, 397 prefix != null ? name : rawName.toString()); 398 } else { 399 startElement(pp.getNamespace(), 400 pp.getName(), 401 pp.getName()); 402 } 403 405 break; 406 case XmlPullParser.TEXT: 407 char[] chars = pp.getTextCharacters(holderForStartAndLength); 408 contentHandler.characters(chars, 409 holderForStartAndLength[0], holderForStartAndLength[1] ); 412 break; 413 case XmlPullParser.END_TAG: 414 if(namespaceAware) { 416 name = pp.getName(); 417 prefix = pp.getPrefix(); 418 if(prefix != null) { 419 rawName.setLength(0); 420 rawName.append(prefix); 421 rawName.append(':'); 422 rawName.append(name); 423 } 424 contentHandler.endElement(pp.getNamespace(), 425 name, 426 prefix != null ? name : rawName.toString() 427 ); 428 int depth = pp.getDepth(); 430 int countPrev = 431 (level > depth) ? pp.getNamespaceCount(pp.getDepth()) : 0; 432 int count = pp.getNamespaceCount(pp.getDepth() - 1); 433 for (int i = count - 1; i >= countPrev; i--) 435 { 436 contentHandler.endPrefixMapping( 437 pp.getNamespacePrefix(i) 438 ); 439 } 440 } else { 441 contentHandler.endElement(pp.getNamespace(), 442 pp.getName(), 443 pp.getName() 444 ); 445 446 } 447 break; 448 case XmlPullParser.END_DOCUMENT: 449 break LOOP; 450 } 451 type = pp.next(); 452 } while(pp.getDepth() > level); 453 } catch (XmlPullParserException ex) { 454 SAXParseException saxException = new SAXParseException ("parsing error: "+ex, this, ex); 455 ex.printStackTrace(); 456 errorHandler.fatalError(saxException); 457 } 458 } 459 460 467 protected void startElement(String namespace, String localName, String qName) throws SAXException { 468 contentHandler.startElement(namespace, localName, qName, this); 469 } 470 471 } 472 473 474 475 | Popular Tags |