simple.util.parse
Class ContentParser

java.lang.Object
  extended by simple.util.parse.Parser
      extended by simple.util.parse.ContentParser
All Implemented Interfaces:
java.io.Serializable, ContentType

public class ContentParser
extends Parser
implements ContentType

ContentParser provides access to the MIME type parts, that is the type subtype and an optional charset parameter. The charset parameter is one of many parameters that can be assiciated with a MIME type. This however only provides access to the charset value.

The getCharset will return val if the MIME type represented is type/subtype; charset=val. The type and subtype are set to the String value null if the setPrimary or setSecondary are given a nullString. If the String that is being parsed does not contain a type or subtype then the toString will return the value null/null otherwise it will recreate the MIME type.

Author:
Niall Gallagher
See Also:
Serialized Form

Field Summary
 
Fields inherited from class simple.util.parse.Parser
buf, count, off
 
Constructor Summary
ContentParser()
          The default constructor will create a ContentParser that contains no charset, type or subtype.
ContentParser(java.lang.String header)
          This is primarily a convineance constructor.
 
Method Summary
 java.lang.String getCharset()
          This is used to retrive the charset of this MIME type.
 java.lang.String getPrimary()
          This is used to retrive the type of this MIME type.
 java.lang.String getSecondary()
          This is used to retrive the subtype of this MIME type.
protected  void init()
          This will initialize the parser when it is ready to parse a new String.
protected  void parse()
          Reads and parses the MIME type from the given String object.
 void setCharset(java.lang.String enc)
          This will set the charset to whatever value is in the String object.
 void setPrimary(java.lang.String primary)
          Sets the type to whatever value is in the String object.
 void setSecondary(java.lang.String type)
          Sets the subtype to whatever value is in the String object.
 java.lang.String toString()
          This will return the String value of the MIME type.
 
Methods inherited from class simple.util.parse.Parser
digit, ensureCapacity, parse, skip, space, toLower
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ContentParser

public ContentParser()
The default constructor will create a ContentParser that contains no charset, type or subtype. This can be used to extract the type, subtype and the optional charset parameter by using the parser's parse(String) method.


ContentParser

public ContentParser(java.lang.String header)
This is primarily a convineance constructor. This will parse the String given to extract the MIME type. This could be achived by calling the default no-arg constructor and then using the instance to invoke the parse method on that String.

Parameters:
header - String containing a MIME type value
Method Detail

setPrimary

public void setPrimary(java.lang.String primary)
Sets the type to whatever value is in the String object. If the String object is null the this object's toString method will contain the value null.

If type is null then the toString method will be null/subtype;param=value. If the type is non-null this will contain the value of the String.

Specified by:
setPrimary in interface ContentType
Parameters:
primary - the type to add to the MIME type

getPrimary

public java.lang.String getPrimary()
This is used to retrive the type of this MIME type. The type part within the MIME type defines the generic type. For example type/subtype;param1=value1. This will return the value of the type part. If there is no type part then this will return null otherwise the type String.

Specified by:
getPrimary in interface ContentType
Returns:
the type part of the MIME type

setSecondary

public void setSecondary(java.lang.String type)
Sets the subtype to whatever value is in the String object. If the String object is null the this object's toString method will contain the value null.

If subtype is null then the toString method will be type/null;param=value. If the type is non-null this will contain the value of the String.

Specified by:
setSecondary in interface ContentType
Parameters:
type - the type to add to the MIME type

getSecondary

public java.lang.String getSecondary()
This is used to retrive the subtype of this MIME type. The subtype part within the MIME type defines the specific type. For example type/subtype;param1=value1. This will return the value of the subtype part. If there is no subtype part then this will return null otherwise the type String.

Specified by:
getSecondary in interface ContentType
Returns:
the subtype part of the MIME type

setCharset

public void setCharset(java.lang.String enc)
This will set the charset to whatever value is in the String object. If the String object is null the this object's toString method will not contain the charset.

If charset is null then the toString method will be type/subtype. If the charset value is non-null this will contain the charset parameter with that value.

Specified by:
setCharset in interface ContentType
Parameters:
enc - the charset value to add to the MIME type

getCharset

public java.lang.String getCharset()
This is used to retrive the charset of this MIME type. The charset part within the MIME type is an optional parameter. For example type/subtype;charset=value. This will return the value of the charset value. If there is no charset param then this will return null otherwise the type String.

Specified by:
getCharset in interface ContentType
Returns:
the charset value for the MIME type

init

protected void init()
This will initialize the parser when it is ready to parse a new String. This will reset the parser to a ready state. The init method is invoked by the parser when the Parser.parse method is invoked.

Specified by:
init in class Parser

parse

protected void parse()
Reads and parses the MIME type from the given String object. This uses the syntax defined by RFC 2616 for the media-type syntax. This parser is only concerned with one parameter, the charset parameter. The syntax for thhe media type is
 media-type = token "/" token *( ";" parameter )
 parameter = token | literal 
 

Specified by:
parse in class Parser

toString

public java.lang.String toString()
This will return the String value of the MIME type. This will return the MIME type with the type, subtype and if there is a charset value specified then a charset parameter.

The charset parameter is an optional parameter to the MIME type. An example a MIME type is type/subtype; charset=value. If the type or subtype is null then the MIME type will be wither null/subtype, type/null or if both are null null/null.

Specified by:
toString in interface ContentType
Overrides:
toString in class java.lang.Object
Returns:
the String representation of the MIME type