OpenOfficeParser extends FileParser { @Override protected final ParseResult parse( @NotNull File file, @NotNull ParseContext context) throws ParseException { OpenDocumentParser tikaParser = new OpenDocumentParser(); BodyContentHandler contentHandler = new BodyContentHandler(-1); Metadata metadata = new Metadata(); TikaInputStream in = null; try { in = TikaInputStream.get(Paths.get(file.getPath())); tikaParser.parse(in, contentHandler, metadata, ParseService.tikaContext()); ParseResult result = new ParseResult(contentHandler.toString()) .setTitle(metadata.get(TikaCoreProperties.TITLE)) .addAuthor(metadata.get(TikaCoreProperties.CREATOR)) .addMiscMetadata(metadata.get(TikaCoreProperties.DESCRIPTION)) .addMiscMetadata(metadata.get(TikaCoreProperties.TRANSITION_SUBJECT_TO_OO_SUBJECT)) .addMiscMetadata(metadata.get(TikaCoreProperties.KEYWORDS)); for (String name : metadata.names()) { if (name.startsWith("custom:")) { result.addMiscMetadata(metadata.get(name)); } } return result; } catch (CharConversionException e) { if (matchesPasswordError(e)) { throw new ParseException(Msg.doc_pw_protected.get()); } else { throw new ParseException(e); } } catch (IOException | SAXException | TikaException e) { throw new ParseException(e); } catch (RuntimeException e) { throw new ParseException(e); } finally { Closeables.closeQuietly(in); } } private  OpenOfficeParser(@NotNull String typeLabel, @NotNull String... extensions); final String getTypeLabel();  }
@Test public void testPasswordProtected() throws Exception { FileParser parser = new OpenOfficeWriterParser(); File file = TestFiles.encrypted_odt.get(); try { parser.parse(file, new ParseContext(file.getName())); } catch (ParseException e) { if (e.getMessage().equals(Msg.doc_pw_protected.get())) { return; } } throw new IllegalStateException(); }
@Test public void testNotPasswordProtected() throws Exception { FileParser parser = new OpenOfficeWriterParser(); File[] files = new File[] { TestFiles.lorem_ipsum_odt.get(), new TFile(TestFiles.lorem_ipsum_odt.get()) }; for (File file : files) { parser.parse(file, new ParseContext(file.getName())); } }
@Test public void testMetaXmlMissing() throws Exception { FileParser parser = new OpenOfficeWriterParser(); File file = TestFiles.missing_meta_xml_entry.get(); parser.parse(file, new ParseContext(file.getName())); }