author | lana |
Fri, 15 Dec 2017 16:38:40 +0000 | |
changeset 48369 | 8647aa05d094 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
42124
640a383428fb
8164479: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
aefimov
parents:
32795
diff
changeset
|
2 |
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. |
12009 | 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.bind.helpers; |
|
27 |
||
28 |
import org.xml.sax.InputSource; |
|
29 |
import org.xml.sax.SAXException; |
|
30 |
import org.xml.sax.XMLReader; |
|
31 |
import org.w3c.dom.Node; |
|
32 |
||
33 |
import javax.xml.bind.JAXBException; |
|
34 |
import javax.xml.bind.PropertyException; |
|
35 |
import javax.xml.bind.UnmarshalException; |
|
36 |
import javax.xml.bind.Unmarshaller; |
|
37 |
import javax.xml.bind.ValidationEventHandler; |
|
38 |
import javax.xml.bind.JAXBElement; |
|
39 |
import javax.xml.bind.annotation.adapters.XmlAdapter; |
|
40 |
import javax.xml.bind.attachment.AttachmentUnmarshaller; |
|
41 |
import javax.xml.parsers.ParserConfigurationException; |
|
42 |
import javax.xml.parsers.SAXParserFactory; |
|
43 |
import javax.xml.stream.XMLEventReader; |
|
44 |
import javax.xml.stream.XMLStreamReader; |
|
45 |
import javax.xml.transform.Source; |
|
46 |
import javax.xml.transform.dom.DOMSource; |
|
47 |
import javax.xml.transform.sax.SAXSource; |
|
48 |
import javax.xml.transform.stream.StreamSource; |
|
49 |
import javax.xml.validation.Schema; |
|
42124
640a383428fb
8164479: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
aefimov
parents:
32795
diff
changeset
|
50 |
import java.io.*; |
12009 | 51 |
import java.net.URL; |
52 |
||
53 |
/** |
|
32795
5a5710ee05a0
8133651: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
25871
diff
changeset
|
54 |
* Partial default {@code Unmarshaller} implementation. |
12009 | 55 |
* |
56 |
* <p> |
|
57 |
* This class provides a partial default implementation for the |
|
58 |
* {@link javax.xml.bind.Unmarshaller}interface. |
|
59 |
* |
|
60 |
* <p> |
|
61 |
* A JAXB Provider has to implement five methods (getUnmarshallerHandler, |
|
62 |
* unmarshal(Node), unmarshal(XMLReader,InputSource), |
|
63 |
* unmarshal(XMLStreamReader), and unmarshal(XMLEventReader). |
|
64 |
* |
|
65 |
* @author <ul> |
|
66 |
* <li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li> |
|
67 |
* </ul> |
|
68 |
* @see javax.xml.bind.Unmarshaller |
|
25840 | 69 |
* @since 1.6, JAXB 1.0 |
12009 | 70 |
*/ |
71 |
public abstract class AbstractUnmarshallerImpl implements Unmarshaller |
|
72 |
{ |
|
73 |
/** handler that will be used to process errors and warnings during unmarshal */ |
|
74 |
private ValidationEventHandler eventHandler = |
|
75 |
new DefaultValidationEventHandler(); |
|
76 |
||
77 |
/** whether or not the unmarshaller will validate */ |
|
78 |
protected boolean validating = false; |
|
79 |
||
80 |
/** |
|
81 |
* XMLReader that will be used to parse a document. |
|
82 |
*/ |
|
83 |
private XMLReader reader = null; |
|
84 |
||
85 |
/** |
|
86 |
* Obtains a configured XMLReader. |
|
87 |
* |
|
88 |
* This method is used when the client-specified |
|
89 |
* {@link SAXSource} object doesn't have XMLReader. |
|
90 |
* |
|
91 |
* {@link Unmarshaller} is not re-entrant, so we will |
|
92 |
* only use one instance of XMLReader. |
|
93 |
*/ |
|
94 |
protected XMLReader getXMLReader() throws JAXBException { |
|
95 |
if(reader==null) { |
|
96 |
try { |
|
97 |
SAXParserFactory parserFactory; |
|
98 |
parserFactory = SAXParserFactory.newInstance(); |
|
99 |
parserFactory.setNamespaceAware(true); |
|
100 |
// there is no point in asking a validation because |
|
101 |
// there is no guarantee that the document will come with |
|
102 |
// a proper schemaLocation. |
|
103 |
parserFactory.setValidating(false); |
|
104 |
reader = parserFactory.newSAXParser().getXMLReader(); |
|
105 |
} catch( ParserConfigurationException e ) { |
|
106 |
throw new JAXBException(e); |
|
107 |
} catch( SAXException e ) { |
|
108 |
throw new JAXBException(e); |
|
109 |
} |
|
110 |
} |
|
111 |
return reader; |
|
112 |
} |
|
113 |
||
114 |
public Object unmarshal( Source source ) throws JAXBException { |
|
115 |
if( source == null ) { |
|
116 |
throw new IllegalArgumentException( |
|
117 |
Messages.format( Messages.MUST_NOT_BE_NULL, "source" ) ); |
|
118 |
} |
|
119 |
||
120 |
if(source instanceof SAXSource) |
|
121 |
return unmarshal( (SAXSource)source ); |
|
122 |
if(source instanceof StreamSource) |
|
123 |
return unmarshal( streamSourceToInputSource((StreamSource)source)); |
|
124 |
if(source instanceof DOMSource) |
|
125 |
return unmarshal( ((DOMSource)source).getNode() ); |
|
126 |
||
127 |
// we don't handle other types of Source |
|
128 |
throw new IllegalArgumentException(); |
|
129 |
} |
|
130 |
||
131 |
// use the client specified XMLReader contained in the SAXSource. |
|
132 |
private Object unmarshal( SAXSource source ) throws JAXBException { |
|
133 |
||
134 |
XMLReader r = source.getXMLReader(); |
|
135 |
if( r == null ) |
|
136 |
r = getXMLReader(); |
|
137 |
||
138 |
return unmarshal( r, source.getInputSource() ); |
|
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* Unmarshals an object by using the specified XMLReader and the InputSource. |
|
143 |
* |
|
144 |
* The callee should call the setErrorHandler method of the XMLReader |
|
145 |
* so that errors are passed to the client-specified ValidationEventHandler. |
|
146 |
*/ |
|
147 |
protected abstract Object unmarshal( XMLReader reader, InputSource source ) throws JAXBException; |
|
148 |
||
149 |
public final Object unmarshal( InputSource source ) throws JAXBException { |
|
150 |
if( source == null ) { |
|
151 |
throw new IllegalArgumentException( |
|
152 |
Messages.format( Messages.MUST_NOT_BE_NULL, "source" ) ); |
|
153 |
} |
|
154 |
||
155 |
return unmarshal( getXMLReader(), source ); |
|
156 |
} |
|
157 |
||
158 |
||
159 |
private Object unmarshal( String url ) throws JAXBException { |
|
160 |
return unmarshal( new InputSource(url) ); |
|
161 |
} |
|
162 |
||
163 |
public final Object unmarshal( URL url ) throws JAXBException { |
|
164 |
if( url == null ) { |
|
165 |
throw new IllegalArgumentException( |
|
166 |
Messages.format( Messages.MUST_NOT_BE_NULL, "url" ) ); |
|
167 |
} |
|
168 |
||
169 |
return unmarshal( url.toExternalForm() ); |
|
170 |
} |
|
171 |
||
172 |
public final Object unmarshal( File f ) throws JAXBException { |
|
173 |
if( f == null ) { |
|
174 |
throw new IllegalArgumentException( |
|
175 |
Messages.format( Messages.MUST_NOT_BE_NULL, "file" ) ); |
|
176 |
} |
|
177 |
||
178 |
try { |
|
42124
640a383428fb
8164479: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
aefimov
parents:
32795
diff
changeset
|
179 |
return unmarshal(new BufferedInputStream(new FileInputStream(f))); |
640a383428fb
8164479: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
aefimov
parents:
32795
diff
changeset
|
180 |
} catch( FileNotFoundException e ) { |
12009 | 181 |
throw new IllegalArgumentException(e.getMessage()); |
182 |
} |
|
183 |
} |
|
184 |
||
185 |
public final Object unmarshal( java.io.InputStream is ) |
|
186 |
throws JAXBException { |
|
187 |
||
188 |
if( is == null ) { |
|
189 |
throw new IllegalArgumentException( |
|
190 |
Messages.format( Messages.MUST_NOT_BE_NULL, "is" ) ); |
|
191 |
} |
|
192 |
||
193 |
InputSource isrc = new InputSource( is ); |
|
194 |
return unmarshal( isrc ); |
|
195 |
} |
|
196 |
||
197 |
public final Object unmarshal( Reader reader ) throws JAXBException { |
|
198 |
if( reader == null ) { |
|
199 |
throw new IllegalArgumentException( |
|
200 |
Messages.format( Messages.MUST_NOT_BE_NULL, "reader" ) ); |
|
201 |
} |
|
202 |
||
203 |
InputSource isrc = new InputSource( reader ); |
|
204 |
return unmarshal( isrc ); |
|
205 |
} |
|
206 |
||
207 |
||
208 |
private static InputSource streamSourceToInputSource( StreamSource ss ) { |
|
209 |
InputSource is = new InputSource(); |
|
210 |
is.setSystemId( ss.getSystemId() ); |
|
211 |
is.setByteStream( ss.getInputStream() ); |
|
212 |
is.setCharacterStream( ss.getReader() ); |
|
213 |
||
214 |
return is; |
|
215 |
} |
|
216 |
||
217 |
||
218 |
/** |
|
219 |
* Indicates whether or not the Unmarshaller is configured to validate |
|
220 |
* during unmarshal operations. |
|
221 |
* <p> |
|
222 |
* <i><b>Note:</b> I named this method isValidating() to stay in-line |
|
223 |
* with JAXP, as opposed to naming it getValidating(). </i> |
|
224 |
* |
|
225 |
* @return true if the Unmarshaller is configured to validate during |
|
226 |
* unmarshal operations, false otherwise |
|
227 |
* @throws JAXBException if an error occurs while retrieving the validating |
|
228 |
* flag |
|
229 |
*/ |
|
230 |
public boolean isValidating() throws JAXBException { |
|
231 |
return validating; |
|
232 |
} |
|
233 |
||
234 |
/** |
|
235 |
* Allow an application to register a validation event handler. |
|
236 |
* <p> |
|
237 |
* The validation event handler will be called by the JAXB Provider if any |
|
238 |
* validation errors are encountered during calls to any of the |
|
32795
5a5710ee05a0
8133651: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
25871
diff
changeset
|
239 |
* {@code unmarshal} methods. If the client application does not register |
12009 | 240 |
* a validation event handler before invoking the unmarshal methods, then |
241 |
* all validation events will be silently ignored and may result in |
|
242 |
* unexpected behaviour. |
|
243 |
* |
|
244 |
* @param handler the validation event handler |
|
245 |
* @throws JAXBException if an error was encountered while setting the |
|
246 |
* event handler |
|
247 |
*/ |
|
248 |
public void setEventHandler(ValidationEventHandler handler) |
|
249 |
throws JAXBException { |
|
250 |
||
251 |
if( handler == null ) { |
|
252 |
eventHandler = new DefaultValidationEventHandler(); |
|
253 |
} else { |
|
254 |
eventHandler = handler; |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
/** |
|
259 |
* Specifies whether or not the Unmarshaller should validate during |
|
32795
5a5710ee05a0
8133651: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
25871
diff
changeset
|
260 |
* unmarshal operations. By default, the {@code Unmarshaller} does |
12009 | 261 |
* not validate. |
262 |
* <p> |
|
263 |
* This method may only be invoked before or after calling one of the |
|
264 |
* unmarshal methods. |
|
265 |
* |
|
266 |
* @param validating true if the Unmarshaller should validate during |
|
267 |
* unmarshal, false otherwise |
|
268 |
* @throws JAXBException if an error occurred while enabling or disabling |
|
269 |
* validation at unmarshal time |
|
270 |
*/ |
|
271 |
public void setValidating(boolean validating) throws JAXBException { |
|
272 |
this.validating = validating; |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Return the current event handler or the default event handler if one |
|
277 |
* hasn't been set. |
|
278 |
* |
|
279 |
* @return the current ValidationEventHandler or the default event handler |
|
280 |
* if it hasn't been set |
|
281 |
* @throws JAXBException if an error was encountered while getting the |
|
282 |
* current event handler |
|
283 |
*/ |
|
284 |
public ValidationEventHandler getEventHandler() throws JAXBException { |
|
285 |
return eventHandler; |
|
286 |
} |
|
287 |
||
288 |
||
289 |
/** |
|
290 |
* Creates an UnmarshalException from a SAXException. |
|
291 |
* |
|
292 |
* This is an utility method provided for the derived classes. |
|
293 |
* |
|
294 |
* <p> |
|
295 |
* When a provider-implemented ContentHandler wants to throw a |
|
296 |
* JAXBException, it needs to wrap the exception by a SAXException. |
|
297 |
* If the unmarshaller implementation blindly wrap SAXException |
|
298 |
* by JAXBException, such an exception will be a JAXBException |
|
299 |
* wrapped by a SAXException wrapped by another JAXBException. |
|
300 |
* This is silly. |
|
301 |
* |
|
302 |
* <p> |
|
303 |
* This method checks the nested exception of SAXException |
|
304 |
* and reduce those excessive wrapping. |
|
305 |
* |
|
306 |
* @return the resulting UnmarshalException |
|
307 |
*/ |
|
308 |
protected UnmarshalException createUnmarshalException( SAXException e ) { |
|
309 |
// check the nested exception to see if it's an UnmarshalException |
|
310 |
Exception nested = e.getException(); |
|
311 |
if(nested instanceof UnmarshalException) |
|
312 |
return (UnmarshalException)nested; |
|
313 |
||
314 |
if(nested instanceof RuntimeException) |
|
315 |
// typically this is an unexpected exception, |
|
316 |
// just throw it rather than wrap it, so that the full stack |
|
317 |
// trace can be displayed. |
|
318 |
throw (RuntimeException)nested; |
|
319 |
||
320 |
||
321 |
// otherwise simply wrap it |
|
322 |
if(nested!=null) |
|
323 |
return new UnmarshalException(nested); |
|
324 |
else |
|
325 |
return new UnmarshalException(e); |
|
326 |
} |
|
327 |
||
328 |
/** |
|
329 |
* Default implementation of the setProperty method always |
|
330 |
* throws PropertyException since there are no required |
|
331 |
* properties. If a provider needs to handle additional |
|
332 |
* properties, it should override this method in a derived class. |
|
333 |
*/ |
|
334 |
public void setProperty( String name, Object value ) |
|
335 |
throws PropertyException { |
|
336 |
||
337 |
if( name == null ) { |
|
338 |
throw new IllegalArgumentException( |
|
339 |
Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) ); |
|
340 |
} |
|
341 |
||
342 |
throw new PropertyException(name, value); |
|
343 |
} |
|
344 |
||
345 |
/** |
|
346 |
* Default implementation of the getProperty method always |
|
347 |
* throws PropertyException since there are no required |
|
348 |
* properties. If a provider needs to handle additional |
|
349 |
* properties, it should override this method in a derived class. |
|
350 |
*/ |
|
351 |
public Object getProperty( String name ) |
|
352 |
throws PropertyException { |
|
353 |
||
354 |
if( name == null ) { |
|
355 |
throw new IllegalArgumentException( |
|
356 |
Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) ); |
|
357 |
} |
|
358 |
||
359 |
throw new PropertyException(name); |
|
360 |
} |
|
361 |
||
362 |
public Object unmarshal(XMLEventReader reader) throws JAXBException { |
|
363 |
||
364 |
throw new UnsupportedOperationException(); |
|
365 |
} |
|
366 |
||
367 |
public Object unmarshal(XMLStreamReader reader) throws JAXBException { |
|
368 |
||
369 |
throw new UnsupportedOperationException(); |
|
370 |
} |
|
371 |
||
372 |
public <T> JAXBElement<T> unmarshal(Node node, Class<T> expectedType) throws JAXBException { |
|
373 |
throw new UnsupportedOperationException(); |
|
374 |
} |
|
375 |
||
376 |
public <T> JAXBElement<T> unmarshal(Source source, Class<T> expectedType) throws JAXBException { |
|
377 |
throw new UnsupportedOperationException(); |
|
378 |
} |
|
379 |
||
380 |
public <T> JAXBElement<T> unmarshal(XMLStreamReader reader, Class<T> expectedType) throws JAXBException { |
|
381 |
throw new UnsupportedOperationException(); |
|
382 |
} |
|
383 |
||
384 |
public <T> JAXBElement<T> unmarshal(XMLEventReader reader, Class<T> expectedType) throws JAXBException { |
|
385 |
throw new UnsupportedOperationException(); |
|
386 |
} |
|
387 |
||
388 |
public void setSchema(Schema schema) { |
|
389 |
throw new UnsupportedOperationException(); |
|
390 |
} |
|
391 |
||
392 |
public Schema getSchema() { |
|
393 |
throw new UnsupportedOperationException(); |
|
394 |
} |
|
395 |
||
396 |
public void setAdapter(XmlAdapter adapter) { |
|
397 |
if(adapter==null) |
|
398 |
throw new IllegalArgumentException(); |
|
399 |
setAdapter((Class)adapter.getClass(),adapter); |
|
400 |
} |
|
401 |
||
402 |
public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) { |
|
403 |
throw new UnsupportedOperationException(); |
|
404 |
} |
|
405 |
||
406 |
public <A extends XmlAdapter> A getAdapter(Class<A> type) { |
|
407 |
throw new UnsupportedOperationException(); |
|
408 |
} |
|
409 |
||
410 |
public void setAttachmentUnmarshaller(AttachmentUnmarshaller au) { |
|
411 |
throw new UnsupportedOperationException(); |
|
412 |
} |
|
413 |
||
414 |
public AttachmentUnmarshaller getAttachmentUnmarshaller() { |
|
415 |
throw new UnsupportedOperationException(); |
|
416 |
} |
|
417 |
||
418 |
public void setListener(Listener listener) { |
|
419 |
throw new UnsupportedOperationException(); |
|
420 |
} |
|
421 |
||
422 |
public Listener getListener() { |
|
423 |
throw new UnsupportedOperationException(); |
|
424 |
} |
|
425 |
} |