325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.encoding;
325N/A
325N/Aimport com.sun.xml.internal.ws.util.xml.XmlUtil;
325N/A
325N/Aimport javax.activation.ActivationDataFlavor;
325N/Aimport javax.activation.DataContentHandler;
325N/Aimport javax.activation.DataSource;
325N/Aimport javax.xml.transform.OutputKeys;
325N/Aimport javax.xml.transform.Source;
325N/Aimport javax.xml.transform.Transformer;
325N/Aimport javax.xml.transform.stream.StreamResult;
325N/Aimport javax.xml.transform.stream.StreamSource;
325N/Aimport java.awt.datatransfer.DataFlavor;
325N/Aimport java.io.IOException;
325N/Aimport java.io.InputStreamReader;
325N/Aimport java.io.OutputStream;
325N/Aimport java.io.OutputStreamWriter;
325N/A
325N/A/**
325N/A * JAF data handler for XML content
325N/A *
325N/A * @author Jitendra Kotamraju
325N/A */
325N/Apublic class XmlDataContentHandler implements DataContentHandler {
325N/A
325N/A private final DataFlavor[] flavors;
325N/A
325N/A public XmlDataContentHandler() throws ClassNotFoundException {
325N/A flavors = new DataFlavor[3];
325N/A flavors[0] = new ActivationDataFlavor(StreamSource.class, "text/xml", "XML");
325N/A flavors[1] = new ActivationDataFlavor(StreamSource.class, "application/xml", "XML");
325N/A flavors[2] = new ActivationDataFlavor(String.class, "text/xml", "XML String");
325N/A }
325N/A
325N/A public DataFlavor[] getTransferDataFlavors() {
325N/A return flavors;
325N/A }
325N/A
325N/A public Object getTransferData(DataFlavor df, DataSource ds)
325N/A throws IOException {
325N/A
325N/A for (DataFlavor aFlavor : flavors) {
325N/A if (aFlavor.equals(df)) {
325N/A return getContent(ds);
325N/A }
325N/A }
325N/A return null;
325N/A }
325N/A
325N/A /**
325N/A * Create an object from the input stream
325N/A */
325N/A public Object getContent(DataSource ds) throws IOException {
325N/A String ctStr = ds.getContentType();
325N/A String charset = null;
325N/A if (ctStr != null) {
325N/A ContentType ct = new ContentType(ctStr);
325N/A if (!isXml(ct)) {
325N/A throw new IOException(
325N/A "Cannot convert DataSource with content type \""
325N/A + ctStr + "\" to object in XmlDataContentHandler");
325N/A }
325N/A charset = ct.getParameter("charset");
325N/A }
325N/A return (charset != null)
325N/A ? new StreamSource(new InputStreamReader(ds.getInputStream()), charset)
325N/A : new StreamSource(ds.getInputStream());
325N/A }
325N/A
325N/A /**
325N/A * Convert the object to a byte stream
325N/A */
325N/A public void writeTo(Object obj, String mimeType, OutputStream os)
325N/A throws IOException {
325N/A
325N/A if (!(obj instanceof DataSource || obj instanceof Source || obj instanceof String)) {
325N/A throw new IOException("Invalid Object type = "+obj.getClass()+
325N/A ". XmlDataContentHandler can only convert DataSource|Source|String to XML.");
325N/A }
325N/A
325N/A ContentType ct = new ContentType(mimeType);
325N/A if (!isXml(ct)) {
325N/A throw new IOException(
325N/A "Invalid content type \"" + mimeType + "\" for XmlDataContentHandler");
325N/A }
325N/A
325N/A String charset = ct.getParameter("charset");
325N/A if (obj instanceof String) {
325N/A String s = (String) obj;
325N/A if (charset == null) {
325N/A charset = "utf-8";
325N/A }
325N/A OutputStreamWriter osw = new OutputStreamWriter(os, charset);
325N/A osw.write(s, 0, s.length());
325N/A osw.flush();
325N/A return;
325N/A }
325N/A
325N/A Source source = (obj instanceof DataSource)
325N/A ? (Source)getContent((DataSource)obj) : (Source)obj;
325N/A try {
325N/A Transformer transformer = XmlUtil.newTransformer();
325N/A if (charset != null) {
325N/A transformer.setOutputProperty(OutputKeys.ENCODING, charset);
325N/A }
325N/A StreamResult result = new StreamResult(os);
325N/A transformer.transform(source, result);
325N/A } catch (Exception ex) {
325N/A throw new IOException(
325N/A "Unable to run the JAXP transformer in XmlDataContentHandler "
325N/A + ex.getMessage());
325N/A }
325N/A }
325N/A
325N/A private boolean isXml(ContentType ct) {
325N/A return ct.getSubType().equals("xml") &&
325N/A (ct.getPrimaryType().equals("text") || ct.getPrimaryType().equals("application"));
325N/A }
325N/A
325N/A}