Source Home >> Java Source 1.6.0 >> com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX V 0.09
  • 001/*
  • 002 * The contents of this file are subject to the terms
  • 003 * of the Common Development and Distribution License
  • 004 * (the "License"). You may not use this file except
  • 005 * in compliance with the License.
  • 006 *
  • 007 * You can obtain a copy of the license at
  • 008 * https://jaxp.dev.java.net/CDDLv1.0.html.
  • 009 * See the License for the specific language governing
  • 010 * permissions and limitations under the License.
  • 011 *
  • 012 * When distributing Covered Code, include this CDDL
  • 013 * HEADER in each file and include the License file at
  • 014 * https://jaxp.dev.java.net/CDDLv1.0.html
  • 015 * If applicable add the following below this CDDL HEADER
  • 016 * with the fields enclosed by brackets "[]" replaced with
  • 017 * your own identifying information: Portions Copyright
  • 018 * [year] [name of copyright owner]
  • 019 */
  • 020
  • 021/*
  • 022 * $Id: StAXStream2SAX.java,v 1.3 2005/11/03 17:53:11 jeffsuttor Exp $
  • 023 * @(#)StAXStream2SAX.java 1.6 06/01/27
  • 024 *
  • 025 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
  • 026 */
  • 027
  • 028package com.sun.org.apache.xalan.internal.xsltc.trax;
  • 029
  • 030import java.io.IOException;
  • 031import java.util.Hashtable;
  • 032import java.util.Stack;
  • 033import java.util.Vector;
  • 034import java.util.Iterator;
  • 035
  • 036import org.xml.sax.Attributes;
  • 037import org.xml.sax.ContentHandler;
  • 038import org.xml.sax.DTDHandler;
  • 039import org.xml.sax.EntityResolver;
  • 040import org.xml.sax.ErrorHandler;
  • 041import org.xml.sax.InputSource;
  • 042import org.xml.sax.Locator;
  • 043import org.xml.sax.SAXException;
  • 044import org.xml.sax.SAXNotRecognizedException;
  • 045import org.xml.sax.SAXNotSupportedException;
  • 046import org.xml.sax.XMLReader;
  • 047import org.xml.sax.ext.LexicalHandler;
  • 048import org.xml.sax.ext.Locator2;
  • 049import org.xml.sax.helpers.AttributesImpl;
  • 050import com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl;
  • 051
  • 052
  • 053
  • 054import javax.xml.namespace.QName;
  • 055import javax.xml.stream.XMLStreamReader;
  • 056import javax.xml.stream.XMLStreamConstants;
  • 057import javax.xml.stream.XMLStreamException;
  • 058import javax.xml.stream.events.Attribute;
  • 059import javax.xml.stream.events.Characters;
  • 060import javax.xml.stream.events.EndElement;
  • 061import javax.xml.stream.events.Namespace;
  • 062import javax.xml.stream.events.ProcessingInstruction;
  • 063import javax.xml.stream.events.StartElement;
  • 064import javax.xml.stream.events.StartDocument;
  • 065import javax.xml.stream.events.XMLEvent;
  • 066
  • 067
  • 068
  • 069/**
  • 070 * @author Padmaja Vedula
  • 071 * @author Sunitha Reddy
  • 072 */
  • 073public class StAXStream2SAX implements XMLReader, Locator {
  • 074
  • 075 //private final static String EMPTYSTRING = "";
  • 076 //private static final String XMLNS_PREFIX = "xmlns";
  • 077
  • 078 // StAX Stream source
  • 079 private final XMLStreamReader staxStreamReader;
  • 080
  • 081 //private Node _dom = null;
  • 082 private ContentHandler _sax = null;
  • 083 private LexicalHandler _lex = null;
  • 084 private SAXImpl _saxImpl = null;
  • 085 //private Hashtable _nsPrefixes = new Hashtable();
  • 086
  • 087 public StAXStream2SAX(XMLStreamReader staxSrc) {
  • 088 staxStreamReader = staxSrc;
  • 089 }
  • 090
  • 091 public ContentHandler getContentHandler() {
  • 092 return _sax;
  • 093 }
  • 094
  • 095 public void setContentHandler(ContentHandler handler) throws
  • 096 NullPointerException
  • 097 {
  • 098 _sax = handler;
  • 099 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 //Main Work Starts Here.
  • 119 public void parse() throws IOException, SAXException, XMLStreamException {
  • 120 bridge();
  • 121 }
  • 122
  • 123
  • 124 /**
  • 125 * This class is only used internally so this method should never
  • 126 * be called.
  • 127 */
  • 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 // remembers the nest level of elements to know when we are done.
  • 137 int depth=0;
  • 138
  • 139 // if the parser is at the start tag, proceed to the first element
  • 140 int event = staxStreamReader.getEventType();
  • 141 if(event == XMLStreamConstants.START_DOCUMENT) {
  • 142 event = staxStreamReader.nextTag();
  • 143
  • 144 }
  • 145
  • 146
  • 147 if( event!=XMLStreamConstants.START_ELEMENT)
  • 148 throw new IllegalStateException("The current event is not START_ELEMENT\n but" + event);
  • 149
  • 150 handleStartDocument();
  • 151
  • 152 do {
  • 153 // These are all of the events listed in the javadoc for
  • 154 // XMLEvent.
  • 155 // The spec only really describes 11 of them.
  • 156 switch (event) {
  • 157 case XMLStreamConstants.START_ELEMENT :
  • 158 depth++;
  • 159 handleStartElement();
  • 160 break;
  • 161 case XMLStreamConstants.END_ELEMENT :
  • 162 handleEndElement();
  • 163 depth--;
  • 164 break;
  • 165 case XMLStreamConstants.CHARACTERS :
  • 166 handleCharacters();
  • 167 break;
  • 168 case XMLStreamConstants.ENTITY_REFERENCE :
  • 169 handleEntityReference();
  • 170 break;
  • 171 case XMLStreamConstants.PROCESSING_INSTRUCTION :
  • 172 handlePI();
  • 173 break;
  • 174 case XMLStreamConstants.COMMENT :
  • 175 handleComment();
  • 176 break;
  • 177 case XMLStreamConstants.DTD :
  • 178 handleDTD();
  • 179 break;
  • 180 case XMLStreamConstants.ATTRIBUTE :
  • 181 handleAttribute();
  • 182 break;
  • 183 case XMLStreamConstants.NAMESPACE :
  • 184 handleNamespace();
  • 185 break;
  • 186 case XMLStreamConstants.CDATA :
  • 187 handleCDATA();
  • 188 break;
  • 189 case XMLStreamConstants.ENTITY_DECLARATION :
  • 190 handleEntityDecl();
  • 191 break;
  • 192 case XMLStreamConstants.NOTATION_DECLARATION :
  • 193 handleNotationDecl();
  • 194 break;
  • 195 case XMLStreamConstants.SPACE :
  • 196 handleSpace();
  • 197 break;
  • 198 default :
  • 199 throw new InternalError("processing event: " + event);
  • 200 }
  • 201
  • 202 event=staxStreamReader.next();
  • 203 } while (depth!=0);
  • 204
  • 205 handleEndDocument();
  • 206 } catch (SAXException e) {
  • 207 throw new XMLStreamException(e);
  • 208 }
  • 209 }
  • 210
  • 211 private void handleEndDocument() throws SAXException {
  • 212 _sax.endDocument();
  • 213 }
  • 214
  • 215 private void handleStartDocument() throws SAXException {
  • 216 _sax.setDocumentLocator(new Locator2() {
  • 217 public int getColumnNumber() {
  • 218 return staxStreamReader.getLocation().getColumnNumber();
  • 219 }
  • 220 public int getLineNumber() {
  • 221 return staxStreamReader.getLocation().getLineNumber();
  • 222 }
  • 223 public String getPublicId() {
  • 224 return staxStreamReader.getLocation().getPublicId();
  • 225 }
  • 226 public String getSystemId() {
  • 227 return staxStreamReader.getLocation().getSystemId();
  • 228 }
  • 229 public String getXMLVersion() {
  • 230 return staxStreamReader.getVersion();
  • 231 }
  • 232 public String getEncoding() {
  • 233 return staxStreamReader.getEncoding();
  • 234 }
  • 235 });
  • 236 _sax.startDocument();
  • 237 }
  • 238
  • 239 private void handlePI() throws XMLStreamException {
  • 240 try {
  • 241 _sax.processingInstruction(
  • 242 staxStreamReader.getPITarget(),
  • 243 staxStreamReader.getPIData());
  • 244 } catch (SAXException e) {
  • 245 throw new XMLStreamException(e);
  • 246 }
  • 247 }
  • 248
  • 249 private void handleCharacters() throws XMLStreamException {
  • 250
  • 251 // workaround for bugid 5046319 - switch over to commented section
  • 252 // below when it is fixed.
  • 253 int textLength = staxStreamReader.getTextLength();
  • 254 char[] chars = new char[textLength];
  • 255
  • 256 staxStreamReader.getTextCharacters(0, chars, 0, textLength);
  • 257
  • 258 try {
  • 259 _sax.characters(chars, 0, chars.length);
  • 260 } catch (SAXException e) {
  • 261 throw new XMLStreamException(e);
  • 262 }
  • 263
  • 264
  • 265// int start = 0;
  • 266// int len;
  • 267// do {
  • 268// len = staxStreamReader.getTextCharacters(start, buf, 0, buf.length);
  • 269// start += len;
  • 270// try {
  • 271// _sax.characters(buf, 0, len);
  • 272// } catch (SAXException e) {
  • 273// throw new XMLStreamException(e);
  • 274// }
  • 275// } while (len == buf.length);
  • 276 }
  • 277
  • 278 private void handleEndElement() throws XMLStreamException {
  • 279 QName qName = staxStreamReader.getName();
  • 280
  • 281 try {
  • 282 //construct prefix:localName from qName
  • 283 String qname = "";
  • 284 if (qName.getPrefix() != null && qName.getPrefix().trim().length() != 0){
  • 285 qname = qName.getPrefix() + ":";
  • 286 }
  • 287 qname += qName.getLocalPart();
  • 288
  • 289 // fire endElement
  • 290 _sax.endElement(
  • 291 qName.getNamespaceURI(),
  • 292 qName.getLocalPart(),
  • 293 qname);
  • 294
  • 295 // end namespace bindings
  • 296 int nsCount = staxStreamReader.getNamespaceCount();
  • 297 for (int i = nsCount - 1; i >= 0; i--) {
  • 298 String prefix = staxStreamReader.getNamespacePrefix(i);
  • 299 if (prefix == null) { // true for default namespace
  • 300 prefix = "";
  • 301 }
  • 302 _sax.endPrefixMapping(prefix);
  • 303 }
  • 304 } catch (SAXException e) {
  • 305 throw new XMLStreamException(e);
  • 306 }
  • 307 }
  • 308
  • 309 private void handleStartElement() throws XMLStreamException {
  • 310
  • 311 try {
  • 312 // start namespace bindings
  • 313 int nsCount = staxStreamReader.getNamespaceCount();
  • 314 for (int i = 0; i < nsCount; i++) {
  • 315 String prefix = staxStreamReader.getNamespacePrefix(i);
  • 316 if (prefix == null) { // true for default namespace
  • 317 prefix = "";
  • 318 }
  • 319 _sax.startPrefixMapping(
  • 320 prefix,
  • 321 staxStreamReader.getNamespaceURI(i));
  • 322 }
  • 323
  • 324 // fire startElement
  • 325 QName qName = staxStreamReader.getName();
  • 326 String prefix = qName.getPrefix();
  • 327 String rawname;
  • 328 if(prefix==null || prefix.length()==0)
  • 329 rawname = qName.getLocalPart();
  • 330 else
  • 331 rawname = prefix + ':' + qName.getLocalPart();
  • 332 Attributes attrs = getAttributes();
  • 333 _sax.startElement(
  • 334 qName.getNamespaceURI(),
  • 335 qName.getLocalPart(),
  • 336 rawname,
  • 337 attrs);
  • 338 } catch (SAXException e) {
  • 339 throw new XMLStreamException(e);
  • 340 }
  • 341 }
  • 342
  • 343 /**
  • 344 * Get the attributes associated with the given START_ELEMENT or ATTRIBUTE
  • 345 * StAXevent.
  • 346 *
  • 347 * @return the StAX attributes converted to an org.xml.sax.Attributes
  • 348 */
  • 349 private Attributes getAttributes() {
  • 350 AttributesImpl attrs = new AttributesImpl();
  • 351
  • 352 int eventType = staxStreamReader.getEventType();
  • 353 if (eventType != XMLStreamConstants.ATTRIBUTE
  • 354 && eventType != XMLStreamConstants.START_ELEMENT) {
  • 355 throw new InternalError(
  • 356 "getAttributes() attempting to process: " + eventType);
  • 357 }
  • 358
  • 359 // in SAX, namespace declarations are not part of attributes by default.
  • 360 // (there's a property to control that, but as far as we are concerned
  • 361 // we don't use it.) So don't add xmlns:* to attributes.
  • 362
  • 363 // gather non-namespace attrs
  • 364 for (int i = 0; i < staxStreamReader.getAttributeCount(); i++) {
  • 365 String uri = staxStreamReader.getAttributeNamespace(i);
  • 366 if(uri==null) uri="";
  • 367 String localName = staxStreamReader.getAttributeLocalName(i);
  • 368 String prefix = staxStreamReader.getAttributePrefix(i);
  • 369 String qName;
  • 370 if(prefix==null || prefix.length()==0)
  • 371 qName = localName;
  • 372 else
  • 373 qName = prefix + ':' + localName;
  • 374 String type = staxStreamReader.getAttributeType(i);
  • 375 String value = staxStreamReader.getAttributeValue(i);
  • 376
  • 377 attrs.addAttribute(uri, localName, qName, type, value);
  • 378 }
  • 379
  • 380 return attrs;
  • 381 }
  • 382
  • 383 private void handleNamespace() {
  • 384 // no-op ???
  • 385 // namespace events don't normally occur outside of a startElement
  • 386 // or endElement
  • 387 }
  • 388
  • 389 private void handleAttribute() {
  • 390 // no-op ???
  • 391 // attribute events don't normally occur outside of a startElement
  • 392 // or endElement
  • 393 }
  • 394
  • 395 private void handleDTD() {
  • 396 // no-op ???
  • 397 // it seems like we need to pass this info along, but how?
  • 398 }
  • 399
  • 400 private void handleComment() {
  • 401 // no-op ???
  • 402 }
  • 403
  • 404 private void handleEntityReference() {
  • 405 // no-op ???
  • 406 }
  • 407
  • 408 private void handleSpace() {
  • 409 // no-op ???
  • 410 // this event is listed in the javadoc, but not in the spec.
  • 411 }
  • 412
  • 413 private void handleNotationDecl() {
  • 414 // no-op ???
  • 415 // this event is listed in the javadoc, but not in the spec.
  • 416 }
  • 417
  • 418 private void handleEntityDecl() {
  • 419 // no-op ???
  • 420 // this event is listed in the javadoc, but not in the spec.
  • 421 }
  • 422
  • 423 private void handleCDATA() {
  • 424 // no-op ???
  • 425 // this event is listed in the javadoc, but not in the spec.
  • 426 }
  • 427
  • 428
  • 429 /**
  • 430 * This class is only used internally so this method should never
  • 431 * be called.
  • 432 */
  • 433 public DTDHandler getDTDHandler() {
  • 434 return null;
  • 435 }
  • 436
  • 437 /**
  • 438 * This class is only used internally so this method should never
  • 439 * be called.
  • 440 */
  • 441 public ErrorHandler getErrorHandler() {
  • 442 return null;
  • 443 }
  • 444
  • 445 /**
  • 446 * This class is only used internally so this method should never
  • 447 * be called.
  • 448 */
  • 449 public boolean getFeature(String name) throws SAXNotRecognizedException,
  • 450 SAXNotSupportedException
  • 451 {
  • 452 return false;
  • 453 }
  • 454
  • 455 /**
  • 456 * This class is only used internally so this method should never
  • 457 * be called.
  • 458 */
  • 459 public void setFeature(String name, boolean value) throws
  • 460 SAXNotRecognizedException, SAXNotSupportedException
  • 461 {
  • 462 }
  • 463
  • 464 /**
  • 465 * This class is only used internally so this method should never
  • 466 * be called.
  • 467 */
  • 468 public void setDTDHandler(DTDHandler handler) throws NullPointerException {
  • 469 }
  • 470
  • 471 /**
  • 472 * This class is only used internally so this method should never
  • 473 * be called.
  • 474 */
  • 475 public void setEntityResolver(EntityResolver resolver) throws
  • 476 NullPointerException
  • 477 {
  • 478 }
  • 479
  • 480 /**
  • 481 * This class is only used internally so this method should never
  • 482 * be called.
  • 483 */
  • 484 public EntityResolver getEntityResolver() {
  • 485 return null;
  • 486 }
  • 487
  • 488 /**
  • 489 * This class is only used internally so this method should never
  • 490 * be called.
  • 491 */
  • 492 public void setErrorHandler(ErrorHandler handler) throws
  • 493 NullPointerException
  • 494 {
  • 495 }
  • 496
  • 497 /**
  • 498 * This class is only used internally so this method should never
  • 499 * be called.
  • 500 */
  • 501 public void setProperty(String name, Object value) throws
  • 502 SAXNotRecognizedException, SAXNotSupportedException {
  • 503 }
  • 504
  • 505 /**
  • 506 * This class is only used internally so this method should never
  • 507 * be called.
  • 508 */
  • 509 public Object getProperty(String name) throws SAXNotRecognizedException,
  • 510 SAXNotSupportedException
  • 511 {
  • 512 return null;
  • 513 }
  • 514
  • 515 /**
  • 516 * This class is only used internally so this method should never
  • 517 * be called.
  • 518 */
  • 519 public int getColumnNumber() {
  • 520 return 0;
  • 521 }
  • 522
  • 523 /**
  • 524 * This class is only used internally so this method should never
  • 525 * be called.
  • 526 */
  • 527 public int getLineNumber() {
  • 528 return 0;
  • 529 }
  • 530
  • 531 /**
  • 532 * This class is only used internally so this method should never
  • 533 * be called.
  • 534 */
  • 535 public String getPublicId() {
  • 536 return null;
  • 537 }
  • 538
  • 539 /**
  • 540 * This class is only used internally so this method should never
  • 541 * be called.
  • 542 */
  • 543 public String getSystemId() {
  • 544 return null;
  • 545 }
  • 546
  • 547}

文件:StAXStream2SAX.java
包名:com.sun.org.apache.xalan.internal.xsltc.trax
类名:StAXStream2SAX
继承:
接口:[XMLReader][Locator]