001: /*
002: * $Id: FastInfosetReflection.java,v 1.4 2007/07/16 16:41:25 ofung Exp $
003: * $Revision: 1.4 $
004: * $Date: 2007/07/16 16:41:25 $
005: */
006:
007: /*
008: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
009: *
010: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
011: *
012: * The contents of this file are subject to the terms of either the GNU
013: * General Public License Version 2 only ("GPL") or the Common Development
014: * and Distribution License("CDDL") (collectively, the "License"). You
015: * may not use this file except in compliance with the License. You can obtain
016: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
017: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
018: * language governing permissions and limitations under the License.
019: *
020: * When distributing the software, include this License Header Notice in each
021: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
022: * Sun designates this particular file as subject to the "Classpath" exception
023: * as provided by Sun in the GPL Version 2 section of the License file that
024: * accompanied this code. If applicable, add the following below the License
025: * Header, with the fields enclosed by brackets [] replaced by your own
026: * identifying information: "Portions Copyrighted [year]
027: * [name of copyright owner]"
028: *
029: * Contributor(s):
030: *
031: * If you wish your version of this file to be governed by only the CDDL or
032: * only the GPL Version 2, indicate your decision by adding "[Contributor]
033: * elects to include this software in this distribution under the [CDDL or GPL
034: * Version 2] license." If you don't indicate a single choice of license, a
035: * recipient has the option to distribute your version of this file under
036: * either the CDDL, the GPL Version 2 or to extend the choice of license to
037: * its licensees as provided above. However, if you add GPL Version 2 code
038: * and therefore, elected the GPL Version 2 license, then the option applies
039: * only if the new code is made subject to such option by the copyright
040: * holder.
041: */
042:
043: package com.sun.xml.messaging.saaj.util;
044:
045: import java.lang.reflect.*;
046: import javax.xml.transform.Source;
047: import javax.xml.transform.Result;
048: import java.io.InputStream;
049: import java.io.OutputStream;
050:
051: import org.w3c.dom.Document;
052: import org.w3c.dom.Node;
053:
054: /**
055: *
056: * @author Santiago.PericasGeertsen@sun.com
057: * @author Paul.Sandoz@sun.com
058: */
059: public class FastInfosetReflection {
060:
061: /**
062: * FI DOMDocumentParser constructor using reflection.
063: */
064: static Constructor fiDOMDocumentParser_new;
065:
066: /**
067: * FI <code>DOMDocumentParser.parse()</code> method via reflection.
068: */
069: static Method fiDOMDocumentParser_parse;
070:
071: /**
072: * FI DOMDocumentSerializer constructor using reflection.
073: */
074: static Constructor fiDOMDocumentSerializer_new;
075:
076: /**
077: * FI <code>FastInfosetSource.serialize(Document)</code> method via reflection.
078: */
079: static Method fiDOMDocumentSerializer_serialize;
080:
081: /**
082: * FI <code>FastInfosetSource.setOutputStream(OutputStream)</code> method via reflection.
083: */
084: static Method fiDOMDocumentSerializer_setOutputStream;
085:
086: /**
087: * FI FastInfosetSource constructor using reflection.
088: */
089: static Class fiFastInfosetSource_class;
090:
091: /**
092: * FI FastInfosetSource constructor using reflection.
093: */
094: static Constructor fiFastInfosetSource_new;
095:
096: /**
097: * FI <code>FastInfosetSource.getInputStream()</code> method via reflection.
098: */
099: static Method fiFastInfosetSource_getInputStream;
100:
101: /**
102: * FI <code>FastInfosetSource.setInputSTream()</code> method via reflection.
103: */
104: static Method fiFastInfosetSource_setInputStream;
105:
106: /**
107: * FI FastInfosetResult constructor using reflection.
108: */
109: static Constructor fiFastInfosetResult_new;
110:
111: /**
112: * FI <code>FastInfosetResult.getOutputSTream()</code> method via reflection.
113: */
114: static Method fiFastInfosetResult_getOutputStream;
115:
116: static {
117: try {
118: Class clazz = null;
119: clazz = Class
120: .forName("com.sun.xml.fastinfoset.dom.DOMDocumentParser");
121: fiDOMDocumentParser_new = clazz.getConstructor(null);
122: fiDOMDocumentParser_parse = clazz.getMethod("parse",
123: new Class[] { org.w3c.dom.Document.class,
124: java.io.InputStream.class });
125:
126: clazz = Class
127: .forName("com.sun.xml.fastinfoset.dom.DOMDocumentSerializer");
128: fiDOMDocumentSerializer_new = clazz.getConstructor(null);
129: fiDOMDocumentSerializer_serialize = clazz
130: .getMethod("serialize",
131: new Class[] { org.w3c.dom.Node.class });
132: fiDOMDocumentSerializer_setOutputStream = clazz.getMethod(
133: "setOutputStream",
134: new Class[] { java.io.OutputStream.class });
135:
136: fiFastInfosetSource_class = clazz = Class
137: .forName("org.jvnet.fastinfoset.FastInfosetSource");
138: fiFastInfosetSource_new = clazz
139: .getConstructor(new Class[] { java.io.InputStream.class });
140: fiFastInfosetSource_getInputStream = clazz.getMethod(
141: "getInputStream", null);
142: fiFastInfosetSource_setInputStream = clazz.getMethod(
143: "setInputStream",
144: new Class[] { java.io.InputStream.class });
145:
146: clazz = Class
147: .forName("org.jvnet.fastinfoset.FastInfosetResult");
148: fiFastInfosetResult_new = clazz
149: .getConstructor(new Class[] { java.io.OutputStream.class });
150: fiFastInfosetResult_getOutputStream = clazz.getMethod(
151: "getOutputStream", null);
152: } catch (Exception e) {
153: // falls through
154: }
155: }
156:
157: // -- DOMDocumentParser ----------------------------------------------
158:
159: public static Object DOMDocumentParser_new() throws Exception {
160: if (fiDOMDocumentParser_new == null) {
161: throw new RuntimeException(
162: "Unable to locate Fast Infoset implementation");
163: }
164: return fiDOMDocumentParser_new.newInstance(null);
165: }
166:
167: public static void DOMDocumentParser_parse(Object parser,
168: Document d, InputStream s) throws Exception {
169: if (fiDOMDocumentParser_parse == null) {
170: throw new RuntimeException(
171: "Unable to locate Fast Infoset implementation");
172: }
173: fiDOMDocumentParser_parse.invoke(parser, new Object[] { d, s });
174: }
175:
176: // -- DOMDocumentSerializer-------------------------------------------
177:
178: public static Object DOMDocumentSerializer_new() throws Exception {
179: if (fiDOMDocumentSerializer_new == null) {
180: throw new RuntimeException(
181: "Unable to locate Fast Infoset implementation");
182: }
183: return fiDOMDocumentSerializer_new.newInstance(null);
184: }
185:
186: public static void DOMDocumentSerializer_serialize(
187: Object serializer, Node node) throws Exception {
188: if (fiDOMDocumentSerializer_serialize == null) {
189: throw new RuntimeException(
190: "Unable to locate Fast Infoset implementation");
191: }
192: fiDOMDocumentSerializer_serialize.invoke(serializer,
193: new Object[] { node });
194: }
195:
196: public static void DOMDocumentSerializer_setOutputStream(
197: Object serializer, OutputStream os) throws Exception {
198: if (fiDOMDocumentSerializer_setOutputStream == null) {
199: throw new RuntimeException(
200: "Unable to locate Fast Infoset implementation");
201: }
202: fiDOMDocumentSerializer_setOutputStream.invoke(serializer,
203: new Object[] { os });
204: }
205:
206: // -- FastInfosetSource ----------------------------------------------
207:
208: public static boolean isFastInfosetSource(Source source) {
209: return source.getClass().getName().equals(
210: "org.jvnet.fastinfoset.FastInfosetSource");
211: }
212:
213: public static Class getFastInfosetSource_class() {
214: if (fiFastInfosetSource_class == null) {
215: throw new RuntimeException(
216: "Unable to locate Fast Infoset implementation");
217: }
218:
219: return fiFastInfosetSource_class;
220: }
221:
222: public static Source FastInfosetSource_new(InputStream is)
223: throws Exception {
224: if (fiFastInfosetSource_new == null) {
225: throw new RuntimeException(
226: "Unable to locate Fast Infoset implementation");
227: }
228: return (Source) fiFastInfosetSource_new
229: .newInstance(new Object[] { is });
230: }
231:
232: public static InputStream FastInfosetSource_getInputStream(
233: Source source) throws Exception {
234: if (fiFastInfosetSource_getInputStream == null) {
235: throw new RuntimeException(
236: "Unable to locate Fast Infoset implementation");
237: }
238: return (InputStream) fiFastInfosetSource_getInputStream.invoke(
239: source, null);
240: }
241:
242: public static void FastInfosetSource_setInputStream(Source source,
243: InputStream is) throws Exception {
244: if (fiFastInfosetSource_setInputStream == null) {
245: throw new RuntimeException(
246: "Unable to locate Fast Infoset implementation");
247: }
248: fiFastInfosetSource_setInputStream.invoke(source,
249: new Object[] { is });
250: }
251:
252: // -- FastInfosetResult ----------------------------------------------
253:
254: public static boolean isFastInfosetResult(Result result) {
255: return result.getClass().getName().equals(
256: "org.jvnet.fastinfoset.FastInfosetResult");
257: }
258:
259: public static Result FastInfosetResult_new(OutputStream os)
260: throws Exception {
261: if (fiFastInfosetResult_new == null) {
262: throw new RuntimeException(
263: "Unable to locate Fast Infoset implementation");
264: }
265: return (Result) fiFastInfosetResult_new
266: .newInstance(new Object[] { os });
267: }
268:
269: public static OutputStream FastInfosetResult_getOutputStream(
270: Result result) throws Exception {
271: if (fiFastInfosetResult_getOutputStream == null) {
272: throw new RuntimeException(
273: "Unable to locate Fast Infoset implementation");
274: }
275: return (OutputStream) fiFastInfosetResult_getOutputStream
276: .invoke(result, null);
277: }
278: }
|