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