uk.ac.starlink.util
Class ContentType

java.lang.Object
  extended by uk.ac.starlink.util.ContentType

public class ContentType
extends java.lang.Object

Represents a Content Type (MIME type) string. Most of the work is done by the {link #parseContentType} factory method.

This takes care of things like optional whitespace and case folding, so for instance if ctypeTxt has the value

    APPLICATION / X-VOTABLE+XML ; content=datalink; CHARSET="iso\-8859\-1"
 
then
    ContentType ctype = CointentType.parse(ctypeTxt);
    assert ctype.matches("application", "x-votable+xml");
    assert ctype.getParameter("charset").equals("iso-8859-1");
 

Since:
17 Nov 2017
See Also:
RFC 2045

Constructor Summary
ContentType(java.lang.String type, java.lang.String subtype)
          Constructs a ContentType from type and subtype strings.
ContentType(java.lang.String type, java.lang.String subtype, java.util.Map<java.lang.String,java.lang.String> params)
          Constructs a ContentType from its constituent parts.
 
Method Summary
 boolean equals(java.lang.Object o)
           
 java.lang.String getParameter(java.lang.String paramName)
          Returns the value of a parameter of this content type.
 java.util.Map<java.lang.String,java.lang.String> getParameters()
          Returns the parameter name/value pairs of this content type.
 java.lang.String getSubtype()
          Returns the Subtype part of this content type.
 java.lang.String getType()
          Returns the Type part of this content type.
 int hashCode()
           
static void main(java.lang.String[] args)
          Parses a single content-type string supplied on the command line, and prints a representation of the parsed form on standard output.
 boolean matches(java.lang.String type, java.lang.String subtype)
          Indicates whether the type and subtype match a given pair.
static ContentType parseContentType(java.lang.String txt)
          Parses a Content-Type (MIME type) string in accordance with the syntax rules in RFC2045.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ContentType

public ContentType(java.lang.String type,
                   java.lang.String subtype)
Constructs a ContentType from type and subtype strings. Case is normalised (to lower case).

Parameters:
type - type part
subtype - subtype part

ContentType

public ContentType(java.lang.String type,
                   java.lang.String subtype,
                   java.util.Map<java.lang.String,java.lang.String> params)
Constructs a ContentType from its constituent parts. Case is normalised (to lower case) for the case-insensitive parts, that is type, subtype and parameter names.

Parameters:
type - type part
subtype - subtype part
params - map of parameters
Method Detail

getType

public java.lang.String getType()
Returns the Type part of this content type.

Returns:
type

getSubtype

public java.lang.String getSubtype()
Returns the Subtype part of this content type.

Returns:
subtype

getParameters

public java.util.Map<java.lang.String,java.lang.String> getParameters()
Returns the parameter name/value pairs of this content type. The parameter names (keys of the returned map) will always be in lower case.

Returns:
name/value pairs as an ordered map

matches

public boolean matches(java.lang.String type,
                       java.lang.String subtype)
Indicates whether the type and subtype match a given pair.

Parameters:
type - required type part (case-insensitive)
subtype - required subtype part (case-insensitive)
Returns:
true iff type and subtype match those of this content-type

getParameter

public java.lang.String getParameter(java.lang.String paramName)
Returns the value of a parameter of this content type.

Parameters:
paramName - parameter name (case-insensitive)
Returns:
parameter value, or null if no such parameter

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

parseContentType

public static ContentType parseContentType(java.lang.String txt)
Parses a Content-Type (MIME type) string in accordance with the syntax rules in RFC2045. Such strings look something like "type/subtype[;p1=v1;p2=v2...]". It may not be completely bulletproof, but should do a fairly good job of the parse. However, it makes no attempt to restrict the type or subtype to IANA-approved values, and it may parse some strings which are not strictly legal.

Null is returned if the string cannot be parsed.

Parameters:
txt - content-type string of the approximate form type/subtype(;param=value)*
Returns:
ContentType object if txt can be parsed, otherwise null
See Also:
RFC 2045, sec 5.1, RFC 822, sec 3.3

main

public static void main(java.lang.String[] args)
Parses a single content-type string supplied on the command line, and prints a representation of the parsed form on standard output.