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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 /*
ohair@286 27 * EfficientStreamingTransformer.java
ohair@286 28 *
ohair@286 29 * Created on July 29, 2002, 3:49 PM
ohair@286 30 */
ohair@286 31
ohair@286 32 package com.sun.xml.internal.messaging.saaj.util.transform;
ohair@286 33
ohair@286 34 import java.io.*;
ohair@286 35
ohair@286 36 import java.net.URISyntaxException;
ohair@286 37 import javax.xml.transform.dom.DOMSource;
ohair@286 38 import javax.xml.transform.dom.DOMResult;
ohair@286 39 import javax.xml.transform.stream.StreamResult;
ohair@286 40 import javax.xml.transform.stream.StreamSource;
ohair@286 41
ohair@286 42 import org.w3c.dom.Document;
ohair@286 43
ohair@286 44 import com.sun.xml.internal.messaging.saaj.util.XMLDeclarationParser;
ohair@286 45 import com.sun.xml.internal.messaging.saaj.util.FastInfosetReflection;
ohair@286 46 import java.net.URI;
ohair@286 47 import javax.xml.transform.Transformer;
ohair@286 48 import javax.xml.transform.TransformerException;
ohair@286 49 import javax.xml.transform.TransformerFactory;
ohair@286 50
ohair@286 51 /**
ohair@286 52 * This class is a proxy for a Transformer object with optimizations
ohair@286 53 * for certain cases. If source and result are of type stream, then
ohair@286 54 * bytes are simply copied whenever possible (note that this assumes
ohair@286 55 * that the input is well formed). In addition, it provides support for
ohair@286 56 * FI using native DOM parsers and serializers.
ohair@286 57 *
ohair@286 58 * @author Panos Kougiouris panos@acm.org
ohair@286 59 * @author Santiago.PericasGeertsen@sun.com
ohair@286 60 *
ohair@286 61 */
ohair@286 62 public class EfficientStreamingTransformer
ohair@286 63 extends javax.xml.transform.Transformer {
ohair@286 64
ohair@286 65 //static final String version;
ohair@286 66 //static final String vendor;
ohair@286 67 // removing static : security issue : CR 6813167Z
ohair@286 68 private final TransformerFactory transformerFactory = TransformerFactory.newInstance();
ohair@286 69
ohair@286 70 /**
ohair@286 71 removing support for Java 1.4 and 1.3 : CR6658158
ohair@286 72 static {
ohair@286 73 version = System.getProperty("java.vm.version");
ohair@286 74 vendor = System.getProperty("java.vm.vendor");
ohair@286 75 if (vendor.startsWith("Sun") &&
ohair@286 76 (version.startsWith("1.4") || version.startsWith("1.3"))) {
ohair@286 77 transformerFactory =
ohair@286 78 new com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl();
ohair@286 79 }
ohair@286 80 }*/
ohair@286 81
ohair@286 82 /**
ohair@286 83 * TransformerFactory instance.
ohair@286 84 */
ohair@286 85
ohair@286 86 /**
ohair@286 87 * Underlying XSLT transformer.
ohair@286 88 */
ohair@286 89 private Transformer m_realTransformer = null;
ohair@286 90
ohair@286 91 /**
ohair@286 92 * Undelying FI DOM parser.
ohair@286 93 */
ohair@286 94 private Object m_fiDOMDocumentParser = null;
ohair@286 95
ohair@286 96 /**
ohair@286 97 * Underlying FI DOM serializer.
ohair@286 98 */
ohair@286 99 private Object m_fiDOMDocumentSerializer = null;
ohair@286 100
ohair@286 101 private EfficientStreamingTransformer() {
ohair@286 102 }
ohair@286 103
ohair@286 104 private void materialize() throws TransformerException {
ohair@286 105 if (m_realTransformer == null) {
ohair@286 106 m_realTransformer = transformerFactory.newTransformer();
ohair@286 107 }
ohair@286 108 }
ohair@286 109
ohair@286 110 public void clearParameters() {
ohair@286 111 if (m_realTransformer != null)
ohair@286 112 m_realTransformer.clearParameters();
ohair@286 113 }
ohair@286 114
ohair@286 115 public javax.xml.transform.ErrorListener getErrorListener() {
ohair@286 116 try {
ohair@286 117 materialize();
ohair@286 118 return m_realTransformer.getErrorListener();
ohair@286 119 } catch (TransformerException e) {
ohair@286 120 // will be caught later
ohair@286 121 }
ohair@286 122 return null;
ohair@286 123 }
ohair@286 124
ohair@286 125 public java.util.Properties getOutputProperties() {
ohair@286 126 try {
ohair@286 127 materialize();
ohair@286 128 return m_realTransformer.getOutputProperties();
ohair@286 129 } catch (TransformerException e) {
ohair@286 130 // will be caught later
ohair@286 131 }
ohair@286 132 return null;
ohair@286 133 }
ohair@286 134
ohair@286 135 public String getOutputProperty(String str)
ohair@286 136 throws java.lang.IllegalArgumentException {
ohair@286 137 try {
ohair@286 138 materialize();
ohair@286 139 return m_realTransformer.getOutputProperty(str);
ohair@286 140 } catch (TransformerException e) {
ohair@286 141 // will be caught later
ohair@286 142 }
ohair@286 143 return null;
ohair@286 144 }
ohair@286 145
ohair@286 146 public Object getParameter(String str) {
ohair@286 147 try {
ohair@286 148 materialize();
ohair@286 149 return m_realTransformer.getParameter(str);
ohair@286 150 } catch (TransformerException e) {
ohair@286 151 // will be caught later
ohair@286 152 }
ohair@286 153 return null;
ohair@286 154 }
ohair@286 155
ohair@286 156 public javax.xml.transform.URIResolver getURIResolver() {
ohair@286 157 try {
ohair@286 158 materialize();
ohair@286 159 return m_realTransformer.getURIResolver();
ohair@286 160 } catch (TransformerException e) {
ohair@286 161 // will be caught later
ohair@286 162 }
ohair@286 163 return null;
ohair@286 164 }
ohair@286 165
ohair@286 166 public void setErrorListener(
ohair@286 167 javax.xml.transform.ErrorListener errorListener)
ohair@286 168 throws java.lang.IllegalArgumentException {
ohair@286 169 try {
ohair@286 170 materialize();
ohair@286 171 m_realTransformer.setErrorListener(errorListener);
ohair@286 172 } catch (TransformerException e) {
ohair@286 173 // will be caught later
ohair@286 174 }
ohair@286 175 }
ohair@286 176
ohair@286 177 public void setOutputProperties(java.util.Properties properties)
ohair@286 178 throws java.lang.IllegalArgumentException {
ohair@286 179 try {
ohair@286 180 materialize();
ohair@286 181 m_realTransformer.setOutputProperties(properties);
ohair@286 182 } catch (TransformerException e) {
ohair@286 183 // will be caught later
ohair@286 184 }
ohair@286 185 }
ohair@286 186
ohair@286 187 public void setOutputProperty(String str, String str1)
ohair@286 188 throws java.lang.IllegalArgumentException {
ohair@286 189 try {
ohair@286 190 materialize();
ohair@286 191 m_realTransformer.setOutputProperty(str, str1);
ohair@286 192 } catch (TransformerException e) {
ohair@286 193 // will be caught later
ohair@286 194 }
ohair@286 195 }
ohair@286 196
ohair@286 197 public void setParameter(String str, Object obj) {
ohair@286 198 try {
ohair@286 199 materialize();
ohair@286 200 m_realTransformer.setParameter(str, obj);
ohair@286 201 } catch (TransformerException e) {
ohair@286 202 // will be caught later
ohair@286 203 }
ohair@286 204 }
ohair@286 205
ohair@286 206 public void setURIResolver(javax.xml.transform.URIResolver uRIResolver) {
ohair@286 207 try {
ohair@286 208 materialize();
ohair@286 209 m_realTransformer.setURIResolver(uRIResolver);
ohair@286 210 } catch (TransformerException e) {
ohair@286 211 // will be caught later
ohair@286 212 }
ohair@286 213 }
ohair@286 214
ohair@286 215 private InputStream getInputStreamFromSource(StreamSource s)
ohair@286 216 throws TransformerException {
ohair@286 217
ohair@286 218 InputStream stream = s.getInputStream();
ohair@286 219 if (stream != null)
ohair@286 220 return stream;
ohair@286 221
ohair@286 222 if (s.getReader() != null)
ohair@286 223 return null;
ohair@286 224
ohair@286 225 String systemId = s.getSystemId();
ohair@286 226 if (systemId != null) {
ohair@286 227 try {
ohair@286 228 String fileURL = systemId;
ohair@286 229
ohair@286 230 if (systemId.startsWith("file:///"))
ohair@286 231 {
ohair@286 232 /*
ohair@286 233 systemId is:
ohair@286 234 file:///<drive>:/some/path/file.xml
ohair@286 235 or
ohair@286 236 file:///some/path/file.xml
ohair@286 237 */
ohair@286 238
ohair@286 239 String absolutePath = systemId.substring(7);
ohair@286 240 /*
ohair@286 241 /<drive>:/some/path/file.xml
ohair@286 242 or
ohair@286 243 /some/path/file.xml
ohair@286 244 */
ohair@286 245
ohair@286 246 boolean hasDriveDesignator = absolutePath.indexOf(":") > 0;
ohair@286 247 if (hasDriveDesignator) {
ohair@286 248 String driveDesignatedPath = absolutePath.substring(1);
ohair@286 249 /*
ohair@286 250 <drive>:/some/path/file.xml */
ohair@286 251 fileURL = driveDesignatedPath;
ohair@286 252 }
ohair@286 253 else {
ohair@286 254 /*
ohair@286 255 /some/path/file.xml */
ohair@286 256 fileURL = absolutePath;
ohair@286 257 }
ohair@286 258 }
ohair@286 259 //return new FileInputStream(fileURL);
ohair@286 260 try {
ohair@286 261 return new FileInputStream(new File(new URI(fileURL)));
ohair@286 262 } catch (URISyntaxException ex) {
ohair@286 263 throw new TransformerException(ex);
ohair@286 264 }
ohair@286 265 } catch (IOException e) {
ohair@286 266 throw new TransformerException(e.toString());
ohair@286 267 }
ohair@286 268 }
ohair@286 269
ohair@286 270 throw new TransformerException("Unexpected StreamSource object");
ohair@286 271 }
ohair@286 272
ohair@286 273 //------------------------------------------------------------------------
ohair@286 274
ohair@286 275 public void transform(
ohair@286 276 javax.xml.transform.Source source,
ohair@286 277 javax.xml.transform.Result result)
ohair@286 278 throws javax.xml.transform.TransformerException
ohair@286 279 {
ohair@286 280 // StreamSource -> StreamResult
ohair@286 281 if ((source instanceof StreamSource)
ohair@286 282 && (result instanceof StreamResult)) {
ohair@286 283 try {
ohair@286 284 StreamSource streamSource = (StreamSource) source;
ohair@286 285 InputStream is = getInputStreamFromSource(streamSource);
ohair@286 286
ohair@286 287 OutputStream os = ((StreamResult) result).getOutputStream();
ohair@286 288 if (os == null)
ohair@286 289 // TODO: We might want to fix this if it were to be used beyond
ohair@286 290 // XmlDataContentHandler that we know uses only OutputStream
ohair@286 291 throw new TransformerException("Unexpected StreamResult object contains null OutputStream");
ohair@286 292
ohair@286 293 if (is != null) {
ohair@286 294 if (is.markSupported())
ohair@286 295 is.mark(Integer.MAX_VALUE);
ohair@286 296 int num;
ohair@286 297 byte[] b = new byte[8192];
ohair@286 298 while ((num = is.read(b)) != -1) {
ohair@286 299 os.write(b, 0, num);
ohair@286 300 }
ohair@286 301 if (is.markSupported())
ohair@286 302 is.reset();
ohair@286 303 return;
ohair@286 304 }
ohair@286 305
ohair@286 306 Reader reader = streamSource.getReader();
ohair@286 307 if (reader != null) {
ohair@286 308
ohair@286 309 if (reader.markSupported())
ohair@286 310 reader.mark(Integer.MAX_VALUE);
ohair@286 311
ohair@286 312 PushbackReader pushbackReader = new PushbackReader(reader, 4096);
ohair@286 313 //some size to unread <?xml ....?>
ohair@286 314 XMLDeclarationParser ev =
ohair@286 315 new XMLDeclarationParser(pushbackReader);
ohair@286 316 try {
ohair@286 317 ev.parse();
ohair@286 318 } catch (Exception ex) {
ohair@286 319 throw new TransformerException(
ohair@286 320 "Unable to run the JAXP transformer on a stream "
ohair@286 321 + ex.getMessage());
ohair@286 322 }
ohair@286 323 Writer writer =
ohair@286 324 new OutputStreamWriter(os /*, ev.getEncoding()*/);
ohair@286 325 ev.writeTo(writer); // doesn't write any, if no header
ohair@286 326
ohair@286 327 int num;
ohair@286 328 char[] ac = new char[8192];
ohair@286 329 while ((num = pushbackReader.read(ac)) != -1) {
ohair@286 330 writer.write(ac, 0, num);
ohair@286 331 }
ohair@286 332 writer.flush();
ohair@286 333
ohair@286 334 if (reader.markSupported())
ohair@286 335 reader.reset();
ohair@286 336 return;
ohair@286 337 }
ohair@286 338 } catch (IOException e) {
ohair@286 339 e.printStackTrace();
ohair@286 340 throw new TransformerException(e.toString());
ohair@286 341 }
ohair@286 342
ohair@286 343 throw new TransformerException("Unexpected StreamSource object");
ohair@286 344 }
ohair@286 345 // FastInfosetSource -> DOMResult
ohair@286 346 else if (FastInfosetReflection.isFastInfosetSource(source)
ohair@286 347 && (result instanceof DOMResult))
ohair@286 348 {
ohair@286 349 try {
ohair@286 350 // Use reflection to avoid a static dep with FI
ohair@286 351 if (m_fiDOMDocumentParser == null) {
ohair@286 352 m_fiDOMDocumentParser = FastInfosetReflection.DOMDocumentParser_new();
ohair@286 353 }
ohair@286 354
ohair@286 355 // m_fiDOMDocumentParser.parse(document, source.getInputStream())
ohair@286 356 FastInfosetReflection.DOMDocumentParser_parse(
ohair@286 357 m_fiDOMDocumentParser,
ohair@286 358 (Document) ((DOMResult) result).getNode(),
ohair@286 359 FastInfosetReflection.FastInfosetSource_getInputStream(source));
ohair@286 360
ohair@286 361 // We're done!
ohair@286 362 return;
ohair@286 363 }
ohair@286 364 catch (Exception e) {
ohair@286 365 throw new TransformerException(e);
ohair@286 366 }
ohair@286 367 }
ohair@286 368 // DOMSource -> FastInfosetResult
ohair@286 369 else if ((source instanceof DOMSource)
ohair@286 370 && FastInfosetReflection.isFastInfosetResult(result))
ohair@286 371 {
ohair@286 372 try {
ohair@286 373 // Use reflection to avoid a static dep with FI
ohair@286 374 if (m_fiDOMDocumentSerializer == null) {
ohair@286 375 m_fiDOMDocumentSerializer = FastInfosetReflection.DOMDocumentSerializer_new();
ohair@286 376 }
ohair@286 377
ohair@286 378 // m_fiDOMDocumentSerializer.setOutputStream(result.getOutputStream())
ohair@286 379 FastInfosetReflection.DOMDocumentSerializer_setOutputStream(
ohair@286 380 m_fiDOMDocumentSerializer,
ohair@286 381 FastInfosetReflection.FastInfosetResult_getOutputStream(result));
ohair@286 382
ohair@286 383 // m_fiDOMDocumentSerializer.serialize(node)
ohair@286 384 FastInfosetReflection.DOMDocumentSerializer_serialize(
ohair@286 385 m_fiDOMDocumentSerializer,
ohair@286 386 ((DOMSource) source).getNode());
ohair@286 387
ohair@286 388 // We're done!
ohair@286 389 return;
ohair@286 390 }
ohair@286 391 catch (Exception e) {
ohair@286 392 throw new TransformerException(e);
ohair@286 393 }
ohair@286 394 }
ohair@286 395
ohair@286 396 // All other cases -- use transformer object
ohair@286 397
ohair@286 398 materialize();
ohair@286 399 m_realTransformer.transform(source, result);
ohair@286 400 }
ohair@286 401
ohair@286 402 /**
ohair@286 403 * Threadlocal to hold a Transformer instance for this thread.
ohair@286 404 * CR : 6813167
ohair@286 405 */
ohair@286 406 //private static ThreadLocal effTransformer = new ThreadLocal();
ohair@286 407
ohair@286 408 /**
ohair@286 409 * Return Transformer instance for this thread, allocating a new one if
ohair@286 410 * necessary. Note that this method does not clear global parameters,
ohair@286 411 * properties or any other data set on a previously used transformer.
ohair@286 412 */
ohair@286 413 public static Transformer newTransformer() {
ohair@286 414 //CR : 6813167
ohair@286 415 /*Transformer tt = (Transformer) effTransformer.get();
ohair@286 416 if (tt == null) {
ohair@286 417 effTransformer.set(tt = new EfficientStreamingTransformer());
ohair@286 418 }
ohair@286 419 return tt;*/
ohair@286 420 return new EfficientStreamingTransformer();
ohair@286 421 }
ohair@286 422
ohair@286 423 }

mercurial