286N/A/*
286N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xalan.internal.xsltc.trax;
286N/A
286N/Aimport java.io.IOException;
286N/Aimport java.util.Hashtable;
286N/Aimport java.util.Stack;
286N/Aimport java.util.Vector;
286N/Aimport java.util.Iterator;
286N/A
286N/Aimport org.xml.sax.Attributes;
286N/Aimport org.xml.sax.ContentHandler;
286N/Aimport org.xml.sax.DTDHandler;
286N/Aimport org.xml.sax.EntityResolver;
286N/Aimport org.xml.sax.ErrorHandler;
286N/Aimport org.xml.sax.InputSource;
286N/Aimport org.xml.sax.Locator;
286N/Aimport org.xml.sax.SAXException;
286N/Aimport org.xml.sax.SAXNotRecognizedException;
286N/Aimport org.xml.sax.SAXNotSupportedException;
286N/Aimport org.xml.sax.XMLReader;
286N/Aimport org.xml.sax.ext.LexicalHandler;
286N/Aimport org.xml.sax.ext.Locator2;
286N/Aimport org.xml.sax.helpers.AttributesImpl;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl;
286N/A
286N/A
286N/A
286N/Aimport javax.xml.namespace.QName;
286N/Aimport javax.xml.stream.XMLStreamReader;
286N/Aimport javax.xml.stream.XMLStreamConstants;
286N/Aimport javax.xml.stream.XMLStreamException;
286N/Aimport javax.xml.stream.events.Attribute;
286N/Aimport javax.xml.stream.events.Characters;
286N/Aimport javax.xml.stream.events.EndElement;
286N/Aimport javax.xml.stream.events.Namespace;
286N/Aimport javax.xml.stream.events.ProcessingInstruction;
286N/Aimport javax.xml.stream.events.StartElement;
286N/Aimport javax.xml.stream.events.StartDocument;
286N/Aimport javax.xml.stream.events.XMLEvent;
286N/A
286N/A
286N/A
286N/A/**
286N/A * @author Padmaja Vedula
286N/A * @author Sunitha Reddy
286N/A */
286N/Apublic class StAXStream2SAX implements XMLReader, Locator {
286N/A
286N/A //private final static String EMPTYSTRING = "";
286N/A //private static final String XMLNS_PREFIX = "xmlns";
286N/A
286N/A // StAX Stream source
286N/A private final XMLStreamReader staxStreamReader;
286N/A
286N/A //private Node _dom = null;
286N/A private ContentHandler _sax = null;
286N/A private LexicalHandler _lex = null;
286N/A private SAXImpl _saxImpl = null;
286N/A //private Hashtable _nsPrefixes = new Hashtable();
286N/A
286N/A public StAXStream2SAX(XMLStreamReader staxSrc) {
286N/A staxStreamReader = staxSrc;
286N/A }
286N/A
286N/A public ContentHandler getContentHandler() {
286N/A return _sax;
286N/A }
286N/A
286N/A public void setContentHandler(ContentHandler handler) throws
286N/A NullPointerException
286N/A {
286N/A _sax = handler;
286N/A if (handler instanceof LexicalHandler) {
286N/A _lex = (LexicalHandler) handler;
286N/A }
286N/A
286N/A if (handler instanceof SAXImpl) {
286N/A _saxImpl = (SAXImpl)handler;
286N/A }
286N/A }
286N/A
286N/A
286N/A public void parse(InputSource unused) throws IOException, SAXException {
286N/A try {
286N/A bridge();
286N/A } catch (XMLStreamException e) {
286N/A throw new SAXException(e);
286N/A }
286N/A }
286N/A
286N/A
286N/A //Main Work Starts Here.
286N/A public void parse() throws IOException, SAXException, XMLStreamException {
286N/A bridge();
286N/A }
286N/A
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public void parse(String sysId) throws IOException, SAXException {
286N/A throw new IOException("This method is not yet implemented.");
286N/A }
286N/A
286N/A
286N/A public void bridge() throws XMLStreamException {
286N/A
286N/A try {
286N/A // remembers the nest level of elements to know when we are done.
286N/A int depth=0;
286N/A
286N/A // skip over START_DOCUMENT
286N/A int event = staxStreamReader.getEventType();
286N/A if (event == XMLStreamConstants.START_DOCUMENT) {
286N/A event = staxStreamReader.next();
286N/A }
286N/A
286N/A // If not a START_ELEMENT (e.g., a DTD), skip to next tag
286N/A if (event != XMLStreamConstants.START_ELEMENT) {
286N/A event = staxStreamReader.nextTag();
286N/A // An error if a START_ELEMENT isn't found now
286N/A if (event != XMLStreamConstants.START_ELEMENT) {
286N/A throw new IllegalStateException("The current event is " +
286N/A "not START_ELEMENT\n but" + event);
286N/A }
286N/A }
286N/A
286N/A handleStartDocument();
286N/A
286N/A do {
286N/A // These are all of the events listed in the javadoc for
286N/A // XMLEvent.
286N/A // The spec only really describes 11 of them.
286N/A switch (event) {
286N/A case XMLStreamConstants.START_ELEMENT :
286N/A depth++;
286N/A handleStartElement();
286N/A break;
286N/A case XMLStreamConstants.END_ELEMENT :
286N/A handleEndElement();
286N/A depth--;
286N/A break;
286N/A case XMLStreamConstants.CHARACTERS :
286N/A handleCharacters();
286N/A break;
286N/A case XMLStreamConstants.ENTITY_REFERENCE :
286N/A handleEntityReference();
286N/A break;
286N/A case XMLStreamConstants.PROCESSING_INSTRUCTION :
286N/A handlePI();
286N/A break;
286N/A case XMLStreamConstants.COMMENT :
286N/A handleComment();
286N/A break;
286N/A case XMLStreamConstants.DTD :
286N/A handleDTD();
286N/A break;
286N/A case XMLStreamConstants.ATTRIBUTE :
286N/A handleAttribute();
286N/A break;
286N/A case XMLStreamConstants.NAMESPACE :
286N/A handleNamespace();
286N/A break;
286N/A case XMLStreamConstants.CDATA :
286N/A handleCDATA();
286N/A break;
286N/A case XMLStreamConstants.ENTITY_DECLARATION :
286N/A handleEntityDecl();
286N/A break;
286N/A case XMLStreamConstants.NOTATION_DECLARATION :
286N/A handleNotationDecl();
286N/A break;
286N/A case XMLStreamConstants.SPACE :
286N/A handleSpace();
286N/A break;
286N/A default :
286N/A throw new InternalError("processing event: " + event);
286N/A }
286N/A
286N/A event=staxStreamReader.next();
286N/A } while (depth!=0);
286N/A
286N/A handleEndDocument();
286N/A } catch (SAXException e) {
286N/A throw new XMLStreamException(e);
286N/A }
286N/A }
286N/A
286N/A private void handleEndDocument() throws SAXException {
286N/A _sax.endDocument();
286N/A }
286N/A
286N/A private void handleStartDocument() throws SAXException {
286N/A _sax.setDocumentLocator(new Locator2() {
286N/A public int getColumnNumber() {
286N/A return staxStreamReader.getLocation().getColumnNumber();
286N/A }
286N/A public int getLineNumber() {
286N/A return staxStreamReader.getLocation().getLineNumber();
286N/A }
286N/A public String getPublicId() {
286N/A return staxStreamReader.getLocation().getPublicId();
286N/A }
286N/A public String getSystemId() {
286N/A return staxStreamReader.getLocation().getSystemId();
286N/A }
286N/A public String getXMLVersion() {
286N/A return staxStreamReader.getVersion();
286N/A }
286N/A public String getEncoding() {
286N/A return staxStreamReader.getEncoding();
286N/A }
286N/A });
286N/A _sax.startDocument();
286N/A }
286N/A
286N/A private void handlePI() throws XMLStreamException {
286N/A try {
286N/A _sax.processingInstruction(
286N/A staxStreamReader.getPITarget(),
286N/A staxStreamReader.getPIData());
286N/A } catch (SAXException e) {
286N/A throw new XMLStreamException(e);
286N/A }
286N/A }
286N/A
286N/A private void handleCharacters() throws XMLStreamException {
286N/A
286N/A // workaround for bugid 5046319 - switch over to commented section
286N/A // below when it is fixed.
286N/A int textLength = staxStreamReader.getTextLength();
286N/A char[] chars = new char[textLength];
286N/A
286N/A staxStreamReader.getTextCharacters(0, chars, 0, textLength);
286N/A
286N/A try {
286N/A _sax.characters(chars, 0, chars.length);
286N/A } catch (SAXException e) {
286N/A throw new XMLStreamException(e);
286N/A }
286N/A
286N/A
286N/A// int start = 0;
286N/A// int len;
286N/A// do {
286N/A// len = staxStreamReader.getTextCharacters(start, buf, 0, buf.length);
286N/A// start += len;
286N/A// try {
286N/A// _sax.characters(buf, 0, len);
286N/A// } catch (SAXException e) {
286N/A// throw new XMLStreamException(e);
286N/A// }
286N/A// } while (len == buf.length);
286N/A }
286N/A
286N/A private void handleEndElement() throws XMLStreamException {
286N/A QName qName = staxStreamReader.getName();
286N/A
286N/A try {
286N/A //construct prefix:localName from qName
286N/A String qname = "";
286N/A if (qName.getPrefix() != null && qName.getPrefix().trim().length() != 0){
286N/A qname = qName.getPrefix() + ":";
286N/A }
286N/A qname += qName.getLocalPart();
286N/A
286N/A // fire endElement
286N/A _sax.endElement(
286N/A qName.getNamespaceURI(),
286N/A qName.getLocalPart(),
286N/A qname);
286N/A
286N/A // end namespace bindings
286N/A int nsCount = staxStreamReader.getNamespaceCount();
286N/A for (int i = nsCount - 1; i >= 0; i--) {
286N/A String prefix = staxStreamReader.getNamespacePrefix(i);
286N/A if (prefix == null) { // true for default namespace
286N/A prefix = "";
286N/A }
286N/A _sax.endPrefixMapping(prefix);
286N/A }
286N/A } catch (SAXException e) {
286N/A throw new XMLStreamException(e);
286N/A }
286N/A }
286N/A
286N/A private void handleStartElement() throws XMLStreamException {
286N/A
286N/A try {
286N/A // start namespace bindings
286N/A int nsCount = staxStreamReader.getNamespaceCount();
286N/A for (int i = 0; i < nsCount; i++) {
286N/A String prefix = staxStreamReader.getNamespacePrefix(i);
286N/A if (prefix == null) { // true for default namespace
286N/A prefix = "";
286N/A }
286N/A _sax.startPrefixMapping(
286N/A prefix,
286N/A staxStreamReader.getNamespaceURI(i));
286N/A }
286N/A
286N/A // fire startElement
286N/A QName qName = staxStreamReader.getName();
286N/A String prefix = qName.getPrefix();
286N/A String rawname;
286N/A if(prefix==null || prefix.length()==0)
286N/A rawname = qName.getLocalPart();
286N/A else
286N/A rawname = prefix + ':' + qName.getLocalPart();
286N/A Attributes attrs = getAttributes();
286N/A _sax.startElement(
286N/A qName.getNamespaceURI(),
286N/A qName.getLocalPart(),
286N/A rawname,
286N/A attrs);
286N/A } catch (SAXException e) {
286N/A throw new XMLStreamException(e);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Get the attributes associated with the given START_ELEMENT or ATTRIBUTE
286N/A * StAXevent.
286N/A *
286N/A * @return the StAX attributes converted to an org.xml.sax.Attributes
286N/A */
286N/A private Attributes getAttributes() {
286N/A AttributesImpl attrs = new AttributesImpl();
286N/A
286N/A int eventType = staxStreamReader.getEventType();
286N/A if (eventType != XMLStreamConstants.ATTRIBUTE
286N/A && eventType != XMLStreamConstants.START_ELEMENT) {
286N/A throw new InternalError(
286N/A "getAttributes() attempting to process: " + eventType);
286N/A }
286N/A
286N/A // in SAX, namespace declarations are not part of attributes by default.
286N/A // (there's a property to control that, but as far as we are concerned
286N/A // we don't use it.) So don't add xmlns:* to attributes.
286N/A
286N/A // gather non-namespace attrs
286N/A for (int i = 0; i < staxStreamReader.getAttributeCount(); i++) {
286N/A String uri = staxStreamReader.getAttributeNamespace(i);
286N/A if(uri==null) uri="";
286N/A String localName = staxStreamReader.getAttributeLocalName(i);
286N/A String prefix = staxStreamReader.getAttributePrefix(i);
286N/A String qName;
286N/A if(prefix==null || prefix.length()==0)
286N/A qName = localName;
286N/A else
286N/A qName = prefix + ':' + localName;
286N/A String type = staxStreamReader.getAttributeType(i);
286N/A String value = staxStreamReader.getAttributeValue(i);
286N/A
286N/A attrs.addAttribute(uri, localName, qName, type, value);
286N/A }
286N/A
286N/A return attrs;
286N/A }
286N/A
286N/A private void handleNamespace() {
286N/A // no-op ???
286N/A // namespace events don't normally occur outside of a startElement
286N/A // or endElement
286N/A }
286N/A
286N/A private void handleAttribute() {
286N/A // no-op ???
286N/A // attribute events don't normally occur outside of a startElement
286N/A // or endElement
286N/A }
286N/A
286N/A private void handleDTD() {
286N/A // no-op ???
286N/A // it seems like we need to pass this info along, but how?
286N/A }
286N/A
286N/A private void handleComment() {
286N/A // no-op ???
286N/A }
286N/A
286N/A private void handleEntityReference() {
286N/A // no-op ???
286N/A }
286N/A
286N/A private void handleSpace() {
286N/A // no-op ???
286N/A // this event is listed in the javadoc, but not in the spec.
286N/A }
286N/A
286N/A private void handleNotationDecl() {
286N/A // no-op ???
286N/A // this event is listed in the javadoc, but not in the spec.
286N/A }
286N/A
286N/A private void handleEntityDecl() {
286N/A // no-op ???
286N/A // this event is listed in the javadoc, but not in the spec.
286N/A }
286N/A
286N/A private void handleCDATA() {
286N/A // no-op ???
286N/A // this event is listed in the javadoc, but not in the spec.
286N/A }
286N/A
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public DTDHandler getDTDHandler() {
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public ErrorHandler getErrorHandler() {
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public boolean getFeature(String name) throws SAXNotRecognizedException,
286N/A SAXNotSupportedException
286N/A {
286N/A return false;
286N/A }
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public void setFeature(String name, boolean value) throws
286N/A SAXNotRecognizedException, SAXNotSupportedException
286N/A {
286N/A }
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public void setDTDHandler(DTDHandler handler) throws NullPointerException {
286N/A }
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public void setEntityResolver(EntityResolver resolver) throws
286N/A NullPointerException
286N/A {
286N/A }
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public EntityResolver getEntityResolver() {
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public void setErrorHandler(ErrorHandler handler) throws
286N/A NullPointerException
286N/A {
286N/A }
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public void setProperty(String name, Object value) throws
286N/A SAXNotRecognizedException, SAXNotSupportedException {
286N/A }
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public Object getProperty(String name) throws SAXNotRecognizedException,
286N/A SAXNotSupportedException
286N/A {
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public int getColumnNumber() {
286N/A return 0;
286N/A }
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public int getLineNumber() {
286N/A return 0;
286N/A }
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public String getPublicId() {
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * This class is only used internally so this method should never
286N/A * be called.
286N/A */
286N/A public String getSystemId() {
286N/A return null;
286N/A }
286N/A
286N/A}