001 /*
002 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.xml.transform.sax;
027
028 import javax.xml.transform.Source;
029 import javax.xml.transform.stream.StreamSource;
030
031 import org.xml.sax.InputSource;
032 import org.xml.sax.XMLReader;
033
034 /**
035 * <p>Acts as an holder for SAX-style Source.</p>
036 *
037 * <p>Note that XSLT requires namespace support. Attempting to transform an
038 * input source that is not
039 * generated with a namespace-aware parser may result in errors.
040 * Parsers can be made namespace aware by calling the
041 * {@link javax.xml.parsers.SAXParserFactory#setNamespaceAware(boolean awareness)} method.</p>
042 *
043 * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
044 * @version $Revision: 1.3 $, $Date: 2005/11/03 19:34:26 $
045 */
046 public class SAXSource implements Source {
047
048 /**
049 * If {@link javax.xml.transform.TransformerFactory#getFeature}
050 * returns true when passed this value as an argument,
051 * the Transformer supports Source input of this type.
052 */
053 public static final String FEATURE = "http://javax.xml.transform.sax.SAXSource/feature";
054
055 /**
056 * <p>Zero-argument default constructor. If this constructor is used, and
057 * no SAX source is set using
058 * {@link #setInputSource(InputSource inputSource)} , then the
059 * <code>Transformer</code> will
060 * create an empty source {@link org.xml.sax.InputSource} using
061 * {@link org.xml.sax.InputSource#InputSource() new InputSource()}.</p>
062 *
063 * @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)
064 */
065 public SAXSource() {
066 }
067
068 /**
069 * Create a <code>SAXSource</code>, using an {@link org.xml.sax.XMLReader}
070 * and a SAX InputSource. The {@link javax.xml.transform.Transformer}
071 * or {@link javax.xml.transform.sax.SAXTransformerFactory} will set itself
072 * to be the reader's {@link org.xml.sax.ContentHandler}, and then will call
073 * reader.parse(inputSource).
074 *
075 * @param reader An XMLReader to be used for the parse.
076 * @param inputSource A SAX input source reference that must be non-null
077 * and that will be passed to the reader parse method.
078 */
079 public SAXSource(XMLReader reader, InputSource inputSource) {
080 this .reader = reader;
081 this .inputSource = inputSource;
082 }
083
084 /**
085 * Create a <code>SAXSource</code>, using a SAX <code>InputSource</code>.
086 * The {@link javax.xml.transform.Transformer} or
087 * {@link javax.xml.transform.sax.SAXTransformerFactory} creates a
088 * reader via {@link org.xml.sax.helpers.XMLReaderFactory}
089 * (if setXMLReader is not used), sets itself as
090 * the reader's {@link org.xml.sax.ContentHandler}, and calls
091 * reader.parse(inputSource).
092 *
093 * @param inputSource An input source reference that must be non-null
094 * and that will be passed to the parse method of the reader.
095 */
096 public SAXSource(InputSource inputSource) {
097 this .inputSource = inputSource;
098 }
099
100 /**
101 * Set the XMLReader to be used for the Source.
102 *
103 * @param reader A valid XMLReader or XMLFilter reference.
104 */
105 public void setXMLReader(XMLReader reader) {
106 this .reader = reader;
107 }
108
109 /**
110 * Get the XMLReader to be used for the Source.
111 *
112 * @return A valid XMLReader or XMLFilter reference, or null.
113 */
114 public XMLReader getXMLReader() {
115 return reader;
116 }
117
118 /**
119 * Set the SAX InputSource to be used for the Source.
120 *
121 * @param inputSource A valid InputSource reference.
122 */
123 public void setInputSource(InputSource inputSource) {
124 this .inputSource = inputSource;
125 }
126
127 /**
128 * Get the SAX InputSource to be used for the Source.
129 *
130 * @return A valid InputSource reference, or null.
131 */
132 public InputSource getInputSource() {
133 return inputSource;
134 }
135
136 /**
137 * Set the system identifier for this Source. If an input source
138 * has already been set, it will set the system ID or that
139 * input source, otherwise it will create a new input source.
140 *
141 * <p>The system identifier is optional if there is a byte stream
142 * or a character stream, but it is still useful to provide one,
143 * since the application can use it to resolve relative URIs
144 * and can include it in error messages and warnings (the parser
145 * will attempt to open a connection to the URI only if
146 * no byte stream or character stream is specified).</p>
147 *
148 * @param systemId The system identifier as a URI string.
149 */
150 public void setSystemId(String systemId) {
151
152 if (null == inputSource) {
153 inputSource = new InputSource(systemId);
154 } else {
155 inputSource.setSystemId(systemId);
156 }
157 }
158
159 /**
160 * <p>Get the base ID (URI or system ID) from where URIs
161 * will be resolved.</p>
162 *
163 * @return Base URL for the <code>Source</code>, or <code>null</code>.
164 */
165 public String getSystemId() {
166
167 if (inputSource == null) {
168 return null;
169 } else {
170 return inputSource.getSystemId();
171 }
172 }
173
174 /**
175 * The XMLReader to be used for the source tree input. May be null.
176 */
177 private XMLReader reader;
178
179 /**
180 * <p>The SAX InputSource to be used for the source tree input.
181 * Should not be <code>null</code>.</p>
182 */
183 private InputSource inputSource;
184
185 /**
186 * Attempt to obtain a SAX InputSource object from a Source
187 * object.
188 *
189 * @param source Must be a non-null Source reference.
190 *
191 * @return An InputSource, or null if Source can not be converted.
192 */
193 public static InputSource sourceToInputSource(Source source) {
194
195 if (source instanceof SAXSource) {
196 return ((SAXSource) source).getInputSource();
197 } else if (source instanceof StreamSource) {
198 StreamSource ss = (StreamSource) source;
199 InputSource isource = new InputSource(ss.getSystemId());
200
201 isource.setByteStream(ss.getInputStream());
202 isource.setCharacterStream(ss.getReader());
203 isource.setPublicId(ss.getPublicId());
204
205 return isource;
206 } else {
207 return null;
208 }
209 }
210 }
|