View Javadoc
1   /*
2    * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package javax.xml.transform.sax;
27  
28  import javax.xml.transform.Source;
29  import javax.xml.transform.stream.StreamSource;
30  
31  import org.xml.sax.InputSource;
32  import org.xml.sax.XMLReader;
33  
34  /**
35   * <p>Acts as an holder for SAX-style Source.</p>
36   *
37   * <p>Note that XSLT requires namespace support. Attempting to transform an
38   * input source that is not
39   * generated with a namespace-aware parser may result in errors.
40   * Parsers can be made namespace aware by calling the
41   * {@link javax.xml.parsers.SAXParserFactory#setNamespaceAware(boolean awareness)} method.</p>
42   *
43   * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
44   */
45  public class SAXSource implements Source {
46  
47      /**
48       * If {@link javax.xml.transform.TransformerFactory#getFeature}
49       * returns true when passed this value as an argument,
50       * the Transformer supports Source input of this type.
51       */
52      public static final String FEATURE =
53          "http://javax.xml.transform.sax.SAXSource/feature";
54  
55      /**
56       * <p>Zero-argument default constructor.  If this constructor is used, and
57       * no SAX source is set using
58       * {@link #setInputSource(InputSource inputSource)} , then the
59       * <code>Transformer</code> will
60       * create an empty source {@link org.xml.sax.InputSource} using
61       * {@link org.xml.sax.InputSource#InputSource() new InputSource()}.</p>
62       *
63       * @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)
64       */
65      public SAXSource() { }
66  
67      /**
68       * Create a <code>SAXSource</code>, using an {@link org.xml.sax.XMLReader}
69       * and a SAX InputSource. The {@link javax.xml.transform.Transformer}
70       * or {@link javax.xml.transform.sax.SAXTransformerFactory} will set itself
71       * to be the reader's {@link org.xml.sax.ContentHandler}, and then will call
72       * reader.parse(inputSource).
73       *
74       * @param reader An XMLReader to be used for the parse.
75       * @param inputSource A SAX input source reference that must be non-null
76       * and that will be passed to the reader parse method.
77       */
78      public SAXSource(XMLReader reader, InputSource inputSource) {
79          this.reader      = reader;
80          this.inputSource = inputSource;
81      }
82  
83      /**
84       * Create a <code>SAXSource</code>, using a SAX <code>InputSource</code>.
85       * The {@link javax.xml.transform.Transformer} or
86       * {@link javax.xml.transform.sax.SAXTransformerFactory} creates a
87       * reader via {@link org.xml.sax.helpers.XMLReaderFactory}
88       * (if setXMLReader is not used), sets itself as
89       * the reader's {@link org.xml.sax.ContentHandler}, and calls
90       * reader.parse(inputSource).
91       *
92       * @param inputSource An input source reference that must be non-null
93       * and that will be passed to the parse method of the reader.
94       */
95      public SAXSource(InputSource inputSource) {
96          this.inputSource = inputSource;
97      }
98  
99      /**
100      * Set the XMLReader to be used for the Source.
101      *
102      * @param reader A valid XMLReader or XMLFilter reference.
103      */
104     public void setXMLReader(XMLReader reader) {
105         this.reader = reader;
106     }
107 
108     /**
109      * Get the XMLReader to be used for the Source.
110      *
111      * @return A valid XMLReader or XMLFilter reference, or null.
112      */
113     public XMLReader getXMLReader() {
114         return reader;
115     }
116 
117     /**
118      * Set the SAX InputSource to be used for the Source.
119      *
120      * @param inputSource A valid InputSource reference.
121      */
122     public void setInputSource(InputSource inputSource) {
123         this.inputSource = inputSource;
124     }
125 
126     /**
127      * Get the SAX InputSource to be used for the Source.
128      *
129      * @return A valid InputSource reference, or null.
130      */
131     public InputSource getInputSource() {
132         return inputSource;
133     }
134 
135     /**
136      * Set the system identifier for this Source.  If an input source
137      * has already been set, it will set the system ID or that
138      * input source, otherwise it will create a new input source.
139      *
140      * <p>The system identifier is optional if there is a byte stream
141      * or a character stream, but it is still useful to provide one,
142      * since the application can use it to resolve relative URIs
143      * and can include it in error messages and warnings (the parser
144      * will attempt to open a connection to the URI only if
145      * no byte stream or character stream is specified).</p>
146      *
147      * @param systemId The system identifier as a URI string.
148      */
149     public void setSystemId(String systemId) {
150 
151         if (null == inputSource) {
152             inputSource = new InputSource(systemId);
153         } else {
154             inputSource.setSystemId(systemId);
155         }
156     }
157 
158     /**
159      * <p>Get the base ID (URI or system ID) from where URIs
160      * will be resolved.</p>
161      *
162      * @return Base URL for the <code>Source</code>, or <code>null</code>.
163      */
164     public String getSystemId() {
165 
166         if (inputSource == null) {
167             return null;
168         } else {
169             return inputSource.getSystemId();
170         }
171     }
172 
173     /**
174      * The XMLReader to be used for the source tree input. May be null.
175      */
176     private XMLReader reader;
177 
178     /**
179      * <p>The SAX InputSource to be used for the source tree input.
180      * Should not be <code>null</code>.</p>
181      */
182     private InputSource inputSource;
183 
184     /**
185      * Attempt to obtain a SAX InputSource object from a Source
186      * object.
187      *
188      * @param source Must be a non-null Source reference.
189      *
190      * @return An InputSource, or null if Source can not be converted.
191      */
192     public static InputSource sourceToInputSource(Source source) {
193 
194         if (source instanceof SAXSource) {
195             return ((SAXSource) source).getInputSource();
196         } else if (source instanceof StreamSource) {
197             StreamSource ss      = (StreamSource) source;
198             InputSource  isource = new InputSource(ss.getSystemId());
199 
200             isource.setByteStream(ss.getInputStream());
201             isource.setCharacterStream(ss.getReader());
202             isource.setPublicId(ss.getPublicId());
203 
204             return isource;
205         } else {
206             return null;
207         }
208     }
209 }