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 javax.xml.transform.stax;
286N/A
286N/Aimport javax.xml.stream.XMLEventReader;
286N/Aimport javax.xml.stream.XMLStreamConstants;
286N/Aimport javax.xml.stream.XMLStreamException;
286N/Aimport javax.xml.stream.XMLStreamReader;
286N/Aimport javax.xml.stream.events.XMLEvent;
286N/Aimport javax.xml.transform.Source;
286N/A
286N/A/**
286N/A * <p>Acts as a holder for an XML {@link Source} in the
286N/A * form of a StAX reader,i.e.
286N/A * {@link XMLStreamReader} or {@link XMLEventReader}.
286N/A * <code>StAXSource</code> can be used in all cases that accept
286N/A * a <code>Source</code>, e.g. {@link javax.xml.transform.Transformer},
286N/A * {@link javax.xml.validation.Validator} which accept
286N/A * <code>Source</code> as input.
286N/A *
286N/A * <p><code>StAXSource</code>s are consumed during processing
286N/A * and are not reusable.</p>
286N/A *
286N/A * @author <a href="mailto:Neeraj.Bajaj@Sun.com">Neeraj Bajaj</a>
286N/A * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
286N/A *
286N/A * @see <a href="http://jcp.org/en/jsr/detail?id=173">
286N/A * JSR 173: Streaming API for XML</a>
286N/A * @see XMLStreamReader
286N/A * @see XMLEventReader
286N/A *
286N/A * @since 1.6
286N/A */
286N/Apublic class StAXSource implements Source {
286N/A
286N/A /** If {@link javax.xml.transform.TransformerFactory#getFeature(String name)}
286N/A * returns true when passed this value as an argument,
286N/A * the Transformer supports Source input of this type.
286N/A */
286N/A public static final String FEATURE =
286N/A "http://javax.xml.transform.stax.StAXSource/feature";
286N/A
286N/A /** <p><code>XMLEventReader</code> to be used for source input.</p> */
286N/A private XMLEventReader xmlEventReader = null;
286N/A
286N/A /** <p><code>XMLStreamReader</code> to be used for source input.</p> */
286N/A private XMLStreamReader xmlStreamReader = null;
286N/A
286N/A /** <p>System identifier of source input.</p> */
286N/A private String systemId = null;
286N/A
286N/A /**
286N/A * <p>Creates a new instance of a <code>StAXSource</code>
286N/A * by supplying an {@link XMLEventReader}.</p>
286N/A *
286N/A * <p><code>XMLEventReader</code> must be a
286N/A * non-<code>null</code> reference.</p>
286N/A *
286N/A * <p><code>XMLEventReader</code> must be in
286N/A * {@link XMLStreamConstants#START_DOCUMENT} or
286N/A * {@link XMLStreamConstants#START_ELEMENT} state.</p>
286N/A *
286N/A * @param xmlEventReader <code>XMLEventReader</code> used to create
286N/A * this <code>StAXSource</code>.
286N/A *
286N/A * @throws XMLStreamException If <code>xmlEventReader</code> access
286N/A * throws an <code>Exception</code>.
286N/A * @throws IllegalArgumentException If <code>xmlEventReader</code> ==
286N/A * <code>null</code>.
286N/A * @throws IllegalStateException If <code>xmlEventReader</code>
286N/A * is not in <code>XMLStreamConstants.START_DOCUMENT</code> or
286N/A * <code>XMLStreamConstants.START_ELEMENT</code> state.
286N/A */
286N/A public StAXSource(final XMLEventReader xmlEventReader)
286N/A throws XMLStreamException {
286N/A
286N/A if (xmlEventReader == null) {
286N/A throw new IllegalArgumentException(
286N/A "StAXSource(XMLEventReader) with XMLEventReader == null");
286N/A }
286N/A
286N/A // TODO: This is ugly ...
286N/A // there is no way to know the current position(event) of
286N/A // XMLEventReader. peek() is the only way to know the next event.
286N/A // The next event on the input stream should be
286N/A // XMLStreamConstants.START_DOCUMENT or
286N/A // XMLStreamConstants.START_ELEMENT.
286N/A XMLEvent event = xmlEventReader.peek();
286N/A int eventType = event.getEventType();
286N/A if (eventType != XMLStreamConstants.START_DOCUMENT
286N/A && eventType != XMLStreamConstants.START_ELEMENT) {
286N/A throw new IllegalStateException(
286N/A "StAXSource(XMLEventReader) with XMLEventReader "
286N/A + "not in XMLStreamConstants.START_DOCUMENT or "
286N/A + "XMLStreamConstants.START_ELEMENT state");
286N/A }
286N/A
286N/A this.xmlEventReader = xmlEventReader;
286N/A systemId = event.getLocation().getSystemId();
286N/A }
286N/A
286N/A /**
286N/A * <p>Creates a new instance of a <code>StAXSource</code>
286N/A * by supplying an {@link XMLStreamReader}.</p>
286N/A *
286N/A * <p><code>XMLStreamReader</code> must be a
286N/A * non-<code>null</code> reference.</p>
286N/A *
286N/A * <p><code>XMLStreamReader</code> must be in
286N/A * {@link XMLStreamConstants#START_DOCUMENT} or
286N/A * {@link XMLStreamConstants#START_ELEMENT} state.</p>
286N/A *
286N/A * @param xmlStreamReader <code>XMLStreamReader</code> used to create
286N/A * this <code>StAXSource</code>.
286N/A *
286N/A * @throws IllegalArgumentException If <code>xmlStreamReader</code> ==
286N/A * <code>null</code>.
286N/A * @throws IllegalStateException If <code>xmlStreamReader</code>
286N/A * is not in <code>XMLStreamConstants.START_DOCUMENT</code> or
286N/A * <code>XMLStreamConstants.START_ELEMENT</code> state.
286N/A */
286N/A public StAXSource(final XMLStreamReader xmlStreamReader) {
286N/A
286N/A if (xmlStreamReader == null) {
286N/A throw new IllegalArgumentException(
286N/A "StAXSource(XMLStreamReader) with XMLStreamReader == null");
286N/A }
286N/A
286N/A int eventType = xmlStreamReader.getEventType();
286N/A if (eventType != XMLStreamConstants.START_DOCUMENT
286N/A && eventType != XMLStreamConstants.START_ELEMENT) {
286N/A throw new IllegalStateException(
286N/A "StAXSource(XMLStreamReader) with XMLStreamReader"
286N/A + "not in XMLStreamConstants.START_DOCUMENT or "
286N/A + "XMLStreamConstants.START_ELEMENT state");
286N/A }
286N/A
286N/A this.xmlStreamReader = xmlStreamReader;
286N/A systemId = xmlStreamReader.getLocation().getSystemId();
286N/A }
286N/A
286N/A /**
286N/A * <p>Get the <code>XMLEventReader</code> used by this
286N/A * <code>StAXSource</code>.</p>
286N/A *
286N/A * <p><code>XMLEventReader</code> will be <code>null</code>.
286N/A * if this <code>StAXSource</code> was created with a
286N/A * <code>XMLStreamReader</code>.</p>
286N/A *
286N/A * @return <code>XMLEventReader</code> used by this
286N/A * <code>StAXSource</code>.
286N/A */
286N/A public XMLEventReader getXMLEventReader() {
286N/A
286N/A return xmlEventReader;
286N/A }
286N/A
286N/A /**
286N/A * <p>Get the <code>XMLStreamReader</code> used by this
286N/A * <code>StAXSource</code>.</p>
286N/A *
286N/A * <p><code>XMLStreamReader</code> will be <code>null</code>
286N/A * if this <code>StAXSource</code> was created with a
286N/A * <code>XMLEventReader</code>.</p>
286N/A *
286N/A * @return <code>XMLStreamReader</code> used by this
286N/A * <code>StAXSource</code>.
286N/A */
286N/A public XMLStreamReader getXMLStreamReader() {
286N/A
286N/A return xmlStreamReader;
286N/A }
286N/A
286N/A /**
286N/A * <p>In the context of a <code>StAXSource</code>, it is not appropriate
286N/A * to explicitly set the system identifier.
286N/A * The <code>XMLStreamReader</code> or <code>XMLEventReader</code>
286N/A * used to construct this <code>StAXSource</code> determines the
286N/A * system identifier of the XML source.</p>
286N/A *
286N/A * <p>An {@link UnsupportedOperationException} is <strong>always</strong>
286N/A * thrown by this method.</p>
286N/A *
286N/A * @param systemId Ignored.
286N/A *
286N/A * @throws UnsupportedOperationException Is <strong>always</strong>
286N/A * thrown by this method.
286N/A */
286N/A public void setSystemId(final String systemId) {
286N/A
286N/A throw new UnsupportedOperationException(
286N/A "StAXSource#setSystemId(systemId) cannot set the "
286N/A + "system identifier for a StAXSource");
286N/A }
286N/A
286N/A /**
286N/A * <p>Get the system identifier used by this
286N/A * <code>StAXSource</code>.</p>
286N/A *
286N/A * <p>The <code>XMLStreamReader</code> or <code>XMLEventReader</code>
286N/A * used to construct this <code>StAXSource</code> is queried to determine
286N/A * the system identifier of the XML source.</p>
286N/A *
286N/A * <p>The system identifier may be <code>null</code> or
286N/A * an empty <code>""</code> <code>String</code>.</p>
286N/A *
286N/A * @return System identifier used by this <code>StAXSource</code>.
286N/A */
286N/A public String getSystemId() {
286N/A
286N/A return systemId;
286N/A }
286N/A}