KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > transform > sax > SAXSource


1 // $Id: SAXSource.java,v 1.7.14.1.2.2 2004/07/13 22:27:50 jsuttor Exp $
2
/*
3  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
4  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
5  */

6
7 /*
8  * @(#)SAXSource.java 1.15 04/07/13
9  */

10 package javax.xml.transform.sax;
11
12 import javax.xml.transform.Source JavaDoc;
13 import javax.xml.transform.stream.StreamSource JavaDoc;
14
15 import org.xml.sax.InputSource JavaDoc;
16 import org.xml.sax.XMLReader JavaDoc;
17
18 /**
19  * <p>Acts as an holder for SAX-style Source.</p>
20  *
21  * <p>Note that XSLT requires namespace support. Attempting to transform an
22  * input source that is not
23  * generated with a namespace-aware parser may result in errors.
24  * Parsers can be made namespace aware by calling the
25  * {@link javax.xml.parsers.SAXParserFactory#setNamespaceAware(boolean awareness)} method.</p>
26  *
27  * @author <a HREF="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
28  * @version $Revision: 1.7.14.1.2.2 $, $Date: 2004/07/13 22:27:50 $
29  */

30 public class SAXSource implements Source JavaDoc {
31
32     /**
33      * If {@link javax.xml.transform.TransformerFactory#getFeature}
34      * returns true when passed this value as an argument,
35      * the Transformer supports Source input of this type.
36      */

37     public static final String JavaDoc FEATURE =
38         "http://javax.xml.transform.sax.SAXSource/feature";
39
40     /**
41      * <p>Zero-argument default constructor. If this constructor is used, and
42      * no SAX source is set using
43      * {@link #setInputSource(InputSource inputSource)} , then the
44      * <code>Transformer</code> will
45      * create an empty source {@link org.xml.sax.InputSource} using
46      * {@link org.xml.sax.InputSource#InputSource() new InputSource()}.</p>
47      *
48      * @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget)
49      */

50     public SAXSource() { }
51
52     /**
53      * Create a <code>SAXSource</code>, using an {@link org.xml.sax.XMLReader}
54      * and a SAX InputSource. The {@link javax.xml.transform.Transformer}
55      * or {@link javax.xml.transform.sax.SAXTransformerFactory} will set itself
56      * to be the reader's {@link org.xml.sax.ContentHandler}, and then will call
57      * reader.parse(inputSource).
58      *
59      * @param reader An XMLReader to be used for the parse.
60      * @param inputSource A SAX input source reference that must be non-null
61      * and that will be passed to the reader parse method.
62      */

63     public SAXSource(XMLReader JavaDoc reader, InputSource JavaDoc inputSource) {
64         this.reader = reader;
65         this.inputSource = inputSource;
66     }
67
68     /**
69      * Create a <code>SAXSource</code>, using a SAX <code>InputSource</code>.
70      * The {@link javax.xml.transform.Transformer} or
71      * {@link javax.xml.transform.sax.SAXTransformerFactory} creates a
72      * reader via {@link org.xml.sax.helpers.XMLReaderFactory}
73      * (if setXMLReader is not used), sets itself as
74      * the reader's {@link org.xml.sax.ContentHandler}, and calls
75      * reader.parse(inputSource).
76      *
77      * @param inputSource An input source reference that must be non-null
78      * and that will be passed to the parse method of the reader.
79      */

80     public SAXSource(InputSource JavaDoc inputSource) {
81         this.inputSource = inputSource;
82     }
83
84     /**
85      * Set the XMLReader to be used for the Source.
86      *
87      * @param reader A valid XMLReader or XMLFilter reference.
88      */

89     public void setXMLReader(XMLReader JavaDoc reader) {
90         this.reader = reader;
91     }
92
93     /**
94      * Get the XMLReader to be used for the Source.
95      *
96      * @return A valid XMLReader or XMLFilter reference, or null.
97      */

98     public XMLReader JavaDoc getXMLReader() {
99         return reader;
100     }
101
102     /**
103      * Set the SAX InputSource to be used for the Source.
104      *
105      * @param inputSource A valid InputSource reference.
106      */

107     public void setInputSource(InputSource JavaDoc inputSource) {
108         this.inputSource = inputSource;
109     }
110
111     /**
112      * Get the SAX InputSource to be used for the Source.
113      *
114      * @return A valid InputSource reference, or null.
115      */

116     public InputSource JavaDoc getInputSource() {
117         return inputSource;
118     }
119
120     /**
121      * Set the system identifier for this Source. If an input source
122      * has already been set, it will set the system ID or that
123      * input source, otherwise it will create a new input source.
124      *
125      * <p>The system identifier is optional if there is a byte stream
126      * or a character stream, but it is still useful to provide one,
127      * since the application can use it to resolve relative URIs
128      * and can include it in error messages and warnings (the parser
129      * will attempt to open a connection to the URI only if
130      * no byte stream or character stream is specified).</p>
131      *
132      * @param systemId The system identifier as a URI string.
133      */

134     public void setSystemId(String JavaDoc systemId) {
135
136         if (null == inputSource) {
137             inputSource = new InputSource JavaDoc(systemId);
138         } else {
139             inputSource.setSystemId(systemId);
140         }
141     }
142
143     /**
144      * <p>Get the base ID (URI or system ID) from where URIs
145      * will be resolved.</p>
146      *
147      * @return Base URL for the <code>Source</code>, or <code>null</code>.
148      */

149     public String JavaDoc getSystemId() {
150
151         if (inputSource == null) {
152             return null;
153         } else {
154             return inputSource.getSystemId();
155         }
156     }
157
158     /**
159      * The XMLReader to be used for the source tree input. May be null.
160      */

161     private XMLReader JavaDoc reader;
162
163     /**
164      * <p>The SAX InputSource to be used for the source tree input.
165      * Should not be <code>null<code>.</p>
166      */

167     private InputSource JavaDoc inputSource;
168
169     /**
170      * Attempt to obtain a SAX InputSource object from a Source
171      * object.
172      *
173      * @param source Must be a non-null Source reference.
174      *
175      * @return An InputSource, or null if Source can not be converted.
176      */

177     public static InputSource JavaDoc sourceToInputSource(Source JavaDoc source) {
178
179         if (source instanceof SAXSource JavaDoc) {
180             return ((SAXSource JavaDoc) source).getInputSource();
181         } else if (source instanceof StreamSource JavaDoc) {
182             StreamSource JavaDoc ss = (StreamSource JavaDoc) source;
183             InputSource JavaDoc isource = new InputSource JavaDoc(ss.getSystemId());
184
185             isource.setByteStream(ss.getInputStream());
186             isource.setCharacterStream(ss.getReader());
187             isource.setPublicId(ss.getPublicId());
188
189             return isource;
190         } else {
191             return null;
192         }
193     }
194 }
195
196
Popular Tags