1 20 21 27 28 package com.sun.org.apache.xalan.internal.xsltc.trax; 29 30 import java.io.IOException ; 31 import java.util.Hashtable ; 32 import java.util.Stack ; 33 import java.util.Vector ; 34 import java.util.Iterator ; 35 36 import org.xml.sax.Attributes ; 37 import org.xml.sax.ContentHandler ; 38 import org.xml.sax.DTDHandler ; 39 import org.xml.sax.EntityResolver ; 40 import org.xml.sax.ErrorHandler ; 41 import org.xml.sax.InputSource ; 42 import org.xml.sax.Locator ; 43 import org.xml.sax.SAXException ; 44 import org.xml.sax.SAXNotRecognizedException ; 45 import org.xml.sax.SAXNotSupportedException ; 46 import org.xml.sax.XMLReader ; 47 import org.xml.sax.ext.LexicalHandler ; 48 import org.xml.sax.ext.Locator2 ; 49 import org.xml.sax.helpers.AttributesImpl ; 50 import com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl; 51 52 53 54 import javax.xml.namespace.QName ; 55 import javax.xml.stream.XMLStreamReader; 56 import javax.xml.stream.XMLStreamConstants; 57 import javax.xml.stream.XMLStreamException; 58 import javax.xml.stream.events.Attribute; 59 import javax.xml.stream.events.Characters; 60 import javax.xml.stream.events.EndElement; 61 import javax.xml.stream.events.Namespace; 62 import javax.xml.stream.events.ProcessingInstruction; 63 import javax.xml.stream.events.StartElement; 64 import javax.xml.stream.events.StartDocument; 65 import javax.xml.stream.events.XMLEvent; 66 67 68 69 73 public class StAXStream2SAX implements XMLReader , Locator { 74 75 78 private final XMLStreamReader staxStreamReader; 80 81 private ContentHandler _sax = null; 83 private LexicalHandler _lex = null; 84 private SAXImpl _saxImpl = null; 85 87 public StAXStream2SAX(XMLStreamReader staxSrc) { 88 staxStreamReader = staxSrc; 89 } 90 91 public ContentHandler getContentHandler() { 92 return _sax; 93 } 94 95 public void setContentHandler(ContentHandler handler) throws 96 NullPointerException 97 { 98 _sax = handler; 99 if (handler instanceof LexicalHandler ) { 100 _lex = (LexicalHandler ) handler; 101 } 102 103 if (handler instanceof SAXImpl) { 104 _saxImpl = (SAXImpl)handler; 105 } 106 } 107 108 109 public void parse(InputSource unused) throws IOException , SAXException { 110 try { 111 bridge(); 112 } catch (XMLStreamException e) { 113 throw new SAXException (e); 114 } 115 } 116 117 118 public void parse() throws IOException , SAXException , XMLStreamException { 120 bridge(); 121 } 122 123 124 128 public void parse(String sysId) throws IOException , SAXException { 129 throw new IOException ("This method is not yet implemented."); 130 } 131 132 133 public void bridge() throws XMLStreamException { 134 135 try { 136 int depth=0; 138 139 int event = staxStreamReader.getEventType(); 141 if (event == XMLStreamConstants.START_DOCUMENT) { 142 event = staxStreamReader.next(); 143 } 144 145 if (event != XMLStreamConstants.START_ELEMENT) { 147 event = staxStreamReader.nextTag(); 148 if (event != XMLStreamConstants.START_ELEMENT) { 150 throw new IllegalStateException ("The current event is " + 151 "not START_ELEMENT\n but" + event); 152 } 153 } 154 155 handleStartDocument(); 156 157 do { 158 switch (event) { 162 case XMLStreamConstants.START_ELEMENT : 163 depth++; 164 handleStartElement(); 165 break; 166 case XMLStreamConstants.END_ELEMENT : 167 handleEndElement(); 168 depth--; 169 break; 170 case XMLStreamConstants.CHARACTERS : 171 handleCharacters(); 172 break; 173 case XMLStreamConstants.ENTITY_REFERENCE : 174 handleEntityReference(); 175 break; 176 case XMLStreamConstants.PROCESSING_INSTRUCTION : 177 handlePI(); 178 break; 179 case XMLStreamConstants.COMMENT : 180 handleComment(); 181 break; 182 case XMLStreamConstants.DTD : 183 handleDTD(); 184 break; 185 case XMLStreamConstants.ATTRIBUTE : 186 handleAttribute(); 187 break; 188 case XMLStreamConstants.NAMESPACE : 189 handleNamespace(); 190 break; 191 case XMLStreamConstants.CDATA : 192 handleCDATA(); 193 break; 194 case XMLStreamConstants.ENTITY_DECLARATION : 195 handleEntityDecl(); 196 break; 197 case XMLStreamConstants.NOTATION_DECLARATION : 198 handleNotationDecl(); 199 break; 200 case XMLStreamConstants.SPACE : 201 handleSpace(); 202 break; 203 default : 204 throw new InternalError ("processing event: " + event); 205 } 206 207 event=staxStreamReader.next(); 208 } while (depth!=0); 209 210 handleEndDocument(); 211 } catch (SAXException e) { 212 throw new XMLStreamException(e); 213 } 214 } 215 216 private void handleEndDocument() throws SAXException { 217 _sax.endDocument(); 218 } 219 220 private void handleStartDocument() throws SAXException { 221 _sax.setDocumentLocator(new Locator2 () { 222 public int getColumnNumber() { 223 return staxStreamReader.getLocation().getColumnNumber(); 224 } 225 public int getLineNumber() { 226 return staxStreamReader.getLocation().getLineNumber(); 227 } 228 public String getPublicId() { 229 return staxStreamReader.getLocation().getPublicId(); 230 } 231 public String getSystemId() { 232 return staxStreamReader.getLocation().getSystemId(); 233 } 234 public String getXMLVersion() { 235 return staxStreamReader.getVersion(); 236 } 237 public String getEncoding() { 238 return staxStreamReader.getEncoding(); 239 } 240 }); 241 _sax.startDocument(); 242 } 243 244 private void handlePI() throws XMLStreamException { 245 try { 246 _sax.processingInstruction( 247 staxStreamReader.getPITarget(), 248 staxStreamReader.getPIData()); 249 } catch (SAXException e) { 250 throw new XMLStreamException(e); 251 } 252 } 253 254 private void handleCharacters() throws XMLStreamException { 255 256 int textLength = staxStreamReader.getTextLength(); 259 char[] chars = new char[textLength]; 260 261 staxStreamReader.getTextCharacters(0, chars, 0, textLength); 262 263 try { 264 _sax.characters(chars, 0, chars.length); 265 } catch (SAXException e) { 266 throw new XMLStreamException(e); 267 } 268 269 270 } 282 283 private void handleEndElement() throws XMLStreamException { 284 QName qName = staxStreamReader.getName(); 285 286 try { 287 String qname = ""; 289 if (qName.getPrefix() != null && qName.getPrefix().trim().length() != 0){ 290 qname = qName.getPrefix() + ":"; 291 } 292 qname += qName.getLocalPart(); 293 294 _sax.endElement( 296 qName.getNamespaceURI(), 297 qName.getLocalPart(), 298 qname); 299 300 int nsCount = staxStreamReader.getNamespaceCount(); 302 for (int i = nsCount - 1; i >= 0; i--) { 303 String prefix = staxStreamReader.getNamespacePrefix(i); 304 if (prefix == null) { prefix = ""; 306 } 307 _sax.endPrefixMapping(prefix); 308 } 309 } catch (SAXException e) { 310 throw new XMLStreamException(e); 311 } 312 } 313 314 private void handleStartElement() throws XMLStreamException { 315 316 try { 317 int nsCount = staxStreamReader.getNamespaceCount(); 319 for (int i = 0; i < nsCount; i++) { 320 String prefix = staxStreamReader.getNamespacePrefix(i); 321 if (prefix == null) { prefix = ""; 323 } 324 _sax.startPrefixMapping( 325 prefix, 326 staxStreamReader.getNamespaceURI(i)); 327 } 328 329 QName qName = staxStreamReader.getName(); 331 String prefix = qName.getPrefix(); 332 String rawname; 333 if(prefix==null || prefix.length()==0) 334 rawname = qName.getLocalPart(); 335 else 336 rawname = prefix + ':' + qName.getLocalPart(); 337 Attributes attrs = getAttributes(); 338 _sax.startElement( 339 qName.getNamespaceURI(), 340 qName.getLocalPart(), 341 rawname, 342 attrs); 343 } catch (SAXException e) { 344 throw new XMLStreamException(e); 345 } 346 } 347 348 354 private Attributes getAttributes() { 355 AttributesImpl attrs = new AttributesImpl (); 356 357 int eventType = staxStreamReader.getEventType(); 358 if (eventType != XMLStreamConstants.ATTRIBUTE 359 && eventType != XMLStreamConstants.START_ELEMENT) { 360 throw new InternalError ( 361 "getAttributes() attempting to process: " + eventType); 362 } 363 364 368 for (int i = 0; i < staxStreamReader.getAttributeCount(); i++) { 370 String uri = staxStreamReader.getAttributeNamespace(i); 371 if(uri==null) uri=""; 372 String localName = staxStreamReader.getAttributeLocalName(i); 373 String prefix = staxStreamReader.getAttributePrefix(i); 374 String qName; 375 if(prefix==null || prefix.length()==0) 376 qName = localName; 377 else 378 qName = prefix + ':' + localName; 379 String type = staxStreamReader.getAttributeType(i); 380 String value = staxStreamReader.getAttributeValue(i); 381 382 attrs.addAttribute(uri, localName, qName, type, value); 383 } 384 385 return attrs; 386 } 387 388 private void handleNamespace() { 389 } 393 394 private void handleAttribute() { 395 } 399 400 private void handleDTD() { 401 } 404 405 private void handleComment() { 406 } 408 409 private void handleEntityReference() { 410 } 412 413 private void handleSpace() { 414 } 417 418 private void handleNotationDecl() { 419 } 422 423 private void handleEntityDecl() { 424 } 427 428 private void handleCDATA() { 429 } 432 433 434 438 public DTDHandler getDTDHandler() { 439 return null; 440 } 441 442 446 public ErrorHandler getErrorHandler() { 447 return null; 448 } 449 450 454 public boolean getFeature(String name) throws SAXNotRecognizedException , 455 SAXNotSupportedException 456 { 457 return false; 458 } 459 460 464 public void setFeature(String name, boolean value) throws 465 SAXNotRecognizedException , SAXNotSupportedException 466 { 467 } 468 469 473 public void setDTDHandler(DTDHandler handler) throws NullPointerException { 474 } 475 476 480 public void setEntityResolver(EntityResolver resolver) throws 481 NullPointerException 482 { 483 } 484 485 489 public EntityResolver getEntityResolver() { 490 return null; 491 } 492 493 497 public void setErrorHandler(ErrorHandler handler) throws 498 NullPointerException 499 { 500 } 501 502 506 public void setProperty(String name, Object value) throws 507 SAXNotRecognizedException , SAXNotSupportedException { 508 } 509 510 514 public Object getProperty(String name) throws SAXNotRecognizedException , 515 SAXNotSupportedException 516 { 517 return null; 518 } 519 520 524 public int getColumnNumber() { 525 return 0; 526 } 527 528 532 public int getLineNumber() { 533 return 0; 534 } 535 536 540 public String getPublicId() { 541 return null; 542 } 543 544 548 public String getSystemId() { 549 return null; 550 } 551 552 } 553 | Popular Tags |