001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: XmlDataContentHandler.java,v 1.2 2007/07/16 16:41:22 ofung Exp $
022: * $Revision: 1.2 $
023: * $Date: 2007/07/16 16:41:22 $
024: */
025:
026: /*
027: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
028: *
029: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
030: *
031: * The contents of this file are subject to the terms of either the GNU
032: * General Public License Version 2 only ("GPL") or the Common Development
033: * and Distribution License("CDDL") (collectively, the "License"). You
034: * may not use this file except in compliance with the License. You can obtain
035: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
036: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
037: * language governing permissions and limitations under the License.
038: *
039: * When distributing the software, include this License Header Notice in each
040: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
041: * Sun designates this particular file as subject to the "Classpath" exception
042: * as provided by Sun in the GPL Version 2 section of the License file that
043: * accompanied this code. If applicable, add the following below the License
044: * Header, with the fields enclosed by brackets [] replaced by your own
045: * identifying information: "Portions Copyrighted [year]
046: * [name of copyright owner]"
047: *
048: * Contributor(s):
049: *
050: * If you wish your version of this file to be governed by only the CDDL or
051: * only the GPL Version 2, indicate your decision by adding "[Contributor]
052: * elects to include this software in this distribution under the [CDDL or GPL
053: * Version 2] license." If you don't indicate a single choice of license, a
054: * recipient has the option to distribute your version of this file under
055: * either the CDDL, the GPL Version 2 or to extend the choice of license to
056: * its licensees as provided above. However, if you add GPL Version 2 code
057: * and therefore, elected the GPL Version 2 license, then the option applies
058: * only if the new code is made subject to such option by the copyright
059: * holder.
060: */
061: package com.sun.xml.messaging.saaj.soap;
062:
063: import java.awt.datatransfer.DataFlavor;
064: import java.io.IOException;
065: import java.io.OutputStream;
066:
067: import javax.activation.*;
068: import javax.xml.transform.Source;
069: import javax.xml.transform.Transformer;
070: import javax.xml.transform.stream.StreamResult;
071: import javax.xml.transform.stream.StreamSource;
072:
073: import com.sun.xml.messaging.saaj.util.transform.EfficientStreamingTransformer;
074:
075: /**
076: * JAF data handler for XML content
077: *
078: * @author Anil Vijendran
079: */
080: public class XmlDataContentHandler implements DataContentHandler {
081: public final String STR_SRC = "javax.xml.transform.stream.StreamSource";
082: private static Class streamSourceClass = null;
083:
084: public XmlDataContentHandler() throws ClassNotFoundException {
085: if (streamSourceClass == null) {
086: streamSourceClass = Class.forName(STR_SRC);
087: }
088: }
089:
090: /**
091: * return the DataFlavors for this <code>DataContentHandler</code>
092: * @return The DataFlavors.
093: */
094: public DataFlavor[] getTransferDataFlavors() { // throws Exception;
095: DataFlavor flavors[] = new DataFlavor[2];
096:
097: flavors[0] = new ActivationDataFlavor(streamSourceClass,
098: "text/xml", "XML");
099: flavors[1] = new ActivationDataFlavor(streamSourceClass,
100: "application/xml", "XML");
101:
102: return flavors;
103: }
104:
105: /**
106: * return the Transfer Data of type DataFlavor from InputStream
107: * @param df The DataFlavor.
108: * @param ins The InputStream corresponding to the data.
109: * @return The constructed Object.
110: */
111: public Object getTransferData(DataFlavor flavor,
112: DataSource dataSource) throws IOException {
113: if (flavor.getMimeType().startsWith("text/xml")
114: || flavor.getMimeType().startsWith("application/xml")) {
115: if (flavor.getRepresentationClass().getName().equals(
116: STR_SRC)) {
117: return new StreamSource(dataSource.getInputStream());
118: }
119: }
120: return null;
121: }
122:
123: /**
124: *
125: */
126: public Object getContent(DataSource dataSource) throws IOException {
127: return new StreamSource(dataSource.getInputStream());
128: }
129:
130: /**
131: * construct an object from a byte stream
132: * (similar semantically to previous method, we are deciding
133: * which one to support)
134: */
135: public void writeTo(Object obj, String mimeType, OutputStream os)
136: throws IOException {
137: if (!mimeType.equals("text/xml")
138: && !mimeType.equals("application/xml"))
139: throw new IOException("Invalid content type \"" + mimeType
140: + "\" for XmlDCH");
141:
142: try {
143: Transformer transformer = EfficientStreamingTransformer
144: .newTransformer();
145: StreamResult result = new StreamResult(os);
146: if (obj instanceof DataSource) {
147: // Streaming transform applies only to javax.xml.transform.StreamSource
148: transformer.transform(
149: (Source) getContent((DataSource) obj), result);
150: } else {
151: transformer.transform((Source) obj, result);
152: }
153: } catch (Exception ex) {
154: throw new IOException(
155: "Unable to run the JAXP transformer on a stream "
156: + ex.getMessage());
157: }
158: }
159: }
|