/** * * @return parsed media type or <code>null</code> if parse fails or if media type string was * not specified */ public MediaType getMediaType() { if (rawMediaTypeString != null) { return MediaType.parse(rawMediaTypeString); } return null; }
public static MediaType video(String type) { return MediaType.parse("video/" + type); }
public static MediaType application(String type) { return MediaType.parse("application/" + type); }
public static MediaType audio(String type) { return MediaType.parse("audio/" + type); }
public static MediaType text(String type) { return MediaType.parse("text/" + type); }
public static MediaType image(String type) { return MediaType.parse("image/" + type); }
/** * Returns the base form of the MediaType, excluding * any parameters, such as "text/plain" for * "text/plain; charset=utf-8" */ public MediaType getBaseType() { if (parameters.isEmpty()) { return this; } else { return MediaType.parse(string.substring(0, semicolon)); } }
public MediaType detect(InputStream input, Metadata metadata) { String type = metadata.get("my-custom-type-override"); if (type != null) { return MediaType.parse(type); } else { return MediaType.OCTET_STREAM; } } };
private Renderer getRenderer(String mimeType) { MediaType mt = MediaType.parse(mimeType); String type = TYPES_TO_RENDERER.get(mt); if (type == null) { throw new RuntimeException("unparseable content type " + mimeType); } return XhtmlRendererFactory.getRenderer(type); }
@Override public MediaType detect(InputStream input, Metadata metadata) throws IOException { String type = metadata.get(TikaCoreProperties.CONTENT_TYPE_OVERRIDE); if (type == null) { return MediaType.OCTET_STREAM; } else { return MediaType.parse(type); } } }
/** * Check if there is a converter available which allows to convert the Tika metadata to XMP * * @param mimetype * the Mimetype * @return true if the Metadata object can be converted or false if not */ public static boolean isConverterAvailable(String mimetype) { MediaType type = MediaType.parse( mimetype ); if (type != null) { return (getConverterMap().get( type ) != null); } return false; }
private static Set<MediaType> readMimeTypes(Element mimeTypes) { Set<MediaType> types = new HashSet<MediaType>(); NodeList children = mimeTypes.getChildNodes(); for(int i=0; i<children.getLength(); i++) { Node node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element child = (Element) node; if (child.getTagName().equals(MIMETYPE_TAG)) { types.add( MediaType.parse( getString(child) ) ); } } } return types; }
public static void describeMediaType() { MediaType type = MediaType.parse("text/plain; charset=UTF-8"); System.out.println("type: " + type.getType()); System.out.println("subtype: " + type.getSubtype()); Map<String, String> parameters = type.getParameters(); System.out.println("parameters:"); for (String name : parameters.keySet()) { System.out.println(" " + name + "=" + parameters.get(name)); } }
public static void main(String[] args) throws Exception { MediaTypeRegistry registry = MediaTypeRegistry.getDefaultRegistry(); MediaType type = MediaType.parse("image/svg+xml"); while (type != null) { System.out.println(type); type = registry.getSupertype(type); } } }
/** * Detects AutoCAD formats that live in OPC packaging */ private static MediaType detectAutoCADOPC(OPCPackage pkg) { PackageRelationshipCollection dwfxSeq = pkg.getRelationshipsByType("http://schemas.autodesk.com/dwfx/2007/relationships/documentsequence"); if (dwfxSeq.size() == 1) { return MediaType.parse("model/vnd.dwfx+xps"); } else { // Non-AutoCAD Package received return null; } }
/** * Parses and normalises the given media type string and checks whether * the result equals the given base type or is a specialization of it. * The given base type should already be normalised. * * @since Apache Tika 1.2 * @param a media type * @param b base type, normalised * @return <code>true</code> if b equals a or is a specialization of it, * <code>false</code> otherwise */ public boolean isInstanceOf(String a, MediaType b) { return isInstanceOf(normalize(MediaType.parse(a)), b); }
public UniversalEncodingListener(Metadata metadata) { MediaType type = MediaType.parse(metadata.get(Metadata.CONTENT_TYPE)); if (type != null) { hint = type.getParameters().get("charset"); } if (hint == null) { hint = metadata.get(Metadata.CONTENT_ENCODING); } }
/** * Extracts a charset from a Content-Type HTTP header. * * @param metadata parser metadata * @return a charset if there is one specified, or null */ private static Charset charsetFromContentType(Metadata metadata) { String contentType = metadata.get(Metadata.CONTENT_TYPE); MediaType mediatype = MediaType.parse(contentType); if (mediatype == null) return null; String charsetLabel = mediatype.getParameters().get("charset"); return getCharsetByLabel(charsetLabel); }
/** * @since TIKA-121 */ @Test public void testParseNoParams() { String mimeStringNoParams = "text/html"; MediaType type = MediaType.parse(mimeStringNoParams); assertNotNull(type); assertNotNull(type.getParameters()); assertNotNull(type.getParameters().keySet()); assertEquals(0, type.getParameters().keySet().size()); }
/** * @since TIKA-121 */ @Test public void testParseNoParamsWithSemi() { String mimeStringNoParamsWithSemi = "text/html;"; MediaType type = MediaType.parse(mimeStringNoParamsWithSemi); assertNotNull(type); assertNotNull(type.getParameters()); assertNotNull(type.getParameters().keySet()); assertEquals(0, type.getParameters().keySet().size()); }