aoqi@0: /*
aoqi@0: * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package javax.activation;
aoqi@0:
aoqi@0: import java.awt.datatransfer.DataFlavor;
aoqi@0: import java.io.IOException;
aoqi@0: import javax.activation.MimeType;
aoqi@0:
aoqi@0: /**
aoqi@0: * The ActivationDataFlavor class is a special subclass of
aoqi@0: * java.awt.datatransfer.DataFlavor
. It allows the JAF to
aoqi@0: * set all three values stored by the DataFlavor class via a new
aoqi@0: * constructor. It also contains improved MIME parsing in the equals
aoqi@0: *
method. Except for the improved parsing, its semantics are
aoqi@0: * identical to that of the JDK's DataFlavor class.
aoqi@0: *
aoqi@0: * @since 1.6
aoqi@0: */
aoqi@0:
aoqi@0: public class ActivationDataFlavor extends DataFlavor {
aoqi@0:
aoqi@0: /*
aoqi@0: * Raison d'etre:
aoqi@0: *
aoqi@0: * The DataFlavor class included in JDK 1.1 has several limitations
aoqi@0: * including piss poor MIME type parsing, and the limitation of
aoqi@0: * only supporting serialized objects and InputStreams as
aoqi@0: * representation objects. This class 'fixes' that.
aoqi@0: */
aoqi@0:
aoqi@0: // I think for now I'll keep copies of all the variables and
aoqi@0: // then later I may choose try to better coexist with the base
aoqi@0: // class *sigh*
aoqi@0: private String mimeType = null;
aoqi@0: private MimeType mimeObject = null;
aoqi@0: private String humanPresentableName = null;
aoqi@0: private Class representationClass = null;
aoqi@0:
aoqi@0: /**
aoqi@0: * Construct a DataFlavor that represents an arbitrary
aoqi@0: * Java object. This constructor is an extension of the
aoqi@0: * JDK's DataFlavor in that it allows the explicit setting
aoqi@0: * of all three DataFlavor attributes.
aoqi@0: *
aoqi@0: * The returned DataFlavor will have the following characteristics: aoqi@0: *
aoqi@0: * representationClass = representationClass
aoqi@0: * mimeType = mimeType
aoqi@0: * humanName = humanName
aoqi@0: *
aoqi@0: * aoqi@0: * @param representationClass the class used in this DataFlavor aoqi@0: * @param mimeType the MIME type of the data represented by this class aoqi@0: * @param humanPresentableName the human presentable name of the flavor aoqi@0: */ aoqi@0: public ActivationDataFlavor(Class representationClass, aoqi@0: String mimeType, String humanPresentableName) { aoqi@0: super(mimeType, humanPresentableName); // need to call super aoqi@0: aoqi@0: // init private variables: aoqi@0: this.mimeType = mimeType; aoqi@0: this.humanPresentableName = humanPresentableName; aoqi@0: this.representationClass = representationClass; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Construct a DataFlavor that represents a MimeType. aoqi@0: *
aoqi@0: * The returned DataFlavor will have the following characteristics: aoqi@0: *
aoqi@0: * If the mimeType is "application/x-java-serialized-object; aoqi@0: * class=", the result is the same as calling new aoqi@0: * DataFlavor(Class.forName()) as above. aoqi@0: *
aoqi@0: * otherwise: aoqi@0: *
aoqi@0: * representationClass = InputStream
aoqi@0: * mimeType = mimeType
aoqi@0: * aoqi@0: * @param representationClass the class used in this DataFlavor aoqi@0: * @param humanPresentableName the human presentable name of the flavor aoqi@0: */ aoqi@0: public ActivationDataFlavor(Class representationClass, aoqi@0: String humanPresentableName) { aoqi@0: super(representationClass, humanPresentableName); aoqi@0: this.mimeType = super.getMimeType(); aoqi@0: this.representationClass = representationClass; aoqi@0: this.humanPresentableName = humanPresentableName; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Construct a DataFlavor that represents a MimeType. aoqi@0: *
aoqi@0: * The returned DataFlavor will have the following characteristics: aoqi@0: *
aoqi@0: * If the mimeType is "application/x-java-serialized-object; class=", aoqi@0: * the result is the same as calling new DataFlavor(Class.forName()) as aoqi@0: * above, otherwise: aoqi@0: *
aoqi@0: * representationClass = InputStream
aoqi@0: * mimeType = mimeType
aoqi@0: *
aoqi@0: * @param mimeType the MIME type of the data represented by this class
aoqi@0: * @param humanPresentableName the human presentable name of the flavor
aoqi@0: */
aoqi@0: public ActivationDataFlavor(String mimeType, String humanPresentableName) {
aoqi@0: super(mimeType, humanPresentableName);
aoqi@0: this.mimeType = mimeType;
aoqi@0: try {
aoqi@0: this.representationClass = Class.forName("java.io.InputStream");
aoqi@0: } catch (ClassNotFoundException ex) {
aoqi@0: // XXX - should never happen, ignore it
aoqi@0: }
aoqi@0: this.humanPresentableName = humanPresentableName;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Return the MIME type for this DataFlavor.
aoqi@0: *
aoqi@0: * @return the MIME type
aoqi@0: */
aoqi@0: public String getMimeType() {
aoqi@0: return mimeType;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Return the representation class.
aoqi@0: *
aoqi@0: * @return the representation class
aoqi@0: */
aoqi@0: public Class getRepresentationClass() {
aoqi@0: return representationClass;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Return the Human Presentable name.
aoqi@0: *
aoqi@0: * @return the human presentable name
aoqi@0: */
aoqi@0: public String getHumanPresentableName() {
aoqi@0: return humanPresentableName;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set the human presentable name.
aoqi@0: *
aoqi@0: * @param humanPresentableName the name to set
aoqi@0: */
aoqi@0: public void setHumanPresentableName(String humanPresentableName) {
aoqi@0: this.humanPresentableName = humanPresentableName;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Compares the DataFlavor passed in with this DataFlavor; calls
aoqi@0: * the isMimeTypeEqual
method.
aoqi@0: *
aoqi@0: * @param dataFlavor the DataFlavor to compare with
aoqi@0: * @return true if the MIME type and representation class
aoqi@0: * are the same
aoqi@0: */
aoqi@0: public boolean equals(DataFlavor dataFlavor) {
aoqi@0: return (isMimeTypeEqual(dataFlavor) &&
aoqi@0: dataFlavor.getRepresentationClass() == representationClass);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Is the string representation of the MIME type passed in equivalent
aoqi@0: * to the MIME type of this DataFlavor.
aoqi@0: * aoqi@0: * ActivationDataFlavor delegates the comparison of MIME types to aoqi@0: * the MimeType class included as part of the JavaBeans Activation aoqi@0: * Framework. This provides a more robust comparison than is normally aoqi@0: * available in the DataFlavor class. aoqi@0: * aoqi@0: * @param mimeType the MIME type aoqi@0: * @return true if the same MIME type aoqi@0: */ aoqi@0: public boolean isMimeTypeEqual(String mimeType) { aoqi@0: MimeType mt = null; aoqi@0: try { aoqi@0: if (mimeObject == null) aoqi@0: mimeObject = new MimeType(this.mimeType); aoqi@0: mt = new MimeType(mimeType); aoqi@0: } catch (MimeTypeParseException e) { aoqi@0: // something didn't parse, do a crude comparison aoqi@0: return this.mimeType.equalsIgnoreCase(mimeType); aoqi@0: } aoqi@0: aoqi@0: return mimeObject.match(mt); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Called on DataFlavor for every MIME Type parameter to allow DataFlavor aoqi@0: * subclasses to handle special parameters like the text/plain charset aoqi@0: * parameters, whose values are case insensitive. (MIME type parameter aoqi@0: * values are supposed to be case sensitive). aoqi@0: *
aoqi@0: * This method is called for each parameter name/value pair and should aoqi@0: * return the normalized representation of the parameterValue. aoqi@0: * This method is never invoked by this implementation. aoqi@0: * aoqi@0: * @param parameterName the parameter name aoqi@0: * @param parameterValue the parameter value aoqi@0: * @return the normalized parameter value aoqi@0: * @deprecated aoqi@0: */ aoqi@0: protected String normalizeMimeTypeParameter(String parameterName, aoqi@0: String parameterValue) { aoqi@0: return parameterValue; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Called for each MIME type string to give DataFlavor subtypes the aoqi@0: * opportunity to change how the normalization of MIME types is aoqi@0: * accomplished. aoqi@0: * One possible use would be to add default parameter/value pairs in cases aoqi@0: * where none are present in the MIME type string passed in. aoqi@0: * This method is never invoked by this implementation. aoqi@0: * aoqi@0: * @param mimeType the MIME type aoqi@0: * @return the normalized MIME type aoqi@0: * @deprecated aoqi@0: */ aoqi@0: protected String normalizeMimeType(String mimeType) { aoqi@0: return mimeType; aoqi@0: } aoqi@0: }