Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/transform/stax/StAXSource.java
32288 views
/*1* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.transform.stax;2627import javax.xml.stream.XMLEventReader;28import javax.xml.stream.XMLStreamConstants;29import javax.xml.stream.XMLStreamException;30import javax.xml.stream.XMLStreamReader;31import javax.xml.stream.events.XMLEvent;32import javax.xml.transform.Source;3334/**35* <p>Acts as a holder for an XML {@link Source} in the36* form of a StAX reader,i.e.37* {@link XMLStreamReader} or {@link XMLEventReader}.38* <code>StAXSource</code> can be used in all cases that accept39* a <code>Source</code>, e.g. {@link javax.xml.transform.Transformer},40* {@link javax.xml.validation.Validator} which accept41* <code>Source</code> as input.42*43* <p><code>StAXSource</code>s are consumed during processing44* and are not reusable.</p>45*46* @author <a href="mailto:[email protected]">Neeraj Bajaj</a>47* @author <a href="mailto:[email protected]">Jeff Suttor</a>48*49* @see <a href="http://jcp.org/en/jsr/detail?id=173">50* JSR 173: Streaming API for XML</a>51* @see XMLStreamReader52* @see XMLEventReader53*54* @since 1.655*/56public class StAXSource implements Source {5758/** If {@link javax.xml.transform.TransformerFactory#getFeature(String name)}59* returns true when passed this value as an argument,60* the Transformer supports Source input of this type.61*/62public static final String FEATURE =63"http://javax.xml.transform.stax.StAXSource/feature";6465/** <p><code>XMLEventReader</code> to be used for source input.</p> */66private XMLEventReader xmlEventReader = null;6768/** <p><code>XMLStreamReader</code> to be used for source input.</p> */69private XMLStreamReader xmlStreamReader = null;7071/** <p>System identifier of source input.</p> */72private String systemId = null;7374/**75* <p>Creates a new instance of a <code>StAXSource</code>76* by supplying an {@link XMLEventReader}.</p>77*78* <p><code>XMLEventReader</code> must be a79* non-<code>null</code> reference.</p>80*81* <p><code>XMLEventReader</code> must be in82* {@link XMLStreamConstants#START_DOCUMENT} or83* {@link XMLStreamConstants#START_ELEMENT} state.</p>84*85* @param xmlEventReader <code>XMLEventReader</code> used to create86* this <code>StAXSource</code>.87*88* @throws XMLStreamException If <code>xmlEventReader</code> access89* throws an <code>Exception</code>.90* @throws IllegalArgumentException If <code>xmlEventReader</code> ==91* <code>null</code>.92* @throws IllegalStateException If <code>xmlEventReader</code>93* is not in <code>XMLStreamConstants.START_DOCUMENT</code> or94* <code>XMLStreamConstants.START_ELEMENT</code> state.95*/96public StAXSource(final XMLEventReader xmlEventReader)97throws XMLStreamException {9899if (xmlEventReader == null) {100throw new IllegalArgumentException(101"StAXSource(XMLEventReader) with XMLEventReader == null");102}103104// TODO: This is ugly ...105// there is no way to know the current position(event) of106// XMLEventReader. peek() is the only way to know the next event.107// The next event on the input stream should be108// XMLStreamConstants.START_DOCUMENT or109// XMLStreamConstants.START_ELEMENT.110XMLEvent event = xmlEventReader.peek();111int eventType = event.getEventType();112if (eventType != XMLStreamConstants.START_DOCUMENT113&& eventType != XMLStreamConstants.START_ELEMENT) {114throw new IllegalStateException(115"StAXSource(XMLEventReader) with XMLEventReader "116+ "not in XMLStreamConstants.START_DOCUMENT or "117+ "XMLStreamConstants.START_ELEMENT state");118}119120this.xmlEventReader = xmlEventReader;121systemId = event.getLocation().getSystemId();122}123124/**125* <p>Creates a new instance of a <code>StAXSource</code>126* by supplying an {@link XMLStreamReader}.</p>127*128* <p><code>XMLStreamReader</code> must be a129* non-<code>null</code> reference.</p>130*131* <p><code>XMLStreamReader</code> must be in132* {@link XMLStreamConstants#START_DOCUMENT} or133* {@link XMLStreamConstants#START_ELEMENT} state.</p>134*135* @param xmlStreamReader <code>XMLStreamReader</code> used to create136* this <code>StAXSource</code>.137*138* @throws IllegalArgumentException If <code>xmlStreamReader</code> ==139* <code>null</code>.140* @throws IllegalStateException If <code>xmlStreamReader</code>141* is not in <code>XMLStreamConstants.START_DOCUMENT</code> or142* <code>XMLStreamConstants.START_ELEMENT</code> state.143*/144public StAXSource(final XMLStreamReader xmlStreamReader) {145146if (xmlStreamReader == null) {147throw new IllegalArgumentException(148"StAXSource(XMLStreamReader) with XMLStreamReader == null");149}150151int eventType = xmlStreamReader.getEventType();152if (eventType != XMLStreamConstants.START_DOCUMENT153&& eventType != XMLStreamConstants.START_ELEMENT) {154throw new IllegalStateException(155"StAXSource(XMLStreamReader) with XMLStreamReader"156+ "not in XMLStreamConstants.START_DOCUMENT or "157+ "XMLStreamConstants.START_ELEMENT state");158}159160this.xmlStreamReader = xmlStreamReader;161systemId = xmlStreamReader.getLocation().getSystemId();162}163164/**165* <p>Get the <code>XMLEventReader</code> used by this166* <code>StAXSource</code>.</p>167*168* <p><code>XMLEventReader</code> will be <code>null</code>.169* if this <code>StAXSource</code> was created with a170* <code>XMLStreamReader</code>.</p>171*172* @return <code>XMLEventReader</code> used by this173* <code>StAXSource</code>.174*/175public XMLEventReader getXMLEventReader() {176177return xmlEventReader;178}179180/**181* <p>Get the <code>XMLStreamReader</code> used by this182* <code>StAXSource</code>.</p>183*184* <p><code>XMLStreamReader</code> will be <code>null</code>185* if this <code>StAXSource</code> was created with a186* <code>XMLEventReader</code>.</p>187*188* @return <code>XMLStreamReader</code> used by this189* <code>StAXSource</code>.190*/191public XMLStreamReader getXMLStreamReader() {192193return xmlStreamReader;194}195196/**197* <p>In the context of a <code>StAXSource</code>, it is not appropriate198* to explicitly set the system identifier.199* The <code>XMLStreamReader</code> or <code>XMLEventReader</code>200* used to construct this <code>StAXSource</code> determines the201* system identifier of the XML source.</p>202*203* <p>An {@link UnsupportedOperationException} is <strong>always</strong>204* thrown by this method.</p>205*206* @param systemId Ignored.207*208* @throws UnsupportedOperationException Is <strong>always</strong>209* thrown by this method.210*/211public void setSystemId(final String systemId) {212213throw new UnsupportedOperationException(214"StAXSource#setSystemId(systemId) cannot set the "215+ "system identifier for a StAXSource");216}217218/**219* <p>Get the system identifier used by this220* <code>StAXSource</code>.</p>221*222* <p>The <code>XMLStreamReader</code> or <code>XMLEventReader</code>223* used to construct this <code>StAXSource</code> is queried to determine224* the system identifier of the XML source.</p>225*226* <p>The system identifier may be <code>null</code> or227* an empty <code>""</code> <code>String</code>.</p>228*229* @return System identifier used by this <code>StAXSource</code>.230*/231public String getSystemId() {232233return systemId;234}235}236237238