Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxws/src/share/jaf_classes/javax/activation/ActivationDataFlavor.java
38873 views
/*1* Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.activation;2627import java.awt.datatransfer.DataFlavor;28import java.io.IOException;29import javax.activation.MimeType;3031/**32* The ActivationDataFlavor class is a special subclass of33* <code>java.awt.datatransfer.DataFlavor</code>. It allows the JAF to34* set all three values stored by the DataFlavor class via a new35* constructor. It also contains improved MIME parsing in the <code>equals36* </code> method. Except for the improved parsing, its semantics are37* identical to that of the JDK's DataFlavor class.38*39* @since 1.640*/4142public class ActivationDataFlavor extends DataFlavor {4344/*45* Raison d'etre:46*47* The DataFlavor class included in JDK 1.1 has several limitations48* including piss poor MIME type parsing, and the limitation of49* only supporting serialized objects and InputStreams as50* representation objects. This class 'fixes' that.51*/5253// I think for now I'll keep copies of all the variables and54// then later I may choose try to better coexist with the base55// class *sigh*56private String mimeType = null;57private MimeType mimeObject = null;58private String humanPresentableName = null;59private Class representationClass = null;6061/**62* Construct a DataFlavor that represents an arbitrary63* Java object. This constructor is an extension of the64* JDK's DataFlavor in that it allows the explicit setting65* of all three DataFlavor attributes.66* <p>67* The returned DataFlavor will have the following characteristics:68* <p>69* representationClass = representationClass<br>70* mimeType = mimeType<br>71* humanName = humanName72* <p>73*74* @param representationClass the class used in this DataFlavor75* @param mimeType the MIME type of the data represented by this class76* @param humanPresentableName the human presentable name of the flavor77*/78public ActivationDataFlavor(Class representationClass,79String mimeType, String humanPresentableName) {80super(mimeType, humanPresentableName); // need to call super8182// init private variables:83this.mimeType = mimeType;84this.humanPresentableName = humanPresentableName;85this.representationClass = representationClass;86}8788/**89* Construct a DataFlavor that represents a MimeType.90* <p>91* The returned DataFlavor will have the following characteristics:92* <p>93* If the mimeType is "application/x-java-serialized-object;94* class=", the result is the same as calling new95* DataFlavor(Class.forName()) as above.96* <p>97* otherwise:98* <p>99* representationClass = InputStream<p>100* mimeType = mimeType<p>101*102* @param representationClass the class used in this DataFlavor103* @param humanPresentableName the human presentable name of the flavor104*/105public ActivationDataFlavor(Class representationClass,106String humanPresentableName) {107super(representationClass, humanPresentableName);108this.mimeType = super.getMimeType();109this.representationClass = representationClass;110this.humanPresentableName = humanPresentableName;111}112113/**114* Construct a DataFlavor that represents a MimeType.115* <p>116* The returned DataFlavor will have the following characteristics:117* <p>118* If the mimeType is "application/x-java-serialized-object; class=",119* the result is the same as calling new DataFlavor(Class.forName()) as120* above, otherwise:121* <p>122* representationClass = InputStream<p>123* mimeType = mimeType124*125* @param mimeType the MIME type of the data represented by this class126* @param humanPresentableName the human presentable name of the flavor127*/128public ActivationDataFlavor(String mimeType, String humanPresentableName) {129super(mimeType, humanPresentableName);130this.mimeType = mimeType;131try {132this.representationClass = Class.forName("java.io.InputStream");133} catch (ClassNotFoundException ex) {134// XXX - should never happen, ignore it135}136this.humanPresentableName = humanPresentableName;137}138139/**140* Return the MIME type for this DataFlavor.141*142* @return the MIME type143*/144public String getMimeType() {145return mimeType;146}147148/**149* Return the representation class.150*151* @return the representation class152*/153public Class getRepresentationClass() {154return representationClass;155}156157/**158* Return the Human Presentable name.159*160* @return the human presentable name161*/162public String getHumanPresentableName() {163return humanPresentableName;164}165166/**167* Set the human presentable name.168*169* @param humanPresentableName the name to set170*/171public void setHumanPresentableName(String humanPresentableName) {172this.humanPresentableName = humanPresentableName;173}174175/**176* Compares the DataFlavor passed in with this DataFlavor; calls177* the <code>isMimeTypeEqual</code> method.178*179* @param dataFlavor the DataFlavor to compare with180* @return true if the MIME type and representation class181* are the same182*/183public boolean equals(DataFlavor dataFlavor) {184return (isMimeTypeEqual(dataFlavor) &&185dataFlavor.getRepresentationClass() == representationClass);186}187188/**189* Is the string representation of the MIME type passed in equivalent190* to the MIME type of this DataFlavor. <p>191*192* ActivationDataFlavor delegates the comparison of MIME types to193* the MimeType class included as part of the JavaBeans Activation194* Framework. This provides a more robust comparison than is normally195* available in the DataFlavor class.196*197* @param mimeType the MIME type198* @return true if the same MIME type199*/200public boolean isMimeTypeEqual(String mimeType) {201MimeType mt = null;202try {203if (mimeObject == null)204mimeObject = new MimeType(this.mimeType);205mt = new MimeType(mimeType);206} catch (MimeTypeParseException e) {207// something didn't parse, do a crude comparison208return this.mimeType.equalsIgnoreCase(mimeType);209}210211return mimeObject.match(mt);212}213214/**215* Called on DataFlavor for every MIME Type parameter to allow DataFlavor216* subclasses to handle special parameters like the text/plain charset217* parameters, whose values are case insensitive. (MIME type parameter218* values are supposed to be case sensitive).219* <p>220* This method is called for each parameter name/value pair and should221* return the normalized representation of the parameterValue.222* This method is never invoked by this implementation.223*224* @param parameterName the parameter name225* @param parameterValue the parameter value226* @return the normalized parameter value227* @deprecated228*/229protected String normalizeMimeTypeParameter(String parameterName,230String parameterValue) {231return parameterValue;232}233234/**235* Called for each MIME type string to give DataFlavor subtypes the236* opportunity to change how the normalization of MIME types is237* accomplished.238* One possible use would be to add default parameter/value pairs in cases239* where none are present in the MIME type string passed in.240* This method is never invoked by this implementation.241*242* @param mimeType the MIME type243* @return the normalized MIME type244* @deprecated245*/246protected String normalizeMimeType(String mimeType) {247return mimeType;248}249}250251252