📄 mimemediatype.java
字号:
/*
* Copyright (c) 2001 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Sun Microsystems, Inc. for Project JXTA."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact Project JXTA at http://www.jxta.org.
*
* 5. Products derived from this software may not be called "JXTA",
* nor may "JXTA" appear in their name, without prior written
* permission of Sun.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Project JXTA. For more
* information on Project JXTA, please see
* <http://www.jxta.org/>.
*
* This license is based on the BSD license adopted by the Apache Foundation.
*
* $Id: MimeMediaType.java,v 1.2 2002/03/04 20:18:17 echtcherbina Exp $
*/
package net.jxta.document;
import java.lang.String;
import java.util.Enumeration;
import java.util.Vector;
import java.lang.IllegalArgumentException;
/**
* Mime Media Types are used to describe the format of data streams. Its format
* is defined by
* {@link <a href=http://www.ietf.org/rfc/rfc2046.txt">IETF RFC 2046 <i>MIME : Media Types</i></a>}.
* This class managed parsing of Mime Media Types from strings and piecemeal
* construction of Mime Media Type descriptors.
*
* <p/>FIXME bondolo@jxta.org 20010219 Doesnt yet handle parameters
*
* @see net.jxta.document.Document
* @see net.jxta.document.StructuredDocument
* @see net.jxta.document.StructuredTextDocument
*
* @version $Revision: 1.2 $
* @since JXTA 1.0
*/
public class MimeMediaType extends Object {
/**
* The primary media type
**/
private String type = null;
/**
* The specific media sub-type
**/
private String subtype = null;
/**
* The parameters for this media type
**/
private Vector parameters = new Vector();
/**
* Creates a new MimeMediaType
*
* @since JXTA 1.0
*
* @param someString string representing a mime-type
*/
public MimeMediaType( final String someString ) {
if( null == someString )
throw new IllegalArgumentException( "input cannot be null" );
String cleaned = someString.trim().toLowerCase();
if( 0 == cleaned.length() )
throw new IllegalArgumentException( "input cannot be null" );
// determine the type
int typeSepAt = findNextSeperator( cleaned );
if( (-1 == typeSepAt) || (0 == typeSepAt) )
throw new IllegalArgumentException(
"expected seperator or seperator in unexpected location" );
String itsType = cleaned.substring( 0, typeSepAt );
// determine the sub-type
int subtypeSepAt = findNextSeperator( cleaned, typeSepAt + 1 );
String itsSubType = null;
String itsParams = null;
if(-1 == subtypeSepAt)
itsSubType = cleaned.substring( typeSepAt + 1 );
else {
itsSubType = cleaned.substring( typeSepAt + 1, subtypeSepAt);
itsParams = cleaned.substring( subtypeSepAt );
// include the seperator, its significant
}
Vector someParams = new Vector();
setType( itsType );
setSubtype( itsSubType );
setParameters( new String [0] );
}
/**
* Creates a new type/subtype MimeMediaType
*
* @since JXTA 1.0
*
* @param type string representing a mime type
* @param subtype string representing a mime subtype
**/
public MimeMediaType( final String type, final String subtype ) {
this( type, subtype, (String) null );
}
/**
* Creates a new type/subtype MimeMediaType
*
* @since JXTA 1.0
*
* @param type string representing a mime type
* @param subtype string representing a mime subtype
* @param parameters parameters to the mime-type constructor
**/
public MimeMediaType( final String type, final String subtype,
final String parameters ) {
Vector someParams = new Vector();
if( null != parameters ) {
// FIXME bondolo@jxta.org 20010219 proccessing of parameters still needs to be done.
}
setType( type );
setSubtype( subtype );
setParameters( new String [0] );
}
/**
* Creates a new type/subtype MimeMediaType
*
* @since JXTA 1.0
* @param parameters parameters for this mimetype
* @param type string representing a mime type
* @param subtype string representing a mime subtype
*/
public MimeMediaType( final String type, final String subtype,
final String [] parameters ) {
setType( type );
setSubtype( subtype );
setParameters( parameters );
}
/* inherits javadoc */
public Object clone() {
Object retValue = new MimeMediaType( toString() );
return retValue;
}
/* inherits javadoc */
public boolean equals(Object obj) {
if (this == obj)
return true;
if( !(obj instanceof MimeMediaType) )
return false;
boolean retValue = (hashCode() == obj.hashCode());
return retValue;
}
/* inherits javadoc */
public int hashCode() {
return toString().hashCode();
}
/* inherits javadoc */
public String toString() {
if( !isValid() )
return null;
String retValue = type + "/" + subtype;
for( Enumeration eachParameter = parameters.elements();
eachParameter.hasMoreElements(); )
retValue += ";" + (String) eachParameter.nextElement();
return retValue;
}
/**
* Check if mime-type is valid
*
* @since JXTA 1.0
*
* @return booelan true if mime-type is well-formed
**/
public boolean isValid() {
boolean result = ( (null != type) && (null != subtype) );
return result;
}
/**
* Get the "root" mime-type/subtype without any of the parameters.
*
* @since JXTA 1.0
*
* @return full mime-type/subtype
**/
public String getMimeMediaType() {
if( !isValid() )
return null;
String retValue = type + "/" + subtype;
return retValue;
}
/**
* Get type of the mime-type
*
* @since JXTA 1.0
*
* @return type of the mime-type
**/
public String getType() {
return type;
}
/**
* Check if the mime-type is for provisional. See Section 2.1 of
* {@link <a href=http://www.ietf.org/rfc/rfc2048.txt">IETF RFC 2048 <i>MIME : Registration Procedures</i></a>}
*
* @since JXTA 1.0
*
* @return boolean true if it is a provisional type
**/
public boolean isExperimentalType() {
if( (null == type) || (type.length() < 2) )
return false;
if( type.startsWith( "x-" ) || type.startsWith( "x." ) )
return true;
if( (null == subtype) || (subtype.length() < 2) )
return false;
return ( subtype.startsWith( "x-" ) || subtype.startsWith( "x." ) );
}
/**
* Set the type of MimeMediaType
*
* @since JXTA 1.0
*
* @param type type value
**/
public void setType( final String type ) {
if( null == type )
throw new IllegalArgumentException( "type cannot be null" );
String cleaned = type.trim().toLowerCase();
if( 0 == cleaned.length() )
throw new IllegalArgumentException( "type cannot be null" );
if( -1 != findNextSeperator( cleaned ) )
throw new IllegalArgumentException( "type cannot contain a seperator" );
this.type = cleaned;
}
/**
* Get the Subtype of the mime-type
*
* @since JXTA 1.0
*
* @return subtype of the mime-type
*/
public String getSubtype() {
return subtype;
}
/**
* Check if the mime-type is for debugging. This method will be
* removed
*
* @since JXTA 1.0
*
* @return boolean true if it is a debugging type
**/
public boolean isExperimentalSubtype() {
if( (null == subtype) || (subtype.length() < 2) )
return false;
return( ('x' == subtype.charAt(0)) && ('-' == subtype.charAt(1)) );
}
/**
* Set the subtype of MimeMediaType
*
* @since JXTA 1.0
*
* @param subtype subtype value
**/
public void setSubtype( String subtype ) {
if( null == subtype )
throw new IllegalArgumentException( "subtype cannot be null" );
String cleaned = subtype.trim().toLowerCase();
if( 0 == cleaned.length() )
throw new IllegalArgumentException( "subtype cannot be null" );
if( -1 != findNextSeperator( cleaned ) )
throw new IllegalArgumentException( "subtype cannot contain a seperator" );
this.subtype = cleaned;
}
/**
* Get parameters value of a mime-type
*
* @since JXTA 1.0
*
* @return Enumeration return enumeration of parameters for this mime-type
**/
public Enumeration getParameters() {
return parameters.elements();
}
/**
* Set parameter to a mime-type
*
* @since JXTA 1.0
*
* @param parameters array of parameters
**/
public void setParameters( String [] parameters ) {
this.parameters = new Vector();
for( int eachParameter = 0; eachParameter < parameters.length; eachParameter++ )
addParameter( parameters[eachParameter] );
}
/**
* Add parameter to a mime-type
*
* @since JXTA 1.0
*
* @param newParam new parameter to add to the type
**/
public void addParameter( String newParam ) {
parameters.addElement( newParam );
}
private final static String CTL =
"\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007" +
"\u0008\u0009\n\u000b\u000c\r\u000e\u000f" +
"\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017" +
"\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f" +
"\u007f";
private final static String space = "\u0020";
private final static String tspecials = "()<>@,;:\\\"/[]?=";
private final static String terminator = CTL + space + tspecials;
/**
* Find next separator position in mime-type
*
* @since JXTA 1.0
*
* @param source source location
* @return int separator location
**/
private int findNextSeperator( final String source ) {
return findNextSeperator( source, 0 );
}
/**
* Find next separator position in mime-type
*
* @since JXTA 1.0
*
* @param source source location
* @param from from location
* @return int separator location
**/
private static int findNextSeperator( final String source, int from ) {
int seperator = -1;
// find a seperator
for( int eachChar = from; eachChar < source.length(); eachChar++ )
if( -1 != terminator.indexOf( source.charAt( eachChar ) ) ) {
seperator = eachChar;
break;
}
return seperator;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -