Package org.apache.tika.mime

Examples of org.apache.tika.mime.MediaType


                if (inputDoc.getContent() != null) {
                    Metadata meta = new Metadata();
                    meta.set(Metadata.RESOURCE_NAME_KEY, inputDoc.getUrl());
                    MimeType mimetype = null;
                    try {
                        MediaType mediaType = detector
                                .detect(new ByteArrayInputStream(inputDoc
                                        .getContent()), meta);
                        mimetype = mimetypes.forName(mediaType.getType() + "/"
                                + mediaType.getSubtype());
                    } catch (IOException e) {
                        LOG.error("Exception", e);
                    } catch (MimeTypeException e) {
                        LOG.error("Exception", e);
                    }
View Full Code Here


        if (!stream.markSupported()) {
            stream = new BufferedInputStream(stream);
        }

        // Automatically detect the MIME type of the document
        MediaType type = types.detect(stream, metadata);
        metadata.set(Metadata.CONTENT_TYPE, type.toString());

        // Parse the document
        super.parse(stream, handler, metadata);
    }
View Full Code Here

* Test cases for the {@link MagicDetector} class.
*/
public class MagicDetectorTest extends TestCase {

    public void testDetectNull() throws Exception {
        MediaType html = new MediaType("text", "html");
        Detector detector = new MagicDetector(html, "<html".getBytes("ASCII"));
        assertEquals(
                MediaType.OCTET_STREAM,
                detector.detect(null, new Metadata()));
    }
View Full Code Here

                MediaType.OCTET_STREAM,
                detector.detect(null, new Metadata()));
    }

    public void testDetectSimple() throws Exception {
        MediaType html = new MediaType("text", "html");
        Detector detector = new MagicDetector(html, "<html".getBytes("ASCII"));

        assertDetect(detector, html, "<html");
        assertDetect(detector, html, "<html><head/><body/></html>");
        assertDetect(detector, MediaType.OCTET_STREAM, "<HTML");
View Full Code Here

        assertDetect(detector, MediaType.OCTET_STREAM, " <html");
        assertDetect(detector, MediaType.OCTET_STREAM, "");
    }

    public void testDetectOffsetRange() throws Exception {
        MediaType html = new MediaType("text", "html");
        Detector detector = new MagicDetector(
                html, "<html".getBytes("ASCII"), null, 0, 64);

        assertDetect(detector, html, "<html");
        assertDetect(detector, html, "<html><head/><body/></html>");
View Full Code Here

        assertDetect(detector, MediaType.OCTET_STREAM, "");
}

    public void testDetectMask() throws Exception {
        MediaType html = new MediaType("text", "html");
        byte up = (byte) 0xdf;
        Detector detector = new MagicDetector(
                html,
                new byte[] { '<''H''T''M''L' },
                new byte[] { (byte) 0xff, up, up, up, up },
View Full Code Here

        this.detectors = detectors;
    }

    public MediaType detect(InputStream input, Metadata metadata)
            throws IOException {
        MediaType type = MediaType.OCTET_STREAM;
        for (Detector detector : detectors) {
            MediaType detected = detector.detect(input, metadata);
            if (detected.isSpecializationOf(type)) {
                type = detected;
            }
        }
        return type;
    }
View Full Code Here

     */
    public MediaType detect(InputStream input, Metadata metadata) {
        // Look for a type hint in the input metadata
        String hint = metadata.get(Metadata.CONTENT_TYPE);
        if (hint != null) {
            MediaType type = MediaType.parse(hint);
            if (type != null) {
                return type;
            }
        }
        return MediaType.OCTET_STREAM;
View Full Code Here

        checkArgument(representations != null && representations.length > 0);
        int maxIndex = 0;
        double maxQ = 0.0;
        for (int i = 0; i < representations.length; i++) {
            double q = 0.0;
            MediaType type = registry.normalize(representations[i].getType());
            for (MediaRange range : ranges) {
                q = Math.max(q, range.match(type, registry));
            }
            if (q > maxQ) {
                maxIndex = i;
View Full Code Here

    private final MediaType type;

    private final double q;

    public static MediaRange parse(String range, MediaTypeRegistry registry) {
        MediaType type = MediaType.parse(range);
        if (type == null) {
            return null;
        }
        type = registry.normalize(type);

        Map<String, String> parameters =
                new HashMap<String, String>(type.getParameters());
        String q = parameters.remove("q");
        if (q != null) {
            try {
                return new MediaRange(
                        new MediaType(type.getBaseType(), parameters),
                        Double.parseDouble(q));
            } catch (NumberFormatException e) {
                return null;
            }
        }
View Full Code Here

TOP

Related Classes of org.apache.tika.mime.MediaType

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.