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