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 */
25
26package javax.activation;
27
28import java.awt.datatransfer.DataFlavor;
29import java.io.IOException;
30import javax.activation.MimeType;
31
32/**
33 * The ActivationDataFlavor class is a special subclass of
34 * {@code java.awt.datatransfer.DataFlavor}. It allows the JAF to
35 * set all three values stored by the DataFlavor class via a new
36 * constructor. It also contains improved MIME parsing in the {@code equals}
37 * method. Except for the improved parsing, its semantics are
38 * identical to that of the JDK's DataFlavor class.
39 *
40 * @since 1.6
41 */
42
43public class ActivationDataFlavor extends DataFlavor {
44
45    /*
46     * Raison d'etre:
47     *
48     * The DataFlavor class included in JDK 1.1 has several limitations
49     * including piss poor MIME type parsing, and the limitation of
50     * only supporting serialized objects and InputStreams as
51     * representation objects. This class 'fixes' that.
52     */
53
54    // I think for now I'll keep copies of all the variables and
55    // then later I may choose try to better coexist with the base
56    // class *sigh*
57    private String mimeType = null;
58    private MimeType mimeObject = null;
59    private String humanPresentableName = null;
60    private Class representationClass = null;
61
62    /**
63     * Construct a DataFlavor that represents an arbitrary
64     * Java object. This constructor is an extension of the
65     * JDK's DataFlavor in that it allows the explicit setting
66     * of all three DataFlavor attributes.
67     * <p>
68     * The returned DataFlavor will have the following characteristics:
69     * <p>
70     * representationClass = representationClass<br>
71     * mimeType            = mimeType<br>
72     * humanName           = humanName
73     *
74     * @param representationClass the class used in this DataFlavor
75     * @param mimeType the MIME type of the data represented by this class
76     * @param humanPresentableName the human presentable name of the flavor
77     */
78    public ActivationDataFlavor(Class representationClass,
79                      String mimeType, String humanPresentableName) {
80        super(mimeType, humanPresentableName); // need to call super
81
82        // init private variables:
83        this.mimeType = mimeType;
84        this.humanPresentableName = humanPresentableName;
85        this.representationClass = representationClass;
86    }
87
88    /**
89     * Construct a DataFlavor that represents a MimeType.
90     * <p>
91     * The returned DataFlavor will have the following characteristics:
92     * <p>
93     * If the mimeType is "application/x-java-serialized-object;
94     * class=", the result is the same as calling new
95     * DataFlavor(Class.forName()) as above.
96     * <p>
97     * otherwise:
98     * <p>
99     * representationClass = InputStream<p>
100     * mimeType = mimeType
101     *
102     * @param representationClass the class used in this DataFlavor
103     * @param humanPresentableName the human presentable name of the flavor
104     */
105    public ActivationDataFlavor(Class representationClass,
106                                String humanPresentableName) {
107        super(representationClass, humanPresentableName);
108        this.mimeType = super.getMimeType();
109        this.representationClass = representationClass;
110        this.humanPresentableName = humanPresentableName;
111    }
112
113    /**
114     * Construct a DataFlavor that represents a MimeType.
115     * <p>
116     * The returned DataFlavor will have the following characteristics:
117     * <p>
118     * If the mimeType is "application/x-java-serialized-object; class=",
119     * the result is the same as calling new DataFlavor(Class.forName()) as
120     * above, otherwise:
121     * <p>
122     * representationClass = InputStream<p>
123     * mimeType = mimeType
124     *
125     * @param mimeType the MIME type of the data represented by this class
126     * @param humanPresentableName the human presentable name of the flavor
127     */
128    public ActivationDataFlavor(String mimeType, String humanPresentableName) {
129        super(mimeType, humanPresentableName);
130        this.mimeType = mimeType;
131        try {
132            this.representationClass = Class.forName("java.io.InputStream");
133        } catch (ClassNotFoundException ex) {
134            // XXX - should never happen, ignore it
135        }
136        this.humanPresentableName = humanPresentableName;
137    }
138
139    /**
140     * Return the MIME type for this DataFlavor.
141     *
142     * @return  the MIME type
143     */
144    public String getMimeType() {
145        return mimeType;
146    }
147
148    /**
149     * Return the representation class.
150     *
151     * @return  the representation class
152     */
153    public Class getRepresentationClass() {
154        return representationClass;
155    }
156
157    /**
158     * Return the Human Presentable name.
159     *
160     * @return  the human presentable name
161     */
162    public String getHumanPresentableName() {
163        return humanPresentableName;
164    }
165
166    /**
167     * Set the human presentable name.
168     *
169     * @param humanPresentableName      the name to set
170     */
171    public void setHumanPresentableName(String humanPresentableName) {
172        this.humanPresentableName = humanPresentableName;
173    }
174
175    /**
176     * Compares the DataFlavor passed in with this DataFlavor; calls
177     * the {@code isMimeTypeEqual} method.
178     *
179     * @param dataFlavor        the DataFlavor to compare with
180     * @return                  true if the MIME type and representation class
181     *                          are the same
182     */
183    public boolean equals(DataFlavor dataFlavor) {
184        return (isMimeTypeEqual(dataFlavor) &&
185                dataFlavor.getRepresentationClass() == representationClass);
186    }
187
188    /**
189     * Is the string representation of the MIME type passed in equivalent
190     * to the MIME type of this DataFlavor. <p>
191     *
192     * ActivationDataFlavor delegates the comparison of MIME types to
193     * the MimeType class included as part of the JavaBeans Activation
194     * Framework. This provides a more robust comparison than is normally
195     * available in the DataFlavor class.
196     *
197     * @param mimeType  the MIME type
198     * @return          true if the same MIME type
199     */
200    public boolean isMimeTypeEqual(String mimeType) {
201        MimeType mt = null;
202        try {
203            if (mimeObject == null)
204                mimeObject = new MimeType(this.mimeType);
205            mt = new MimeType(mimeType);
206        } catch (MimeTypeParseException e) {
207            // something didn't parse, do a crude comparison
208            return this.mimeType.equalsIgnoreCase(mimeType);
209        }
210
211        return mimeObject.match(mt);
212    }
213
214    /**
215     * Called on DataFlavor for every MIME Type parameter to allow DataFlavor
216     * subclasses to handle special parameters like the text/plain charset
217     * parameters, whose values are case insensitive.  (MIME type parameter
218     * values are supposed to be case sensitive).
219     * <p>
220     * This method is called for each parameter name/value pair and should
221     * return the normalized representation of the parameterValue.
222     * This method is never invoked by this implementation.
223     *
224     * @param parameterName     the parameter name
225     * @param parameterValue    the parameter value
226     * @return                  the normalized parameter value
227     * @deprecated
228     */
229    protected String normalizeMimeTypeParameter(String parameterName,
230                                                String parameterValue) {
231        return parameterValue;
232    }
233
234    /**
235     * Called for each MIME type string to give DataFlavor subtypes the
236     * opportunity to change how the normalization of MIME types is
237     * accomplished.
238     * One possible use would be to add default parameter/value pairs in cases
239     * where none are present in the MIME type string passed in.
240     * This method is never invoked by this implementation.
241     *
242     * @param mimeType  the MIME type
243     * @return          the normalized MIME type
244     * @deprecated
245     */
246    protected String normalizeMimeType(String mimeType) {
247        return mimeType;
248    }
249}
250