Wed, 27 Apr 2016 15:32:20 -0700
Added tag jdk8u102-b05 for changeset 1f512419ae33
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package javax.activation; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.awt.datatransfer.DataFlavor; |
aoqi@0 | 29 | import java.io.IOException; |
aoqi@0 | 30 | import javax.activation.MimeType; |
aoqi@0 | 31 | |
aoqi@0 | 32 | /** |
aoqi@0 | 33 | * The ActivationDataFlavor class is a special subclass of |
aoqi@0 | 34 | * <code>java.awt.datatransfer.DataFlavor</code>. It allows the JAF to |
aoqi@0 | 35 | * set all three values stored by the DataFlavor class via a new |
aoqi@0 | 36 | * constructor. It also contains improved MIME parsing in the <code>equals |
aoqi@0 | 37 | * </code> method. Except for the improved parsing, its semantics are |
aoqi@0 | 38 | * identical to that of the JDK's DataFlavor class. |
aoqi@0 | 39 | * |
aoqi@0 | 40 | * @since 1.6 |
aoqi@0 | 41 | */ |
aoqi@0 | 42 | |
aoqi@0 | 43 | public class ActivationDataFlavor extends DataFlavor { |
aoqi@0 | 44 | |
aoqi@0 | 45 | /* |
aoqi@0 | 46 | * Raison d'etre: |
aoqi@0 | 47 | * |
aoqi@0 | 48 | * The DataFlavor class included in JDK 1.1 has several limitations |
aoqi@0 | 49 | * including piss poor MIME type parsing, and the limitation of |
aoqi@0 | 50 | * only supporting serialized objects and InputStreams as |
aoqi@0 | 51 | * representation objects. This class 'fixes' that. |
aoqi@0 | 52 | */ |
aoqi@0 | 53 | |
aoqi@0 | 54 | // I think for now I'll keep copies of all the variables and |
aoqi@0 | 55 | // then later I may choose try to better coexist with the base |
aoqi@0 | 56 | // class *sigh* |
aoqi@0 | 57 | private String mimeType = null; |
aoqi@0 | 58 | private MimeType mimeObject = null; |
aoqi@0 | 59 | private String humanPresentableName = null; |
aoqi@0 | 60 | private Class representationClass = null; |
aoqi@0 | 61 | |
aoqi@0 | 62 | /** |
aoqi@0 | 63 | * Construct a DataFlavor that represents an arbitrary |
aoqi@0 | 64 | * Java object. This constructor is an extension of the |
aoqi@0 | 65 | * JDK's DataFlavor in that it allows the explicit setting |
aoqi@0 | 66 | * of all three DataFlavor attributes. |
aoqi@0 | 67 | * <p> |
aoqi@0 | 68 | * The returned DataFlavor will have the following characteristics: |
aoqi@0 | 69 | * <p> |
aoqi@0 | 70 | * representationClass = representationClass<br> |
aoqi@0 | 71 | * mimeType = mimeType<br> |
aoqi@0 | 72 | * humanName = humanName |
aoqi@0 | 73 | * <p> |
aoqi@0 | 74 | * |
aoqi@0 | 75 | * @param representationClass the class used in this DataFlavor |
aoqi@0 | 76 | * @param mimeType the MIME type of the data represented by this class |
aoqi@0 | 77 | * @param humanPresentableName the human presentable name of the flavor |
aoqi@0 | 78 | */ |
aoqi@0 | 79 | public ActivationDataFlavor(Class representationClass, |
aoqi@0 | 80 | String mimeType, String humanPresentableName) { |
aoqi@0 | 81 | super(mimeType, humanPresentableName); // need to call super |
aoqi@0 | 82 | |
aoqi@0 | 83 | // init private variables: |
aoqi@0 | 84 | this.mimeType = mimeType; |
aoqi@0 | 85 | this.humanPresentableName = humanPresentableName; |
aoqi@0 | 86 | this.representationClass = representationClass; |
aoqi@0 | 87 | } |
aoqi@0 | 88 | |
aoqi@0 | 89 | /** |
aoqi@0 | 90 | * Construct a DataFlavor that represents a MimeType. |
aoqi@0 | 91 | * <p> |
aoqi@0 | 92 | * The returned DataFlavor will have the following characteristics: |
aoqi@0 | 93 | * <p> |
aoqi@0 | 94 | * If the mimeType is "application/x-java-serialized-object; |
aoqi@0 | 95 | * class=", the result is the same as calling new |
aoqi@0 | 96 | * DataFlavor(Class.forName()) as above. |
aoqi@0 | 97 | * <p> |
aoqi@0 | 98 | * otherwise: |
aoqi@0 | 99 | * <p> |
aoqi@0 | 100 | * representationClass = InputStream<p> |
aoqi@0 | 101 | * mimeType = mimeType<p> |
aoqi@0 | 102 | * |
aoqi@0 | 103 | * @param representationClass the class used in this DataFlavor |
aoqi@0 | 104 | * @param humanPresentableName the human presentable name of the flavor |
aoqi@0 | 105 | */ |
aoqi@0 | 106 | public ActivationDataFlavor(Class representationClass, |
aoqi@0 | 107 | String humanPresentableName) { |
aoqi@0 | 108 | super(representationClass, humanPresentableName); |
aoqi@0 | 109 | this.mimeType = super.getMimeType(); |
aoqi@0 | 110 | this.representationClass = representationClass; |
aoqi@0 | 111 | this.humanPresentableName = humanPresentableName; |
aoqi@0 | 112 | } |
aoqi@0 | 113 | |
aoqi@0 | 114 | /** |
aoqi@0 | 115 | * Construct a DataFlavor that represents a MimeType. |
aoqi@0 | 116 | * <p> |
aoqi@0 | 117 | * The returned DataFlavor will have the following characteristics: |
aoqi@0 | 118 | * <p> |
aoqi@0 | 119 | * If the mimeType is "application/x-java-serialized-object; class=", |
aoqi@0 | 120 | * the result is the same as calling new DataFlavor(Class.forName()) as |
aoqi@0 | 121 | * above, otherwise: |
aoqi@0 | 122 | * <p> |
aoqi@0 | 123 | * representationClass = InputStream<p> |
aoqi@0 | 124 | * mimeType = mimeType |
aoqi@0 | 125 | * |
aoqi@0 | 126 | * @param mimeType the MIME type of the data represented by this class |
aoqi@0 | 127 | * @param humanPresentableName the human presentable name of the flavor |
aoqi@0 | 128 | */ |
aoqi@0 | 129 | public ActivationDataFlavor(String mimeType, String humanPresentableName) { |
aoqi@0 | 130 | super(mimeType, humanPresentableName); |
aoqi@0 | 131 | this.mimeType = mimeType; |
aoqi@0 | 132 | try { |
aoqi@0 | 133 | this.representationClass = Class.forName("java.io.InputStream"); |
aoqi@0 | 134 | } catch (ClassNotFoundException ex) { |
aoqi@0 | 135 | // XXX - should never happen, ignore it |
aoqi@0 | 136 | } |
aoqi@0 | 137 | this.humanPresentableName = humanPresentableName; |
aoqi@0 | 138 | } |
aoqi@0 | 139 | |
aoqi@0 | 140 | /** |
aoqi@0 | 141 | * Return the MIME type for this DataFlavor. |
aoqi@0 | 142 | * |
aoqi@0 | 143 | * @return the MIME type |
aoqi@0 | 144 | */ |
aoqi@0 | 145 | public String getMimeType() { |
aoqi@0 | 146 | return mimeType; |
aoqi@0 | 147 | } |
aoqi@0 | 148 | |
aoqi@0 | 149 | /** |
aoqi@0 | 150 | * Return the representation class. |
aoqi@0 | 151 | * |
aoqi@0 | 152 | * @return the representation class |
aoqi@0 | 153 | */ |
aoqi@0 | 154 | public Class getRepresentationClass() { |
aoqi@0 | 155 | return representationClass; |
aoqi@0 | 156 | } |
aoqi@0 | 157 | |
aoqi@0 | 158 | /** |
aoqi@0 | 159 | * Return the Human Presentable name. |
aoqi@0 | 160 | * |
aoqi@0 | 161 | * @return the human presentable name |
aoqi@0 | 162 | */ |
aoqi@0 | 163 | public String getHumanPresentableName() { |
aoqi@0 | 164 | return humanPresentableName; |
aoqi@0 | 165 | } |
aoqi@0 | 166 | |
aoqi@0 | 167 | /** |
aoqi@0 | 168 | * Set the human presentable name. |
aoqi@0 | 169 | * |
aoqi@0 | 170 | * @param humanPresentableName the name to set |
aoqi@0 | 171 | */ |
aoqi@0 | 172 | public void setHumanPresentableName(String humanPresentableName) { |
aoqi@0 | 173 | this.humanPresentableName = humanPresentableName; |
aoqi@0 | 174 | } |
aoqi@0 | 175 | |
aoqi@0 | 176 | /** |
aoqi@0 | 177 | * Compares the DataFlavor passed in with this DataFlavor; calls |
aoqi@0 | 178 | * the <code>isMimeTypeEqual</code> method. |
aoqi@0 | 179 | * |
aoqi@0 | 180 | * @param dataFlavor the DataFlavor to compare with |
aoqi@0 | 181 | * @return true if the MIME type and representation class |
aoqi@0 | 182 | * are the same |
aoqi@0 | 183 | */ |
aoqi@0 | 184 | public boolean equals(DataFlavor dataFlavor) { |
aoqi@0 | 185 | return (isMimeTypeEqual(dataFlavor) && |
aoqi@0 | 186 | dataFlavor.getRepresentationClass() == representationClass); |
aoqi@0 | 187 | } |
aoqi@0 | 188 | |
aoqi@0 | 189 | /** |
aoqi@0 | 190 | * Is the string representation of the MIME type passed in equivalent |
aoqi@0 | 191 | * to the MIME type of this DataFlavor. <p> |
aoqi@0 | 192 | * |
aoqi@0 | 193 | * ActivationDataFlavor delegates the comparison of MIME types to |
aoqi@0 | 194 | * the MimeType class included as part of the JavaBeans Activation |
aoqi@0 | 195 | * Framework. This provides a more robust comparison than is normally |
aoqi@0 | 196 | * available in the DataFlavor class. |
aoqi@0 | 197 | * |
aoqi@0 | 198 | * @param mimeType the MIME type |
aoqi@0 | 199 | * @return true if the same MIME type |
aoqi@0 | 200 | */ |
aoqi@0 | 201 | public boolean isMimeTypeEqual(String mimeType) { |
aoqi@0 | 202 | MimeType mt = null; |
aoqi@0 | 203 | try { |
aoqi@0 | 204 | if (mimeObject == null) |
aoqi@0 | 205 | mimeObject = new MimeType(this.mimeType); |
aoqi@0 | 206 | mt = new MimeType(mimeType); |
aoqi@0 | 207 | } catch (MimeTypeParseException e) { |
aoqi@0 | 208 | // something didn't parse, do a crude comparison |
aoqi@0 | 209 | return this.mimeType.equalsIgnoreCase(mimeType); |
aoqi@0 | 210 | } |
aoqi@0 | 211 | |
aoqi@0 | 212 | return mimeObject.match(mt); |
aoqi@0 | 213 | } |
aoqi@0 | 214 | |
aoqi@0 | 215 | /** |
aoqi@0 | 216 | * Called on DataFlavor for every MIME Type parameter to allow DataFlavor |
aoqi@0 | 217 | * subclasses to handle special parameters like the text/plain charset |
aoqi@0 | 218 | * parameters, whose values are case insensitive. (MIME type parameter |
aoqi@0 | 219 | * values are supposed to be case sensitive). |
aoqi@0 | 220 | * <p> |
aoqi@0 | 221 | * This method is called for each parameter name/value pair and should |
aoqi@0 | 222 | * return the normalized representation of the parameterValue. |
aoqi@0 | 223 | * This method is never invoked by this implementation. |
aoqi@0 | 224 | * |
aoqi@0 | 225 | * @param parameterName the parameter name |
aoqi@0 | 226 | * @param parameterValue the parameter value |
aoqi@0 | 227 | * @return the normalized parameter value |
aoqi@0 | 228 | * @deprecated |
aoqi@0 | 229 | */ |
aoqi@0 | 230 | protected String normalizeMimeTypeParameter(String parameterName, |
aoqi@0 | 231 | String parameterValue) { |
aoqi@0 | 232 | return parameterValue; |
aoqi@0 | 233 | } |
aoqi@0 | 234 | |
aoqi@0 | 235 | /** |
aoqi@0 | 236 | * Called for each MIME type string to give DataFlavor subtypes the |
aoqi@0 | 237 | * opportunity to change how the normalization of MIME types is |
aoqi@0 | 238 | * accomplished. |
aoqi@0 | 239 | * One possible use would be to add default parameter/value pairs in cases |
aoqi@0 | 240 | * where none are present in the MIME type string passed in. |
aoqi@0 | 241 | * This method is never invoked by this implementation. |
aoqi@0 | 242 | * |
aoqi@0 | 243 | * @param mimeType the MIME type |
aoqi@0 | 244 | * @return the normalized MIME type |
aoqi@0 | 245 | * @deprecated |
aoqi@0 | 246 | */ |
aoqi@0 | 247 | protected String normalizeMimeType(String mimeType) { |
aoqi@0 | 248 | return mimeType; |
aoqi@0 | 249 | } |
aoqi@0 | 250 | } |