Package com.norconex.commons.lang.file
Class ContentType
- java.lang.Object
-
- com.norconex.commons.lang.file.ContentType
-
- All Implemented Interfaces:
Serializable
public final class ContentType extends Object implements Serializable
Represents a file Content-Type (also called MIME-Type or Media Type).
To detect the content type of a file, consider using an open-source library such as Apache Tika.
To provide your own extension mappings or display names, copy the appropriate.propertiesfile to your classpath root, with the word "custom" inserted:ContentType-custom-[...]. The actual custom names and classpath location are:
Original Custom com.norconex.commmons.lang.file.ContentType-extensions.properties ContentType-custom-extensions.properties com.norconex.commmons.lang.file.ContentType-name[_locale].properties ContentType-custom-name[_locale].propertiess - Since:
- 1.4.0
- Author:
- Pascal Essiembre
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static ContentTypeBMPstatic ContentTypeCSVstatic ContentTypeGIFstatic ContentTypeHTMLstatic ContentTypeJPEGstatic ContentTypePDFstatic ContentTypePNGstatic ContentTypeTEXTstatic ContentTypeTSVstatic ContentTypeXMLstatic ContentTypeZIP
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object obj)ContentFamilygetContentFamily()StringgetDisplayName()Gets a name for the content type suitable for display to a user.StringgetDisplayName(Locale locale)Gets a name for the content type suitable for display to a user.StringgetExtension()Gets the file extension usually associated with this content type.String[]getExtensions()Gets the file extensions usually associated with this content type.inthashCode()booleanmatches(String contentType)Whether the given string matches this content type.ContentTypetoBaseType()Returns a content-type without any parameters (removes ";" and any values afterwards).StringtoBaseTypeString()Returns the raw content-type representation without any parameters (removes ";" and any values afterwards).StringtoString()Returns the raw content-type representation.static ContentTypevalueOf(String contentType)Creates a new content type.static ContentType[]valuesOf(String... contentTypes)Creates a null-safe array of content types.static List<ContentType>valuesOf(List<String> contentTypes)Creates a null-safe array of content types.
-
-
-
Field Detail
-
TEXT
public static final ContentType TEXT
-
HTML
public static final ContentType HTML
-
PDF
public static final ContentType PDF
-
XML
public static final ContentType XML
-
CSV
public static final ContentType CSV
-
TSV
public static final ContentType TSV
-
ZIP
public static final ContentType ZIP
- Since:
- 1.14.0
-
JPEG
public static final ContentType JPEG
-
GIF
public static final ContentType GIF
-
BMP
public static final ContentType BMP
-
PNG
public static final ContentType PNG
-
-
Method Detail
-
valueOf
public static ContentType valueOf(String contentType)
Creates a new content type. Returns an existing instance if the same content type is requested more than once.- Parameters:
contentType- the official media type name- Returns:
- content type instance or
nullif content type string isnullor blank.
-
valuesOf
public static ContentType[] valuesOf(String... contentTypes)
Creates a null-safe array of content types. The same number of elements as the supplied strings are returned. Anullvalue will return an empty array. Each content types are individually obtained by invokingvalueOf(String).- Parameters:
contentTypes- the official media type names- Returns:
- content type array.
-
valuesOf
public static List<ContentType> valuesOf(List<String> contentTypes)
Creates a null-safe array of content types. The same number of elements as the supplied strings are returned. Anullvalue will return an empty array. Each content types are individually obtained by invokingvalueOf(String).- Parameters:
contentTypes- the official media type names- Returns:
- content type array.
- Since:
- 2.0.0
-
getDisplayName
public String getDisplayName()
Gets a name for the content type suitable for display to a user. The system locale is used to defined the language of the display name. If no name has been defined for a content type, the raw content type is returned (equivalent totoString()).- Returns:
- display name
-
getDisplayName
public String getDisplayName(Locale locale)
Gets a name for the content type suitable for display to a user. If the locale isnull, the system locale is used. If no name has been defined for a content type with the provided locale, the name defaults to English. If no name has been defined for any locale, the raw content type is returned (equivalent totoString()).- Parameters:
locale- the locale to use to get the display name- Returns:
- display name
-
getContentFamily
public ContentFamily getContentFamily()
-
getExtension
public String getExtension()
Gets the file extension usually associated with this content type. If the content type has more than one extension, the first one is returned.- Returns:
- file extension or empty string if no extension is defined
-
getExtensions
public String[] getExtensions()
Gets the file extensions usually associated with this content type. Most content types only have one commonly used extension.- Returns:
- file extension or empty array if no extension is defined
-
matches
public boolean matches(String contentType)
Whether the given string matches this content type.- Parameters:
contentType- the content type- Returns:
trueif the given string matches this content type
-
toString
public String toString()
Returns the raw content-type representation.
-
toBaseTypeString
public String toBaseTypeString()
Returns the raw content-type representation without any parameters (removes ";" and any values afterwards).- Returns:
- content type as string without parameters
- Since:
- 1.14.0
-
toBaseType
public ContentType toBaseType()
Returns a content-type without any parameters (removes ";" and any values afterwards). Invoking a content type without parameters will return itself.- Returns:
- content type without parameters
- Since:
- 1.14.0
-
-