Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/transform/sax/SAXSource.java
32288 views
/*1* Copyright (c) 2000, 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.sax;2627import javax.xml.transform.Source;28import javax.xml.transform.stream.StreamSource;2930import org.xml.sax.InputSource;31import org.xml.sax.XMLReader;3233/**34* <p>Acts as an holder for SAX-style Source.</p>35*36* <p>Note that XSLT requires namespace support. Attempting to transform an37* input source that is not38* generated with a namespace-aware parser may result in errors.39* Parsers can be made namespace aware by calling the40* {@link javax.xml.parsers.SAXParserFactory#setNamespaceAware(boolean awareness)} method.</p>41*42* @author <a href="mailto:[email protected]">Jeff Suttor</a>43*/44public class SAXSource implements Source {4546/**47* If {@link javax.xml.transform.TransformerFactory#getFeature}48* returns true when passed this value as an argument,49* the Transformer supports Source input of this type.50*/51public static final String FEATURE =52"http://javax.xml.transform.sax.SAXSource/feature";5354/**55* <p>Zero-argument default constructor. If this constructor is used, and56* no SAX source is set using57* {@link #setInputSource(InputSource inputSource)} , then the58* <code>Transformer</code> will59* create an empty source {@link org.xml.sax.InputSource} using60* {@link org.xml.sax.InputSource#InputSource() new InputSource()}.</p>61*62* @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)63*/64public SAXSource() { }6566/**67* Create a <code>SAXSource</code>, using an {@link org.xml.sax.XMLReader}68* and a SAX InputSource. The {@link javax.xml.transform.Transformer}69* or {@link javax.xml.transform.sax.SAXTransformerFactory} will set itself70* to be the reader's {@link org.xml.sax.ContentHandler}, and then will call71* reader.parse(inputSource).72*73* @param reader An XMLReader to be used for the parse.74* @param inputSource A SAX input source reference that must be non-null75* and that will be passed to the reader parse method.76*/77public SAXSource(XMLReader reader, InputSource inputSource) {78this.reader = reader;79this.inputSource = inputSource;80}8182/**83* Create a <code>SAXSource</code>, using a SAX <code>InputSource</code>.84* The {@link javax.xml.transform.Transformer} or85* {@link javax.xml.transform.sax.SAXTransformerFactory} creates a86* reader via {@link org.xml.sax.helpers.XMLReaderFactory}87* (if setXMLReader is not used), sets itself as88* the reader's {@link org.xml.sax.ContentHandler}, and calls89* reader.parse(inputSource).90*91* @param inputSource An input source reference that must be non-null92* and that will be passed to the parse method of the reader.93*/94public SAXSource(InputSource inputSource) {95this.inputSource = inputSource;96}9798/**99* Set the XMLReader to be used for the Source.100*101* @param reader A valid XMLReader or XMLFilter reference.102*/103public void setXMLReader(XMLReader reader) {104this.reader = reader;105}106107/**108* Get the XMLReader to be used for the Source.109*110* @return A valid XMLReader or XMLFilter reference, or null.111*/112public XMLReader getXMLReader() {113return reader;114}115116/**117* Set the SAX InputSource to be used for the Source.118*119* @param inputSource A valid InputSource reference.120*/121public void setInputSource(InputSource inputSource) {122this.inputSource = inputSource;123}124125/**126* Get the SAX InputSource to be used for the Source.127*128* @return A valid InputSource reference, or null.129*/130public InputSource getInputSource() {131return inputSource;132}133134/**135* Set the system identifier for this Source. If an input source136* has already been set, it will set the system ID or that137* input source, otherwise it will create a new input source.138*139* <p>The system identifier is optional if there is a byte stream140* or a character stream, but it is still useful to provide one,141* since the application can use it to resolve relative URIs142* and can include it in error messages and warnings (the parser143* will attempt to open a connection to the URI only if144* no byte stream or character stream is specified).</p>145*146* @param systemId The system identifier as a URI string.147*/148public void setSystemId(String systemId) {149150if (null == inputSource) {151inputSource = new InputSource(systemId);152} else {153inputSource.setSystemId(systemId);154}155}156157/**158* <p>Get the base ID (URI or system ID) from where URIs159* will be resolved.</p>160*161* @return Base URL for the <code>Source</code>, or <code>null</code>.162*/163public String getSystemId() {164165if (inputSource == null) {166return null;167} else {168return inputSource.getSystemId();169}170}171172/**173* The XMLReader to be used for the source tree input. May be null.174*/175private XMLReader reader;176177/**178* <p>The SAX InputSource to be used for the source tree input.179* Should not be <code>null</code>.</p>180*/181private InputSource inputSource;182183/**184* Attempt to obtain a SAX InputSource object from a Source185* object.186*187* @param source Must be a non-null Source reference.188*189* @return An InputSource, or null if Source can not be converted.190*/191public static InputSource sourceToInputSource(Source source) {192193if (source instanceof SAXSource) {194return ((SAXSource) source).getInputSource();195} else if (source instanceof StreamSource) {196StreamSource ss = (StreamSource) source;197InputSource isource = new InputSource(ss.getSystemId());198199isource.setByteStream(ss.getInputStream());200isource.setCharacterStream(ss.getReader());201isource.setPublicId(ss.getPublicId());202203return isource;204} else {205return null;206}207}208}209210211