1 20 21 27 28 package com.sun.org.apache.xalan.internal.xsltc.trax; 29 30 import java.io.IOException ; 31 import java.util.Iterator ; 32 33 import org.xml.sax.Attributes ; 34 import org.xml.sax.ContentHandler ; 35 import org.xml.sax.DTDHandler ; 36 import org.xml.sax.EntityResolver ; 37 import org.xml.sax.ErrorHandler ; 38 import org.xml.sax.InputSource ; 39 import org.xml.sax.Locator ; 40 import org.xml.sax.SAXException ; 41 import org.xml.sax.SAXNotRecognizedException ; 42 import org.xml.sax.SAXNotSupportedException ; 43 import org.xml.sax.XMLReader ; 44 import org.xml.sax.ext.LexicalHandler ; 45 import org.xml.sax.helpers.AttributesImpl ; 46 import org.xml.sax.ext.Locator2 ; 47 import com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl; 48 49 import javax.xml.namespace.QName ; 50 import javax.xml.stream.XMLEventReader; 51 import javax.xml.stream.XMLStreamConstants; 52 import javax.xml.stream.XMLStreamException; 53 import javax.xml.stream.events.Attribute; 54 import javax.xml.stream.events.Characters; 55 import javax.xml.stream.events.EndElement; 56 import javax.xml.stream.events.Namespace; 57 import javax.xml.stream.events.ProcessingInstruction; 58 import javax.xml.stream.events.StartElement; 59 import javax.xml.stream.events.XMLEvent; 60 import javax.xml.stream.events.StartDocument; 61 62 63 68 public class StAXEvent2SAX implements XMLReader , Locator { 69 70 73 private final XMLEventReader staxEventReader; 75 76 private ContentHandler _sax = null; 78 private LexicalHandler _lex = null; 79 private SAXImpl _saxImpl = null; 80 private String version = null; 82 private String encoding = null; 83 84 85 public StAXEvent2SAX(XMLEventReader staxCore) { 86 staxEventReader = staxCore; 87 } 88 89 public ContentHandler getContentHandler() { 90 return _sax; 91 } 92 93 public void setContentHandler(ContentHandler handler) throws 94 NullPointerException 95 { 96 _sax = handler; 97 if (handler instanceof LexicalHandler ) { 98 _lex = (LexicalHandler ) handler; 99 } 100 101 if (handler instanceof SAXImpl) { 102 _saxImpl = (SAXImpl)handler; 103 } 104 } 105 106 107 public void parse(InputSource unused) throws IOException , SAXException { 108 try { 109 bridge(); 110 } catch (XMLStreamException e) { 111 throw new SAXException (e); 112 } 113 } 114 115 116 public void parse() throws IOException , SAXException , XMLStreamException { 118 bridge(); 119 } 120 121 122 138 139 142 private void bridge() throws XMLStreamException { 143 144 try { 145 int depth=0; 147 148 XMLEvent event = staxEventReader.peek(); 149 150 if (!event.isStartDocument() && !event.isStartElement()) { 151 throw new IllegalStateException (); 152 } 153 154 while( !event.isStartElement() ) { 156 if (event.getEventType() == XMLStreamConstants.START_DOCUMENT){ 157 version = ((StartDocument)event).getVersion(); 158 if (((StartDocument)event).encodingSet()) 159 encoding = ((StartDocument)event).getCharacterEncodingScheme(); 160 } 161 162 event = staxEventReader.nextEvent(); 163 } 164 165 handleStartDocument(event); 166 167 do { 168 switch (event.getEventType()) { 172 case XMLStreamConstants.START_ELEMENT : 173 depth++; 174 handleStartElement(event.asStartElement()); 175 break; 176 case XMLStreamConstants.END_ELEMENT : 177 handleEndElement(event.asEndElement()); 178 depth--; 179 break; 180 case XMLStreamConstants.CHARACTERS : 181 handleCharacters(event.asCharacters()); 182 break; 183 case XMLStreamConstants.ENTITY_REFERENCE : 184 handleEntityReference(); 185 break; 186 case XMLStreamConstants.PROCESSING_INSTRUCTION : 187 handlePI((ProcessingInstruction)event); 188 break; 189 case XMLStreamConstants.COMMENT : 190 handleComment(); 191 break; 192 case XMLStreamConstants.DTD : 193 handleDTD(); 194 break; 195 case XMLStreamConstants.ATTRIBUTE : 196 handleAttribute(); 197 break; 198 case XMLStreamConstants.NAMESPACE : 199 handleNamespace(); 200 break; 201 case XMLStreamConstants.CDATA : 202 handleCDATA(); 203 break; 204 case XMLStreamConstants.ENTITY_DECLARATION : 205 handleEntityDecl(); 206 break; 207 case XMLStreamConstants.NOTATION_DECLARATION : 208 handleNotationDecl(); 209 break; 210 case XMLStreamConstants.SPACE : 211 handleSpace(); 212 break; 213 default : 214 throw new InternalError ("processing event: " + event); 215 } 216 217 event=staxEventReader.nextEvent(); 218 } while (depth!=0); 219 220 handleEndDocument(); 221 } catch (SAXException e) { 222 throw new XMLStreamException(e); 223 } 224 } 225 226 227 private void handleEndDocument() throws SAXException { 228 _sax.endDocument(); 229 } 230 231 private void handleStartDocument(final XMLEvent event) throws SAXException { 232 _sax.setDocumentLocator(new Locator2 () { 233 public int getColumnNumber() { 234 return event.getLocation().getColumnNumber(); 235 } 236 public int getLineNumber() { 237 return event.getLocation().getLineNumber(); 238 } 239 public String getPublicId() { 240 return event.getLocation().getPublicId(); 241 } 242 public String getSystemId() { 243 return event.getLocation().getSystemId(); 244 } 245 public String getXMLVersion(){ 246 return version; 247 } 248 public String getEncoding(){ 249 return encoding; 250 } 251 252 }); 253 _sax.startDocument(); 254 } 255 256 private void handlePI(ProcessingInstruction event) 257 throws XMLStreamException { 258 try { 259 _sax.processingInstruction( 260 event.getTarget(), 261 event.getData()); 262 } catch (SAXException e) { 263 throw new XMLStreamException(e); 264 } 265 } 266 267 private void handleCharacters(Characters event) throws XMLStreamException { 268 try { 269 _sax.characters( 270 event.getData().toCharArray(), 271 0, 272 event.getData().length()); 273 } catch (SAXException e) { 274 throw new XMLStreamException(e); 275 } 276 } 277 278 private void handleEndElement(EndElement event) throws XMLStreamException { 279 QName qName = event.getName(); 280 281 String qname = ""; 283 if (qName.getPrefix() != null && qName.getPrefix().trim().length() != 0){ 284 qname = qName.getPrefix() + ":"; 285 } 286 qname += qName.getLocalPart(); 287 288 try { 289 _sax.endElement( 291 qName.getNamespaceURI(), 292 qName.getLocalPart(), 293 qname); 294 295 for( Iterator i = event.getNamespaces(); i.hasNext();) { 297 String prefix = (String )i.next(); 298 if( prefix == null ) { prefix = ""; 300 } 301 _sax.endPrefixMapping(prefix); 302 } 303 } catch (SAXException e) { 304 throw new XMLStreamException(e); 305 } 306 } 307 308 private void handleStartElement(StartElement event) 309 throws XMLStreamException { 310 try { 311 for (Iterator i = event.getNamespaces(); i.hasNext();) { 313 String prefix = ((Namespace)i.next()).getPrefix(); 314 if (prefix == null) { prefix = ""; 316 } 317 _sax.startPrefixMapping( 318 prefix, 319 event.getNamespaceURI(prefix)); 320 } 321 322 QName qName = event.getName(); 324 String prefix = qName.getPrefix(); 325 String rawname; 326 if (prefix == null || prefix.length() == 0) { 327 rawname = qName.getLocalPart(); 328 } else { 329 rawname = prefix + ':' + qName.getLocalPart(); 330 } 331 332 Attributes saxAttrs = getAttributes(event); 333 _sax.startElement( 334 qName.getNamespaceURI(), 335 qName.getLocalPart(), 336 rawname, 337 saxAttrs); 338 } catch (SAXException e) { 339 throw new XMLStreamException(e); 340 } 341 } 342 343 348 private Attributes getAttributes(StartElement event) { 349 AttributesImpl attrs = new AttributesImpl (); 350 351 if ( !event.isStartElement() ) { 352 throw new InternalError ( 353 "getAttributes() attempting to process: " + event); 354 } 355 356 360 for (Iterator i = event.getAttributes(); i.hasNext();) { 362 Attribute staxAttr = (javax.xml.stream.events.Attribute)i.next(); 363 364 String uri = staxAttr.getName().getNamespaceURI(); 365 if (uri == null) { 366 uri = ""; 367 } 368 String localName = staxAttr.getName().getLocalPart(); 369 String prefix = staxAttr.getName().getPrefix(); 370 String qName; 371 if (prefix == null || prefix.length() == 0) { 372 qName = localName; 373 } else { 374 qName = prefix + ':' + localName; 375 } 376 String type = staxAttr.getDTDType(); 377 String value = staxAttr.getValue(); 378 379 attrs.addAttribute(uri, localName, qName, type, value); 380 } 381 382 return attrs; 383 } 384 385 private void handleNamespace() { 386 } 390 391 private void handleAttribute() { 392 } 396 397 private void handleDTD() { 398 } 401 402 private void handleComment() { 403 } 405 406 private void handleEntityReference() { 407 } 409 410 private void handleSpace() { 411 } 414 415 private void handleNotationDecl() { 416 } 419 420 private void handleEntityDecl() { 421 } 424 425 private void handleCDATA() { 426 } 429 430 431 435 public DTDHandler getDTDHandler() { 436 return null; 437 } 438 439 443 public ErrorHandler getErrorHandler() { 444 return null; 445 } 446 447 451 public boolean getFeature(String name) throws SAXNotRecognizedException , 452 SAXNotSupportedException 453 { 454 return false; 455 } 456 457 461 public void setFeature(String name, boolean value) throws 462 SAXNotRecognizedException , SAXNotSupportedException 463 { 464 } 465 466 470 public void parse(String sysId) throws IOException , SAXException { 471 throw new IOException ("This method is not yet implemented."); 472 } 473 474 478 public void setDTDHandler(DTDHandler handler) throws NullPointerException { 479 } 480 481 485 public void setEntityResolver(EntityResolver resolver) throws 486 NullPointerException 487 { 488 } 489 490 494 public EntityResolver getEntityResolver() { 495 return null; 496 } 497 498 502 public void setErrorHandler(ErrorHandler handler) throws 503 NullPointerException 504 { 505 } 506 507 511 public void setProperty(String name, Object value) throws 512 SAXNotRecognizedException , SAXNotSupportedException { 513 } 514 515 519 public Object getProperty(String name) throws SAXNotRecognizedException , 520 SAXNotSupportedException 521 { 522 return null; 523 } 524 525 529 public int getColumnNumber() { 530 return 0; 531 } 532 533 537 public int getLineNumber() { 538 return 0; 539 } 540 541 545 public String getPublicId() { 546 return null; 547 } 548 549 553 public String getSystemId() { 554 return null; 555 } 556 } 557 | Popular Tags |