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 26 package javax.activation; 27 28 import java.awt.datatransfer.DataFlavor; 29 import java.io.IOException; 30 import javax.activation.MimeType; 31 32 /** 33 * The ActivationDataFlavor class is a special subclass of 34 * <code>java.awt.datatransfer.DataFlavor</code>. 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 * </code> 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 43 public 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 * <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 representationClass, 80 String mimeType, String 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 representationClass, 107 String 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 mimeType, String humanPresentableName) { 130 super(mimeType, humanPresentableName); 131 this.mimeType = mimeType; 132 try { 133 this.representationClass = Class.forName("java.io.InputStream"); 134 } catch (ClassNotFoundException 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 getMimeType() { 146 return mimeType; 147 } 148 149 /** 150 * Return the representation class. 151 * 152 * @return the representation class 153 */ 154 public Class getRepresentationClass() { 155 return representationClass; 156 } 157 158 /** 159 * Return the Human Presentable name. 160 * 161 * @return the human presentable name 162 */ 163 public String 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 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 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 mimeType) { 202 MimeType mt = null; 203 try { 204 if (mimeObject == null) 205 mimeObject = new MimeType(this.mimeType); 206 mt = new MimeType(mimeType); 207 } catch (MimeTypeParseException e) { 208 // something didn't parse, do a crude comparison 209 return this.mimeType.equalsIgnoreCase(mimeType); 210 } 211 212 return mimeObject.match(mt); 213 } 214 215 /** 216 * Called on DataFlavor for every MIME Type parameter to allow DataFlavor 217 * subclasses to handle special parameters like the text/plain charset 218 * parameters, whose values are case insensitive. (MIME type parameter 219 * values are supposed to be case sensitive). 220 * <p> 221 * This method is called for each parameter name/value pair and should 222 * return the normalized representation of the parameterValue. 223 * This method is never invoked by this implementation. 224 * 225 * @param parameterName the parameter name 226 * @param parameterValue the parameter value 227 * @return the normalized parameter value 228 * @deprecated 229 */ 230 protected String normalizeMimeTypeParameter(String parameterName, 231 String parameterValue) { 232 return parameterValue; 233 } 234 235 /** 236 * Called for each MIME type string to give DataFlavor subtypes the 237 * opportunity to change how the normalization of MIME types is 238 * accomplished. 239 * One possible use would be to add default parameter/value pairs in cases 240 * where none are present in the MIME type string passed in. 241 * This method is never invoked by this implementation. 242 * 243 * @param mimeType the MIME type 244 * @return the normalized MIME type 245 * @deprecated 246 */ 247 protected String normalizeMimeType(String mimeType) { 248 return mimeType; 249 } 250 }