Search in sources :

Example 96 with AutoDetectParser

use of org.apache.tika.parser.AutoDetectParser in project tika by apache.

the class Seven7ParserTest method testPasswordProtected.

@Test
public void testPasswordProtected() throws Exception {
    Parser parser = new AutoDetectParser();
    ContentHandler handler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    // No password, will fail with EncryptedDocumentException
    boolean ex = false;
    try (InputStream stream = Seven7ParserTest.class.getResourceAsStream("/test-documents/test7Z_protected_passTika.7z")) {
        parser.parse(stream, handler, metadata, recursingContext);
        fail("Shouldn't be able to read a password protected 7z without the password");
    } catch (EncryptedDocumentException e) {
        // Good
        ex = true;
    }
    assertTrue("test no password", ex);
    ex = false;
    // Wrong password currently silently gives no content
    // Ideally we'd like Commons Compress to give an error, but it doesn't...
    recursingContext.set(PasswordProvider.class, new PasswordProvider() {

        @Override
        public String getPassword(Metadata metadata) {
            return "wrong";
        }
    });
    handler = new BodyContentHandler();
    try (InputStream stream = Seven7ParserTest.class.getResourceAsStream("/test-documents/test7Z_protected_passTika.7z")) {
        parser.parse(stream, handler, metadata, recursingContext);
        fail("Shouldn't be able to read a password protected 7z with wrong password");
    } catch (TikaException e) {
        //if JCE is installed, the cause will be: Caused by: org.tukaani.xz.CorruptedInputException: Compressed data is corrupt
        //if JCE is not installed, the message will include
        // "(do you have the JCE  Unlimited Strength Jurisdiction Policy Files installed?")
        ex = true;
    }
    assertTrue("TikaException for bad password", ex);
    // Will be empty
    assertEquals("", handler.toString());
    ex = false;
    // Right password works fine if JCE Unlimited Strength has been installed!!!
    if (isStrongCryptoAvailable()) {
        recursingContext.set(PasswordProvider.class, new PasswordProvider() {

            @Override
            public String getPassword(Metadata metadata) {
                return "Tika";
            }
        });
        handler = new BodyContentHandler();
        try (InputStream stream = Seven7ParserTest.class.getResourceAsStream("/test-documents/test7Z_protected_passTika.7z")) {
            parser.parse(stream, handler, metadata, recursingContext);
        }
        assertEquals(TYPE_7ZIP.toString(), metadata.get(Metadata.CONTENT_TYPE));
        String content = handler.toString();
        // Should get filename
        assertContains("text.txt", content);
        // Should get contents from the text file in the 7z file
        assertContains("TEST DATA FOR TIKA.", content);
        assertContains("This is text inside an encrypted 7zip (7z) file.", content);
        assertContains("It should be processed by Tika just fine!", content);
        assertContains("TIKA-1521", content);
    } else {
        //if jce is not installed, test for IOException wrapped in TikaException
        boolean ioe = false;
        recursingContext.set(PasswordProvider.class, new PasswordProvider() {

            @Override
            public String getPassword(Metadata metadata) {
                return "Tika";
            }
        });
        handler = new BodyContentHandler();
        try (InputStream stream = Seven7ParserTest.class.getResourceAsStream("/test-documents/test7Z_protected_passTika.7z")) {
            parser.parse(stream, handler, metadata, recursingContext);
        } catch (TikaException e) {
            ioe = true;
        }
        assertTrue("IOException because JCE was not installed", ioe);
    }
}
Also used : BodyContentHandler(org.apache.tika.sax.BodyContentHandler) EncryptedDocumentException(org.apache.tika.exception.EncryptedDocumentException) TikaException(org.apache.tika.exception.TikaException) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) PasswordProvider(org.apache.tika.parser.PasswordProvider) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ContentHandler(org.xml.sax.ContentHandler) Parser(org.apache.tika.parser.Parser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) Test(org.junit.Test)

Example 97 with AutoDetectParser

use of org.apache.tika.parser.AutoDetectParser in project tika by apache.

the class TarParserTest method testEmbedded.

/**
     * Tests that the ParseContext parser is correctly
     *  fired for all the embedded entries.
     */
@Test
public void testEmbedded() throws Exception {
    // Should auto-detect!
    Parser parser = new AutoDetectParser();
    ContentHandler handler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    try (InputStream stream = ZipParserTest.class.getResourceAsStream("/test-documents/test-documents.tar")) {
        parser.parse(stream, handler, metadata, trackingContext);
    }
    // Should have found all 9 documents, but not the directory
    assertEquals(9, tracker.filenames.size());
    assertEquals(9, tracker.mediatypes.size());
    assertEquals(9, tracker.modifiedAts.size());
    // Should have names but not content types, as tar doesn't
    //  store the content types
    assertEquals("test-documents/testEXCEL.xls", tracker.filenames.get(0));
    assertEquals("test-documents/testHTML.html", tracker.filenames.get(1));
    assertEquals("test-documents/testOpenOffice2.odt", tracker.filenames.get(2));
    assertEquals("test-documents/testPDF.pdf", tracker.filenames.get(3));
    assertEquals("test-documents/testPPT.ppt", tracker.filenames.get(4));
    assertEquals("test-documents/testRTF.rtf", tracker.filenames.get(5));
    assertEquals("test-documents/testTXT.txt", tracker.filenames.get(6));
    assertEquals("test-documents/testWORD.doc", tracker.filenames.get(7));
    assertEquals("test-documents/testXML.xml", tracker.filenames.get(8));
    for (String type : tracker.mediatypes) {
        assertNull(type);
    }
    for (String crt : tracker.createdAts) {
        assertNull(crt);
    }
    for (String mod : tracker.modifiedAts) {
        assertNotNull(mod);
        assertTrue("Modified at " + mod, mod.startsWith("20"));
    }
}
Also used : BodyContentHandler(org.apache.tika.sax.BodyContentHandler) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ContentHandler(org.xml.sax.ContentHandler) Parser(org.apache.tika.parser.Parser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) Test(org.junit.Test)

Example 98 with AutoDetectParser

use of org.apache.tika.parser.AutoDetectParser in project tika by apache.

the class ZipParserTest method testZipParsing.

@Test
public void testZipParsing() throws Exception {
    // Should auto-detect!
    Parser parser = new AutoDetectParser();
    ContentHandler handler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    try (InputStream stream = ZipParserTest.class.getResourceAsStream("/test-documents/test-documents.zip")) {
        parser.parse(stream, handler, metadata, recursingContext);
    }
    assertEquals("application/zip", metadata.get(Metadata.CONTENT_TYPE));
    String content = handler.toString();
    assertContains("testEXCEL.xls", content);
    assertContains("Sample Excel Worksheet", content);
    assertContains("testHTML.html", content);
    assertContains("Test Indexation Html", content);
    assertContains("testOpenOffice2.odt", content);
    assertContains("This is a sample Open Office document", content);
    assertContains("testPDF.pdf", content);
    assertContains("Apache Tika", content);
    assertContains("testPPT.ppt", content);
    assertContains("Sample Powerpoint Slide", content);
    assertContains("testRTF.rtf", content);
    assertContains("indexation Word", content);
    assertContains("testTXT.txt", content);
    assertContains("Test d'indexation de Txt", content);
    assertContains("testWORD.doc", content);
    assertContains("This is a sample Microsoft Word Document", content);
    assertContains("testXML.xml", content);
    assertContains("Rida Benjelloun", content);
}
Also used : BodyContentHandler(org.apache.tika.sax.BodyContentHandler) TikaInputStream(org.apache.tika.io.TikaInputStream) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ContentHandler(org.xml.sax.ContentHandler) Parser(org.apache.tika.parser.Parser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) Test(org.junit.Test)

Example 99 with AutoDetectParser

use of org.apache.tika.parser.AutoDetectParser in project tika by apache.

the class ZipParserTest method testEmbedded.

/**
     * Tests that the ParseContext parser is correctly
     *  fired for all the embedded entries.
     */
@Test
public void testEmbedded() throws Exception {
    // Should auto-detect!
    Parser parser = new AutoDetectParser();
    ContentHandler handler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    try (InputStream stream = ZipParserTest.class.getResourceAsStream("/test-documents/test-documents.zip")) {
        parser.parse(stream, handler, metadata, trackingContext);
    }
    // Should have found all 9 documents
    assertEquals(9, tracker.filenames.size());
    assertEquals(9, tracker.mediatypes.size());
    assertEquals(9, tracker.modifiedAts.size());
    // Should have names and modified dates, but not content types, 
    //  as zip doesn't store the content types
    assertEquals("testEXCEL.xls", tracker.filenames.get(0));
    assertEquals("testHTML.html", tracker.filenames.get(1));
    assertEquals("testOpenOffice2.odt", tracker.filenames.get(2));
    assertEquals("testPDF.pdf", tracker.filenames.get(3));
    assertEquals("testPPT.ppt", tracker.filenames.get(4));
    assertEquals("testRTF.rtf", tracker.filenames.get(5));
    assertEquals("testTXT.txt", tracker.filenames.get(6));
    assertEquals("testWORD.doc", tracker.filenames.get(7));
    assertEquals("testXML.xml", tracker.filenames.get(8));
    for (String type : tracker.mediatypes) {
        assertNull(type);
    }
    for (String crt : tracker.createdAts) {
        assertNull(crt);
    }
    for (String mod : tracker.modifiedAts) {
        assertNotNull(mod);
        assertTrue("Modified at " + mod, mod.startsWith("20"));
    }
}
Also used : BodyContentHandler(org.apache.tika.sax.BodyContentHandler) TikaInputStream(org.apache.tika.io.TikaInputStream) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ContentHandler(org.xml.sax.ContentHandler) Parser(org.apache.tika.parser.Parser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) Test(org.junit.Test)

Example 100 with AutoDetectParser

use of org.apache.tika.parser.AutoDetectParser in project tika by apache.

the class ZipParserTest method testPlaceholders.

// TIKA-1036
@Test
public void testPlaceholders() throws Exception {
    String xml = getXML("testEmbedded.zip").xml;
    assertContains("<div class=\"embedded\" id=\"test1.txt\" />", xml);
    assertContains("<div class=\"embedded\" id=\"test2.txt\" />", xml);
    // Also make sure EMBEDDED_RELATIONSHIP_ID was
    // passed when parsing the embedded docs:
    Parser parser = new AutoDetectParser();
    ParseContext context = new ParseContext();
    context.set(Parser.class, parser);
    GatherRelIDsDocumentExtractor relIDs = new GatherRelIDsDocumentExtractor();
    context.set(EmbeddedDocumentExtractor.class, relIDs);
    try (InputStream input = getResourceAsStream("/test-documents/testEmbedded.zip")) {
        parser.parse(input, new BodyContentHandler(), new Metadata(), context);
    }
    assertTrue(relIDs.allRelIDs.contains("test1.txt"));
    assertTrue(relIDs.allRelIDs.contains("test2.txt"));
}
Also used : BodyContentHandler(org.apache.tika.sax.BodyContentHandler) TikaInputStream(org.apache.tika.io.TikaInputStream) InputStream(java.io.InputStream) ParseContext(org.apache.tika.parser.ParseContext) Metadata(org.apache.tika.metadata.Metadata) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) Parser(org.apache.tika.parser.Parser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) Test(org.junit.Test)

Aggregations

AutoDetectParser (org.apache.tika.parser.AutoDetectParser)164 Metadata (org.apache.tika.metadata.Metadata)136 Test (org.junit.Test)122 InputStream (java.io.InputStream)117 Parser (org.apache.tika.parser.Parser)111 ParseContext (org.apache.tika.parser.ParseContext)103 BodyContentHandler (org.apache.tika.sax.BodyContentHandler)96 ContentHandler (org.xml.sax.ContentHandler)91 TikaTest (org.apache.tika.TikaTest)82 TikaInputStream (org.apache.tika.io.TikaInputStream)63 ByteArrayInputStream (java.io.ByteArrayInputStream)34 CompositeParser (org.apache.tika.parser.CompositeParser)28 TikaConfig (org.apache.tika.config.TikaConfig)18 ExcelParserTest (org.apache.tika.parser.microsoft.ExcelParserTest)17 WordParserTest (org.apache.tika.parser.microsoft.WordParserTest)17 TesseractOCRParser (org.apache.tika.parser.ocr.TesseractOCRParser)15 EmptyParser (org.apache.tika.parser.EmptyParser)13 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)13 DefaultHandler (org.xml.sax.helpers.DefaultHandler)12 TikaException (org.apache.tika.exception.TikaException)11