286N/A/*
286N/A * Copyright (c) 2000, 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.sax;
286N/A
286N/Aimport javax.xml.transform.Source;
286N/Aimport javax.xml.transform.stream.StreamSource;
286N/A
286N/Aimport org.xml.sax.InputSource;
286N/Aimport org.xml.sax.XMLReader;
286N/A
286N/A/**
286N/A * <p>Acts as an holder for SAX-style Source.</p>
286N/A *
286N/A * <p>Note that XSLT requires namespace support. Attempting to transform an
286N/A * input source that is not
286N/A * generated with a namespace-aware parser may result in errors.
286N/A * Parsers can be made namespace aware by calling the
286N/A * {@link javax.xml.parsers.SAXParserFactory#setNamespaceAware(boolean awareness)} method.</p>
286N/A *
286N/A * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
286N/A */
286N/Apublic class SAXSource implements Source {
286N/A
286N/A /**
286N/A * If {@link javax.xml.transform.TransformerFactory#getFeature}
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.sax.SAXSource/feature";
286N/A
286N/A /**
286N/A * <p>Zero-argument default constructor. If this constructor is used, and
286N/A * no SAX source is set using
286N/A * {@link #setInputSource(InputSource inputSource)} , then the
286N/A * <code>Transformer</code> will
286N/A * create an empty source {@link org.xml.sax.InputSource} using
286N/A * {@link org.xml.sax.InputSource#InputSource() new InputSource()}.</p>
286N/A *
286N/A * @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)
286N/A */
286N/A public SAXSource() { }
286N/A
286N/A /**
286N/A * Create a <code>SAXSource</code>, using an {@link org.xml.sax.XMLReader}
286N/A * and a SAX InputSource. The {@link javax.xml.transform.Transformer}
286N/A * or {@link javax.xml.transform.sax.SAXTransformerFactory} will set itself
286N/A * to be the reader's {@link org.xml.sax.ContentHandler}, and then will call
286N/A * reader.parse(inputSource).
286N/A *
286N/A * @param reader An XMLReader to be used for the parse.
286N/A * @param inputSource A SAX input source reference that must be non-null
286N/A * and that will be passed to the reader parse method.
286N/A */
286N/A public SAXSource(XMLReader reader, InputSource inputSource) {
286N/A this.reader = reader;
286N/A this.inputSource = inputSource;
286N/A }
286N/A
286N/A /**
286N/A * Create a <code>SAXSource</code>, using a SAX <code>InputSource</code>.
286N/A * The {@link javax.xml.transform.Transformer} or
286N/A * {@link javax.xml.transform.sax.SAXTransformerFactory} creates a
286N/A * reader via {@link org.xml.sax.helpers.XMLReaderFactory}
286N/A * (if setXMLReader is not used), sets itself as
286N/A * the reader's {@link org.xml.sax.ContentHandler}, and calls
286N/A * reader.parse(inputSource).
286N/A *
286N/A * @param inputSource An input source reference that must be non-null
286N/A * and that will be passed to the parse method of the reader.
286N/A */
286N/A public SAXSource(InputSource inputSource) {
286N/A this.inputSource = inputSource;
286N/A }
286N/A
286N/A /**
286N/A * Set the XMLReader to be used for the Source.
286N/A *
286N/A * @param reader A valid XMLReader or XMLFilter reference.
286N/A */
286N/A public void setXMLReader(XMLReader reader) {
286N/A this.reader = reader;
286N/A }
286N/A
286N/A /**
286N/A * Get the XMLReader to be used for the Source.
286N/A *
286N/A * @return A valid XMLReader or XMLFilter reference, or null.
286N/A */
286N/A public XMLReader getXMLReader() {
286N/A return reader;
286N/A }
286N/A
286N/A /**
286N/A * Set the SAX InputSource to be used for the Source.
286N/A *
286N/A * @param inputSource A valid InputSource reference.
286N/A */
286N/A public void setInputSource(InputSource inputSource) {
286N/A this.inputSource = inputSource;
286N/A }
286N/A
286N/A /**
286N/A * Get the SAX InputSource to be used for the Source.
286N/A *
286N/A * @return A valid InputSource reference, or null.
286N/A */
286N/A public InputSource getInputSource() {
286N/A return inputSource;
286N/A }
286N/A
286N/A /**
286N/A * Set the system identifier for this Source. If an input source
286N/A * has already been set, it will set the system ID or that
286N/A * input source, otherwise it will create a new input source.
286N/A *
286N/A * <p>The system identifier is optional if there is a byte stream
286N/A * or a character stream, but it is still useful to provide one,
286N/A * since the application can use it to resolve relative URIs
286N/A * and can include it in error messages and warnings (the parser
286N/A * will attempt to open a connection to the URI only if
286N/A * no byte stream or character stream is specified).</p>
286N/A *
286N/A * @param systemId The system identifier as a URI string.
286N/A */
286N/A public void setSystemId(String systemId) {
286N/A
286N/A if (null == inputSource) {
286N/A inputSource = new InputSource(systemId);
286N/A } else {
286N/A inputSource.setSystemId(systemId);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * <p>Get the base ID (URI or system ID) from where URIs
286N/A * will be resolved.</p>
286N/A *
286N/A * @return Base URL for the <code>Source</code>, or <code>null</code>.
286N/A */
286N/A public String getSystemId() {
286N/A
286N/A if (inputSource == null) {
286N/A return null;
286N/A } else {
286N/A return inputSource.getSystemId();
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * The XMLReader to be used for the source tree input. May be null.
286N/A */
286N/A private XMLReader reader;
286N/A
286N/A /**
286N/A * <p>The SAX InputSource to be used for the source tree input.
286N/A * Should not be <code>null</code>.</p>
286N/A */
286N/A private InputSource inputSource;
286N/A
286N/A /**
286N/A * Attempt to obtain a SAX InputSource object from a Source
286N/A * object.
286N/A *
286N/A * @param source Must be a non-null Source reference.
286N/A *
286N/A * @return An InputSource, or null if Source can not be converted.
286N/A */
286N/A public static InputSource sourceToInputSource(Source source) {
286N/A
286N/A if (source instanceof SAXSource) {
286N/A return ((SAXSource) source).getInputSource();
286N/A } else if (source instanceof StreamSource) {
286N/A StreamSource ss = (StreamSource) source;
286N/A InputSource isource = new InputSource(ss.getSystemId());
286N/A
286N/A isource.setByteStream(ss.getInputStream());
286N/A isource.setCharacterStream(ss.getReader());
286N/A isource.setPublicId(ss.getPublicId());
286N/A
286N/A return isource;
286N/A } else {
286N/A return null;
286N/A }
286N/A }
286N/A}