⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mimemediatype.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      }  else if ( inAttribute ) {
        int endAttr = findNextSeperator( itsParams, currentCharIdx );
        
        if( -1 == endAttr )
          throw new IllegalArgumentException( "malformed mime parameter at idx = " + currentCharIdx );
        
        if( '=' != itsParams.charAt( endAttr )  )
          throw new IllegalArgumentException( "malformed mime parameter at idx = " + currentCharIdx );
        
        if( 0 == (endAttr - currentCharIdx) )
          throw new IllegalArgumentException( "malformed mime parameter at idx = " + currentCharIdx );
        
        currentAttribute = itsParams.substring( currentCharIdx, endAttr ).toLowerCase();
        
        currentCharIdx = endAttr; // skip the equals.
        inAttribute = false;
        inValue = true;
        inQuoted = false;
        nextEscaped = false;
        currentValue = new StringBuffer();
      } else if( inValue ) {
        if( inQuoted ) {
          if( nextEscaped ) {
            currentValue.append( currentChar );
            nextEscaped = false;
          } else {
            if( '\\' == currentChar ) {
              nextEscaped = true;
            } else if( '"' == currentChar ) {
              inQuoted = false;
            } else if( '\r' == currentChar ) {
              throw new IllegalArgumentException( "malformed mime parameter at idx = " + currentCharIdx );
            } else
              currentValue.append( currentChar );
          }
        } else
          if( -1 == terminator.indexOf( currentChar ) )
            currentValue.append( currentChar );
          else {
            if( '"' == currentChar ) {
              inQuoted = true;
            } else {
              parameter newparam = new parameter( currentAttribute, currentValue.toString() );
              
              if ( replace )
                while ( parameters.remove( newparam ) )
                  ;
              
              parameters.add( newparam );
              
              inValue = false;
              inSeperator = true;
              currentCharIdx--; // unget
            }
          }
      } else
        throw new IllegalStateException( "malformed mime parameter at idx = " + currentCharIdx );
      
      currentCharIdx++;
    }
    
    // finish off the last value.
    if( inValue ) {
      if( nextEscaped || inQuoted )
        throw new IllegalArgumentException( "malformed mime parameter at idx = " + currentCharIdx );
      
      parameter newparam = new parameter( currentAttribute, currentValue.toString() );
      
      if ( replace )
        while ( parameters.remove( newparam ) )
          ;
      
      parameters.add( newparam );
      
      inValue = false;
      inSeperator = true;
    }
    
    if( !inSeperator )
      throw new IllegalArgumentException( "malformed mime parameter at idx = " + currentCharIdx );
  }
  
  /* inherits javadoc */
  public Object clone() {
    return this;  // immutable object.
  }
  
  /* 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() {
    StringBuffer retValue = new StringBuffer( type  );
    retValue.append( '/' );
    retValue.append(subtype );
    
    for( Enumeration eachParameter = parameters.elements();
    eachParameter.hasMoreElements(); ) {
      retValue.append( ';' );
      retValue.append( ((parameter)eachParameter.nextElement()).toString() );
    }
    
    return retValue.toString();
  }
  
  /**
   * Get the "root" mime-type/subtype without any of the parameters.
   *
   * @since JXTA 1.0
   *
   * @return full mime-type/subtype
   **/
  public String getMimeMediaType() {
    StringBuffer retValue = new StringBuffer( type  );
    retValue.append( '/' );
    retValue.append(subtype );
    
    return retValue.toString();
  }
  
  /**
   * 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
   **/
  private void setType( 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
   **/
  private 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 the value of the first occurance of the specified parameter from the
   * parameter list.
   *
   *  @param param  the parameter to retrieve.
   *  @return the value of the specifid parameter or null if the parameter was
   * not found.
   **/
  public String getParameter( String param ) {
    for( Enumeration eachParam = parameters.elements(); eachParam.hasMoreElements(); ) {
      parameter aParam = (parameter) eachParam.nextElement();
    
    if ( aParam.attribute.equalsIgnoreCase( param ) )
      return aParam.value;
  }
  return null;
}

/**
 * Find next separator position  in mime-type
 *
 * @since JXTA 1.0
 *
 * @param source  source location
 * @return int separator location
 **/
private static int findNextSeperator( 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( 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 + -