Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
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.ws.encoding;
28 import com.sun.xml.internal.ws.util.xml.XmlUtil;
30 import javax.activation.ActivationDataFlavor;
31 import javax.activation.DataContentHandler;
32 import javax.activation.DataSource;
33 import javax.xml.transform.OutputKeys;
34 import javax.xml.transform.Source;
35 import javax.xml.transform.Transformer;
36 import javax.xml.transform.stream.StreamResult;
37 import javax.xml.transform.stream.StreamSource;
38 import java.awt.datatransfer.DataFlavor;
39 import java.io.IOException;
40 import java.io.InputStreamReader;
41 import java.io.OutputStream;
42 import java.io.OutputStreamWriter;
43 import java.util.Arrays;
45 /**
46 * JAF data handler for XML content
47 *
48 * @author Jitendra Kotamraju
49 */
50 public class XmlDataContentHandler implements DataContentHandler {
52 private final DataFlavor[] flavors;
54 public XmlDataContentHandler() throws ClassNotFoundException {
55 flavors = new DataFlavor[3];
56 flavors[0] = new ActivationDataFlavor(StreamSource.class, "text/xml", "XML");
57 flavors[1] = new ActivationDataFlavor(StreamSource.class, "application/xml", "XML");
58 flavors[2] = new ActivationDataFlavor(String.class, "text/xml", "XML String");
59 }
61 public DataFlavor[] getTransferDataFlavors() {
62 return Arrays.copyOf(flavors, flavors.length);
63 }
65 public Object getTransferData(DataFlavor df, DataSource ds)
66 throws IOException {
68 for (DataFlavor aFlavor : flavors) {
69 if (aFlavor.equals(df)) {
70 return getContent(ds);
71 }
72 }
73 return null;
74 }
76 /**
77 * Create an object from the input stream
78 */
79 public Object getContent(DataSource ds) throws IOException {
80 String ctStr = ds.getContentType();
81 String charset = null;
82 if (ctStr != null) {
83 ContentType ct = new ContentType(ctStr);
84 if (!isXml(ct)) {
85 throw new IOException(
86 "Cannot convert DataSource with content type \""
87 + ctStr + "\" to object in XmlDataContentHandler");
88 }
89 charset = ct.getParameter("charset");
90 }
91 return (charset != null)
92 ? new StreamSource(new InputStreamReader(ds.getInputStream()), charset)
93 : new StreamSource(ds.getInputStream());
94 }
96 /**
97 * Convert the object to a byte stream
98 */
99 public void writeTo(Object obj, String mimeType, OutputStream os)
100 throws IOException {
102 if (!(obj instanceof DataSource || obj instanceof Source || obj instanceof String)) {
103 throw new IOException("Invalid Object type = "+obj.getClass()+
104 ". XmlDataContentHandler can only convert DataSource|Source|String to XML.");
105 }
107 ContentType ct = new ContentType(mimeType);
108 if (!isXml(ct)) {
109 throw new IOException(
110 "Invalid content type \"" + mimeType + "\" for XmlDataContentHandler");
111 }
113 String charset = ct.getParameter("charset");
114 if (obj instanceof String) {
115 String s = (String) obj;
116 if (charset == null) {
117 charset = "utf-8";
118 }
119 OutputStreamWriter osw = new OutputStreamWriter(os, charset);
120 osw.write(s, 0, s.length());
121 osw.flush();
122 return;
123 }
125 Source source = (obj instanceof DataSource)
126 ? (Source)getContent((DataSource)obj) : (Source)obj;
127 try {
128 Transformer transformer = XmlUtil.newTransformer();
129 if (charset != null) {
130 transformer.setOutputProperty(OutputKeys.ENCODING, charset);
131 }
132 StreamResult result = new StreamResult(os);
133 transformer.transform(source, result);
134 } catch (Exception ex) {
135 throw new IOException(
136 "Unable to run the JAXP transformer in XmlDataContentHandler "
137 + ex.getMessage());
138 }
139 }
141 private boolean isXml(ContentType ct) {
142 return ct.getSubType().equals("xml") &&
143 (ct.getPrimaryType().equals("text") || ct.getPrimaryType().equals("application"));
144 }
146 }