public final class MimePath extends Object
In order to fully understand the scale of the problem the mime path is trying to describe you should consider two things. First a document can contain several different embedded fragments each of a different mime type. Second, each embeded fragment itself can possibly contain one or more other embedded fragments and this nesting can in theory go indefinitely deep.
In reality the nesting probably will not be very deep. As an example of a document containing an embedded fragment of another document of the different mime type you could imagine a JSP page containing a Java scriplet. The main document is the JSP page of the 'text/x-jsp' mime type, which includes a fragment of Java source code of the 'text/x-java' mime type.
The mime path comes handy when we want to distinguish between the ordinary 'text/x-java' mime type and the 'text/x-java' mime type embedded in the JSP page, because both of those 'text/x-java' mime types will have a different mime path. The ordinary 'text/x-java' mime type has a mime path consisting of just one mime type - 'text/x-java'. The 'text/x-java' mime type embeded in the JSP page, however, has a mime path comprised from two mime types 'text/x-jsp' and 'text/x-java'. The order of mime types in a mime path is obviously very important, because it describes how the mime types are embedded.
The mime path can be represented as a String
simply by
concatenating all its mime types separated by the '/' character. Since
mime types always contain one and only one '/' character it is clear which
'/' character belongs to a mime type and which is the mime path separator.
In the above example the mime path of the 'text/x-java' mime type embedded in the 'text/x-jsp' mime type can be represented as 'text/x-jsp/text/x-java'.
For some languages it is not uncommon to allow embedding of itself. For example
in Ruby it is allowed to use Ruby code within strings and Ruby will
evaluate this code when evaluating the value of the strings. Depending on the
implementation of a lexer there can be tokens with MimePath
that
contains several consecutive mime types that are the same.
The format of a valid mime type string is described in
RFC 4288.
MimePath
performs internall checks according to this specification.
Identity: By definition two MimePath
instances are equal
if they represent the same string mime path. The implementation guarantees
that by caching and reusing instances of the MimePath
that it
creates. The MimePath
instances can be used as keys in maps.
Lifecycle: Although the instances of MimePath
are
internally cached and should survive for certain time without being referenced
from outside of the MimePath API, clients are strongly encouraged to hold
a reference to the MimePath
they obtained throughout the whole
lifecycle of their component. For example an opened java editor with a document
should keep its instance of the 'text/x-java' MimePath
for the
whole time the editor is open.
MimeLookup
,
RFC 4288Modifier and Type | Field and Description |
---|---|
static MimePath |
EMPTY
The root of all mime paths.
|
Modifier and Type | Method and Description |
---|---|
static MimePath |
get(MimePath prefix,
String mimeType)
Gets the mime path corresponding to a mime type embedded in another
mime type.
|
static MimePath |
get(String mimeType)
Gets the mime path for the given mime type.
|
List<MimePath> |
getIncludedPaths()
Returns the included Mime paths.
|
String |
getInheritedType()
Returns the inherited Mime type.
|
String |
getMimeType(int index)
Get mime type of this mime-path at the given index.
|
String |
getPath()
Get string path represented by this mime-path.
|
MimePath |
getPrefix(int size)
Return prefix mime-path with the given number of mime-type components
ranging from zero till the size of this mime-path.
|
static MimePath |
parse(String path)
Parses a mime path string and returns its
MimePath representation. |
int |
size()
Get total number of mime-types in the mime-path.
|
String |
toString() |
static boolean |
validate(CharSequence path)
Validates a path to check if it's a valid mime path.
|
static boolean |
validate(CharSequence type,
CharSequence subtype)
Validates components of a mime type.
|
public static final MimePath EMPTY
public static MimePath get(String mimeType)
MimePath
will contain exactly one element and it will be the mime type passed in
as the parameter.mimeType
- The mime type to get the mime path for. If null
or empty string is passed in the EMPTY
mime path will be
returned.MimePath
for the given mime type or
MimePath.EMPTY
if the mime type is null
or empty
string.public static MimePath get(MimePath prefix, String mimeType)
prefix
parameter.
For example for a java scriplet embedded in a jsp page the prefix
would
be the mime path 'text/x-jsp' and mimeType
would be 'text/x-java'.
The method will return the 'text/x-jsp/text/x-java' mime path.
prefix
- The mime path determining the mime type that embedds the mime
type passed in in the second parameter. It can be MimePath.EMPTY
in which
case the call will be equivalent to calling get(mimeType)
method.mimeType
- The mime type that is embedded in the mime type determined
by the prefix
mime path.public static MimePath parse(String path)
MimePath
representation.
The format of a mime path string representation is a string of mime type components comprising the mime path separated by the '/' character. For example a mime path representing the 'text/x-java' mime type embedded in the 'text/x-jsp' mime type can be represented as the following string - 'text/x-jsp/text/x-java'.
The mime path string can be an empty string, which represents the
MimePath.EMPTY
mime path. By definition all valid mime paths except of
the empty one have to contain odd number of '/' characters.
path
- The mime path string representation.public static boolean validate(CharSequence type, CharSequence subtype)
type
- The type component of a mime type to validate. If null
the type component will not be validated.subtype
- The subtype component of a mime type to validate. If null
the subtype component will not be validated.true
if non-null
components passed in
are valid mime type components, otherwise false
.public static boolean validate(CharSequence path)
true
the path is a valid mime path string and can
be used in the MimePath.parse()
method.path
- The path string to validate.true
if the path string is a valid mime path.public String getPath()
"text/x-jsp/text/x-java"
.public int size()
MimePath.EMPTY
mime-path has zero size.
"text/x-jsp/text/x-java"
has size 2.public String getMimeType(int index)
"text/x-jsp/text/x-java"
getMimeType(0)
returns "text/x-jsp"
and getMimeType(1)
returns "text/x-java"
.index
- >=0 && < MimePath.size()
.IndexOutOfBoundsException
- in case the index is not within
required bounds.public MimePath getPrefix(int size)
size
- >=0 && <= MimePath.size()
.
MimePath.EMPTY
will be returned.
size()
this
will be returned.IndexOutOfBoundsException
- in case the index is not within
required bounds.public String getInheritedType()
MimePath.EMPTY
, returns null
. For most other mime types, returns
""
. If the mime type derives from another one, such as text/ant+xml derives
from xml, the return value will be the base mime type (text/xml in the example case).
For MimePaths that identified embedded content (more components on the MimePath), the method returns the parent MIME of the last MIME type on the path
null
, if no parent exists (for MimePath.EMPTY
)public List<MimePath> getIncludedPaths()
If a MIME type on the path has a generic MIME type (i.e. text/x-ant+xml has a generic MIME type text/xml), that generic type will be inserted. For example, for text/java/text/x-ant+xml/text/javascript, the result will list:
MimePath.EMPTY
MimePaths, the list contains at least one entry, and the last
entry is the MimePath.EMPTY
. Note also, that the complete MimePath is always returned
as the 1st member of the list.
The returned sequence of MimePaths is suitable for searching settings or services
for the (embedded) content whose type is described by MimePath as it is ordered from the
most specific to the least specific paths (including generalization) and always contains
the mime type of the identified contents. The last component (""
) represents
default settings (services).
Note that for MimePaths created from a mime type (not empty!) string, the
getInheritedPaths().get(1)
is a parent mime type. Either empty,
or the generalized MIME.
The caller should not modify the returned List.