aoqi@0: /*
aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
aoqi@0:
aoqi@0: import java.util.Iterator;
aoqi@0:
aoqi@0: import javax.xml.namespace.QName;
aoqi@0: import javax.xml.stream.Location;
aoqi@0: import javax.xml.stream.XMLEventReader;
aoqi@0: import javax.xml.stream.XMLStreamConstants;
aoqi@0: import javax.xml.stream.XMLStreamException;
aoqi@0: import javax.xml.stream.events.Attribute;
aoqi@0: import javax.xml.stream.events.Characters;
aoqi@0: import javax.xml.stream.events.EndElement;
aoqi@0: import javax.xml.stream.events.Namespace;
aoqi@0: import javax.xml.stream.events.StartElement;
aoqi@0: import javax.xml.stream.events.XMLEvent;
aoqi@0:
aoqi@0: import org.xml.sax.Attributes;
aoqi@0: import org.xml.sax.SAXException;
aoqi@0: import org.xml.sax.helpers.AttributesImpl;
aoqi@0:
aoqi@0: /**
aoqi@0: * This is a simple utility class that adapts StAX events from an
aoqi@0: * {@link XMLEventReader} to unmarshaller events on a
aoqi@0: * {@link XmlVisitor}, bridging between the two
aoqi@0: * parser technologies.
aoqi@0: *
aoqi@0: * @author Ryan.Shoemaker@Sun.COM
aoqi@0: * @version 1.0
aoqi@0: */
aoqi@0: final class StAXEventConnector extends StAXConnector {
aoqi@0:
aoqi@0: // StAX event source
aoqi@0: private final XMLEventReader staxEventReader;
aoqi@0:
aoqi@0: /** Current event. */
aoqi@0: private XMLEvent event;
aoqi@0:
aoqi@0: /**
aoqi@0: * Shared and reused {@link Attributes}.
aoqi@0: */
aoqi@0: private final AttributesImpl attrs = new AttributesImpl();
aoqi@0:
aoqi@0: /**
aoqi@0: * SAX may fire consective characters event, but we don't allow it.
aoqi@0: * so use this buffer to perform buffering.
aoqi@0: */
aoqi@0: private final StringBuilder buffer = new StringBuilder();
aoqi@0:
aoqi@0: private boolean seenText;
aoqi@0:
aoqi@0: /**
aoqi@0: * Construct a new StAX to SAX adapter that will convert a StAX event
aoqi@0: * stream into a SAX event stream.
aoqi@0: *
aoqi@0: * @param staxCore
aoqi@0: * StAX event source
aoqi@0: * @param visitor
aoqi@0: * sink
aoqi@0: */
aoqi@0: public StAXEventConnector(XMLEventReader staxCore, XmlVisitor visitor) {
aoqi@0: super(visitor);
aoqi@0: staxEventReader = staxCore;
aoqi@0: }
aoqi@0:
aoqi@0: public void bridge() throws XMLStreamException {
aoqi@0:
aoqi@0: try {
aoqi@0: // remembers the nest level of elements to know when we are done.
aoqi@0: int depth=0;
aoqi@0:
aoqi@0: event = staxEventReader.peek();
aoqi@0:
aoqi@0: if( !event.isStartDocument() && !event.isStartElement() )
aoqi@0: throw new IllegalStateException();
aoqi@0:
aoqi@0: // if the parser is on START_DOCUMENT, skip ahead to the first element
aoqi@0: do {
aoqi@0: event = staxEventReader.nextEvent();
aoqi@0: } while( !event.isStartElement() );
aoqi@0:
aoqi@0: handleStartDocument(event.asStartElement().getNamespaceContext());
aoqi@0:
aoqi@0: OUTER:
aoqi@0: while(true) {
aoqi@0: // These are all of the events listed in the javadoc for
aoqi@0: // XMLEvent.
aoqi@0: // The spec only really describes 11 of them.
aoqi@0: switch (event.getEventType()) {
aoqi@0: case XMLStreamConstants.START_ELEMENT :
aoqi@0: handleStartElement(event.asStartElement());
aoqi@0: depth++;
aoqi@0: break;
aoqi@0: case XMLStreamConstants.END_ELEMENT :
aoqi@0: depth--;
aoqi@0: handleEndElement(event.asEndElement());
aoqi@0: if(depth==0) break OUTER;
aoqi@0: break;
aoqi@0: case XMLStreamConstants.CHARACTERS :
aoqi@0: case XMLStreamConstants.CDATA :
aoqi@0: case XMLStreamConstants.SPACE :
aoqi@0: handleCharacters(event.asCharacters());
aoqi@0: break;
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0: event=staxEventReader.nextEvent();
aoqi@0: }
aoqi@0:
aoqi@0: handleEndDocument();
aoqi@0: event = null; // avoid keeping a stale reference
aoqi@0: } catch (SAXException e) {
aoqi@0: throw new XMLStreamException(e);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: protected Location getCurrentLocation() {
aoqi@0: return event.getLocation();
aoqi@0: }
aoqi@0:
aoqi@0: protected String getCurrentQName() {
aoqi@0: QName qName;
aoqi@0: if(event.isEndElement())
aoqi@0: qName = event.asEndElement().getName();
aoqi@0: else
aoqi@0: qName = event.asStartElement().getName();
aoqi@0: return getQName(qName.getPrefix(), qName.getLocalPart());
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0: private void handleCharacters(Characters event) throws SAXException, XMLStreamException {
aoqi@0: if(!predictor.expectText())
aoqi@0: return; // text isn't expected. simply skip
aoqi@0:
aoqi@0: seenText = true;
aoqi@0:
aoqi@0: // check the next event
aoqi@0: XMLEvent next;
aoqi@0: while(true) {
aoqi@0: next = staxEventReader.peek();
aoqi@0: if(!isIgnorable(next))
aoqi@0: break;
aoqi@0: staxEventReader.nextEvent();
aoqi@0: }
aoqi@0:
aoqi@0: if(isTag(next)) {
aoqi@0: // this is by far the common case --- you have abc or abc...
aoqi@0: visitor.text(event.getData());
aoqi@0: return;
aoqi@0: }
aoqi@0:
aoqi@0: // otherwise we have things like "abcdef".
aoqi@0: // concatenate all text
aoqi@0: buffer.append(event.getData());
aoqi@0:
aoqi@0: while(true) {
aoqi@0: while(true) {
aoqi@0: next = staxEventReader.peek();
aoqi@0: if(!isIgnorable(next))
aoqi@0: break;
aoqi@0: staxEventReader.nextEvent();
aoqi@0: }
aoqi@0:
aoqi@0: if(isTag(next)) {
aoqi@0: // found all adjacent text
aoqi@0: visitor.text(buffer);
aoqi@0: buffer.setLength(0);
aoqi@0: return;
aoqi@0: }
aoqi@0:
aoqi@0: buffer.append(next.asCharacters().getData());
aoqi@0: staxEventReader.nextEvent(); // consume
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: private boolean isTag(XMLEvent event) {
aoqi@0: int eventType = event.getEventType();
aoqi@0: return eventType==XMLEvent.START_ELEMENT || eventType==XMLEvent.END_ELEMENT;
aoqi@0: }
aoqi@0:
aoqi@0: private boolean isIgnorable(XMLEvent event) {
aoqi@0: int eventType = event.getEventType();
aoqi@0: return eventType==XMLEvent.COMMENT || eventType==XMLEvent.PROCESSING_INSTRUCTION;
aoqi@0: }
aoqi@0:
aoqi@0: private void handleEndElement(EndElement event) throws SAXException {
aoqi@0: if(!seenText && predictor.expectText()) {
aoqi@0: visitor.text("");
aoqi@0: }
aoqi@0:
aoqi@0: // fire endElement
aoqi@0: QName qName = event.getName();
aoqi@0: tagName.uri = fixNull(qName.getNamespaceURI());
aoqi@0: tagName.local = qName.getLocalPart();
aoqi@0: visitor.endElement(tagName);
aoqi@0:
aoqi@0: // end namespace bindings
aoqi@0: for( Iterator i = event.getNamespaces(); i.hasNext();) {
aoqi@0: String prefix = fixNull(i.next().getPrefix()); // be defensive
aoqi@0: visitor.endPrefixMapping(prefix);
aoqi@0: }
aoqi@0:
aoqi@0: seenText = false;
aoqi@0: }
aoqi@0:
aoqi@0: private void handleStartElement(StartElement event) throws SAXException {
aoqi@0: // start namespace bindings
aoqi@0: for (Iterator i = event.getNamespaces(); i.hasNext();) {
aoqi@0: Namespace ns = (Namespace)i.next();
aoqi@0: visitor.startPrefixMapping(
aoqi@0: fixNull(ns.getPrefix()),
aoqi@0: fixNull(ns.getNamespaceURI()));
aoqi@0: }
aoqi@0:
aoqi@0: // fire startElement
aoqi@0: QName qName = event.getName();
aoqi@0: tagName.uri = fixNull(qName.getNamespaceURI());
aoqi@0: String localName = qName.getLocalPart();
aoqi@0: tagName.uri = fixNull(qName.getNamespaceURI());
aoqi@0: tagName.local = localName;
aoqi@0: tagName.atts = getAttributes(event);
aoqi@0: visitor.startElement(tagName);
aoqi@0:
aoqi@0: seenText = false;
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the attributes associated with the given START_ELEMENT StAXevent.
aoqi@0: *
aoqi@0: * @return the StAX attributes converted to an org.xml.sax.Attributes
aoqi@0: */
aoqi@0: private Attributes getAttributes(StartElement event) {
aoqi@0: attrs.clear();
aoqi@0:
aoqi@0: // in SAX, namespace declarations are not part of attributes by default.
aoqi@0: // (there's a property to control that, but as far as we are concerned
aoqi@0: // we don't use it.) So don't add xmlns:* to attributes.
aoqi@0:
aoqi@0: // gather non-namespace attrs
aoqi@0: for (Iterator i = event.getAttributes(); i.hasNext();) {
aoqi@0: Attribute staxAttr = (Attribute)i.next();
aoqi@0:
aoqi@0: QName name = staxAttr.getName();
aoqi@0: String uri = fixNull(name.getNamespaceURI());
aoqi@0: String localName = name.getLocalPart();
aoqi@0: String prefix = name.getPrefix();
aoqi@0: String qName;
aoqi@0: if (prefix == null || prefix.length() == 0)
aoqi@0: qName = localName;
aoqi@0: else
aoqi@0: qName = prefix + ':' + localName;
aoqi@0: String type = staxAttr.getDTDType();
aoqi@0: String value = staxAttr.getValue();
aoqi@0:
aoqi@0: attrs.addAttribute(uri, localName, qName, type, value);
aoqi@0: }
aoqi@0:
aoqi@0: return attrs;
aoqi@0: }
aoqi@0: }