Package org.simpleframework.http.parse
Class ContentTypeParser
- java.lang.Object
-
- org.simpleframework.common.parse.Parser
-
- org.simpleframework.http.parse.ContentTypeParser
-
- All Implemented Interfaces:
ContentType
public class ContentTypeParser 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. Thecharset
parameter is one of many parameters that can be associated with a MIME type. This however this exposes this parameter with a typed method.The
getCharset
will 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.
-
-
Field Summary
Fields Modifier and Type Field Description private ParseBuffer
charset
Used to store the characters for the charset parameter.private KeyMap<java.lang.String>
map
Used to store the name value pairs of the parameters.private ParseBuffer
name
Used to collect the name of a content type parameter.private ParseBuffer
primary
Used to store the characters consumed for the primary type.private ParseBuffer
secondary
Used to store the characters consumed for the secondary type.private ParseBuffer
type
Used to store the characters consumed for the type.private ParseBuffer
value
Used to collect the value of the content type parameter.
-
Constructor Summary
Constructors Constructor Description ContentTypeParser()
The default constructor will create aContentParser
that contains no charset, primary or secondary.ContentTypeParser(java.lang.String header)
This is primarily a convenience constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
charset()
This is used to read the value from thecharset
param.private void
clear()
This is used to clear all previously collected tokens.private java.lang.String
encode()
This will return the value of the MIME type as a string.private java.lang.String
encode(java.lang.StringBuilder text)
This will return the value of the MIME type as a string.java.lang.String
getCharset()
This is used to retrieve thecharset
of this MIME type.java.lang.String
getParameter(java.lang.String name)
This is used to retrieve an arbitrary parameter from the MIME type header.java.lang.String
getPrimary()
This is used to retrieve the primary type of this MIME type.java.lang.String
getSecondary()
This is used to retrieve the secondary type of this MIME type.java.lang.String
getType()
This method is used to get the primary and secondary parts joined together with a "/".protected void
init()
This will initialize the parser when it is ready to parse a newString
.private void
insert()
This will add the name and value tokens to the parameters map.private void
insert(ParseBuffer name, ParseBuffer value)
This will add the given name and value to the parameters map.private void
name()
This will simply read all characters from the buffer before the first '=' character.private void
pack()
This is used to remove all whitespace characters from theString
excluding the whitespace within literals.private void
parameter()
This is a parameter as defined by RFC 2616.private void
parameters()
This will read the parameters from the MIME type.protected void
parse()
Reads and parses the MIME type from the givenString
object.private void
primary()
This reads the type from the MIME type.private boolean
quote(char ch)
This method is used to determine if the specified character is a quote character.private void
secondary()
This reads the subtype from the MIME type.void
setCharset(java.lang.String enc)
This will set thecharset
to whatever value the string contains.void
setParameter(java.lang.String name, java.lang.String value)
This will add a named parameter to the content type header.void
setPrimary(java.lang.String value)
This sets the primary type to whatever value is in the string provided is.void
setSecondary(java.lang.String value)
This sets the secondary type to whatever value is in the string provided is.java.lang.String
toString()
This will return the value of the MIME type as a string.private void
value()
This is used to read a parameters value from the buf.
-
-
-
Field Detail
-
secondary
private ParseBuffer secondary
Used to store the characters consumed for the secondary type.
-
primary
private ParseBuffer primary
Used to store the characters consumed for the primary type.
-
charset
private ParseBuffer charset
Used to store the characters for the charset parameter.
-
type
private ParseBuffer type
Used to store the characters consumed for the type.
-
name
private ParseBuffer name
Used to collect the name of a content type parameter.
-
value
private ParseBuffer value
Used to collect the value of the content type parameter.
-
map
private KeyMap<java.lang.String> map
Used to store the name value pairs of the parameters.
-
-
Constructor Detail
-
ContentTypeParser
public ContentTypeParser()
The default constructor will create aContentParser
that contains no charset, primary or secondary. This can be used to extract the primary, secondary and the optional charset parameter by using the parser'sparse(String)
method.
-
ContentTypeParser
public ContentTypeParser(java.lang.String header)
This is primarily a convenience constructor. This will parse theString
given to extract the MIME type. This could be achieved by calling the default no-arg constructor and then using the instance to invoke theparse
method on thatString
.- Parameters:
header
-String
containing a MIME type value
-
-
Method Detail
-
getType
public java.lang.String getType()
This method is used to get the primary and secondary parts joined together with a "/". This is typically how a content type is examined. Here convenience is most important, we can easily compare content types without any parameters.- Specified by:
getType
in interfaceContentType
- Returns:
- this returns the primary and secondary types
-
setPrimary
public void setPrimary(java.lang.String value)
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:
setPrimary
in interfaceContentType
- Parameters:
value
- 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 returnnull
otherwise the string value.- Specified by:
getPrimary
in interfaceContentType
- Returns:
- the primary type part of this MIME type
-
setSecondary
public void setSecondary(java.lang.String value)
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:
setSecondary
in interfaceContentType
- Parameters:
value
- 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 returnnull
otherwise the string value.- Specified by:
getSecondary
in interfaceContentType
- Returns:
- the primary type part of this MIME type
-
setCharset
public void setCharset(java.lang.String enc)
This will set thecharset
to whatever value the string contains. If the string is null then this will not set the parameter to any value and thetoString
method will not contain any details of the parameter.- Specified by:
setCharset
in interfaceContentType
- Parameters:
enc
- parameter value to add to the MIME type
-
getCharset
public java.lang.String getCharset()
This is used to retrieve thecharset
of 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:
getCharset
in 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 forboundary
or other such parameters are not lost when the header is parsed. This will return the value, unquoted if required, as a string.- Specified by:
getParameter
in 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 theboundary
as well as other common parameters can be set with this method.- Specified by:
setParameter
in 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.parse
method is invoked.
-
clear
private void clear()
This is used to clear all previously collected tokens. This allows the parser to be reused when there are multiple source strings to be parsed. Clearing of the tokens is performed when the parser is initialized.
-
parse
protected void parse()
Reads and parses the MIME type from the givenString
object. This uses the syntax defined by RFC 2616 for the media-type syntax. This parser is only concerned with one parameter, thecharset
parameter. The syntax for the media type ismedia-type = token "/" token *( ";" parameter ) parameter = token | literal
-
pack
private void pack()
This is used to remove all whitespace characters from theString
excluding the whitespace within literals. The definition of a literal can be found in RFC 2616.The definition of a literal for RFC 2616 is anything between 2 quotes but excluding quotes that are prefixed with the backward slash character.
-
primary
private void primary()
This reads the type from the MIME type. This will fill the typeParseBuffer
. This will read all chars upto but not including the first instance of a '/'. The type of a media-type as defined by RFC 2616 istype/subtype;param=val;param2=val
.
-
secondary
private void secondary()
This reads the subtype from the MIME type. This will fill the subtypeParseBuffer
. This will read all chars upto but not including the first instance of a ';'. The subtype of a media-type as defined by RFC 2616 istype/subtype;param=val;param2=val
.
-
parameters
private void parameters()
This will read the parameters from the MIME type. This will search for thecharset
parameter within the set of parameters which are given to the type. Thecharset
param is the only parameter that this parser will tokenize.This will remove any parameters that preceed the charset parameter. Once the
charset
is retrived the MIME type is considered to be parsed.
-
insert
private void insert()
This will add the name and value tokens to the parameters map. If any previous value of the given name has been inserted into the map then this will overwrite that value. This is used to ensure that the string value is inserted to the map.
-
insert
private void insert(ParseBuffer name, ParseBuffer value)
This will add the given name and value to the parameters map. If any previous value of the given name has been inserted into the map then this will overwrite that value. This is used to ensure that the string value is inserted to the map.- Parameters:
name
- this is the name of the value to be insertedvalue
- this is the value of a that is to be inserted
-
parameter
private void parameter()
This is a parameter as defined by RFC 2616. The parameter is added to a MIME type e.g.type/subtype;param=val
etc. The parameter name and value are not stored. This is used to simply update the read offset past the parameter. The reason for reading the parameters is to search for thecharset
parameter which will indicate the encoding.
-
name
private void name()
This will simply read all characters from the buffer before the first '=' character. This represents a parameter name (see RFC 2616 for token). The parameter name is not buffered it is simply read from the buffer. This will not cause anIndexOutOfBoundsException
as each offset is checked before it is acccessed.
-
value
private void value()
This is used to read a parameters value from the buf. This will read allchar
's upto but excluding the first terminalchar
encountered from the off within the buf, or if the value is a literal it will read a literal from the buffer (literal is any data between quotes except if the quote is prefixed with a backward slash character).
-
quote
private boolean quote(char ch)
This method is used to determine if the specified character is a quote character. The quote character is typically used as a boundary for the values within the header. This accepts a single or double quote.- Parameters:
ch
- the character to determine if it is a quotation- Returns:
- true if the character provided is a quotation character
-
charset
private void charset()
This is used to read the value from thecharset
param. This will fill thecharset
ParseBuffer
and with thecharset
value. This will read a literal or a token as thecharset
value. If thecharset
is a literal then the quotes will be read as part of the charset.
-
encode
private java.lang.String encode()
This will return the value of the MIME type as a string. This will concatenate the primary and secondary type values and add thecharset
parameter to the type which will recreate the content type.- Returns:
- this returns the string representation of the type
-
encode
private java.lang.String encode(java.lang.StringBuilder text)
This will return the value of the MIME type as a string. This will concatenate the primary and secondary type values and add thecharset
parameter to the type which will recreate the content type.- Parameters:
text
- this is the buffer to encode the parameters to- Returns:
- this returns the string representation of the type
-
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 thecharset
parameter to the type which will recreate the content type.- Specified by:
toString
in interfaceContentType
- Overrides:
toString
in classjava.lang.Object
- Returns:
- this returns the string representation of the type
-
-