|
1 /* |
|
2 * Copyright (c) 1997, 2012, 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 com.sun.xml.internal.ws.streaming; |
|
27 |
|
28 import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory; |
|
29 import com.sun.xml.internal.ws.util.FastInfosetUtil; |
|
30 import com.sun.xml.internal.ws.util.xml.XmlUtil; |
|
31 |
|
32 import javax.xml.stream.XMLStreamReader; |
|
33 import javax.xml.transform.Source; |
|
34 import javax.xml.transform.Transformer; |
|
35 import javax.xml.transform.dom.DOMResult; |
|
36 import javax.xml.transform.dom.DOMSource; |
|
37 import javax.xml.transform.sax.SAXSource; |
|
38 import javax.xml.transform.stream.StreamSource; |
|
39 import java.io.InputStream; |
|
40 import java.io.InputStreamReader; |
|
41 import java.io.Reader; |
|
42 import java.lang.reflect.Method; |
|
43 import java.net.URL; |
|
44 |
|
45 /** |
|
46 * @author Santiago.PericasGeertsen@sun.com |
|
47 */ |
|
48 public class SourceReaderFactory { |
|
49 |
|
50 /** |
|
51 * FI FastInfosetSource class. |
|
52 */ |
|
53 static Class fastInfosetSourceClass; |
|
54 |
|
55 /** |
|
56 * FI <code>StAXDocumentSerializer.setEncoding()</code> method via reflection. |
|
57 */ |
|
58 static Method fastInfosetSource_getInputStream; |
|
59 |
|
60 static { |
|
61 // Use reflection to avoid static dependency with FI jar |
|
62 try { |
|
63 fastInfosetSourceClass = |
|
64 Class.forName("com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetSource"); |
|
65 fastInfosetSource_getInputStream = |
|
66 fastInfosetSourceClass.getMethod("getInputStream"); |
|
67 } |
|
68 catch (Exception e) { |
|
69 fastInfosetSourceClass = null; |
|
70 } |
|
71 } |
|
72 |
|
73 public static XMLStreamReader createSourceReader(Source source, boolean rejectDTDs) { |
|
74 return createSourceReader(source, rejectDTDs, null); |
|
75 } |
|
76 |
|
77 public static XMLStreamReader createSourceReader(Source source, boolean rejectDTDs, String charsetName) { |
|
78 try { |
|
79 if (source instanceof StreamSource) { |
|
80 StreamSource streamSource = (StreamSource) source; |
|
81 InputStream is = streamSource.getInputStream(); |
|
82 |
|
83 if (is != null) { |
|
84 // Wrap input stream in Reader if charset is specified |
|
85 if (charsetName != null) { |
|
86 return XMLStreamReaderFactory.create( |
|
87 source.getSystemId(), new InputStreamReader(is, charsetName), rejectDTDs); |
|
88 } |
|
89 else { |
|
90 return XMLStreamReaderFactory.create( |
|
91 source.getSystemId(), is, rejectDTDs); |
|
92 } |
|
93 } |
|
94 else { |
|
95 Reader reader = streamSource.getReader(); |
|
96 if (reader != null) { |
|
97 return XMLStreamReaderFactory.create( |
|
98 source.getSystemId(), reader, rejectDTDs); |
|
99 } |
|
100 else { |
|
101 return XMLStreamReaderFactory.create( |
|
102 source.getSystemId(), new URL(source.getSystemId()).openStream(), rejectDTDs ); |
|
103 } |
|
104 } |
|
105 } |
|
106 else if (source.getClass() == fastInfosetSourceClass) { |
|
107 return FastInfosetUtil.createFIStreamReader((InputStream) |
|
108 fastInfosetSource_getInputStream.invoke(source)); |
|
109 } |
|
110 else if (source instanceof DOMSource) { |
|
111 DOMStreamReader dsr = new DOMStreamReader(); |
|
112 dsr.setCurrentNode(((DOMSource) source).getNode()); |
|
113 return dsr; |
|
114 } |
|
115 else if (source instanceof SAXSource) { |
|
116 // TODO: need SAX to StAX adapter here -- Use transformer for now |
|
117 Transformer tx = XmlUtil.newTransformer(); |
|
118 DOMResult domResult = new DOMResult(); |
|
119 tx.transform(source, domResult); |
|
120 return createSourceReader( |
|
121 new DOMSource(domResult.getNode()), |
|
122 rejectDTDs); |
|
123 } |
|
124 else { |
|
125 throw new XMLReaderException("sourceReader.invalidSource", |
|
126 source.getClass().getName()); |
|
127 } |
|
128 } |
|
129 catch (Exception e) { |
|
130 throw new XMLReaderException(e); |
|
131 } |
|
132 } |
|
133 |
|
134 } |