author | joehw |
Fri, 08 Jan 2016 10:51:34 -0800 | |
changeset 34983 | cab976ee6f21 |
parent 25868 | 686eef1e7a79 |
child 47043 | 70b6df26bd98 |
permissions | -rw-r--r-- |
12005 | 1 |
/* |
34983
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
2 |
* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. |
12005 | 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> |
|
25262 | 44 |
* @since 1.4 |
12005 | 45 |
*/ |
46 |
public class SAXSource implements Source { |
|
47 |
||
48 |
/** |
|
49 |
* If {@link javax.xml.transform.TransformerFactory#getFeature} |
|
50 |
* returns true when passed this value as an argument, |
|
51 |
* the Transformer supports Source input of this type. |
|
52 |
*/ |
|
53 |
public static final String FEATURE = |
|
54 |
"http://javax.xml.transform.sax.SAXSource/feature"; |
|
55 |
||
56 |
/** |
|
57 |
* <p>Zero-argument default constructor. If this constructor is used, and |
|
58 |
* no SAX source is set using |
|
59 |
* {@link #setInputSource(InputSource inputSource)} , then the |
|
60 |
* <code>Transformer</code> will |
|
61 |
* create an empty source {@link org.xml.sax.InputSource} using |
|
62 |
* {@link org.xml.sax.InputSource#InputSource() new InputSource()}.</p> |
|
63 |
* |
|
64 |
* @see javax.xml.transform.Transformer#transform(Source xmlSource, Result outputTarget) |
|
65 |
*/ |
|
66 |
public SAXSource() { } |
|
67 |
||
68 |
/** |
|
69 |
* Create a <code>SAXSource</code>, using an {@link org.xml.sax.XMLReader} |
|
70 |
* and a SAX InputSource. The {@link javax.xml.transform.Transformer} |
|
71 |
* or {@link javax.xml.transform.sax.SAXTransformerFactory} will set itself |
|
72 |
* to be the reader's {@link org.xml.sax.ContentHandler}, and then will call |
|
73 |
* reader.parse(inputSource). |
|
74 |
* |
|
75 |
* @param reader An XMLReader to be used for the parse. |
|
76 |
* @param inputSource A SAX input source reference that must be non-null |
|
77 |
* and that will be passed to the reader parse method. |
|
78 |
*/ |
|
79 |
public SAXSource(XMLReader reader, InputSource inputSource) { |
|
80 |
this.reader = reader; |
|
81 |
this.inputSource = inputSource; |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Create a <code>SAXSource</code>, using a SAX <code>InputSource</code>. |
|
86 |
* The {@link javax.xml.transform.Transformer} or |
|
87 |
* {@link javax.xml.transform.sax.SAXTransformerFactory} creates a |
|
88 |
* reader via {@link org.xml.sax.helpers.XMLReaderFactory} |
|
89 |
* (if setXMLReader is not used), sets itself as |
|
90 |
* the reader's {@link org.xml.sax.ContentHandler}, and calls |
|
91 |
* reader.parse(inputSource). |
|
92 |
* |
|
93 |
* @param inputSource An input source reference that must be non-null |
|
94 |
* and that will be passed to the parse method of the reader. |
|
95 |
*/ |
|
96 |
public SAXSource(InputSource inputSource) { |
|
97 |
this.inputSource = inputSource; |
|
98 |
} |
|
99 |
||
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 |
*/ |
|
34983
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
150 |
@Override |
12005 | 151 |
public void setSystemId(String systemId) { |
152 |
||
153 |
if (null == inputSource) { |
|
154 |
inputSource = new InputSource(systemId); |
|
155 |
} else { |
|
156 |
inputSource.setSystemId(systemId); |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* <p>Get the base ID (URI or system ID) from where URIs |
|
162 |
* will be resolved.</p> |
|
163 |
* |
|
164 |
* @return Base URL for the <code>Source</code>, or <code>null</code>. |
|
165 |
*/ |
|
34983
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
166 |
@Override |
12005 | 167 |
public String getSystemId() { |
168 |
||
169 |
if (inputSource == null) { |
|
170 |
return null; |
|
171 |
} else { |
|
172 |
return inputSource.getSystemId(); |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
/** |
|
177 |
* The XMLReader to be used for the source tree input. May be null. |
|
178 |
*/ |
|
179 |
private XMLReader reader; |
|
180 |
||
181 |
/** |
|
182 |
* <p>The SAX InputSource to be used for the source tree input. |
|
183 |
* Should not be <code>null</code>.</p> |
|
184 |
*/ |
|
185 |
private InputSource inputSource; |
|
186 |
||
187 |
/** |
|
188 |
* Attempt to obtain a SAX InputSource object from a Source |
|
189 |
* object. |
|
190 |
* |
|
191 |
* @param source Must be a non-null Source reference. |
|
192 |
* |
|
193 |
* @return An InputSource, or null if Source can not be converted. |
|
194 |
*/ |
|
195 |
public static InputSource sourceToInputSource(Source source) { |
|
196 |
||
197 |
if (source instanceof SAXSource) { |
|
198 |
return ((SAXSource) source).getInputSource(); |
|
199 |
} else if (source instanceof StreamSource) { |
|
200 |
StreamSource ss = (StreamSource) source; |
|
201 |
InputSource isource = new InputSource(ss.getSystemId()); |
|
202 |
||
203 |
isource.setByteStream(ss.getInputStream()); |
|
204 |
isource.setCharacterStream(ss.getReader()); |
|
205 |
isource.setPublicId(ss.getPublicId()); |
|
206 |
||
207 |
return isource; |
|
208 |
} else { |
|
209 |
return null; |
|
210 |
} |
|
211 |
} |
|
34983
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
212 |
|
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
213 |
/** |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
214 |
* Indicates whether the {@code SAXSource} object is empty. Empty is |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
215 |
* defined as follows: |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
216 |
* <ul> |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
217 |
* <li>if the system identifier and {@code InputSource} are {@code null}; |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
218 |
* </li> |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
219 |
* <li>if the system identifier is {@code null}, and the {@code InputSource} |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
220 |
* is empty. |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
221 |
* </li> |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
222 |
* </ul> |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
223 |
* |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
224 |
* @return true if the {@code SAXSource} object is empty, false otherwise |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
225 |
*/ |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
226 |
@Override |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
227 |
public boolean isEmpty() { |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
228 |
return getSystemId() == null && (inputSource == null || inputSource.isEmpty()); |
cab976ee6f21
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
joehw
parents:
25868
diff
changeset
|
229 |
} |
12005 | 230 |
} |