1 /*
   2  * Copyright (c) 1997, 2010, 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.messaging.saaj.util;
  27 
  28 import java.lang.reflect.*;
  29 import javax.xml.transform.Source;
  30 import javax.xml.transform.Result;
  31 import java.io.InputStream;
  32 import java.io.OutputStream;
  33 
  34 import org.w3c.dom.Document;
  35 import org.w3c.dom.Node;
  36 
  37 /**
  38  *
  39  * @author Santiago.PericasGeertsen@sun.com
  40  * @author Paul.Sandoz@sun.com
  41  */
  42 public class FastInfosetReflection {
  43 
  44     /**
  45      * FI DOMDocumentParser constructor using reflection.
  46      */
  47     static Constructor fiDOMDocumentParser_new;
  48 
  49     /**
  50      * FI <code>DOMDocumentParser.parse()</code> method via reflection.
  51      */
  52     static Method fiDOMDocumentParser_parse;
  53 
  54     /**
  55      * FI DOMDocumentSerializer constructor using reflection.
  56      */
  57     static Constructor fiDOMDocumentSerializer_new;
  58 
  59     /**
  60      * FI <code>FastInfosetSource.serialize(Document)</code> method via reflection.
  61      */
  62     static Method fiDOMDocumentSerializer_serialize;
  63 
  64     /**
  65      * FI <code>FastInfosetSource.setOutputStream(OutputStream)</code> method via reflection.
  66      */
  67     static Method fiDOMDocumentSerializer_setOutputStream;
  68 
  69     /**
  70      * FI FastInfosetSource constructor using reflection.
  71      */
  72     static Class fiFastInfosetSource_class;
  73 
  74     /**
  75      * FI FastInfosetSource constructor using reflection.
  76      */
  77     static Constructor fiFastInfosetSource_new;
  78 
  79     /**
  80      * FI <code>FastInfosetSource.getInputStream()</code> method via reflection.
  81      */
  82     static Method fiFastInfosetSource_getInputStream;
  83 
  84     /**
  85      * FI <code>FastInfosetSource.setInputSTream()</code> method via reflection.
  86      */
  87     static Method fiFastInfosetSource_setInputStream;
  88 
  89     /**
  90      * FI FastInfosetResult constructor using reflection.
  91      */
  92     static Constructor fiFastInfosetResult_new;
  93 
  94     /**
  95      * FI <code>FastInfosetResult.getOutputSTream()</code> method via reflection.
  96      */
  97     static Method fiFastInfosetResult_getOutputStream;
  98 
  99     static {
 100         try {
 101             Class clazz = Class.forName("com.sun.xml.internal.fastinfoset.dom.DOMDocumentParser");
 102             fiDOMDocumentParser_new = clazz.getConstructor((Class[]) null);
 103             fiDOMDocumentParser_parse = clazz.getMethod("parse",
 104                 new Class[] { org.w3c.dom.Document.class, java.io.InputStream.class });
 105 
 106             clazz = Class.forName("com.sun.xml.internal.fastinfoset.dom.DOMDocumentSerializer");
 107             fiDOMDocumentSerializer_new = clazz.getConstructor((Class[])null);
 108             fiDOMDocumentSerializer_serialize = clazz.getMethod("serialize",
 109                 new Class[] { org.w3c.dom.Node.class });
 110             fiDOMDocumentSerializer_setOutputStream = clazz.getMethod("setOutputStream",
 111                 new Class[] { java.io.OutputStream.class });
 112 
 113             fiFastInfosetSource_class = clazz = Class.forName("com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetSource");
 114             fiFastInfosetSource_new = clazz.getConstructor(
 115                 new Class[] { java.io.InputStream.class });
 116             fiFastInfosetSource_getInputStream = clazz.getMethod("getInputStream", (Class[]) null);
 117             fiFastInfosetSource_setInputStream = clazz.getMethod("setInputStream",
 118                 new Class[] { java.io.InputStream.class });
 119 
 120             clazz = Class.forName("com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetResult");
 121             fiFastInfosetResult_new = clazz.getConstructor(
 122                 new Class[] { java.io.OutputStream.class });
 123             fiFastInfosetResult_getOutputStream = clazz.getMethod("getOutputStream", (Class[]) null);
 124         }
 125         catch (Exception e) {
 126             // falls through
 127         }
 128     }
 129 
 130     // -- DOMDocumentParser ----------------------------------------------
 131 
 132     public static Object DOMDocumentParser_new() throws Exception {
 133         if (fiDOMDocumentParser_new == null) {
 134             throw new RuntimeException("Unable to locate Fast Infoset implementation");
 135         }
 136         return fiDOMDocumentParser_new.newInstance((Object[])null);
 137     }
 138 
 139     public static void DOMDocumentParser_parse(Object parser,
 140         Document d, InputStream s) throws Exception
 141     {
 142         if (fiDOMDocumentParser_parse == null) {
 143             throw new RuntimeException("Unable to locate Fast Infoset implementation");
 144         }
 145         fiDOMDocumentParser_parse.invoke(parser, new Object[] { d, s });
 146     }
 147 
 148     // -- DOMDocumentSerializer-------------------------------------------
 149 
 150     public static Object DOMDocumentSerializer_new() throws Exception {
 151         if (fiDOMDocumentSerializer_new == null) {
 152             throw new RuntimeException("Unable to locate Fast Infoset implementation");
 153         }
 154         return fiDOMDocumentSerializer_new.newInstance((Object[])null);
 155     }
 156 
 157     public static void DOMDocumentSerializer_serialize(Object serializer, Node node)
 158         throws Exception
 159     {
 160         if (fiDOMDocumentSerializer_serialize == null) {
 161             throw new RuntimeException("Unable to locate Fast Infoset implementation");
 162         }
 163         fiDOMDocumentSerializer_serialize.invoke(serializer, new Object[] { node });
 164     }
 165 
 166     public static void DOMDocumentSerializer_setOutputStream(Object serializer,
 167         OutputStream os) throws Exception
 168     {
 169         if (fiDOMDocumentSerializer_setOutputStream == null) {
 170             throw new RuntimeException("Unable to locate Fast Infoset implementation");
 171         }
 172         fiDOMDocumentSerializer_setOutputStream.invoke(serializer, new Object[] { os });
 173     }
 174 
 175     // -- FastInfosetSource ----------------------------------------------
 176 
 177     public static boolean isFastInfosetSource(Source source) {
 178         return source.getClass().getName().equals(
 179             "com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetSource");
 180     }
 181 
 182     public static Class getFastInfosetSource_class() {
 183         if (fiFastInfosetSource_class == null) {
 184             throw new RuntimeException("Unable to locate Fast Infoset implementation");
 185         }
 186 
 187         return fiFastInfosetSource_class;
 188     }
 189     public static Source FastInfosetSource_new(InputStream is)
 190         throws Exception
 191     {
 192         if (fiFastInfosetSource_new == null) {
 193             throw new RuntimeException("Unable to locate Fast Infoset implementation");
 194         }
 195         return (Source) fiFastInfosetSource_new.newInstance(new Object[] { is });
 196     }
 197 
 198     public static InputStream FastInfosetSource_getInputStream(Source source)
 199         throws Exception
 200     {
 201         if (fiFastInfosetSource_getInputStream == null) {
 202             throw new RuntimeException("Unable to locate Fast Infoset implementation");
 203         }
 204         return (InputStream) fiFastInfosetSource_getInputStream.invoke(source, (Object[])null);
 205     }
 206 
 207     public static void FastInfosetSource_setInputStream(Source source,
 208         InputStream is) throws Exception
 209     {
 210         if (fiFastInfosetSource_setInputStream == null) {
 211             throw new RuntimeException("Unable to locate Fast Infoset implementation");
 212         }
 213         fiFastInfosetSource_setInputStream.invoke(source, new Object[] { is });
 214     }
 215 
 216     // -- FastInfosetResult ----------------------------------------------
 217 
 218     public static boolean isFastInfosetResult(Result result) {
 219         return result.getClass().getName().equals(
 220             "com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetResult");
 221     }
 222 
 223     public static Result FastInfosetResult_new(OutputStream os)
 224         throws Exception
 225     {
 226         if (fiFastInfosetResult_new == null) {
 227             throw new RuntimeException("Unable to locate Fast Infoset implementation");
 228         }
 229         return (Result) fiFastInfosetResult_new.newInstance(new Object[] { os });
 230     }
 231 
 232     public static OutputStream FastInfosetResult_getOutputStream(Result result)
 233         throws Exception
 234     {
 235         if (fiFastInfosetResult_getOutputStream == null) {
 236             throw new RuntimeException("Unable to locate Fast Infoset implementation");
 237         }
 238         return (OutputStream) fiFastInfosetResult_getOutputStream.invoke(result, (Object[])null);
 239     }
 240 }