001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.ws.util;
027:
028: import java.io.InputStream;
029: import java.io.OutputStream;
030: import java.lang.reflect.Constructor;
031: import java.lang.reflect.Method;
032: import javax.xml.transform.Result;
033: import javax.xml.transform.Source;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Node;
036:
037: /**
038: *
039: * @author Santiago.PericasGeertsen@sun.com
040: * @author Paul.Sandoz@sun.com
041: */
042: public class FastInfosetReflection {
043: /**
044: *
045: */
046: public static ClassLoader fiClassLoader;
047:
048: /**
049: * FI StAXDocumentParser constructor using reflection.
050: */
051: public static Constructor fiStAXDocumentParser_new;
052:
053: /**
054: * FI <code>StAXDocumentParser.setInputStream()</code> method via reflection.
055: */
056: public static Method fiStAXDocumentParser_setInputStream;
057:
058: /**
059: * FI <code>StAXDocumentParser.setStringInterning()</code> method via reflection.
060: */
061: public static Method fiStAXDocumentParser_setStringInterning;
062:
063: /**
064: * FI StAXDocumentSerializer constructor using reflection.
065: */
066: public static Constructor fiStAXDocumentSerializer_new;
067:
068: /**
069: * FI <code>StAXDocumentSerializer.setOutputStream()</code> method via reflection.
070: */
071: public static Method fiStAXDocumentSerializer_setOutputStream;
072:
073: /**
074: * FI <code>StAXDocumentSerializer.setEncoding()</code> method via reflection.
075: */
076: public static Method fiStAXDocumentSerializer_setEncoding;
077:
078: /**
079: * FI DOMDocumentParser constructor using reflection.
080: */
081: public static Constructor fiDOMDocumentParser_new;
082:
083: /**
084: * FI <code>DOMDocumentParser.parse()</code> method via reflection.
085: */
086: public static Method fiDOMDocumentParser_parse;
087:
088: /**
089: * FI DOMDocumentSerializer constructor using reflection.
090: */
091: public static Constructor fiDOMDocumentSerializer_new;
092:
093: /**
094: * FI <code>FastInfosetSource.serialize(Document)</code> method via reflection.
095: */
096: public static Method fiDOMDocumentSerializer_serialize;
097:
098: /**
099: * FI <code>FastInfosetSource.setOutputStream(OutputStream)</code> method via reflection.
100: */
101: public static Method fiDOMDocumentSerializer_setOutputStream;
102:
103: /**
104: * FI FastInfosetSource class.
105: */
106: public static Class fiFastInfosetSource;
107:
108: /**
109: * FI FastInfosetSource constructor using reflection.
110: */
111: public static Constructor fiFastInfosetSource_new;
112:
113: /**
114: * FI <code>FastInfosetSource.getInputStream()</code> method via reflection.
115: */
116: public static Method fiFastInfosetSource_getInputStream;
117:
118: /**
119: * FI <code>FastInfosetSource.setInputSTream()</code> method via reflection.
120: */
121: public static Method fiFastInfosetSource_setInputStream;
122:
123: /**
124: * FI FastInfosetResult class using reflection.
125: */
126: public static Class fiFastInfosetResult;
127:
128: /**
129: * FI FastInfosetResult constructor using reflection.
130: */
131: public static Constructor fiFastInfosetResult_new;
132:
133: /**
134: * FI <code>FastInfosetResult.getOutputSTream()</code> method via reflection.
135: */
136: public static Method fiFastInfosetResult_getOutputStream;
137:
138: static {
139: // Use reflection to avoid static dependency with FI jar
140: try {
141: Class clazz = null;
142: clazz = Class
143: .forName("com.sun.xml.internal.fastinfoset.stax.StAXDocumentParser");
144: fiStAXDocumentParser_new = clazz.getConstructor();
145: fiStAXDocumentParser_setInputStream = clazz.getMethod(
146: "setInputStream", java.io.InputStream.class);
147: fiStAXDocumentParser_setStringInterning = clazz.getMethod(
148: "setStringInterning", boolean.class);
149:
150: clazz = Class
151: .forName("com.sun.xml.internal.fastinfoset.stax.StAXDocumentSerializer");
152: fiStAXDocumentSerializer_new = clazz.getConstructor();
153: fiStAXDocumentSerializer_setOutputStream = clazz.getMethod(
154: "setOutputStream", java.io.OutputStream.class);
155: fiStAXDocumentSerializer_setEncoding = clazz.getMethod(
156: "setEncoding", String.class);
157:
158: clazz = Class
159: .forName("com.sun.xml.internal.fastinfoset.dom.DOMDocumentParser");
160: fiDOMDocumentParser_new = clazz.getConstructor();
161: fiDOMDocumentParser_parse = clazz.getMethod("parse",
162: new Class[] { org.w3c.dom.Document.class,
163: java.io.InputStream.class });
164:
165: clazz = Class
166: .forName("com.sun.xml.internal.fastinfoset.dom.DOMDocumentSerializer");
167: fiDOMDocumentSerializer_new = clazz.getConstructor();
168: fiDOMDocumentSerializer_serialize = clazz
169: .getMethod("serialize",
170: new Class[] { org.w3c.dom.Node.class });
171: fiDOMDocumentSerializer_setOutputStream = clazz.getMethod(
172: "setOutputStream",
173: new Class[] { java.io.OutputStream.class });
174:
175: fiFastInfosetSource = clazz = Class
176: .forName("com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetSource");
177: fiFastInfosetSource_new = clazz
178: .getConstructor(new Class[] { java.io.InputStream.class });
179: fiFastInfosetSource_getInputStream = clazz
180: .getMethod("getInputStream");
181: fiFastInfosetSource_setInputStream = clazz.getMethod(
182: "setInputStream",
183: new Class[] { java.io.InputStream.class });
184:
185: fiFastInfosetResult = clazz = Class
186: .forName("com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetResult");
187: fiFastInfosetResult_new = clazz
188: .getConstructor(new Class[] { java.io.OutputStream.class });
189: fiFastInfosetResult_getOutputStream = clazz
190: .getMethod("getOutputStream");
191: } catch (Exception e) {
192: // falls through
193: }
194: }
195:
196: // -- DOMDocumentParser ----------------------------------------------
197:
198: public static Object DOMDocumentParser_new() throws Exception {
199: if (fiDOMDocumentParser_new == null) {
200: throw new RuntimeException(
201: "Unable to locate Fast Infoset implementation");
202: }
203: return fiDOMDocumentParser_new.newInstance();
204: }
205:
206: public static void DOMDocumentParser_parse(Object parser,
207: Document d, InputStream s) throws Exception {
208: if (fiDOMDocumentParser_parse == null) {
209: throw new RuntimeException(
210: "Unable to locate Fast Infoset implementation");
211: }
212: fiDOMDocumentParser_parse.invoke(parser, d, s);
213: }
214:
215: // -- DOMDocumentSerializer-------------------------------------------
216:
217: public static Object DOMDocumentSerializer_new() throws Exception {
218: if (fiDOMDocumentSerializer_new == null) {
219: throw new RuntimeException(
220: "Unable to locate Fast Infoset implementation");
221: }
222: return fiDOMDocumentSerializer_new.newInstance();
223: }
224:
225: public static void DOMDocumentSerializer_serialize(
226: Object serializer, Node node) throws Exception {
227: if (fiDOMDocumentSerializer_serialize == null) {
228: throw new RuntimeException(
229: "Unable to locate Fast Infoset implementation");
230: }
231: fiDOMDocumentSerializer_serialize.invoke(serializer, node);
232: }
233:
234: public static void DOMDocumentSerializer_setOutputStream(
235: Object serializer, OutputStream os) throws Exception {
236: if (fiDOMDocumentSerializer_setOutputStream == null) {
237: throw new RuntimeException(
238: "Unable to locate Fast Infoset implementation");
239: }
240: fiDOMDocumentSerializer_setOutputStream.invoke(serializer, os);
241: }
242:
243: // -- FastInfosetSource ----------------------------------------------
244:
245: public static boolean isFastInfosetSource(Source source) {
246: return source.getClass() == fiFastInfosetSource;
247: }
248:
249: public static Source FastInfosetSource_new(InputStream is)
250: throws Exception {
251: if (fiFastInfosetSource_new == null) {
252: throw new RuntimeException(
253: "Unable to locate Fast Infoset implementation");
254: }
255: return (Source) fiFastInfosetSource_new.newInstance(is);
256: }
257:
258: public static InputStream FastInfosetSource_getInputStream(
259: Source source) throws Exception {
260: if (fiFastInfosetSource_getInputStream == null) {
261: throw new RuntimeException(
262: "Unable to locate Fast Infoset implementation");
263: }
264: return (InputStream) fiFastInfosetSource_getInputStream
265: .invoke(source);
266: }
267:
268: public static void FastInfosetSource_setInputStream(Source source,
269: InputStream is) throws Exception {
270: if (fiFastInfosetSource_setInputStream == null) {
271: throw new RuntimeException(
272: "Unable to locate Fast Infoset implementation");
273: }
274: fiFastInfosetSource_setInputStream.invoke(source, is);
275: }
276:
277: // -- FastInfosetResult ----------------------------------------------
278:
279: public static boolean isFastInfosetResult(Result result) {
280: return result.getClass() == fiFastInfosetResult;
281: }
282:
283: public static Result FastInfosetResult_new(OutputStream os)
284: throws Exception {
285: if (fiFastInfosetResult_new == null) {
286: throw new RuntimeException(
287: "Unable to locate Fast Infoset implementation");
288: }
289: return (Result) fiFastInfosetResult_new.newInstance(os);
290: }
291:
292: public static OutputStream FastInfosetResult_getOutputStream(
293: Result result) throws Exception {
294: if (fiFastInfosetResult_getOutputStream == null) {
295: throw new RuntimeException(
296: "Unable to locate Fast Infoset implementation");
297: }
298: return (OutputStream) fiFastInfosetResult_getOutputStream
299: .invoke(result);
300: }
301: }
|