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