Search in sources :

Example 1 with DatabaseBuilder

use of com.healthmarketscience.jackcess.DatabaseBuilder in project tika by apache.

the class JackcessParser method parse.

@Override
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    TikaInputStream tis = TikaInputStream.get(stream);
    Database db = null;
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();
    String password = null;
    PasswordProvider passwordProvider = context.get(PasswordProvider.class);
    if (passwordProvider != null) {
        password = passwordProvider.getPassword(metadata);
    }
    try {
        if (password == null) {
            //do this to ensure encryption/wrong password exception vs. more generic
            //"need right codec" error message.
            db = new DatabaseBuilder(tis.getFile()).setCodecProvider(new CryptCodecProvider()).setReadOnly(true).open();
        } else {
            db = new DatabaseBuilder(tis.getFile()).setCodecProvider(new CryptCodecProvider(password)).setReadOnly(true).open();
        }
        //just in case
        db.setLinkResolver(IGNORE_LINK_RESOLVER);
        JackcessExtractor ex = new JackcessExtractor(metadata, context, locale);
        ex.parse(db, xhtml);
    } catch (IllegalStateException e) {
        if (e.getMessage() != null && e.getMessage().contains("Incorrect password")) {
            throw new EncryptedDocumentException(e);
        }
        throw e;
    } finally {
        if (db != null) {
            try {
                db.close();
            } catch (IOException e) {
            //swallow = silent close
            }
        }
    }
    xhtml.endDocument();
}
Also used : DatabaseBuilder(com.healthmarketscience.jackcess.DatabaseBuilder) CryptCodecProvider(com.healthmarketscience.jackcess.CryptCodecProvider) EncryptedDocumentException(org.apache.tika.exception.EncryptedDocumentException) Database(com.healthmarketscience.jackcess.Database) TikaInputStream(org.apache.tika.io.TikaInputStream) IOException(java.io.IOException) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) PasswordProvider(org.apache.tika.parser.PasswordProvider)

Example 2 with DatabaseBuilder

use of com.healthmarketscience.jackcess.DatabaseBuilder in project tdi-studio-se by Talend.

the class CryptCodecOpener method open.

public Database open(File fl, String pwd) throws IOException {
    DatabaseBuilder dbd = new DatabaseBuilder(fl);
    //dbd.setAutoSync(false);
    dbd.setCodecProvider(new CryptCodecProvider(pwd));
    dbd.setReadOnly(false);
    return dbd.open();
}
Also used : DatabaseBuilder(com.healthmarketscience.jackcess.DatabaseBuilder) CryptCodecProvider(com.healthmarketscience.jackcess.CryptCodecProvider)

Aggregations

CryptCodecProvider (com.healthmarketscience.jackcess.CryptCodecProvider)2 DatabaseBuilder (com.healthmarketscience.jackcess.DatabaseBuilder)2 Database (com.healthmarketscience.jackcess.Database)1 IOException (java.io.IOException)1 EncryptedDocumentException (org.apache.tika.exception.EncryptedDocumentException)1 TikaInputStream (org.apache.tika.io.TikaInputStream)1 PasswordProvider (org.apache.tika.parser.PasswordProvider)1 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)1