docflavor.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 916 行 · 第 1/3 页

JAVA
916
字号
   */  public static final String hostEncoding = Charset.defaultCharset().name();  private transient String mediaSubtype;  private transient String mediaType;  private transient TreeMap params;    // name as defined in Serialized Form JDK 1.4  private String myClassName;    /**   * Constructs a <code>DocFlavor</code> object with the given MIME type and    * representation class name.   *    * @param mimeType the MIME type string.   * @param className the fully-qualified name of the representation class.   *    * @throws NullPointerException if mimeType or className are <code>null</code>.   * @throws IllegalArgumentException if given mimeType has syntax errors.   */  public DocFlavor(String mimeType, String className)  {    if (mimeType == null || className == null)      throw new NullPointerException();    params = new TreeMap();    parseMimeType(mimeType);        myClassName = className;  }    /**   * Parses the given string as MIME type.   * The mediatype, mediasubtype and all parameter/value   * combinations are extracted, comments are dropped.   *     * @param mimeType the string to parse   * @throws IllegalArgumentException if not conformant.   */  private void parseMimeType(String mimeType)  {    int MEDIA = 1;    int MEDIASUB = 2;    int PARAM_NAME = 3;    int PARAM_VALUE = 4;    int COMMENT_START = 5;        int state = 0;    int lastState = 0; // keeps track of state before comment    int tok;        try      {        String paramName = null;        StreamTokenizer in = new StreamTokenizer(new StringReader(mimeType));        in.resetSyntax();        // Allowed characters are anything except:        // SPACE, CTLs (= Unicode characters U+0000 - U+001F and U+007F)        // and tspecials ( ) < > @ , ; : \ " / [ ] ? =        in.whitespaceChars(0x00, 0x20);        in.whitespaceChars(0x7F, 0x7F);        in.wordChars('A', 'Z');        in.wordChars('a', 'z');        in.wordChars('0', '9');        in.wordChars(0xA0, 0xFF);        in.wordChars(0x21, 0x21);        in.wordChars(0x23, 0x27);        in.wordChars(0x2A, 0x2B);        in.wordChars(0x2D, 0x2E);        in.wordChars(0x5E, 0x60);        in.wordChars(0x7B, 0x7E);        in.quoteChar('"');        while ((tok = in.nextToken()) != StreamTokenizer.TT_EOF)          {            switch (tok)              {              case StreamTokenizer.TT_WORD:                if (state == 0)                  {                    mediaType = in.sval.toLowerCase();                    state = MEDIA;                    break;                  }                if (state == MEDIA)                  {                    mediaSubtype = in.sval.toLowerCase();                    state = MEDIASUB;                    break;                  }                // begin of parameters is either after mediasub or a parameter value                if (state == MEDIASUB || state == PARAM_VALUE)                  {                    paramName = in.sval.toLowerCase();                    state = PARAM_NAME;                    break;                  }                // a parameter always needs to follow a value                if (state == PARAM_NAME)                  {                    String paramValue = in.sval;                    // if a charset param the value needs to be stored lowercase                    if (paramName.equals("charset"))                      paramValue = paramValue.toLowerCase();                    state = PARAM_VALUE;                    params.put(paramName, paramValue);                    break;                  }                if (state == COMMENT_START)                  {                    // ignore;                    break;                  }                break;              case '/':                // may only occur after the mediatype                if (state != MEDIA)                  throw new IllegalArgumentException();                break;              case '=':                // may only occur after a parameter                if (state != PARAM_NAME)                  throw new IllegalArgumentException();                break;              case ';':                // differentiates mime type and parameters/value combinations                if (state != MEDIASUB && state != PARAM_VALUE)                  throw new IllegalArgumentException();                break;              case '(': // begin comment                lastState = state;                state = COMMENT_START;                break;              case ')': // end comment                state = lastState;                break;              // a parameter always needs to follow a value / or quoted value              case '"':                if (state == PARAM_NAME)                  {                    String paramValue = in.sval;                    // if a charset param the value needs to be stored lowercase                    if (paramName.equals("charset"))                      paramValue = paramValue.toLowerCase();                    state = PARAM_VALUE;                    params.put(paramName, paramValue);                    break;                  }                // only values may be quoted                throw new IllegalArgumentException();              default:                // if any other char is observed its not allowed                throw new IllegalArgumentException();              }          }      }    catch (IOException e)      {        // should not happen as mimetype str cannot be null        throw new InternalError("IOException during parsing String " + mimeType);      }  }    /**   * Checks if this doc flavor object is equal to the given object.   * <p>   * Two doc flavor objects are considered equal if the provided object is not   * <code>null</code> and an instance of <code>DocFlavor</code>. The MIME   * types has to be equal in their media type, media subtype, their   * paramter/value combinations and the representation classname.   * </p>   *    * @param obj the object to test.   * @return <code>true</code> if equal, <code>false</code> otherwise.   */  public boolean equals(Object obj)  {    if (! (obj instanceof DocFlavor))      return false;    DocFlavor tmp = (DocFlavor) obj;    return (getMimeType().equals(tmp.getMimeType())	    && getRepresentationClassName().equals(tmp.getRepresentationClassName()));  }  /**   * Returns the media subtype of this flavor object.   * A mimetype of "text/html; charset=us-ascii" will   * return "html" as the media subtype.    *    * @return The media subtype.   */  public String getMediaSubtype()  {    return mediaSubtype;  }  /**   * Returns the media type of this flavor object.   * A mimetype of "text/html; charset=us-ascii" will   * return "text" as the media type.   *    * @return The media type.   */  public String getMediaType()  {    return mediaType;  }  /**   * Returns the mime type of this flavor object.   * The mimetype will have every parameter value   * enclosed in quotes.   *    * @return The mime type.   */  public String getMimeType()  {    String mimeType = getMediaType() + "/" + getMediaSubtype();    Iterator it = params.entrySet().iterator();    while (it.hasNext())      {	Map.Entry entry = (Map.Entry) it.next();	mimeType += "; " + entry.getKey() + "=\"" + entry.getValue() + "\"";      }    return mimeType;  }  /**   * Returns the value for an optional parameter of the mime type of this   * flavor object.   *    * @param paramName the name of the parameter   * @return The value for the parameter, or <code>null</code> if none bound.   * @throws NullPointerException if paramName is <code>null</code>.   */  public String getParameter(String paramName)  {    if (paramName == null)      throw new NullPointerException();        return (String) params.get(paramName.toLowerCase());  }  /**   * Returns the name of the representation class of this flavor object.   *    * @return The representation classname.   */  public String getRepresentationClassName()  {    return myClassName;  }  /**   * Returns a hash code for this doc flavor object.   *    * @return The hashcode.   */  public int hashCode()  {    return ((mediaType.hashCode()	     * mediaSubtype.hashCode()	     * myClassName.hashCode()) ^ params.hashCode());  }  /**   * Returns a string representation of this doc flavor object.   * The returned string is of the form   * getMimeType() + "; class=\"" + getRepresentationClassName() + "\"";   *    * @return The constructed string representation.   */  public String toString()  {    return getMimeType() + "; class=\"" + getRepresentationClassName() + "\"";  }    // needs special treatment for serialization  private void readObject(ObjectInputStream stream)     throws IOException, ClassNotFoundException  {    params = new TreeMap();    myClassName = (String) stream.readObject();    parseMimeType((String) stream.readObject());  }  private void writeObject(java.io.ObjectOutputStream stream)    throws IOException  {    stream.writeObject(myClassName);    stream.writeObject(getMimeType());  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?