src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/XmlDataContentHandler.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 1997, 2012, 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  */
    26 package com.sun.xml.internal.messaging.saaj.soap;
    28 import java.awt.datatransfer.DataFlavor;
    29 import java.io.IOException;
    30 import java.io.OutputStream;
    32 import javax.activation.*;
    33 import javax.xml.transform.Source;
    34 import javax.xml.transform.Transformer;
    35 import javax.xml.transform.stream.StreamResult;
    36 import javax.xml.transform.stream.StreamSource;
    38 import com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer;
    40 /**
    41  * JAF data handler for XML content
    42  *
    43  * @author Anil Vijendran
    44  */
    45 public class XmlDataContentHandler implements DataContentHandler {
    46     public static final String STR_SRC = "javax.xml.transform.stream.StreamSource";
    47     private static Class streamSourceClass = null;
    49     public XmlDataContentHandler() throws ClassNotFoundException {
    50         if (streamSourceClass == null) {
    51             streamSourceClass = Class.forName(STR_SRC);
    52         }
    53     }
    55     /**
    56      * return the DataFlavors for this <code>DataContentHandler</code>
    57      * @return The DataFlavors.
    58      */
    59     public DataFlavor[] getTransferDataFlavors() { // throws Exception;
    60         DataFlavor flavors[] = new DataFlavor[2];
    62         flavors[0] =
    63             new ActivationDataFlavor(streamSourceClass, "text/xml", "XML");
    64         flavors[1] =
    65             new ActivationDataFlavor(streamSourceClass, "application/xml", "XML");
    67         return flavors;
    68     }
    70     /**
    71      * return the Transfer Data of type DataFlavor from InputStream
    72      * @param df The DataFlavor.
    73      * @param ins The InputStream corresponding to the data.
    74      * @return The constructed Object.
    75      */
    76     public Object getTransferData(DataFlavor flavor, DataSource dataSource)
    77         throws IOException {
    78         if (flavor.getMimeType().startsWith("text/xml") ||
    79                 flavor.getMimeType().startsWith("application/xml")) {
    80             if (flavor.getRepresentationClass().getName().equals(STR_SRC)) {
    81                 return new StreamSource(dataSource.getInputStream());
    82             }
    83         }
    84         return null;
    85     }
    87     /**
    88      *
    89      */
    90     public Object getContent(DataSource dataSource) throws IOException {
    91         return new StreamSource(dataSource.getInputStream());
    92     }
    94     /**
    95      * construct an object from a byte stream
    96      * (similar semantically to previous method, we are deciding
    97      *  which one to support)
    98      */
    99     public void writeTo(Object obj, String mimeType, OutputStream os)
   100         throws IOException {
   101         if (!mimeType.startsWith("text/xml") && !mimeType.startsWith("application/xml"))
   102             throw new IOException(
   103                 "Invalid content type \"" + mimeType + "\" for XmlDCH");
   106         try {
   107             Transformer transformer = EfficientStreamingTransformer.newTransformer();
   108             StreamResult result = new StreamResult(os);
   109             if (obj instanceof DataSource) {
   110                 // Streaming transform applies only to javax.xml.transform.StreamSource
   111                 transformer.transform((Source) getContent((DataSource)obj), result);
   112             } else {
   113                 Source src=null;
   114                 if (obj instanceof String) {
   115                      src= new StreamSource(new java.io.StringReader((String) obj));
   116                 } else {
   117                     src=(Source) obj;
   118                 }
   119                 transformer.transform(src, result);
   120             }
   121         } catch (Exception ex) {
   122             throw new IOException(
   123                 "Unable to run the JAXP transformer on a stream "
   124                     + ex.getMessage());
   125         }
   126     }
   127 }

mercurial