OOXMLParser extends AbstractOfficeParser { public void parse( InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { configure(context); OOXMLExtractorFactory.parse(stream, handler, metadata, context); }  Set<MediaType> getSupportedTypes(ParseContext context); void parse(
            InputStream stream, ContentHandler handler,
            Metadata metadata, ParseContext context);  }
@Test public void testPowerPointMetadataEarly() throws Exception { String[] extensions = new String[]{ "pptx", "pptm", "ppsm", "ppsx", "potm" }; final String[] mimeTypes = new String[]{ "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.ms-powerpoint.presentation.macroenabled.12", "application/vnd.ms-powerpoint.slideshow.macroenabled.12", "application/vnd.openxmlformats-officedocument.presentationml.slideshow", "application/vnd.ms-powerpoint.template.macroenabled.12" }; for (int i = 0; i < extensions.length; i++) { String extension = extensions[i]; final String filename = "testPPT." + extension; final Metadata metadata = new Metadata(); final int currentI = i; ContentHandler handler = new BodyContentHandler() { public void startDocument() { assertEquals( "Mime-type checking for " + filename, mimeTypes[currentI], metadata.get(Metadata.CONTENT_TYPE)); assertEquals("Attachment Test", metadata.get(TikaCoreProperties.TITLE)); assertEquals("Rajiv", metadata.get(TikaCoreProperties.CREATOR)); } }; ParseContext context = new ParseContext(); try (InputStream input = getResourceAsStream("/test-documents/"+filename)) { AUTO_DETECT_PARSER.parse(input, handler, metadata, context); } } }
@Test public void testWordCustomProperties() throws Exception { Metadata metadata = new Metadata(); try (InputStream input = OOXMLParserTest.class.getResourceAsStream( "/test-documents/testWORD_custom_props.docx")) { ContentHandler handler = new BodyContentHandler(-1); ParseContext context = new ParseContext(); context.set(Locale.class, Locale.US); new OOXMLParser().parse(input, handler, metadata, context); } assertEquals( "application/vnd.openxmlformats-officedocument.wordprocessingml.document", metadata.get(Metadata.CONTENT_TYPE)); assertEquals("EJ04325S", metadata.get(TikaCoreProperties.CREATOR)); assertEquals("Etienne Jouvin", metadata.get(TikaCoreProperties.MODIFIER)); assertEquals("2011-07-29T16:52:00Z", metadata.get(TikaCoreProperties.CREATED)); assertEquals("2012-01-03T22:14:00Z", metadata.get(TikaCoreProperties.MODIFIED)); assertEquals("Microsoft Office Word", metadata.get(OfficeOpenXMLExtended.APPLICATION)); assertEquals("1", metadata.get(Office.PAGE_COUNT)); assertEquals("2", metadata.get(Office.WORD_COUNT)); assertEquals("My Title", metadata.get(TikaCoreProperties.TITLE)); assertEquals("My Keyword", metadata.get(Office.KEYWORDS)); assertContains("My Keyword", Arrays.asList(metadata.getValues(TikaCoreProperties.SUBJECT))); assertEquals("Normal.dotm", metadata.get(OfficeOpenXMLExtended.TEMPLATE)); assertEquals("My subject", metadata.get(OfficeOpenXMLCore.SUBJECT)); assertEquals("EDF-DIT", metadata.get(TikaCoreProperties.PUBLISHER)); assertEquals("true", metadata.get("custom:myCustomBoolean")); assertEquals("3", metadata.get("custom:myCustomNumber")); assertEquals("MyStringValue", metadata.get("custom:MyCustomString")); assertEquals("2010-12-30T23:00:00Z", metadata.get("custom:MyCustomDate")); assertEquals("2010-12-29T22:00:00Z", metadata.get("custom:myCustomSecondDate")); }
@Test public void testPowerPointCustomProperties() throws Exception { Metadata metadata = new Metadata(); try (InputStream input = OOXMLParserTest.class.getResourceAsStream( "/test-documents/testPPT_custom_props.pptx")) { ContentHandler handler = new BodyContentHandler(-1); ParseContext context = new ParseContext(); context.set(Locale.class, Locale.US); new OOXMLParser().parse(input, handler, metadata, context); } assertEquals( "application/vnd.openxmlformats-officedocument.presentationml.presentation", metadata.get(Metadata.CONTENT_TYPE)); assertEquals("JOUVIN ETIENNE", metadata.get(TikaCoreProperties.CREATOR)); assertEquals("EJ04325S", metadata.get(TikaCoreProperties.MODIFIER)); assertEquals("2011-08-22T13:30:53Z", metadata.get(TikaCoreProperties.CREATED)); assertEquals("2011-08-22T13:32:49Z", metadata.get(TikaCoreProperties.MODIFIED)); assertEquals("1", metadata.get(Office.SLIDE_COUNT)); assertEquals("3", metadata.get(Office.WORD_COUNT)); assertEquals("Test extraction properties pptx", metadata.get(TikaCoreProperties.TITLE)); assertEquals("true", metadata.get("custom:myCustomBoolean")); assertEquals("3", metadata.get("custom:myCustomNumber")); assertEquals("MyStringValue", metadata.get("custom:MyCustomString")); assertEquals("2010-12-30T22:00:00Z", metadata.get("custom:MyCustomDate")); assertEquals("2010-12-29T22:00:00Z", metadata.get("custom:myCustomSecondDate")); }
@Test public void testEmbeddedPDF() throws Exception { Metadata metadata = new Metadata(); StringWriter sw = new StringWriter(); SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); TransformerHandler handler = factory.newTransformerHandler(); handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "xml"); handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "no"); handler.setResult(new StreamResult(sw)); try (InputStream input = OOXMLParserTest.class.getResourceAsStream( "/test-documents/testWORD_embedded_pdf.docx")) { new OOXMLParser().parse(input, handler, metadata, new ParseContext()); } String xml = sw.toString(); int i = xml.indexOf("Here is the pdf file:"); int j = xml.indexOf("<div class=\"embedded\" id=\"rId5\"/>"); int k = xml.indexOf("Bye Bye"); int l = xml.indexOf("<div class=\"embedded\" id=\"rId6\"/>"); int m = xml.indexOf("Bye for real."); assertTrue(i != -1); assertTrue(j != -1); assertTrue(k != -1); assertTrue(l != -1); assertTrue(m != -1); assertTrue(i < j); assertTrue(j < k); assertTrue(k < l); assertTrue(l < m); }
@Test public void testNoFormat() throws Exception { ContentHandler handler = new BodyContentHandler(); Metadata metadata = new Metadata(); try (InputStream stream = WordParserTest.class.getResourceAsStream( "/test-documents/testWORD_no_format.docx")) { new OOXMLParser().parse(stream, handler, metadata, new ParseContext()); } String content = handler.toString(); assertContains("This is a piece of text that causes an exception", content); }