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: FactoryDataContentHandler.java,v 1.2 2007/07/16 16:41:28 ofung Exp $
022: * $Revision: 1.2 $
023: * $Date: 2007/07/16 16:41:28 $
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 mime.custom;
062:
063: import java.awt.datatransfer.DataFlavor;
064: import java.io.IOException;
065: import java.io.OutputStream;
066:
067: import javax.activation.*;
068:
069: /**
070: * DataContentHandler for Content-Type : custom/factory
071: *
072: * @author Jitendra Kotamraju (jitendra.kotamraju@sun.com)
073: */
074: public class FactoryDataContentHandler implements DataContentHandler {
075:
076: private static ActivationDataFlavor myDF = new ActivationDataFlavor(
077: mime.custom.CustomType.class, "custom/factory",
078: "Custom Type");
079:
080: protected ActivationDataFlavor getDF() {
081: return myDF;
082: }
083:
084: /**
085: * Return the DataFlavors for this <code>DataContentHandler</code>.
086: *
087: * @return The DataFlavors
088: */
089: public DataFlavor[] getTransferDataFlavors() { // throws Exception;
090: return new DataFlavor[] { getDF() };
091: }
092:
093: /**
094: * Return the Transfer Data of type DataFlavor from InputStream.
095: *
096: * @param df The DataFlavor
097: * @param ins The InputStream corresponding to the data
098: * @return String object
099: */
100: public Object getTransferData(DataFlavor df, DataSource ds)
101: throws IOException {
102: // use myDF.equals to be sure to get ActivationDataFlavor.equals,
103: // which properly ignores Content-Type parameters in comparison
104: if (getDF().equals(df))
105: return getContent(ds);
106: else
107: return null;
108: }
109:
110: public Object getContent(DataSource ds) throws IOException {
111: return new CustomType();
112: }
113:
114: /**
115: * Write the object to the output stream, using the specified MIME type.
116: */
117: public void writeTo(Object obj, String type, OutputStream os)
118: throws IOException {
119: if (!(obj instanceof CustomType)) {
120: throw new IOException(
121: getDF().getMimeType()
122: + " DataContentHandler requires CustomType object, "
123: + "instead got "
124: + obj.getClass().toString());
125: }
126: os.write((int) 'F');
127: os.flush();
128: }
129: }
|