Search in sources :

Example 16 with PasswordProvider

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

the class OfficeParser method parse.

protected void parse(DirectoryNode root, ParseContext context, Metadata metadata, XHTMLContentHandler xhtml) throws IOException, SAXException, TikaException {
    // Parse summary entries first, to make metadata available early
    new SummaryExtractor(metadata).parseSummaries(root);
    // Parse remaining document entries
    POIFSDocumentType type = POIFSDocumentType.detectType(root);
    if (type != POIFSDocumentType.UNKNOWN) {
        setType(metadata, type.getType());
    }
    switch(type) {
        case SOLIDWORKS_PART:
        case SOLIDWORKS_ASSEMBLY:
        case SOLIDWORKS_DRAWING:
            break;
        case PUBLISHER:
            PublisherTextExtractor publisherTextExtractor = new PublisherTextExtractor(root);
            xhtml.element("p", publisherTextExtractor.getText());
            break;
        case WORDDOCUMENT:
            new WordExtractor(context, metadata).parse(root, xhtml);
            break;
        case POWERPOINT:
            new HSLFExtractor(context, metadata).parse(root, xhtml);
            break;
        case WORKBOOK:
        case XLR:
            Locale locale = context.get(Locale.class, Locale.getDefault());
            new ExcelExtractor(context, metadata).parse(root, xhtml, locale);
            break;
        case PROJECT:
            // We currently can't do anything beyond the metadata
            break;
        case VISIO:
            VisioTextExtractor visioTextExtractor = new VisioTextExtractor(root);
            for (String text : visioTextExtractor.getAllText()) {
                xhtml.element("p", text);
            }
            break;
        case OUTLOOK:
            OutlookExtractor extractor = new OutlookExtractor(root, context);
            extractor.parse(xhtml, metadata);
            break;
        case ENCRYPTED:
            EncryptionInfo info = new EncryptionInfo(root);
            Decryptor d = Decryptor.getInstance(info);
            try {
                // By default, use the default Office Password
                String password = Decryptor.DEFAULT_PASSWORD;
                // If they supplied a Password Provider, ask that for the password,
                //  and use the provider given one if available (stick with default if not)
                PasswordProvider passwordProvider = context.get(PasswordProvider.class);
                if (passwordProvider != null) {
                    String suppliedPassword = passwordProvider.getPassword(metadata);
                    if (suppliedPassword != null) {
                        password = suppliedPassword;
                    }
                }
                // Check if we've the right password or not
                if (!d.verifyPassword(password)) {
                    throw new EncryptedDocumentException();
                }
                // Decrypt the OLE2 stream, and delegate the resulting OOXML
                //  file to the regular OOXML parser for normal handling
                OOXMLParser parser = new OOXMLParser();
                parser.parse(d.getDataStream(root), new EmbeddedContentHandler(new BodyContentHandler(xhtml)), metadata, context);
            } catch (GeneralSecurityException ex) {
                throw new EncryptedDocumentException(ex);
            }
        default:
            //  is extracted, which happened above
            break;
    }
}
Also used : Locale(java.util.Locale) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) Decryptor(org.apache.poi.poifs.crypt.Decryptor) EncryptedDocumentException(org.apache.tika.exception.EncryptedDocumentException) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) GeneralSecurityException(java.security.GeneralSecurityException) PublisherTextExtractor(org.apache.poi.hpbf.extractor.PublisherTextExtractor) EmbeddedContentHandler(org.apache.tika.sax.EmbeddedContentHandler) PasswordProvider(org.apache.tika.parser.PasswordProvider) OOXMLParser(org.apache.tika.parser.microsoft.ooxml.OOXMLParser) VisioTextExtractor(org.apache.poi.hdgf.extractor.VisioTextExtractor)

Aggregations

PasswordProvider (org.apache.tika.parser.PasswordProvider)16 Metadata (org.apache.tika.metadata.Metadata)12 Test (org.junit.Test)11 TikaTest (org.apache.tika.TikaTest)10 EncryptedDocumentException (org.apache.tika.exception.EncryptedDocumentException)10 ParseContext (org.apache.tika.parser.ParseContext)10 AutoDetectParser (org.apache.tika.parser.AutoDetectParser)9 Parser (org.apache.tika.parser.Parser)9 InputStream (java.io.InputStream)8 BodyContentHandler (org.apache.tika.sax.BodyContentHandler)7 ContentHandler (org.xml.sax.ContentHandler)6 TikaInputStream (org.apache.tika.io.TikaInputStream)5 HashMap (java.util.HashMap)3 Map (java.util.Map)3 CompositeParser (org.apache.tika.parser.CompositeParser)3 TesseractOCRParser (org.apache.tika.parser.ocr.TesseractOCRParser)3 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)3 IOException (java.io.IOException)2 TikaException (org.apache.tika.exception.TikaException)2 CryptCodecProvider (com.healthmarketscience.jackcess.CryptCodecProvider)1