KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > activation > ActivationDataFlavor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)ActivationDataFlavor.java 1.14 05/11/16
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.activation;
29
30 import java.awt.datatransfer.DataFlavor JavaDoc;
31 import java.io.IOException JavaDoc;
32 import javax.activation.MimeType JavaDoc;
33
34 /**
35  * The ActivationDataFlavor class is a special subclass of
36  * <code>java.awt.datatransfer.DataFlavor</code>. It allows the JAF to
37  * set all three values stored by the DataFlavor class via a new
38  * constructor. It also contains improved MIME parsing in the <code>equals
39  * </code> method. Except for the improved parsing, its semantics are
40  * identical to that of the JDK's DataFlavor class.
41  */

42
43 public class ActivationDataFlavor extends DataFlavor JavaDoc {
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 JavaDoc mimeType = null;
58     private MimeType JavaDoc mimeObject = null;
59     private String JavaDoc humanPresentableName = null;
60     private Class JavaDoc 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      * <p>
74      *
75      * @param representationClass the class used in this DataFlavor
76      * @param mimeType the MIME type of the data represented by this class
77      * @param humanPresentableName the human presentable name of the flavor
78      */

79     public ActivationDataFlavor(Class JavaDoc representationClass,
80               String JavaDoc mimeType, String JavaDoc humanPresentableName) {
81     super(mimeType, humanPresentableName); // need to call super
82

83     // init private variables:
84
this.mimeType = mimeType;
85     this.humanPresentableName = humanPresentableName;
86     this.representationClass = representationClass;
87     }
88
89     /**
90      * Construct a DataFlavor that represents a MimeType.
91      * <p>
92      * The returned DataFlavor will have the following characteristics:
93      * <p>
94      * If the mimeType is "application/x-java-serialized-object;
95      * class=", the result is the same as calling new
96      * DataFlavor(Class.forName()) as above.
97      * <p>
98      * otherwise:
99      * <p>
100      * representationClass = InputStream<p>
101      * mimeType = mimeType<p>
102      *
103      * @param representationClass the class used in this DataFlavor
104      * @param humanPresentableName the human presentable name of the flavor
105      */

106     public ActivationDataFlavor(Class JavaDoc representationClass,
107                 String JavaDoc humanPresentableName) {
108     super(representationClass, humanPresentableName);
109     this.mimeType = super.getMimeType();
110     this.representationClass = representationClass;
111         this.humanPresentableName = humanPresentableName;
112     }
113
114     /**
115      * Construct a DataFlavor that represents a MimeType.
116      * <p>
117      * The returned DataFlavor will have the following characteristics:
118      * <p>
119      * If the mimeType is "application/x-java-serialized-object; class=",
120      * the result is the same as calling new DataFlavor(Class.forName()) as
121      * above, otherwise:
122      * <p>
123      * representationClass = InputStream<p>
124      * mimeType = mimeType
125      *
126      * @param mimeType the MIME type of the data represented by this class
127      * @param humanPresentableName the human presentable name of the flavor
128      */

129     public ActivationDataFlavor(String JavaDoc mimeType, String JavaDoc humanPresentableName) {
130     super(mimeType, humanPresentableName);
131     this.mimeType = mimeType;
132     try {
133         this.representationClass = Class.forName("java.io.InputStream");
134     } catch (ClassNotFoundException JavaDoc ex) {
135         // XXX - should never happen, ignore it
136
}
137         this.humanPresentableName = humanPresentableName;
138     }
139
140     /**
141      * Return the MIME type for this DataFlavor.
142      *
143      * @return the MIME type
144      */

145     public String JavaDoc getMimeType() {
146     return mimeType;
147     }
148
149     /**
150      * Return the representation class.
151      *
152      * @return the representation class
153      */

154     public Class JavaDoc getRepresentationClass() {
155     return representationClass;
156     }
157
158     /**
159      * Return the Human Presentable name.
160      *
161      * @return the human presentable name
162      */

163     public String JavaDoc getHumanPresentableName() {
164     return humanPresentableName;
165     }
166
167     /**
168      * Set the human presentable name.
169      *
170      * @param humanPresentableName the name to set
171      */

172     public void setHumanPresentableName(String JavaDoc humanPresentableName) {
173     this.humanPresentableName = humanPresentableName;
174     }
175
176     /**
177      * Compares the DataFlavor passed in with this DataFlavor; calls
178      * the <code>isMimeTypeEqual</code> method.
179      *
180      * @param dataFlavor the DataFlavor to compare with
181      * @return true if the MIME type and representation class
182      * are the same
183      */

184     public boolean equals(DataFlavor JavaDoc dataFlavor) {
185     return (isMimeTypeEqual(dataFlavor) &&
186         dataFlavor.getRepresentationClass() == representationClass);
187     }
188
189     /**
190      * Is the string representation of the MIME type passed in equivalent
191      * to the MIME type of this DataFlavor. <p>
192      *
193      * ActivationDataFlavor delegates the comparison of MIME types to
194      * the MimeType class included as part of the JavaBeans Activation
195      * Framework. This provides a more robust comparison than is normally
196      * available in the DataFlavor class.
197      *
198      * @param mimeType the MIME type
199      * @return true if the same MIME type
200      */

201     public boolean isMimeTypeEqual(String JavaDoc mimeType) {
202     MimeType JavaDoc mt = null;
203     try {
204         if (mimeObject == null)
205         mimeObject = new MimeType JavaDoc(this.mimeType);
206         mt = new MimeType JavaDoc(mimeType);
207     } catch (MimeTypeParseException JavaDoc e) {}
208
209     return mimeObject.match(mt);
210     }
211
212     /**
213      * Called on DataFlavor for every MIME Type parameter to allow DataFlavor
214      * subclasses to handle special parameters like the text/plain charset
215      * parameters, whose values are case insensitive. (MIME type parameter
216      * values are supposed to be case sensitive).
217      * <p>
218      * This method is called for each parameter name/value pair and should
219      * return the normalized representation of the parameterValue.
220      * This method is never invoked by this implementation.
221      *
222      * @param parameterName the parameter name
223      * @param parameterValue the parameter value
224      * @return the normalized parameter value
225      * @deprecated
226      */

227     protected String JavaDoc normalizeMimeTypeParameter(String JavaDoc parameterName,
228                         String JavaDoc parameterValue) {
229     return parameterValue;
230     }
231
232     /**
233      * Called for each MIME type string to give DataFlavor subtypes the
234      * opportunity to change how the normalization of MIME types is
235      * accomplished.
236      * One possible use would be to add default parameter/value pairs in cases
237      * where none are present in the MIME type string passed in.
238      * This method is never invoked by this implementation.
239      *
240      * @param mimeType the MIME type
241      * @return the normalized MIME type
242      * @deprecated
243      */

244     protected String JavaDoc normalizeMimeType(String JavaDoc mimeType) {
245     return mimeType;
246     }
247 }
248
Popular Tags