src/share/jaf_classes/javax/activation/ActivationDataFlavor.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaf_classes/javax/activation/ActivationDataFlavor.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,250 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package javax.activation;
    1.30 +
    1.31 +import java.awt.datatransfer.DataFlavor;
    1.32 +import java.io.IOException;
    1.33 +import javax.activation.MimeType;
    1.34 +
    1.35 +/**
    1.36 + * The ActivationDataFlavor class is a special subclass of
    1.37 + * <code>java.awt.datatransfer.DataFlavor</code>. It allows the JAF to
    1.38 + * set all three values stored by the DataFlavor class via a new
    1.39 + * constructor. It also contains improved MIME parsing in the <code>equals
    1.40 + * </code> method. Except for the improved parsing, its semantics are
    1.41 + * identical to that of the JDK's DataFlavor class.
    1.42 + *
    1.43 + * @since 1.6
    1.44 + */
    1.45 +
    1.46 +public class ActivationDataFlavor extends DataFlavor {
    1.47 +
    1.48 +    /*
    1.49 +     * Raison d'etre:
    1.50 +     *
    1.51 +     * The DataFlavor class included in JDK 1.1 has several limitations
    1.52 +     * including piss poor MIME type parsing, and the limitation of
    1.53 +     * only supporting serialized objects and InputStreams as
    1.54 +     * representation objects. This class 'fixes' that.
    1.55 +     */
    1.56 +
    1.57 +    // I think for now I'll keep copies of all the variables and
    1.58 +    // then later I may choose try to better coexist with the base
    1.59 +    // class *sigh*
    1.60 +    private String mimeType = null;
    1.61 +    private MimeType mimeObject = null;
    1.62 +    private String humanPresentableName = null;
    1.63 +    private Class representationClass = null;
    1.64 +
    1.65 +    /**
    1.66 +     * Construct a DataFlavor that represents an arbitrary
    1.67 +     * Java object. This constructor is an extension of the
    1.68 +     * JDK's DataFlavor in that it allows the explicit setting
    1.69 +     * of all three DataFlavor attributes.
    1.70 +     * <p>
    1.71 +     * The returned DataFlavor will have the following characteristics:
    1.72 +     * <p>
    1.73 +     * representationClass = representationClass<br>
    1.74 +     * mimeType            = mimeType<br>
    1.75 +     * humanName           = humanName
    1.76 +     * <p>
    1.77 +     *
    1.78 +     * @param representationClass the class used in this DataFlavor
    1.79 +     * @param mimeType the MIME type of the data represented by this class
    1.80 +     * @param humanPresentableName the human presentable name of the flavor
    1.81 +     */
    1.82 +    public ActivationDataFlavor(Class representationClass,
    1.83 +                      String mimeType, String humanPresentableName) {
    1.84 +        super(mimeType, humanPresentableName); // need to call super
    1.85 +
    1.86 +        // init private variables:
    1.87 +        this.mimeType = mimeType;
    1.88 +        this.humanPresentableName = humanPresentableName;
    1.89 +        this.representationClass = representationClass;
    1.90 +    }
    1.91 +
    1.92 +    /**
    1.93 +     * Construct a DataFlavor that represents a MimeType.
    1.94 +     * <p>
    1.95 +     * The returned DataFlavor will have the following characteristics:
    1.96 +     * <p>
    1.97 +     * If the mimeType is "application/x-java-serialized-object;
    1.98 +     * class=", the result is the same as calling new
    1.99 +     * DataFlavor(Class.forName()) as above.
   1.100 +     * <p>
   1.101 +     * otherwise:
   1.102 +     * <p>
   1.103 +     * representationClass = InputStream<p>
   1.104 +     * mimeType = mimeType<p>
   1.105 +     *
   1.106 +     * @param representationClass the class used in this DataFlavor
   1.107 +     * @param humanPresentableName the human presentable name of the flavor
   1.108 +     */
   1.109 +    public ActivationDataFlavor(Class representationClass,
   1.110 +                                String humanPresentableName) {
   1.111 +        super(representationClass, humanPresentableName);
   1.112 +        this.mimeType = super.getMimeType();
   1.113 +        this.representationClass = representationClass;
   1.114 +        this.humanPresentableName = humanPresentableName;
   1.115 +    }
   1.116 +
   1.117 +    /**
   1.118 +     * Construct a DataFlavor that represents a MimeType.
   1.119 +     * <p>
   1.120 +     * The returned DataFlavor will have the following characteristics:
   1.121 +     * <p>
   1.122 +     * If the mimeType is "application/x-java-serialized-object; class=",
   1.123 +     * the result is the same as calling new DataFlavor(Class.forName()) as
   1.124 +     * above, otherwise:
   1.125 +     * <p>
   1.126 +     * representationClass = InputStream<p>
   1.127 +     * mimeType = mimeType
   1.128 +     *
   1.129 +     * @param mimeType the MIME type of the data represented by this class
   1.130 +     * @param humanPresentableName the human presentable name of the flavor
   1.131 +     */
   1.132 +    public ActivationDataFlavor(String mimeType, String humanPresentableName) {
   1.133 +        super(mimeType, humanPresentableName);
   1.134 +        this.mimeType = mimeType;
   1.135 +        try {
   1.136 +            this.representationClass = Class.forName("java.io.InputStream");
   1.137 +        } catch (ClassNotFoundException ex) {
   1.138 +            // XXX - should never happen, ignore it
   1.139 +        }
   1.140 +        this.humanPresentableName = humanPresentableName;
   1.141 +    }
   1.142 +
   1.143 +    /**
   1.144 +     * Return the MIME type for this DataFlavor.
   1.145 +     *
   1.146 +     * @return  the MIME type
   1.147 +     */
   1.148 +    public String getMimeType() {
   1.149 +        return mimeType;
   1.150 +    }
   1.151 +
   1.152 +    /**
   1.153 +     * Return the representation class.
   1.154 +     *
   1.155 +     * @return  the representation class
   1.156 +     */
   1.157 +    public Class getRepresentationClass() {
   1.158 +        return representationClass;
   1.159 +    }
   1.160 +
   1.161 +    /**
   1.162 +     * Return the Human Presentable name.
   1.163 +     *
   1.164 +     * @return  the human presentable name
   1.165 +     */
   1.166 +    public String getHumanPresentableName() {
   1.167 +        return humanPresentableName;
   1.168 +    }
   1.169 +
   1.170 +    /**
   1.171 +     * Set the human presentable name.
   1.172 +     *
   1.173 +     * @param humanPresentableName      the name to set
   1.174 +     */
   1.175 +    public void setHumanPresentableName(String humanPresentableName) {
   1.176 +        this.humanPresentableName = humanPresentableName;
   1.177 +    }
   1.178 +
   1.179 +    /**
   1.180 +     * Compares the DataFlavor passed in with this DataFlavor; calls
   1.181 +     * the <code>isMimeTypeEqual</code> method.
   1.182 +     *
   1.183 +     * @param dataFlavor        the DataFlavor to compare with
   1.184 +     * @return                  true if the MIME type and representation class
   1.185 +     *                          are the same
   1.186 +     */
   1.187 +    public boolean equals(DataFlavor dataFlavor) {
   1.188 +        return (isMimeTypeEqual(dataFlavor) &&
   1.189 +                dataFlavor.getRepresentationClass() == representationClass);
   1.190 +    }
   1.191 +
   1.192 +    /**
   1.193 +     * Is the string representation of the MIME type passed in equivalent
   1.194 +     * to the MIME type of this DataFlavor. <p>
   1.195 +     *
   1.196 +     * ActivationDataFlavor delegates the comparison of MIME types to
   1.197 +     * the MimeType class included as part of the JavaBeans Activation
   1.198 +     * Framework. This provides a more robust comparison than is normally
   1.199 +     * available in the DataFlavor class.
   1.200 +     *
   1.201 +     * @param mimeType  the MIME type
   1.202 +     * @return          true if the same MIME type
   1.203 +     */
   1.204 +    public boolean isMimeTypeEqual(String mimeType) {
   1.205 +        MimeType mt = null;
   1.206 +        try {
   1.207 +            if (mimeObject == null)
   1.208 +                mimeObject = new MimeType(this.mimeType);
   1.209 +            mt = new MimeType(mimeType);
   1.210 +        } catch (MimeTypeParseException e) {
   1.211 +            // something didn't parse, do a crude comparison
   1.212 +            return this.mimeType.equalsIgnoreCase(mimeType);
   1.213 +        }
   1.214 +
   1.215 +        return mimeObject.match(mt);
   1.216 +    }
   1.217 +
   1.218 +    /**
   1.219 +     * Called on DataFlavor for every MIME Type parameter to allow DataFlavor
   1.220 +     * subclasses to handle special parameters like the text/plain charset
   1.221 +     * parameters, whose values are case insensitive.  (MIME type parameter
   1.222 +     * values are supposed to be case sensitive).
   1.223 +     * <p>
   1.224 +     * This method is called for each parameter name/value pair and should
   1.225 +     * return the normalized representation of the parameterValue.
   1.226 +     * This method is never invoked by this implementation.
   1.227 +     *
   1.228 +     * @param parameterName     the parameter name
   1.229 +     * @param parameterValue    the parameter value
   1.230 +     * @return                  the normalized parameter value
   1.231 +     * @deprecated
   1.232 +     */
   1.233 +    protected String normalizeMimeTypeParameter(String parameterName,
   1.234 +                                                String parameterValue) {
   1.235 +        return parameterValue;
   1.236 +    }
   1.237 +
   1.238 +    /**
   1.239 +     * Called for each MIME type string to give DataFlavor subtypes the
   1.240 +     * opportunity to change how the normalization of MIME types is
   1.241 +     * accomplished.
   1.242 +     * One possible use would be to add default parameter/value pairs in cases
   1.243 +     * where none are present in the MIME type string passed in.
   1.244 +     * This method is never invoked by this implementation.
   1.245 +     *
   1.246 +     * @param mimeType  the MIME type
   1.247 +     * @return          the normalized MIME type
   1.248 +     * @deprecated
   1.249 +     */
   1.250 +    protected String normalizeMimeType(String mimeType) {
   1.251 +        return mimeType;
   1.252 +    }
   1.253 +}

mercurial