Package org.simpleframework.http.parse
Class ContentParser
- java.lang.Object
-
- org.simpleframework.util.parse.Parser
-
- org.simpleframework.http.parse.ContentParser
-
- All Implemented Interfaces:
ContentType
public class ContentParser extends Parser implements ContentType
This provides access to the MIME type parts, that is the primary type, the secondary type and an optional character set parameter. Thecharsetparameter is one of many parameters that can be associated with a MIME type. This however this exposes this parameter with a typed method.The
getCharsetwill return the character encoding the content type is encoded within. This allows the user of the content to decode it correctly. Other parameters can be acquired from this by simply providing the name of the parameter.- Author:
- Niall Gallagher
-
-
Constructor Summary
Constructors Constructor Description ContentParser()The default constructor will create aContentParserthat contains no charset, type or subtype.ContentParser(java.lang.String header)This is primarily a convenience constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringgetCharset()This is used to retrieve thecharsetof this MIME type.java.lang.StringgetParameter(java.lang.String name)This is used to retrieve an arbitrary parameter from the MIME type header.java.lang.StringgetPrimary()This is used to retrieve the primary type of this MIME type.java.lang.StringgetSecondary()This is used to retrieve the secondary type of this MIME type.protected voidinit()This will initialize the parser when it is ready to parse a newString.protected voidparse()Reads and parses the MIME type from the givenStringobject.voidsetCharset(java.lang.String enc)This will set thecharsetto whatever value the string contains.voidsetParameter(java.lang.String name, java.lang.String value)This will add a named parameter to the content type header.voidsetPrimary(java.lang.String primary)This sets the primary type to whatever value is in the string provided is.voidsetSecondary(java.lang.String type)This sets the secondary type to whatever value is in the string provided is.java.lang.StringtoString()This will return the value of the MIME type as a string.
-
-
-
Constructor Detail
-
ContentParser
public ContentParser()
The default constructor will create aContentParserthat contains no charset, type or subtype. This can be used to extract the type, subtype and the optionalcharsetparameter by using the parser'sparse(String)method.
-
ContentParser
public ContentParser(java.lang.String header)
This is primarily a convenience constructor. This will parse theStringgiven to extract the MIME type. This could be achived by calling the default no-arg constructor and then using the instance to invoke theparsemethod on thatString.- Parameters:
header-Stringcontaining a MIME type value
-
-
Method Detail
-
setPrimary
public void setPrimary(java.lang.String primary)
This sets the primary type to whatever value is in the string provided is. If the string is null then this will contain a null string for the primary type of the parameter, which is likely invalid in most cases.- Specified by:
setPrimaryin interfaceContentType- Parameters:
primary- the type to set for the primary type of this
-
getPrimary
public java.lang.String getPrimary()
This is used to retrieve the primary type of this MIME type. The primary type part within the MIME type defines the generic type. For exampletext/plain; charset=UTF-8. This will return the text value. If there is no primary type then this will returnnullotherwise the string value.- Specified by:
getPrimaryin interfaceContentType- Returns:
- the primary type part of this MIME type
-
setSecondary
public void setSecondary(java.lang.String type)
This sets the secondary type to whatever value is in the string provided is. If the string is null then this will contain a null string for the secondary type of the parameter, which is likely invalid in most cases.- Specified by:
setSecondaryin interfaceContentType- Parameters:
type- the type to set for the primary type of this
-
getSecondary
public java.lang.String getSecondary()
This is used to retrieve the secondary type of this MIME type. The secondary type part within the MIME type defines the generic type. For exampletext/html; charset=UTF-8. This will return the HTML value. If there is no secondary type then this will returnnullotherwise the string value.- Specified by:
getSecondaryin interfaceContentType- Returns:
- the primary type part of this MIME type
-
setCharset
public void setCharset(java.lang.String enc)
This will set thecharsetto whatever value the string contains. If the string is null then this will not set the parameter to any value and thetoStringmethod will not contain any details of the parameter.- Specified by:
setCharsetin interfaceContentType- Parameters:
enc- parameter value to add to the MIME type
-
getCharset
public java.lang.String getCharset()
This is used to retrieve thecharsetof this MIME type. This is a special parameter associated with the type, if the parameter is not contained within the type then this will return null, which typically means the default of ISO-8859-1.- Specified by:
getCharsetin interfaceContentType- Returns:
- the value that this parameter contains
-
getParameter
public java.lang.String getParameter(java.lang.String name)
This is used to retrieve an arbitrary parameter from the MIME type header. This ensures that values forboundaryor other such parameters are not lost when the header is parsed. This will return the value, unquoted if required, as a string.- Specified by:
getParameterin interfaceContentType- Parameters:
name- this is the name of the parameter to be retrieved- Returns:
- this is the value for the parameter, or null if empty
-
setParameter
public void setParameter(java.lang.String name, java.lang.String value)This will add a named parameter to the content type header. If a parameter of the specified name has already been added to the header then that value will be replaced by the new value given. Parameters such as theboundaryas well as other common parameters can be set with this method.- Specified by:
setParameterin interfaceContentType- Parameters:
name- this is the name of the parameter to be addedvalue- this is the value to associate with the name
-
init
protected void init()
This will initialize the parser when it is ready to parse a newString. This will reset the parser to a ready state. The init method is invoked by the parser when theParser.parsemethod is invoked.
-
parse
protected void parse()
Reads and parses the MIME type from the givenStringobject. This uses the syntax defined by RFC 2616 for the media-type syntax. This parser is only concerned with one parameter, thecharsetparameter. The syntax for the media type ismedia-type = token "/" token *( ";" parameter ) parameter = token | literal
-
toString
public java.lang.String toString()
This will return the value of the MIME type as a string. This will concatenate the primary and secondary type values and add thecharsetparameter to the type which will recreate the content type.- Specified by:
toStringin interfaceContentType- Overrides:
toStringin classjava.lang.Object- Returns:
- this returns the string representation of the type
-
-