src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/util/transform/EfficientStreamingTransformer.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/util/transform/EfficientStreamingTransformer.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,423 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * EfficientStreamingTransformer.java
    1.31 + *
    1.32 + * Created on July 29, 2002, 3:49 PM
    1.33 + */
    1.34 +
    1.35 +package com.sun.xml.internal.messaging.saaj.util.transform;
    1.36 +
    1.37 +import java.io.*;
    1.38 +
    1.39 +import java.net.URISyntaxException;
    1.40 +import javax.xml.transform.dom.DOMSource;
    1.41 +import javax.xml.transform.dom.DOMResult;
    1.42 +import javax.xml.transform.stream.StreamResult;
    1.43 +import javax.xml.transform.stream.StreamSource;
    1.44 +
    1.45 +import org.w3c.dom.Document;
    1.46 +
    1.47 +import com.sun.xml.internal.messaging.saaj.util.XMLDeclarationParser;
    1.48 +import com.sun.xml.internal.messaging.saaj.util.FastInfosetReflection;
    1.49 +import java.net.URI;
    1.50 +import javax.xml.transform.Transformer;
    1.51 +import javax.xml.transform.TransformerException;
    1.52 +import javax.xml.transform.TransformerFactory;
    1.53 +
    1.54 +/**
    1.55 + * This class is a proxy for a Transformer object with optimizations
    1.56 + * for certain cases. If source and result are of type stream, then
    1.57 + * bytes are simply copied whenever possible (note that this assumes
    1.58 + * that the input is well formed). In addition, it provides support for
    1.59 + * FI using native DOM parsers and serializers.
    1.60 + *
    1.61 + * @author Panos Kougiouris panos@acm.org
    1.62 + * @author Santiago.PericasGeertsen@sun.com
    1.63 + *
    1.64 + */
    1.65 +public class EfficientStreamingTransformer
    1.66 +    extends javax.xml.transform.Transformer {
    1.67 +
    1.68 +  //static final String version;
    1.69 +  //static final String vendor;
    1.70 +  // removing static : security issue : CR 6813167Z
    1.71 +  private final TransformerFactory transformerFactory = TransformerFactory.newInstance();
    1.72 +
    1.73 +  /**
    1.74 +  removing support for Java 1.4 and 1.3 : CR6658158
    1.75 +  static {
    1.76 +        version = System.getProperty("java.vm.version");
    1.77 +        vendor = System.getProperty("java.vm.vendor");
    1.78 +        if (vendor.startsWith("Sun") &&
    1.79 +            (version.startsWith("1.4") || version.startsWith("1.3"))) {
    1.80 +            transformerFactory =
    1.81 +                new com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl();
    1.82 +        }
    1.83 +  }*/
    1.84 +
    1.85 +    /**
    1.86 +     * TransformerFactory instance.
    1.87 +     */
    1.88 +
    1.89 +    /**
    1.90 +     * Underlying XSLT transformer.
    1.91 +     */
    1.92 +    private Transformer m_realTransformer = null;
    1.93 +
    1.94 +    /**
    1.95 +     * Undelying FI DOM parser.
    1.96 +     */
    1.97 +    private Object m_fiDOMDocumentParser = null;
    1.98 +
    1.99 +    /**
   1.100 +     * Underlying FI DOM serializer.
   1.101 +     */
   1.102 +    private Object m_fiDOMDocumentSerializer = null;
   1.103 +
   1.104 +    private EfficientStreamingTransformer() {
   1.105 +    }
   1.106 +
   1.107 +    private void materialize() throws TransformerException {
   1.108 +        if (m_realTransformer == null) {
   1.109 +            m_realTransformer = transformerFactory.newTransformer();
   1.110 +        }
   1.111 +    }
   1.112 +
   1.113 +    public void clearParameters() {
   1.114 +        if (m_realTransformer != null)
   1.115 +            m_realTransformer.clearParameters();
   1.116 +    }
   1.117 +
   1.118 +    public javax.xml.transform.ErrorListener getErrorListener() {
   1.119 +        try {
   1.120 +            materialize();
   1.121 +            return m_realTransformer.getErrorListener();
   1.122 +        } catch (TransformerException e) {
   1.123 +            // will be caught later
   1.124 +        }
   1.125 +        return null;
   1.126 +    }
   1.127 +
   1.128 +    public java.util.Properties getOutputProperties() {
   1.129 +        try {
   1.130 +            materialize();
   1.131 +            return m_realTransformer.getOutputProperties();
   1.132 +        } catch (TransformerException e) {
   1.133 +            // will be caught later
   1.134 +        }
   1.135 +        return null;
   1.136 +    }
   1.137 +
   1.138 +    public String getOutputProperty(String str)
   1.139 +        throws java.lang.IllegalArgumentException {
   1.140 +        try {
   1.141 +            materialize();
   1.142 +            return m_realTransformer.getOutputProperty(str);
   1.143 +        } catch (TransformerException e) {
   1.144 +            // will be caught later
   1.145 +        }
   1.146 +        return null;
   1.147 +    }
   1.148 +
   1.149 +    public Object getParameter(String str) {
   1.150 +        try {
   1.151 +            materialize();
   1.152 +            return m_realTransformer.getParameter(str);
   1.153 +        } catch (TransformerException e) {
   1.154 +            // will be caught later
   1.155 +        }
   1.156 +        return null;
   1.157 +    }
   1.158 +
   1.159 +    public javax.xml.transform.URIResolver getURIResolver() {
   1.160 +        try {
   1.161 +            materialize();
   1.162 +            return m_realTransformer.getURIResolver();
   1.163 +        } catch (TransformerException e) {
   1.164 +            // will be caught later
   1.165 +        }
   1.166 +        return null;
   1.167 +    }
   1.168 +
   1.169 +    public void setErrorListener(
   1.170 +        javax.xml.transform.ErrorListener errorListener)
   1.171 +        throws java.lang.IllegalArgumentException {
   1.172 +        try {
   1.173 +            materialize();
   1.174 +            m_realTransformer.setErrorListener(errorListener);
   1.175 +        } catch (TransformerException e) {
   1.176 +            // will be caught later
   1.177 +        }
   1.178 +    }
   1.179 +
   1.180 +    public void setOutputProperties(java.util.Properties properties)
   1.181 +        throws java.lang.IllegalArgumentException {
   1.182 +        try {
   1.183 +            materialize();
   1.184 +            m_realTransformer.setOutputProperties(properties);
   1.185 +        } catch (TransformerException e) {
   1.186 +            // will be caught later
   1.187 +        }
   1.188 +    }
   1.189 +
   1.190 +    public void setOutputProperty(String str, String str1)
   1.191 +        throws java.lang.IllegalArgumentException {
   1.192 +        try {
   1.193 +            materialize();
   1.194 +            m_realTransformer.setOutputProperty(str, str1);
   1.195 +        } catch (TransformerException e) {
   1.196 +            // will be caught later
   1.197 +        }
   1.198 +    }
   1.199 +
   1.200 +    public void setParameter(String str, Object obj) {
   1.201 +        try {
   1.202 +            materialize();
   1.203 +            m_realTransformer.setParameter(str, obj);
   1.204 +        } catch (TransformerException e) {
   1.205 +            // will be caught later
   1.206 +        }
   1.207 +    }
   1.208 +
   1.209 +    public void setURIResolver(javax.xml.transform.URIResolver uRIResolver) {
   1.210 +        try {
   1.211 +            materialize();
   1.212 +            m_realTransformer.setURIResolver(uRIResolver);
   1.213 +        } catch (TransformerException e) {
   1.214 +            // will be caught later
   1.215 +        }
   1.216 +    }
   1.217 +
   1.218 +    private InputStream getInputStreamFromSource(StreamSource s)
   1.219 +        throws TransformerException {
   1.220 +
   1.221 +        InputStream stream = s.getInputStream();
   1.222 +        if (stream != null)
   1.223 +            return stream;
   1.224 +
   1.225 +        if (s.getReader() != null)
   1.226 +            return null;
   1.227 +
   1.228 +        String systemId = s.getSystemId();
   1.229 +        if (systemId != null) {
   1.230 +            try {
   1.231 +                String fileURL = systemId;
   1.232 +
   1.233 +                if (systemId.startsWith("file:///"))
   1.234 +                {
   1.235 +                    /*
   1.236 +                     systemId is:
   1.237 +                     file:///<drive>:/some/path/file.xml
   1.238 +                     or
   1.239 +                     file:///some/path/file.xml
   1.240 +                    */
   1.241 +
   1.242 +                    String absolutePath = systemId.substring(7);
   1.243 +                    /*
   1.244 +                     /<drive>:/some/path/file.xml
   1.245 +                     or
   1.246 +                     /some/path/file.xml
   1.247 +                    */
   1.248 +
   1.249 +                    boolean hasDriveDesignator = absolutePath.indexOf(":") > 0;
   1.250 +                    if (hasDriveDesignator) {
   1.251 +                      String driveDesignatedPath = absolutePath.substring(1);
   1.252 +                      /*
   1.253 +                      <drive>:/some/path/file.xml */
   1.254 +                      fileURL = driveDesignatedPath;
   1.255 +                    }
   1.256 +                    else {
   1.257 +                      /*
   1.258 +                      /some/path/file.xml */
   1.259 +                      fileURL = absolutePath;
   1.260 +                    }
   1.261 +                }
   1.262 +                //return new FileInputStream(fileURL);
   1.263 +                try {
   1.264 +                    return new FileInputStream(new File(new URI(fileURL)));
   1.265 +                } catch (URISyntaxException ex) {
   1.266 +                    throw new TransformerException(ex);
   1.267 +                }
   1.268 +            } catch (IOException e) {
   1.269 +                throw new TransformerException(e.toString());
   1.270 +            }
   1.271 +        }
   1.272 +
   1.273 +        throw new TransformerException("Unexpected StreamSource object");
   1.274 +    }
   1.275 +
   1.276 +    //------------------------------------------------------------------------
   1.277 +
   1.278 +    public void transform(
   1.279 +        javax.xml.transform.Source source,
   1.280 +        javax.xml.transform.Result result)
   1.281 +        throws javax.xml.transform.TransformerException
   1.282 +    {
   1.283 +        // StreamSource -> StreamResult
   1.284 +        if ((source instanceof StreamSource)
   1.285 +            && (result instanceof StreamResult)) {
   1.286 +            try {
   1.287 +                StreamSource streamSource = (StreamSource) source;
   1.288 +                InputStream is = getInputStreamFromSource(streamSource);
   1.289 +
   1.290 +                OutputStream os = ((StreamResult) result).getOutputStream();
   1.291 +                if (os == null)
   1.292 +                    // TODO: We might want to fix this if it were to be used beyond
   1.293 +                    // XmlDataContentHandler that we know uses only OutputStream
   1.294 +                    throw new TransformerException("Unexpected StreamResult object contains null OutputStream");
   1.295 +
   1.296 +                if (is != null) {
   1.297 +                    if (is.markSupported())
   1.298 +                        is.mark(Integer.MAX_VALUE);
   1.299 +                    int num;
   1.300 +                    byte[] b = new byte[8192];
   1.301 +                    while ((num = is.read(b)) != -1) {
   1.302 +                        os.write(b, 0, num);
   1.303 +                    }
   1.304 +                    if (is.markSupported())
   1.305 +                        is.reset();
   1.306 +                    return;
   1.307 +                }
   1.308 +
   1.309 +                Reader reader = streamSource.getReader();
   1.310 +                if (reader != null) {
   1.311 +
   1.312 +                    if (reader.markSupported())
   1.313 +                        reader.mark(Integer.MAX_VALUE);
   1.314 +
   1.315 +                    PushbackReader pushbackReader = new PushbackReader(reader, 4096);
   1.316 +                    //some size to unread <?xml ....?>
   1.317 +                    XMLDeclarationParser ev =
   1.318 +                        new XMLDeclarationParser(pushbackReader);
   1.319 +                    try {
   1.320 +                        ev.parse();
   1.321 +                    } catch (Exception ex) {
   1.322 +                        throw new TransformerException(
   1.323 +                            "Unable to run the JAXP transformer on a stream "
   1.324 +                                + ex.getMessage());
   1.325 +                    }
   1.326 +                    Writer writer =
   1.327 +                        new OutputStreamWriter(os /*, ev.getEncoding()*/);
   1.328 +                    ev.writeTo(writer);         // doesn't write any, if no header
   1.329 +
   1.330 +                    int num;
   1.331 +                    char[] ac = new char[8192];
   1.332 +                    while ((num = pushbackReader.read(ac)) != -1) {
   1.333 +                        writer.write(ac, 0, num);
   1.334 +                    }
   1.335 +                    writer.flush();
   1.336 +
   1.337 +                    if (reader.markSupported())
   1.338 +                        reader.reset();
   1.339 +                    return;
   1.340 +                }
   1.341 +            } catch (IOException e) {
   1.342 +                e.printStackTrace();
   1.343 +                throw new TransformerException(e.toString());
   1.344 +            }
   1.345 +
   1.346 +            throw new TransformerException("Unexpected StreamSource object");
   1.347 +        }
   1.348 +        // FastInfosetSource -> DOMResult
   1.349 +        else if (FastInfosetReflection.isFastInfosetSource(source)
   1.350 +                && (result instanceof DOMResult))
   1.351 +        {
   1.352 +            try {
   1.353 +                // Use reflection to avoid a static dep with FI
   1.354 +                if (m_fiDOMDocumentParser == null) {
   1.355 +                    m_fiDOMDocumentParser = FastInfosetReflection.DOMDocumentParser_new();
   1.356 +                }
   1.357 +
   1.358 +                // m_fiDOMDocumentParser.parse(document, source.getInputStream())
   1.359 +                FastInfosetReflection.DOMDocumentParser_parse(
   1.360 +                    m_fiDOMDocumentParser,
   1.361 +                    (Document) ((DOMResult) result).getNode(),
   1.362 +                    FastInfosetReflection.FastInfosetSource_getInputStream(source));
   1.363 +
   1.364 +                // We're done!
   1.365 +                return;
   1.366 +            }
   1.367 +            catch (Exception e) {
   1.368 +                throw new TransformerException(e);
   1.369 +            }
   1.370 +        }
   1.371 +        // DOMSource -> FastInfosetResult
   1.372 +        else if ((source instanceof DOMSource)
   1.373 +                && FastInfosetReflection.isFastInfosetResult(result))
   1.374 +        {
   1.375 +            try {
   1.376 +                // Use reflection to avoid a static dep with FI
   1.377 +                if (m_fiDOMDocumentSerializer == null) {
   1.378 +                    m_fiDOMDocumentSerializer = FastInfosetReflection.DOMDocumentSerializer_new();
   1.379 +                }
   1.380 +
   1.381 +                // m_fiDOMDocumentSerializer.setOutputStream(result.getOutputStream())
   1.382 +                FastInfosetReflection.DOMDocumentSerializer_setOutputStream(
   1.383 +                    m_fiDOMDocumentSerializer,
   1.384 +                    FastInfosetReflection.FastInfosetResult_getOutputStream(result));
   1.385 +
   1.386 +                // m_fiDOMDocumentSerializer.serialize(node)
   1.387 +                FastInfosetReflection.DOMDocumentSerializer_serialize(
   1.388 +                    m_fiDOMDocumentSerializer,
   1.389 +                    ((DOMSource) source).getNode());
   1.390 +
   1.391 +                // We're done!
   1.392 +                return;
   1.393 +            }
   1.394 +            catch (Exception e) {
   1.395 +                throw new TransformerException(e);
   1.396 +            }
   1.397 +        }
   1.398 +
   1.399 +        // All other cases -- use transformer object
   1.400 +
   1.401 +        materialize();
   1.402 +        m_realTransformer.transform(source, result);
   1.403 +    }
   1.404 +
   1.405 +    /**
   1.406 +     * Threadlocal to hold a Transformer instance for this thread.
   1.407 +     * CR : 6813167
   1.408 +     */
   1.409 +    //private static ThreadLocal effTransformer = new ThreadLocal();
   1.410 +
   1.411 +    /**
   1.412 +     * Return Transformer instance for this thread, allocating a new one if
   1.413 +     * necessary. Note that this method does not clear global parameters,
   1.414 +     * properties or any other data set on a previously used transformer.
   1.415 +     */
   1.416 +    public static Transformer newTransformer() {
   1.417 +        //CR : 6813167
   1.418 +        /*Transformer tt = (Transformer) effTransformer.get();
   1.419 +        if (tt == null) {
   1.420 +            effTransformer.set(tt = new EfficientStreamingTransformer());
   1.421 +        }
   1.422 +        return tt;*/
   1.423 +        return new EfficientStreamingTransformer();
   1.424 +    }
   1.425 +
   1.426 +}

mercurial