You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ta...@apache.org on 2016/04/07 21:36:48 UTC
[05/10] tika git commit: TIKA-1936 whitespace clean up
http://git-wip-us.apache.org/repos/asf/tika/blob/d8231f48/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/MetadataExtractor.java
----------------------------------------------------------------------
diff --git a/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/MetadataExtractor.java b/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/MetadataExtractor.java
index 196ec34..25d3596 100644
--- a/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/MetadataExtractor.java
+++ b/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/MetadataExtractor.java
@@ -1,295 +1,295 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.tika.parser.microsoft.ooxml;
-
-import java.math.BigDecimal;
-import java.util.Date;
-
-import org.apache.poi.POIXMLProperties.CoreProperties;
-import org.apache.poi.POIXMLProperties.CustomProperties;
-import org.apache.poi.POIXMLProperties.ExtendedProperties;
-import org.apache.poi.POIXMLTextExtractor;
-import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
-import org.apache.poi.openxml4j.util.Nullable;
-import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor;
-import org.apache.tika.exception.TikaException;
-import org.apache.tika.metadata.MSOffice;
-import org.apache.tika.metadata.Metadata;
-import org.apache.tika.metadata.Office;
-import org.apache.tika.metadata.OfficeOpenXMLCore;
-import org.apache.tika.metadata.OfficeOpenXMLExtended;
-import org.apache.tika.metadata.PagedText;
-import org.apache.tika.metadata.Property;
-import org.apache.tika.metadata.TikaCoreProperties;
-import org.apache.tika.parser.microsoft.SummaryExtractor;
-import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty;
-import org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties;
-
-/**
- * OOXML metadata extractor.
- * <p/>
- * Currently POI doesn't support metadata extraction for OOXML.
- *
- * @see OOXMLExtractor#getMetadataExtractor()
- */
-public class MetadataExtractor {
-
- private final POIXMLTextExtractor extractor;
-
- public MetadataExtractor(POIXMLTextExtractor extractor) {
- this.extractor = extractor;
- }
-
- public void extract(Metadata metadata) throws TikaException {
- if (extractor.getDocument() != null ||
- (extractor instanceof XSSFEventBasedExcelExtractor &&
- extractor.getPackage() != null)) {
- extractMetadata(extractor.getCoreProperties(), metadata);
- extractMetadata(extractor.getExtendedProperties(), metadata);
- extractMetadata(extractor.getCustomProperties(), metadata);
- }
- }
-
- private void extractMetadata(CoreProperties properties, Metadata metadata) {
- PackagePropertiesPart propsHolder = properties
- .getUnderlyingProperties();
-
- addProperty(metadata, OfficeOpenXMLCore.CATEGORY, propsHolder.getCategoryProperty());
- addProperty(metadata, OfficeOpenXMLCore.CONTENT_STATUS, propsHolder
- .getContentStatusProperty());
- addProperty(metadata, TikaCoreProperties.CREATED, propsHolder
- .getCreatedProperty());
- addMultiProperty(metadata, TikaCoreProperties.CREATOR, propsHolder
- .getCreatorProperty());
- addProperty(metadata, TikaCoreProperties.DESCRIPTION, propsHolder
- .getDescriptionProperty());
- addProperty(metadata, TikaCoreProperties.IDENTIFIER, propsHolder
- .getIdentifierProperty());
- addProperty(metadata, TikaCoreProperties.KEYWORDS, propsHolder
- .getKeywordsProperty());
- addProperty(metadata, TikaCoreProperties.LANGUAGE, propsHolder
- .getLanguageProperty());
- addProperty(metadata, TikaCoreProperties.MODIFIER, propsHolder
- .getLastModifiedByProperty());
- addProperty(metadata, TikaCoreProperties.PRINT_DATE, propsHolder
- .getLastPrintedProperty());
- addProperty(metadata, Metadata.LAST_MODIFIED, propsHolder
- .getModifiedProperty());
- addProperty(metadata, TikaCoreProperties.MODIFIED, propsHolder
- .getModifiedProperty());
- addProperty(metadata, OfficeOpenXMLCore.REVISION, propsHolder
- .getRevisionProperty());
- // TODO: Move to OO subject in Tika 2.0
- addProperty(metadata, TikaCoreProperties.TRANSITION_SUBJECT_TO_OO_SUBJECT,
- propsHolder.getSubjectProperty());
- addProperty(metadata, TikaCoreProperties.TITLE, propsHolder.getTitleProperty());
- addProperty(metadata, OfficeOpenXMLCore.VERSION, propsHolder.getVersionProperty());
-
- // Legacy Tika-1.0 style stats
- // TODO Remove these in Tika 2.0
- addProperty(metadata, Metadata.CATEGORY, propsHolder.getCategoryProperty());
- addProperty(metadata, Metadata.CONTENT_STATUS, propsHolder
- .getContentStatusProperty());
- addProperty(metadata, Metadata.REVISION_NUMBER, propsHolder
- .getRevisionProperty());
- addProperty(metadata, Metadata.VERSION, propsHolder.getVersionProperty());
- }
-
- private void extractMetadata(ExtendedProperties properties,
- Metadata metadata) {
- CTProperties propsHolder = properties.getUnderlyingProperties();
-
- addProperty(metadata, OfficeOpenXMLExtended.APPLICATION, propsHolder.getApplication());
- addProperty(metadata, OfficeOpenXMLExtended.APP_VERSION, propsHolder.getAppVersion());
- addProperty(metadata, TikaCoreProperties.PUBLISHER, propsHolder.getCompany());
- addProperty(metadata, OfficeOpenXMLExtended.COMPANY, propsHolder.getCompany());
- SummaryExtractor.addMulti(metadata, OfficeOpenXMLExtended.MANAGER, propsHolder.getManager());
- addProperty(metadata, OfficeOpenXMLExtended.NOTES, propsHolder.getNotes());
- addProperty(metadata, OfficeOpenXMLExtended.PRESENTATION_FORMAT, propsHolder.getPresentationFormat());
- addProperty(metadata, OfficeOpenXMLExtended.TEMPLATE, propsHolder.getTemplate());
- addProperty(metadata, OfficeOpenXMLExtended.TOTAL_TIME, propsHolder.getTotalTime());
-
- if (propsHolder.getPages() > 0) {
- metadata.set(PagedText.N_PAGES, propsHolder.getPages());
- } else if (propsHolder.getSlides() > 0) {
- metadata.set(PagedText.N_PAGES, propsHolder.getSlides());
- }
-
- // Process the document statistics
- addProperty(metadata, Office.PAGE_COUNT, propsHolder.getPages());
- addProperty(metadata, Office.SLIDE_COUNT, propsHolder.getSlides());
- addProperty(metadata, Office.PARAGRAPH_COUNT, propsHolder.getParagraphs());
- addProperty(metadata, Office.LINE_COUNT, propsHolder.getLines());
- addProperty(metadata, Office.WORD_COUNT, propsHolder.getWords());
- addProperty(metadata, Office.CHARACTER_COUNT, propsHolder.getCharacters());
- addProperty(metadata, Office.CHARACTER_COUNT_WITH_SPACES, propsHolder.getCharactersWithSpaces());
-
- // Legacy Tika-1.0 style stats
- // TODO Remove these in Tika 2.0
- addProperty(metadata, Metadata.APPLICATION_NAME, propsHolder.getApplication());
- addProperty(metadata, Metadata.APPLICATION_VERSION, propsHolder.getAppVersion());
- addProperty(metadata, Metadata.MANAGER, propsHolder.getManager());
- addProperty(metadata, Metadata.NOTES, propsHolder.getNotes());
- addProperty(metadata, Metadata.PRESENTATION_FORMAT, propsHolder.getPresentationFormat());
- addProperty(metadata, Metadata.TEMPLATE, propsHolder.getTemplate());
- addProperty(metadata, Metadata.TOTAL_TIME, propsHolder.getTotalTime());
- addProperty(metadata, MSOffice.PAGE_COUNT, propsHolder.getPages());
- addProperty(metadata, MSOffice.SLIDE_COUNT, propsHolder.getSlides());
- addProperty(metadata, MSOffice.PARAGRAPH_COUNT, propsHolder.getParagraphs());
- addProperty(metadata, MSOffice.LINE_COUNT, propsHolder.getLines());
- addProperty(metadata, MSOffice.WORD_COUNT, propsHolder.getWords());
- addProperty(metadata, MSOffice.CHARACTER_COUNT, propsHolder.getCharacters());
- addProperty(metadata, MSOffice.CHARACTER_COUNT_WITH_SPACES, propsHolder.getCharactersWithSpaces());
- }
-
- private void extractMetadata(CustomProperties properties,
- Metadata metadata) {
- org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties
- props = properties.getUnderlyingProperties();
- for (int i = 0; i < props.sizeOfPropertyArray(); i++) {
- CTProperty property = props.getPropertyArray(i);
- String val = null;
- Date date = null;
-
- if (property.isSetLpwstr()) {
- val = property.getLpwstr();
- } else if (property.isSetLpstr()) {
- val = property.getLpstr();
- } else if (property.isSetDate()) {
- date = property.getDate().getTime();
- } else if (property.isSetFiletime()) {
- date = property.getFiletime().getTime();
- } else if (property.isSetBool()) {
- val = Boolean.toString(property.getBool());
- }
-
- // Integers
- else if (property.isSetI1()) {
- val = Integer.toString(property.getI1());
- } else if (property.isSetI2()) {
- val = Integer.toString(property.getI2());
- } else if (property.isSetI4()) {
- val = Integer.toString(property.getI4());
- } else if (property.isSetI8()) {
- val = Long.toString(property.getI8());
- } else if (property.isSetInt()) {
- val = Integer.toString(property.getInt());
- }
-
- // Unsigned Integers
- else if (property.isSetUi1()) {
- val = Integer.toString(property.getUi1());
- } else if (property.isSetUi2()) {
- val = Integer.toString(property.getUi2());
- } else if (property.isSetUi4()) {
- val = Long.toString(property.getUi4());
- } else if (property.isSetUi8()) {
- val = property.getUi8().toString();
- } else if (property.isSetUint()) {
- val = Long.toString(property.getUint());
- }
-
- // Reals
- else if (property.isSetR4()) {
- val = Float.toString(property.getR4());
- } else if (property.isSetR8()) {
- val = Double.toString(property.getR8());
- } else if (property.isSetDecimal()) {
- BigDecimal d = property.getDecimal();
- if (d == null) {
- val = null;
- } else {
- val = d.toPlainString();
- }
- } else if (property.isSetArray()) {
- // TODO Fetch the array values and output
- } else if (property.isSetVector()) {
- // TODO Fetch the vector values and output
- } else if (property.isSetBlob() || property.isSetOblob()) {
- // TODO Decode, if possible
- } else if (property.isSetStream() || property.isSetOstream() ||
- property.isSetVstream()) {
- // TODO Decode, if possible
- } else if (property.isSetStorage() || property.isSetOstorage()) {
- // TODO Decode, if possible
- } else {
- // This type isn't currently supported yet, skip the property
- }
-
- String propName = "custom:" + property.getName();
- if (date != null) {
- Property tikaProp = Property.externalDate(propName);
- metadata.set(tikaProp, date);
- } else if (val != null) {
- metadata.set(propName, val);
- }
- }
- }
-
- private <T> void addProperty(Metadata metadata, Property property, Nullable<T> nullableValue) {
- T value = nullableValue.getValue();
- if (value != null) {
- if (value instanceof Date) {
- metadata.set(property, (Date) value);
- } else if (value instanceof String) {
- metadata.set(property, (String) value);
- } else if (value instanceof Integer) {
- metadata.set(property, (Integer) value);
- } else if (value instanceof Double) {
- metadata.set(property, (Double) value);
- }
- }
- }
-
- private void addProperty(Metadata metadata, String name, Nullable<?> value) {
- if (value.getValue() != null) {
- addProperty(metadata, name, value.getValue().toString());
- }
- }
-
- private void addProperty(Metadata metadata, Property property, String value) {
- if (value != null) {
- metadata.set(property, value);
- }
- }
-
- private void addProperty(Metadata metadata, String name, String value) {
- if (value != null) {
- metadata.set(name, value);
- }
- }
-
- private void addProperty(Metadata metadata, Property property, int value) {
- if (value > 0) {
- metadata.set(property, value);
- }
- }
-
- private void addProperty(Metadata metadata, String name, int value) {
- if (value > 0) {
- metadata.set(name, Integer.toString(value));
- }
- }
-
- private void addMultiProperty(Metadata metadata, Property property, Nullable<String> value) {
- if (value == null) {
- return;
- }
- SummaryExtractor.addMulti(metadata, property, value.getValue());
- }
-
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.parser.microsoft.ooxml;
+
+import java.math.BigDecimal;
+import java.util.Date;
+
+import org.apache.poi.POIXMLProperties.CoreProperties;
+import org.apache.poi.POIXMLProperties.CustomProperties;
+import org.apache.poi.POIXMLProperties.ExtendedProperties;
+import org.apache.poi.POIXMLTextExtractor;
+import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
+import org.apache.poi.openxml4j.util.Nullable;
+import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor;
+import org.apache.tika.exception.TikaException;
+import org.apache.tika.metadata.MSOffice;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.metadata.Office;
+import org.apache.tika.metadata.OfficeOpenXMLCore;
+import org.apache.tika.metadata.OfficeOpenXMLExtended;
+import org.apache.tika.metadata.PagedText;
+import org.apache.tika.metadata.Property;
+import org.apache.tika.metadata.TikaCoreProperties;
+import org.apache.tika.parser.microsoft.SummaryExtractor;
+import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty;
+import org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties;
+
+/**
+ * OOXML metadata extractor.
+ * <p/>
+ * Currently POI doesn't support metadata extraction for OOXML.
+ *
+ * @see OOXMLExtractor#getMetadataExtractor()
+ */
+public class MetadataExtractor {
+
+ private final POIXMLTextExtractor extractor;
+
+ public MetadataExtractor(POIXMLTextExtractor extractor) {
+ this.extractor = extractor;
+ }
+
+ public void extract(Metadata metadata) throws TikaException {
+ if (extractor.getDocument() != null ||
+ (extractor instanceof XSSFEventBasedExcelExtractor &&
+ extractor.getPackage() != null)) {
+ extractMetadata(extractor.getCoreProperties(), metadata);
+ extractMetadata(extractor.getExtendedProperties(), metadata);
+ extractMetadata(extractor.getCustomProperties(), metadata);
+ }
+ }
+
+ private void extractMetadata(CoreProperties properties, Metadata metadata) {
+ PackagePropertiesPart propsHolder = properties
+ .getUnderlyingProperties();
+
+ addProperty(metadata, OfficeOpenXMLCore.CATEGORY, propsHolder.getCategoryProperty());
+ addProperty(metadata, OfficeOpenXMLCore.CONTENT_STATUS, propsHolder
+ .getContentStatusProperty());
+ addProperty(metadata, TikaCoreProperties.CREATED, propsHolder
+ .getCreatedProperty());
+ addMultiProperty(metadata, TikaCoreProperties.CREATOR, propsHolder
+ .getCreatorProperty());
+ addProperty(metadata, TikaCoreProperties.DESCRIPTION, propsHolder
+ .getDescriptionProperty());
+ addProperty(metadata, TikaCoreProperties.IDENTIFIER, propsHolder
+ .getIdentifierProperty());
+ addProperty(metadata, TikaCoreProperties.KEYWORDS, propsHolder
+ .getKeywordsProperty());
+ addProperty(metadata, TikaCoreProperties.LANGUAGE, propsHolder
+ .getLanguageProperty());
+ addProperty(metadata, TikaCoreProperties.MODIFIER, propsHolder
+ .getLastModifiedByProperty());
+ addProperty(metadata, TikaCoreProperties.PRINT_DATE, propsHolder
+ .getLastPrintedProperty());
+ addProperty(metadata, Metadata.LAST_MODIFIED, propsHolder
+ .getModifiedProperty());
+ addProperty(metadata, TikaCoreProperties.MODIFIED, propsHolder
+ .getModifiedProperty());
+ addProperty(metadata, OfficeOpenXMLCore.REVISION, propsHolder
+ .getRevisionProperty());
+ // TODO: Move to OO subject in Tika 2.0
+ addProperty(metadata, TikaCoreProperties.TRANSITION_SUBJECT_TO_OO_SUBJECT,
+ propsHolder.getSubjectProperty());
+ addProperty(metadata, TikaCoreProperties.TITLE, propsHolder.getTitleProperty());
+ addProperty(metadata, OfficeOpenXMLCore.VERSION, propsHolder.getVersionProperty());
+
+ // Legacy Tika-1.0 style stats
+ // TODO Remove these in Tika 2.0
+ addProperty(metadata, Metadata.CATEGORY, propsHolder.getCategoryProperty());
+ addProperty(metadata, Metadata.CONTENT_STATUS, propsHolder
+ .getContentStatusProperty());
+ addProperty(metadata, Metadata.REVISION_NUMBER, propsHolder
+ .getRevisionProperty());
+ addProperty(metadata, Metadata.VERSION, propsHolder.getVersionProperty());
+ }
+
+ private void extractMetadata(ExtendedProperties properties,
+ Metadata metadata) {
+ CTProperties propsHolder = properties.getUnderlyingProperties();
+
+ addProperty(metadata, OfficeOpenXMLExtended.APPLICATION, propsHolder.getApplication());
+ addProperty(metadata, OfficeOpenXMLExtended.APP_VERSION, propsHolder.getAppVersion());
+ addProperty(metadata, TikaCoreProperties.PUBLISHER, propsHolder.getCompany());
+ addProperty(metadata, OfficeOpenXMLExtended.COMPANY, propsHolder.getCompany());
+ SummaryExtractor.addMulti(metadata, OfficeOpenXMLExtended.MANAGER, propsHolder.getManager());
+ addProperty(metadata, OfficeOpenXMLExtended.NOTES, propsHolder.getNotes());
+ addProperty(metadata, OfficeOpenXMLExtended.PRESENTATION_FORMAT, propsHolder.getPresentationFormat());
+ addProperty(metadata, OfficeOpenXMLExtended.TEMPLATE, propsHolder.getTemplate());
+ addProperty(metadata, OfficeOpenXMLExtended.TOTAL_TIME, propsHolder.getTotalTime());
+
+ if (propsHolder.getPages() > 0) {
+ metadata.set(PagedText.N_PAGES, propsHolder.getPages());
+ } else if (propsHolder.getSlides() > 0) {
+ metadata.set(PagedText.N_PAGES, propsHolder.getSlides());
+ }
+
+ // Process the document statistics
+ addProperty(metadata, Office.PAGE_COUNT, propsHolder.getPages());
+ addProperty(metadata, Office.SLIDE_COUNT, propsHolder.getSlides());
+ addProperty(metadata, Office.PARAGRAPH_COUNT, propsHolder.getParagraphs());
+ addProperty(metadata, Office.LINE_COUNT, propsHolder.getLines());
+ addProperty(metadata, Office.WORD_COUNT, propsHolder.getWords());
+ addProperty(metadata, Office.CHARACTER_COUNT, propsHolder.getCharacters());
+ addProperty(metadata, Office.CHARACTER_COUNT_WITH_SPACES, propsHolder.getCharactersWithSpaces());
+
+ // Legacy Tika-1.0 style stats
+ // TODO Remove these in Tika 2.0
+ addProperty(metadata, Metadata.APPLICATION_NAME, propsHolder.getApplication());
+ addProperty(metadata, Metadata.APPLICATION_VERSION, propsHolder.getAppVersion());
+ addProperty(metadata, Metadata.MANAGER, propsHolder.getManager());
+ addProperty(metadata, Metadata.NOTES, propsHolder.getNotes());
+ addProperty(metadata, Metadata.PRESENTATION_FORMAT, propsHolder.getPresentationFormat());
+ addProperty(metadata, Metadata.TEMPLATE, propsHolder.getTemplate());
+ addProperty(metadata, Metadata.TOTAL_TIME, propsHolder.getTotalTime());
+ addProperty(metadata, MSOffice.PAGE_COUNT, propsHolder.getPages());
+ addProperty(metadata, MSOffice.SLIDE_COUNT, propsHolder.getSlides());
+ addProperty(metadata, MSOffice.PARAGRAPH_COUNT, propsHolder.getParagraphs());
+ addProperty(metadata, MSOffice.LINE_COUNT, propsHolder.getLines());
+ addProperty(metadata, MSOffice.WORD_COUNT, propsHolder.getWords());
+ addProperty(metadata, MSOffice.CHARACTER_COUNT, propsHolder.getCharacters());
+ addProperty(metadata, MSOffice.CHARACTER_COUNT_WITH_SPACES, propsHolder.getCharactersWithSpaces());
+ }
+
+ private void extractMetadata(CustomProperties properties,
+ Metadata metadata) {
+ org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties
+ props = properties.getUnderlyingProperties();
+ for (int i = 0; i < props.sizeOfPropertyArray(); i++) {
+ CTProperty property = props.getPropertyArray(i);
+ String val = null;
+ Date date = null;
+
+ if (property.isSetLpwstr()) {
+ val = property.getLpwstr();
+ } else if (property.isSetLpstr()) {
+ val = property.getLpstr();
+ } else if (property.isSetDate()) {
+ date = property.getDate().getTime();
+ } else if (property.isSetFiletime()) {
+ date = property.getFiletime().getTime();
+ } else if (property.isSetBool()) {
+ val = Boolean.toString(property.getBool());
+ }
+
+ // Integers
+ else if (property.isSetI1()) {
+ val = Integer.toString(property.getI1());
+ } else if (property.isSetI2()) {
+ val = Integer.toString(property.getI2());
+ } else if (property.isSetI4()) {
+ val = Integer.toString(property.getI4());
+ } else if (property.isSetI8()) {
+ val = Long.toString(property.getI8());
+ } else if (property.isSetInt()) {
+ val = Integer.toString(property.getInt());
+ }
+
+ // Unsigned Integers
+ else if (property.isSetUi1()) {
+ val = Integer.toString(property.getUi1());
+ } else if (property.isSetUi2()) {
+ val = Integer.toString(property.getUi2());
+ } else if (property.isSetUi4()) {
+ val = Long.toString(property.getUi4());
+ } else if (property.isSetUi8()) {
+ val = property.getUi8().toString();
+ } else if (property.isSetUint()) {
+ val = Long.toString(property.getUint());
+ }
+
+ // Reals
+ else if (property.isSetR4()) {
+ val = Float.toString(property.getR4());
+ } else if (property.isSetR8()) {
+ val = Double.toString(property.getR8());
+ } else if (property.isSetDecimal()) {
+ BigDecimal d = property.getDecimal();
+ if (d == null) {
+ val = null;
+ } else {
+ val = d.toPlainString();
+ }
+ } else if (property.isSetArray()) {
+ // TODO Fetch the array values and output
+ } else if (property.isSetVector()) {
+ // TODO Fetch the vector values and output
+ } else if (property.isSetBlob() || property.isSetOblob()) {
+ // TODO Decode, if possible
+ } else if (property.isSetStream() || property.isSetOstream() ||
+ property.isSetVstream()) {
+ // TODO Decode, if possible
+ } else if (property.isSetStorage() || property.isSetOstorage()) {
+ // TODO Decode, if possible
+ } else {
+ // This type isn't currently supported yet, skip the property
+ }
+
+ String propName = "custom:" + property.getName();
+ if (date != null) {
+ Property tikaProp = Property.externalDate(propName);
+ metadata.set(tikaProp, date);
+ } else if (val != null) {
+ metadata.set(propName, val);
+ }
+ }
+ }
+
+ private <T> void addProperty(Metadata metadata, Property property, Nullable<T> nullableValue) {
+ T value = nullableValue.getValue();
+ if (value != null) {
+ if (value instanceof Date) {
+ metadata.set(property, (Date) value);
+ } else if (value instanceof String) {
+ metadata.set(property, (String) value);
+ } else if (value instanceof Integer) {
+ metadata.set(property, (Integer) value);
+ } else if (value instanceof Double) {
+ metadata.set(property, (Double) value);
+ }
+ }
+ }
+
+ private void addProperty(Metadata metadata, String name, Nullable<?> value) {
+ if (value.getValue() != null) {
+ addProperty(metadata, name, value.getValue().toString());
+ }
+ }
+
+ private void addProperty(Metadata metadata, Property property, String value) {
+ if (value != null) {
+ metadata.set(property, value);
+ }
+ }
+
+ private void addProperty(Metadata metadata, String name, String value) {
+ if (value != null) {
+ metadata.set(name, value);
+ }
+ }
+
+ private void addProperty(Metadata metadata, Property property, int value) {
+ if (value > 0) {
+ metadata.set(property, value);
+ }
+ }
+
+ private void addProperty(Metadata metadata, String name, int value) {
+ if (value > 0) {
+ metadata.set(name, Integer.toString(value));
+ }
+ }
+
+ private void addMultiProperty(Metadata metadata, Property property, Nullable<String> value) {
+ if (value == null) {
+ return;
+ }
+ SummaryExtractor.addMulti(metadata, property, value.getValue());
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/tika/blob/d8231f48/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractor.java
----------------------------------------------------------------------
diff --git a/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractor.java b/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractor.java
index 3a5ff95..f52e52d 100644
--- a/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractor.java
+++ b/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractor.java
@@ -1,56 +1,56 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.tika.parser.microsoft.ooxml;
-
-import java.io.IOException;
-
-import org.apache.poi.POIXMLDocument;
-import org.apache.poi.POIXMLTextExtractor;
-import org.apache.tika.exception.TikaException;
-import org.apache.tika.metadata.Metadata;
-import org.apache.tika.parser.ParseContext;
-import org.apache.xmlbeans.XmlException;
-import org.xml.sax.ContentHandler;
-import org.xml.sax.SAXException;
-
-/**
- * Interface implemented by all Tika OOXML extractors.
- *
- * @see org.apache.poi.POIXMLTextExtractor
- */
-public interface OOXMLExtractor {
-
- /**
- * Returns the opened document.
- *
- * @see POIXMLTextExtractor#getDocument()
- */
- POIXMLDocument getDocument();
-
- /**
- * {@link POIXMLTextExtractor#getMetadataTextExtractor()} not yet supported
- * for OOXML by POI.
- */
- MetadataExtractor getMetadataExtractor();
-
- /**
- * Parses the document into a sequence of XHTML SAX events sent to the
- * given content handler.
- */
- void getXHTML(ContentHandler handler, Metadata metadata, ParseContext context)
- throws SAXException, XmlException, IOException, TikaException;
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.parser.microsoft.ooxml;
+
+import java.io.IOException;
+
+import org.apache.poi.POIXMLDocument;
+import org.apache.poi.POIXMLTextExtractor;
+import org.apache.tika.exception.TikaException;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.parser.ParseContext;
+import org.apache.xmlbeans.XmlException;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+/**
+ * Interface implemented by all Tika OOXML extractors.
+ *
+ * @see org.apache.poi.POIXMLTextExtractor
+ */
+public interface OOXMLExtractor {
+
+ /**
+ * Returns the opened document.
+ *
+ * @see POIXMLTextExtractor#getDocument()
+ */
+ POIXMLDocument getDocument();
+
+ /**
+ * {@link POIXMLTextExtractor#getMetadataTextExtractor()} not yet supported
+ * for OOXML by POI.
+ */
+ MetadataExtractor getMetadataExtractor();
+
+ /**
+ * Parses the document into a sequence of XHTML SAX events sent to the
+ * given content handler.
+ */
+ void getXHTML(ContentHandler handler, Metadata metadata, ParseContext context)
+ throws SAXException, XmlException, IOException, TikaException;
+}
http://git-wip-us.apache.org/repos/asf/tika/blob/d8231f48/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractorFactory.java
----------------------------------------------------------------------
diff --git a/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractorFactory.java b/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractorFactory.java
index 518a000..4ba99f2 100644
--- a/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractorFactory.java
+++ b/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLExtractorFactory.java
@@ -1,132 +1,132 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.tika.parser.microsoft.ooxml;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Locale;
-
-import org.apache.commons.io.input.CloseShieldInputStream;
-import org.apache.poi.POIXMLDocument;
-import org.apache.poi.POIXMLTextExtractor;
-import org.apache.poi.extractor.ExtractorFactory;
-import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
-import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
-import org.apache.poi.openxml4j.opc.OPCPackage;
-import org.apache.poi.openxml4j.opc.PackageAccess;
-import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
-import org.apache.poi.xslf.usermodel.XMLSlideShow;
-import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor;
-import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
-import org.apache.poi.xwpf.usermodel.XWPFDocument;
-import org.apache.tika.exception.TikaException;
-import org.apache.tika.io.TikaInputStream;
-import org.apache.tika.metadata.Metadata;
-import org.apache.tika.mime.MediaType;
-import org.apache.tika.parser.EmptyParser;
-import org.apache.tika.parser.ParseContext;
-import org.apache.tika.parser.opc.OPCDetector;
-import org.apache.xmlbeans.XmlException;
-import org.xml.sax.ContentHandler;
-import org.xml.sax.SAXException;
-
-/**
- * Figures out the correct {@link OOXMLExtractor} for the supplied document and
- * returns it.
- */
-public class OOXMLExtractorFactory {
-
- public static void parse(
- InputStream stream, ContentHandler baseHandler,
- Metadata metadata, ParseContext context)
- throws IOException, SAXException, TikaException {
- Locale locale = context.get(Locale.class, Locale.getDefault());
- ExtractorFactory.setThreadPrefersEventExtractors(true);
-
- try {
- OOXMLExtractor extractor;
- OPCPackage pkg;
-
- // Locate or Open the OPCPackage for the file
- TikaInputStream tis = TikaInputStream.cast(stream);
- if (tis != null && tis.getOpenContainer() instanceof OPCPackage) {
- pkg = (OPCPackage) tis.getOpenContainer();
- } else if (tis != null && tis.hasFile()) {
- pkg = OPCPackage.open(tis.getFile().getPath(), PackageAccess.READ);
- tis.setOpenContainer(pkg);
- } else {
- InputStream shield = new CloseShieldInputStream(stream);
- pkg = OPCPackage.open(shield);
- }
-
- // Get the type, and ensure it's one we handle
- MediaType type = OPCDetector.detectOfficeOpenXML(pkg);
- if (type == null || OOXMLParser.UNSUPPORTED_OOXML_TYPES.contains(type)) {
- // Not a supported type, delegate to Empty Parser
- EmptyParser.INSTANCE.parse(stream, baseHandler, metadata, context);
- return;
- }
- metadata.set(Metadata.CONTENT_TYPE, type.toString());
-
- // Have the appropriate OOXML text extractor picked
- POIXMLTextExtractor poiExtractor = ExtractorFactory.createExtractor(pkg);
-
- POIXMLDocument document = poiExtractor.getDocument();
- if (poiExtractor instanceof XSSFEventBasedExcelExtractor) {
- extractor = new XSSFExcelExtractorDecorator(
- context, (XSSFEventBasedExcelExtractor) poiExtractor, locale);
- } else if (document == null) {
- throw new TikaException(
- "Expecting UserModel based POI OOXML extractor with a document, but none found. " +
- "The extractor returned was a " + poiExtractor
- );
- } else if (document instanceof XMLSlideShow) {
- extractor = new XSLFPowerPointExtractorDecorator(
- context, (XSLFPowerPointExtractor) poiExtractor);
- } else if (document instanceof XWPFDocument) {
- extractor = new XWPFWordExtractorDecorator(
- context, (XWPFWordExtractor) poiExtractor);
- } else {
- extractor = new POIXMLTextExtractorDecorator(context, poiExtractor);
- }
-
- // Get the bulk of the metadata first, so that it's accessible during
- // parsing if desired by the client (see TIKA-1109)
- extractor.getMetadataExtractor().extract(metadata);
-
- // Extract the text, along with any in-document metadata
- extractor.getXHTML(baseHandler, metadata, context);
- } catch (IllegalArgumentException e) {
- if (e.getMessage() != null &&
- e.getMessage().startsWith("No supported documents found")) {
- throw new TikaException(
- "TIKA-418: RuntimeException while getting content"
- + " for thmx and xps file types", e);
- } else {
- throw new TikaException("Error creating OOXML extractor", e);
- }
- } catch (InvalidFormatException e) {
- throw new TikaException("Error creating OOXML extractor", e);
- } catch (OpenXML4JException e) {
- throw new TikaException("Error creating OOXML extractor", e);
- } catch (XmlException e) {
- throw new TikaException("Error creating OOXML extractor", e);
-
- }
- }
-
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.parser.microsoft.ooxml;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Locale;
+
+import org.apache.commons.io.input.CloseShieldInputStream;
+import org.apache.poi.POIXMLDocument;
+import org.apache.poi.POIXMLTextExtractor;
+import org.apache.poi.extractor.ExtractorFactory;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
+import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackageAccess;
+import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
+import org.apache.poi.xslf.usermodel.XMLSlideShow;
+import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor;
+import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
+import org.apache.tika.exception.TikaException;
+import org.apache.tika.io.TikaInputStream;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.mime.MediaType;
+import org.apache.tika.parser.EmptyParser;
+import org.apache.tika.parser.ParseContext;
+import org.apache.tika.parser.opc.OPCDetector;
+import org.apache.xmlbeans.XmlException;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+/**
+ * Figures out the correct {@link OOXMLExtractor} for the supplied document and
+ * returns it.
+ */
+public class OOXMLExtractorFactory {
+
+ public static void parse(
+ InputStream stream, ContentHandler baseHandler,
+ Metadata metadata, ParseContext context)
+ throws IOException, SAXException, TikaException {
+ Locale locale = context.get(Locale.class, Locale.getDefault());
+ ExtractorFactory.setThreadPrefersEventExtractors(true);
+
+ try {
+ OOXMLExtractor extractor;
+ OPCPackage pkg;
+
+ // Locate or Open the OPCPackage for the file
+ TikaInputStream tis = TikaInputStream.cast(stream);
+ if (tis != null && tis.getOpenContainer() instanceof OPCPackage) {
+ pkg = (OPCPackage) tis.getOpenContainer();
+ } else if (tis != null && tis.hasFile()) {
+ pkg = OPCPackage.open(tis.getFile().getPath(), PackageAccess.READ);
+ tis.setOpenContainer(pkg);
+ } else {
+ InputStream shield = new CloseShieldInputStream(stream);
+ pkg = OPCPackage.open(shield);
+ }
+
+ // Get the type, and ensure it's one we handle
+ MediaType type = OPCDetector.detectOfficeOpenXML(pkg);
+ if (type == null || OOXMLParser.UNSUPPORTED_OOXML_TYPES.contains(type)) {
+ // Not a supported type, delegate to Empty Parser
+ EmptyParser.INSTANCE.parse(stream, baseHandler, metadata, context);
+ return;
+ }
+ metadata.set(Metadata.CONTENT_TYPE, type.toString());
+
+ // Have the appropriate OOXML text extractor picked
+ POIXMLTextExtractor poiExtractor = ExtractorFactory.createExtractor(pkg);
+
+ POIXMLDocument document = poiExtractor.getDocument();
+ if (poiExtractor instanceof XSSFEventBasedExcelExtractor) {
+ extractor = new XSSFExcelExtractorDecorator(
+ context, (XSSFEventBasedExcelExtractor) poiExtractor, locale);
+ } else if (document == null) {
+ throw new TikaException(
+ "Expecting UserModel based POI OOXML extractor with a document, but none found. " +
+ "The extractor returned was a " + poiExtractor
+ );
+ } else if (document instanceof XMLSlideShow) {
+ extractor = new XSLFPowerPointExtractorDecorator(
+ context, (XSLFPowerPointExtractor) poiExtractor);
+ } else if (document instanceof XWPFDocument) {
+ extractor = new XWPFWordExtractorDecorator(
+ context, (XWPFWordExtractor) poiExtractor);
+ } else {
+ extractor = new POIXMLTextExtractorDecorator(context, poiExtractor);
+ }
+
+ // Get the bulk of the metadata first, so that it's accessible during
+ // parsing if desired by the client (see TIKA-1109)
+ extractor.getMetadataExtractor().extract(metadata);
+
+ // Extract the text, along with any in-document metadata
+ extractor.getXHTML(baseHandler, metadata, context);
+ } catch (IllegalArgumentException e) {
+ if (e.getMessage() != null &&
+ e.getMessage().startsWith("No supported documents found")) {
+ throw new TikaException(
+ "TIKA-418: RuntimeException while getting content"
+ + " for thmx and xps file types", e);
+ } else {
+ throw new TikaException("Error creating OOXML extractor", e);
+ }
+ } catch (InvalidFormatException e) {
+ throw new TikaException("Error creating OOXML extractor", e);
+ } catch (OpenXML4JException e) {
+ throw new TikaException("Error creating OOXML extractor", e);
+ } catch (XmlException e) {
+ throw new TikaException("Error creating OOXML extractor", e);
+
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/tika/blob/d8231f48/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLParser.java
----------------------------------------------------------------------
diff --git a/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLParser.java b/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLParser.java
index c02a185..22f2cac 100644
--- a/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLParser.java
+++ b/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/OOXMLParser.java
@@ -1,89 +1,89 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.tika.parser.microsoft.ooxml;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
-
-import org.apache.poi.openxml4j.util.ZipSecureFile;
-import org.apache.tika.exception.TikaException;
-import org.apache.tika.metadata.Metadata;
-import org.apache.tika.mime.MediaType;
-import org.apache.tika.parser.AbstractParser;
-import org.apache.tika.parser.ParseContext;
-import org.xml.sax.ContentHandler;
-import org.xml.sax.SAXException;
-
-/**
- * Office Open XML (OOXML) parser.
- */
-public class OOXMLParser extends AbstractParser {
- static {
- //turn off POI's zip bomb detection because we have our own
- ZipSecureFile.setMinInflateRatio(-1.0d);
- }
-
- protected static final Set<MediaType> SUPPORTED_TYPES =
- Collections.unmodifiableSet(new HashSet<MediaType>(Arrays.asList(
- MediaType.application("x-tika-ooxml"),
- MediaType.application("vnd.openxmlformats-officedocument.presentationml.presentation"),
- MediaType.application("vnd.ms-powerpoint.presentation.macroenabled.12"),
- MediaType.application("vnd.openxmlformats-officedocument.presentationml.template"),
- MediaType.application("vnd.openxmlformats-officedocument.presentationml.slideshow"),
- MediaType.application("vnd.ms-powerpoint.slideshow.macroenabled.12"),
- MediaType.application("vnd.ms-powerpoint.addin.macroenabled.12"),
- MediaType.application("vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
- MediaType.application("vnd.ms-excel.sheet.macroenabled.12"),
- MediaType.application("vnd.openxmlformats-officedocument.spreadsheetml.template"),
- MediaType.application("vnd.ms-excel.template.macroenabled.12"),
- MediaType.application("vnd.ms-excel.addin.macroenabled.12"),
- MediaType.application("vnd.openxmlformats-officedocument.wordprocessingml.document"),
- MediaType.application("vnd.ms-word.document.macroenabled.12"),
- MediaType.application("vnd.openxmlformats-officedocument.wordprocessingml.template"),
- MediaType.application("vnd.ms-word.template.macroenabled.12"))));
- /**
- * We claim to support all OOXML files, but we actually don't support a small
- * number of them.
- * This list is used to decline certain formats that are not yet supported
- * by Tika and/or POI.
- */
- protected static final Set<MediaType> UNSUPPORTED_OOXML_TYPES =
- Collections.unmodifiableSet(new HashSet<MediaType>(Arrays.asList(
- MediaType.application("vnd.ms-excel.sheet.binary.macroenabled.12"),
- MediaType.application("vnd.ms-xpsdocument")
- )));
- /**
- * Serial version UID
- */
- private static final long serialVersionUID = 6535995710857776481L;
-
- public Set<MediaType> getSupportedTypes(ParseContext context) {
- return SUPPORTED_TYPES;
- }
-
- public void parse(
- InputStream stream, ContentHandler handler,
- Metadata metadata, ParseContext context)
- throws IOException, SAXException, TikaException {
- // Have the OOXML file processed
- OOXMLExtractorFactory.parse(stream, handler, metadata, context);
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.parser.microsoft.ooxml;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.poi.openxml4j.util.ZipSecureFile;
+import org.apache.tika.exception.TikaException;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.mime.MediaType;
+import org.apache.tika.parser.AbstractParser;
+import org.apache.tika.parser.ParseContext;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+/**
+ * Office Open XML (OOXML) parser.
+ */
+public class OOXMLParser extends AbstractParser {
+ static {
+ //turn off POI's zip bomb detection because we have our own
+ ZipSecureFile.setMinInflateRatio(-1.0d);
+ }
+
+ protected static final Set<MediaType> SUPPORTED_TYPES =
+ Collections.unmodifiableSet(new HashSet<MediaType>(Arrays.asList(
+ MediaType.application("x-tika-ooxml"),
+ MediaType.application("vnd.openxmlformats-officedocument.presentationml.presentation"),
+ MediaType.application("vnd.ms-powerpoint.presentation.macroenabled.12"),
+ MediaType.application("vnd.openxmlformats-officedocument.presentationml.template"),
+ MediaType.application("vnd.openxmlformats-officedocument.presentationml.slideshow"),
+ MediaType.application("vnd.ms-powerpoint.slideshow.macroenabled.12"),
+ MediaType.application("vnd.ms-powerpoint.addin.macroenabled.12"),
+ MediaType.application("vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
+ MediaType.application("vnd.ms-excel.sheet.macroenabled.12"),
+ MediaType.application("vnd.openxmlformats-officedocument.spreadsheetml.template"),
+ MediaType.application("vnd.ms-excel.template.macroenabled.12"),
+ MediaType.application("vnd.ms-excel.addin.macroenabled.12"),
+ MediaType.application("vnd.openxmlformats-officedocument.wordprocessingml.document"),
+ MediaType.application("vnd.ms-word.document.macroenabled.12"),
+ MediaType.application("vnd.openxmlformats-officedocument.wordprocessingml.template"),
+ MediaType.application("vnd.ms-word.template.macroenabled.12"))));
+ /**
+ * We claim to support all OOXML files, but we actually don't support a small
+ * number of them.
+ * This list is used to decline certain formats that are not yet supported
+ * by Tika and/or POI.
+ */
+ protected static final Set<MediaType> UNSUPPORTED_OOXML_TYPES =
+ Collections.unmodifiableSet(new HashSet<MediaType>(Arrays.asList(
+ MediaType.application("vnd.ms-excel.sheet.binary.macroenabled.12"),
+ MediaType.application("vnd.ms-xpsdocument")
+ )));
+ /**
+ * Serial version UID
+ */
+ private static final long serialVersionUID = 6535995710857776481L;
+
+ public Set<MediaType> getSupportedTypes(ParseContext context) {
+ return SUPPORTED_TYPES;
+ }
+
+ public void parse(
+ InputStream stream, ContentHandler handler,
+ Metadata metadata, ParseContext context)
+ throws IOException, SAXException, TikaException {
+ // Have the OOXML file processed
+ OOXMLExtractorFactory.parse(stream, handler, metadata, context);
+ }
+}
http://git-wip-us.apache.org/repos/asf/tika/blob/d8231f48/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/POIXMLTextExtractorDecorator.java
----------------------------------------------------------------------
diff --git a/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/POIXMLTextExtractorDecorator.java b/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/POIXMLTextExtractorDecorator.java
index 38c8ad0..ff44176 100644
--- a/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/POIXMLTextExtractorDecorator.java
+++ b/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/POIXMLTextExtractorDecorator.java
@@ -1,44 +1,44 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.tika.parser.microsoft.ooxml;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.poi.POIXMLTextExtractor;
-import org.apache.poi.openxml4j.opc.PackagePart;
-import org.apache.tika.parser.ParseContext;
-import org.apache.tika.sax.XHTMLContentHandler;
-import org.xml.sax.SAXException;
-
-public class POIXMLTextExtractorDecorator extends AbstractOOXMLExtractor {
-
- public POIXMLTextExtractorDecorator(ParseContext context, POIXMLTextExtractor extractor) {
- super(context, extractor);
- }
-
- @Override
- protected void buildXHTML(XHTMLContentHandler xhtml) throws SAXException {
- // extract document content as a single string (not structured)
- xhtml.element("p", extractor.getText());
- }
-
- @Override
- protected List<PackagePart> getMainDocumentParts() {
- return new ArrayList<PackagePart>();
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.parser.microsoft.ooxml;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.poi.POIXMLTextExtractor;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.tika.parser.ParseContext;
+import org.apache.tika.sax.XHTMLContentHandler;
+import org.xml.sax.SAXException;
+
+public class POIXMLTextExtractorDecorator extends AbstractOOXMLExtractor {
+
+ public POIXMLTextExtractorDecorator(ParseContext context, POIXMLTextExtractor extractor) {
+ super(context, extractor);
+ }
+
+ @Override
+ protected void buildXHTML(XHTMLContentHandler xhtml) throws SAXException {
+ // extract document content as a single string (not structured)
+ xhtml.element("p", extractor.getText());
+ }
+
+ @Override
+ protected List<PackagePart> getMainDocumentParts() {
+ return new ArrayList<PackagePart>();
+ }
+}
http://git-wip-us.apache.org/repos/asf/tika/blob/d8231f48/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/XSLFPowerPointExtractorDecorator.java
----------------------------------------------------------------------
diff --git a/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/XSLFPowerPointExtractorDecorator.java b/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/XSLFPowerPointExtractorDecorator.java
index 102a05e..e58c040 100644
--- a/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/XSLFPowerPointExtractorDecorator.java
+++ b/tika-parser-modules/tika-parser-office-module/src/main/java/org/apache/tika/parser/microsoft/ooxml/XSLFPowerPointExtractorDecorator.java
@@ -1,269 +1,269 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.tika.parser.microsoft.ooxml;
-
-import javax.xml.namespace.QName;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
-import org.apache.poi.openxml4j.opc.PackagePart;
-import org.apache.poi.openxml4j.opc.PackagePartName;
-import org.apache.poi.openxml4j.opc.PackageRelationship;
-import org.apache.poi.openxml4j.opc.PackagingURIHelper;
-import org.apache.poi.openxml4j.opc.TargetMode;
-import org.apache.poi.sl.usermodel.Placeholder;
-import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
-import org.apache.poi.xslf.usermodel.XMLSlideShow;
-import org.apache.poi.xslf.usermodel.XSLFCommentAuthors;
-import org.apache.poi.xslf.usermodel.XSLFComments;
-import org.apache.poi.xslf.usermodel.XSLFGraphicFrame;
-import org.apache.poi.xslf.usermodel.XSLFGroupShape;
-import org.apache.poi.xslf.usermodel.XSLFNotes;
-import org.apache.poi.xslf.usermodel.XSLFNotesMaster;
-import org.apache.poi.xslf.usermodel.XSLFPictureShape;
-import org.apache.poi.xslf.usermodel.XSLFRelation;
-import org.apache.poi.xslf.usermodel.XSLFShape;
-import org.apache.poi.xslf.usermodel.XSLFSheet;
-import org.apache.poi.xslf.usermodel.XSLFSlide;
-import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
-import org.apache.poi.xslf.usermodel.XSLFSlideShow;
-import org.apache.poi.xslf.usermodel.XSLFTable;
-import org.apache.poi.xslf.usermodel.XSLFTableCell;
-import org.apache.poi.xslf.usermodel.XSLFTableRow;
-import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
-import org.apache.poi.xslf.usermodel.XSLFTextShape;
-import org.apache.tika.exception.TikaException;
-import org.apache.tika.parser.ParseContext;
-import org.apache.tika.sax.XHTMLContentHandler;
-import org.apache.xmlbeans.XmlException;
-import org.apache.xmlbeans.XmlObject;
-import org.openxmlformats.schemas.presentationml.x2006.main.CTComment;
-import org.openxmlformats.schemas.presentationml.x2006.main.CTCommentAuthor;
-import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
-import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdList;
-import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry;
-import org.xml.sax.SAXException;
-import org.xml.sax.helpers.AttributesImpl;
-
-public class XSLFPowerPointExtractorDecorator extends AbstractOOXMLExtractor {
- public XSLFPowerPointExtractorDecorator(ParseContext context, XSLFPowerPointExtractor extractor) {
- super(context, extractor);
- }
-
- /**
- * @see org.apache.poi.xslf.extractor.XSLFPowerPointExtractor#getText()
- */
- protected void buildXHTML(XHTMLContentHandler xhtml) throws SAXException, IOException {
- XMLSlideShow slideShow = (XMLSlideShow) extractor.getDocument();
- XSLFCommentAuthors commentAuthors = slideShow.getCommentAuthors();
-
- List<XSLFSlide> slides = slideShow.getSlides();
- for (XSLFSlide slide : slides) {
- String slideDesc;
- if (slide.getPackagePart() != null && slide.getPackagePart().getPartName() != null) {
- slideDesc = getJustFileName(slide.getPackagePart().getPartName().toString());
- slideDesc += "_";
- } else {
- slideDesc = null;
- }
-
- // slide content
- xhtml.startElement("div", "class", "slide-content");
- extractContent(slide.getShapes(), false, xhtml, slideDesc);
- xhtml.endElement("div");
-
- // slide layout which is the master sheet for this slide
- xhtml.startElement("div", "class", "slide-master-content");
- XSLFSlideLayout slideLayout = slide.getMasterSheet();
- extractContent(slideLayout.getShapes(), true, xhtml, null);
- xhtml.endElement("div");
-
- // slide master which is the master sheet for all text layouts
- XSLFSheet slideMaster = slideLayout.getMasterSheet();
- extractContent(slideMaster.getShapes(), true, xhtml, null);
-
- // notes (if present)
- XSLFNotes slideNotes = slide.getNotes();
- if (slideNotes != null) {
- xhtml.startElement("div", "class", "slide-notes");
-
- extractContent(slideNotes.getShapes(), false, xhtml, slideDesc);
-
- // master sheet for this notes
- XSLFNotesMaster notesMaster = slideNotes.getMasterSheet();
- extractContent(notesMaster.getShapes(), true, xhtml, null);
- xhtml.endElement("div");
- }
-
- // comments (if present)
- XSLFComments comments = slide.getComments();
- if (comments != null) {
- StringBuilder authorStringBuilder = new StringBuilder();
- for (int i = 0; i < comments.getNumberOfComments(); i++) {
- authorStringBuilder.setLength(0);
- CTComment comment = comments.getCommentAt(i);
- xhtml.startElement("p", "class", "slide-comment");
- CTCommentAuthor cta = commentAuthors.getAuthorById(comment.getAuthorId());
- if (cta != null) {
- if (cta.getName() != null) {
- authorStringBuilder.append(cta.getName());
- }
- if (cta.getInitials() != null) {
- if (authorStringBuilder.length() > 0) {
- authorStringBuilder.append(" ");
- }
- authorStringBuilder.append("("+cta.getInitials()+")");
- }
- if (comment.getText() != null && authorStringBuilder.length() > 0) {
- authorStringBuilder.append(" - ");
- }
- if (authorStringBuilder.length() > 0) {
- xhtml.startElement("b");
- xhtml.characters(authorStringBuilder.toString());
- xhtml.endElement("b");
- }
- }
- xhtml.characters(comment.getText());
- xhtml.endElement("p");
- }
- }
- }
- }
-
- private void extractContent(List<? extends XSLFShape> shapes, boolean skipPlaceholders, XHTMLContentHandler xhtml, String slideDesc)
- throws SAXException {
- for (XSLFShape sh : shapes) {
- if (sh instanceof XSLFTextShape) {
- XSLFTextShape txt = (XSLFTextShape) sh;
- Placeholder ph = txt.getTextType();
- if (skipPlaceholders && ph != null) {
- continue;
- }
- for (XSLFTextParagraph p : txt.getTextParagraphs()) {
- xhtml.element("p", p.getText());
- }
- } else if (sh instanceof XSLFGroupShape) {
- // recurse into groups of shapes
- XSLFGroupShape group = (XSLFGroupShape) sh;
- extractContent(group.getShapes(), skipPlaceholders, xhtml, slideDesc);
- } else if (sh instanceof XSLFTable) {
- //unlike tables in Word, ppt/x can't have recursive tables...I don't think
- extractTable((XSLFTable)sh, xhtml);
- } else if (sh instanceof XSLFGraphicFrame) {
- XSLFGraphicFrame frame = (XSLFGraphicFrame) sh;
- XmlObject[] sp = frame.getXmlObject().selectPath(
- "declare namespace p='http://schemas.openxmlformats.org/presentationml/2006/main' .//*/p:oleObj");
- if (sp != null) {
- for (XmlObject emb : sp) {
- XmlObject relIDAtt = emb.selectAttribute(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id"));
- if (relIDAtt != null) {
- String relID = relIDAtt.getDomNode().getNodeValue();
- if (slideDesc != null) {
- relID = slideDesc + relID;
- }
- AttributesImpl attributes = new AttributesImpl();
- attributes.addAttribute("", "class", "class", "CDATA", "embedded");
- attributes.addAttribute("", "id", "id", "CDATA", relID);
- xhtml.startElement("div", attributes);
- xhtml.endElement("div");
- }
- }
- }
- } else if (sh instanceof XSLFPictureShape) {
- if (!skipPlaceholders && (sh.getXmlObject() instanceof CTPicture)) {
- CTPicture ctPic = ((CTPicture) sh.getXmlObject());
- if (ctPic.getBlipFill() != null && ctPic.getBlipFill().getBlip() != null) {
- String relID = ctPic.getBlipFill().getBlip().getEmbed();
- if (relID != null) {
- if (slideDesc != null) {
- relID = slideDesc + relID;
- }
- AttributesImpl attributes = new AttributesImpl();
- attributes.addAttribute("", "class", "class", "CDATA", "embedded");
- attributes.addAttribute("", "id", "id", "CDATA", relID);
- xhtml.startElement("div", attributes);
- xhtml.endElement("div");
- }
- }
- }
- }
- }
- }
-
- private void extractTable(XSLFTable tbl, XHTMLContentHandler xhtml) throws SAXException {
- xhtml.startElement("table");
- for (XSLFTableRow row : tbl) {
- xhtml.startElement("tr");
- List<XSLFTableCell> cells = row.getCells();
- for (XSLFTableCell c : row.getCells()) {
- xhtml.startElement("td");
- xhtml.characters(c.getText());
- xhtml.endElement("td");
- }
- xhtml.endElement("tr");
- }
- xhtml.endElement("table");
-
- }
-
- /**
- * In PowerPoint files, slides have things embedded in them,
- * and slide drawings which have the images
- */
- @Override
- protected List<PackagePart> getMainDocumentParts() throws TikaException {
- List<PackagePart> parts = new ArrayList<>();
- XSLFSlideShow document = null;
- try {
- document = new XSLFSlideShow(extractor.getPackage());
- } catch (Exception e) {
- throw new TikaException(e.getMessage()); // Shouldn't happen
- }
-
- CTSlideIdList ctSlideIdList = document.getSlideReferences();
- if (ctSlideIdList != null) {
- for (int i = 0; i < ctSlideIdList.sizeOfSldIdArray(); i++) {
- CTSlideIdListEntry ctSlide = ctSlideIdList.getSldIdArray(i);
- // Add the slide
- PackagePart slidePart;
- try {
- slidePart = document.getSlidePart(ctSlide);
- } catch (IOException e) {
- throw new TikaException("Broken OOXML file", e);
- } catch (XmlException xe) {
- throw new TikaException("Broken OOXML file", xe);
- }
- parts.add(slidePart);
-
- // If it has drawings, return those too
- try {
- for (PackageRelationship rel : slidePart.getRelationshipsByType(XSLFRelation.VML_DRAWING.getRelation())) {
- if (rel.getTargetMode() == TargetMode.INTERNAL) {
- PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
- parts.add(rel.getPackage().getPart(relName));
- }
- }
- } catch (InvalidFormatException e) {
- throw new TikaException("Broken OOXML file", e);
- }
- }
- }
- return parts;
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.parser.microsoft.ooxml;
+
+import javax.xml.namespace.QName;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.openxml4j.opc.PackagePartName;
+import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.poi.openxml4j.opc.PackagingURIHelper;
+import org.apache.poi.openxml4j.opc.TargetMode;
+import org.apache.poi.sl.usermodel.Placeholder;
+import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
+import org.apache.poi.xslf.usermodel.XMLSlideShow;
+import org.apache.poi.xslf.usermodel.XSLFCommentAuthors;
+import org.apache.poi.xslf.usermodel.XSLFComments;
+import org.apache.poi.xslf.usermodel.XSLFGraphicFrame;
+import org.apache.poi.xslf.usermodel.XSLFGroupShape;
+import org.apache.poi.xslf.usermodel.XSLFNotes;
+import org.apache.poi.xslf.usermodel.XSLFNotesMaster;
+import org.apache.poi.xslf.usermodel.XSLFPictureShape;
+import org.apache.poi.xslf.usermodel.XSLFRelation;
+import org.apache.poi.xslf.usermodel.XSLFShape;
+import org.apache.poi.xslf.usermodel.XSLFSheet;
+import org.apache.poi.xslf.usermodel.XSLFSlide;
+import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
+import org.apache.poi.xslf.usermodel.XSLFSlideShow;
+import org.apache.poi.xslf.usermodel.XSLFTable;
+import org.apache.poi.xslf.usermodel.XSLFTableCell;
+import org.apache.poi.xslf.usermodel.XSLFTableRow;
+import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
+import org.apache.poi.xslf.usermodel.XSLFTextShape;
+import org.apache.tika.exception.TikaException;
+import org.apache.tika.parser.ParseContext;
+import org.apache.tika.sax.XHTMLContentHandler;
+import org.apache.xmlbeans.XmlException;
+import org.apache.xmlbeans.XmlObject;
+import org.openxmlformats.schemas.presentationml.x2006.main.CTComment;
+import org.openxmlformats.schemas.presentationml.x2006.main.CTCommentAuthor;
+import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
+import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdList;
+import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
+public class XSLFPowerPointExtractorDecorator extends AbstractOOXMLExtractor {
+ public XSLFPowerPointExtractorDecorator(ParseContext context, XSLFPowerPointExtractor extractor) {
+ super(context, extractor);
+ }
+
+ /**
+ * @see org.apache.poi.xslf.extractor.XSLFPowerPointExtractor#getText()
+ */
+ protected void buildXHTML(XHTMLContentHandler xhtml) throws SAXException, IOException {
+ XMLSlideShow slideShow = (XMLSlideShow) extractor.getDocument();
+ XSLFCommentAuthors commentAuthors = slideShow.getCommentAuthors();
+
+ List<XSLFSlide> slides = slideShow.getSlides();
+ for (XSLFSlide slide : slides) {
+ String slideDesc;
+ if (slide.getPackagePart() != null && slide.getPackagePart().getPartName() != null) {
+ slideDesc = getJustFileName(slide.getPackagePart().getPartName().toString());
+ slideDesc += "_";
+ } else {
+ slideDesc = null;
+ }
+
+ // slide content
+ xhtml.startElement("div", "class", "slide-content");
+ extractContent(slide.getShapes(), false, xhtml, slideDesc);
+ xhtml.endElement("div");
+
+ // slide layout which is the master sheet for this slide
+ xhtml.startElement("div", "class", "slide-master-content");
+ XSLFSlideLayout slideLayout = slide.getMasterSheet();
+ extractContent(slideLayout.getShapes(), true, xhtml, null);
+ xhtml.endElement("div");
+
+ // slide master which is the master sheet for all text layouts
+ XSLFSheet slideMaster = slideLayout.getMasterSheet();
+ extractContent(slideMaster.getShapes(), true, xhtml, null);
+
+ // notes (if present)
+ XSLFNotes slideNotes = slide.getNotes();
+ if (slideNotes != null) {
+ xhtml.startElement("div", "class", "slide-notes");
+
+ extractContent(slideNotes.getShapes(), false, xhtml, slideDesc);
+
+ // master sheet for this notes
+ XSLFNotesMaster notesMaster = slideNotes.getMasterSheet();
+ extractContent(notesMaster.getShapes(), true, xhtml, null);
+ xhtml.endElement("div");
+ }
+
+ // comments (if present)
+ XSLFComments comments = slide.getComments();
+ if (comments != null) {
+ StringBuilder authorStringBuilder = new StringBuilder();
+ for (int i = 0; i < comments.getNumberOfComments(); i++) {
+ authorStringBuilder.setLength(0);
+ CTComment comment = comments.getCommentAt(i);
+ xhtml.startElement("p", "class", "slide-comment");
+ CTCommentAuthor cta = commentAuthors.getAuthorById(comment.getAuthorId());
+ if (cta != null) {
+ if (cta.getName() != null) {
+ authorStringBuilder.append(cta.getName());
+ }
+ if (cta.getInitials() != null) {
+ if (authorStringBuilder.length() > 0) {
+ authorStringBuilder.append(" ");
+ }
+ authorStringBuilder.append("("+cta.getInitials()+")");
+ }
+ if (comment.getText() != null && authorStringBuilder.length() > 0) {
+ authorStringBuilder.append(" - ");
+ }
+ if (authorStringBuilder.length() > 0) {
+ xhtml.startElement("b");
+ xhtml.characters(authorStringBuilder.toString());
+ xhtml.endElement("b");
+ }
+ }
+ xhtml.characters(comment.getText());
+ xhtml.endElement("p");
+ }
+ }
+ }
+ }
+
+ private void extractContent(List<? extends XSLFShape> shapes, boolean skipPlaceholders, XHTMLContentHandler xhtml, String slideDesc)
+ throws SAXException {
+ for (XSLFShape sh : shapes) {
+ if (sh instanceof XSLFTextShape) {
+ XSLFTextShape txt = (XSLFTextShape) sh;
+ Placeholder ph = txt.getTextType();
+ if (skipPlaceholders && ph != null) {
+ continue;
+ }
+ for (XSLFTextParagraph p : txt.getTextParagraphs()) {
+ xhtml.element("p", p.getText());
+ }
+ } else if (sh instanceof XSLFGroupShape) {
+ // recurse into groups of shapes
+ XSLFGroupShape group = (XSLFGroupShape) sh;
+ extractContent(group.getShapes(), skipPlaceholders, xhtml, slideDesc);
+ } else if (sh instanceof XSLFTable) {
+ //unlike tables in Word, ppt/x can't have recursive tables...I don't think
+ extractTable((XSLFTable)sh, xhtml);
+ } else if (sh instanceof XSLFGraphicFrame) {
+ XSLFGraphicFrame frame = (XSLFGraphicFrame) sh;
+ XmlObject[] sp = frame.getXmlObject().selectPath(
+ "declare namespace p='http://schemas.openxmlformats.org/presentationml/2006/main' .//*/p:oleObj");
+ if (sp != null) {
+ for (XmlObject emb : sp) {
+ XmlObject relIDAtt = emb.selectAttribute(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id"));
+ if (relIDAtt != null) {
+ String relID = relIDAtt.getDomNode().getNodeValue();
+ if (slideDesc != null) {
+ relID = slideDesc + relID;
+ }
+ AttributesImpl attributes = new AttributesImpl();
+ attributes.addAttribute("", "class", "class", "CDATA", "embedded");
+ attributes.addAttribute("", "id", "id", "CDATA", relID);
+ xhtml.startElement("div", attributes);
+ xhtml.endElement("div");
+ }
+ }
+ }
+ } else if (sh instanceof XSLFPictureShape) {
+ if (!skipPlaceholders && (sh.getXmlObject() instanceof CTPicture)) {
+ CTPicture ctPic = ((CTPicture) sh.getXmlObject());
+ if (ctPic.getBlipFill() != null && ctPic.getBlipFill().getBlip() != null) {
+ String relID = ctPic.getBlipFill().getBlip().getEmbed();
+ if (relID != null) {
+ if (slideDesc != null) {
+ relID = slideDesc + relID;
+ }
+ AttributesImpl attributes = new AttributesImpl();
+ attributes.addAttribute("", "class", "class", "CDATA", "embedded");
+ attributes.addAttribute("", "id", "id", "CDATA", relID);
+ xhtml.startElement("div", attributes);
+ xhtml.endElement("div");
+ }
+ }
+ }
+ }
+ }
+ }
+
+ private void extractTable(XSLFTable tbl, XHTMLContentHandler xhtml) throws SAXException {
+ xhtml.startElement("table");
+ for (XSLFTableRow row : tbl) {
+ xhtml.startElement("tr");
+ List<XSLFTableCell> cells = row.getCells();
+ for (XSLFTableCell c : row.getCells()) {
+ xhtml.startElement("td");
+ xhtml.characters(c.getText());
+ xhtml.endElement("td");
+ }
+ xhtml.endElement("tr");
+ }
+ xhtml.endElement("table");
+
+ }
+
+ /**
+ * In PowerPoint files, slides have things embedded in them,
+ * and slide drawings which have the images
+ */
+ @Override
+ protected List<PackagePart> getMainDocumentParts() throws TikaException {
+ List<PackagePart> parts = new ArrayList<>();
+ XSLFSlideShow document = null;
+ try {
+ document = new XSLFSlideShow(extractor.getPackage());
+ } catch (Exception e) {
+ throw new TikaException(e.getMessage()); // Shouldn't happen
+ }
+
+ CTSlideIdList ctSlideIdList = document.getSlideReferences();
+ if (ctSlideIdList != null) {
+ for (int i = 0; i < ctSlideIdList.sizeOfSldIdArray(); i++) {
+ CTSlideIdListEntry ctSlide = ctSlideIdList.getSldIdArray(i);
+ // Add the slide
+ PackagePart slidePart;
+ try {
+ slidePart = document.getSlidePart(ctSlide);
+ } catch (IOException e) {
+ throw new TikaException("Broken OOXML file", e);
+ } catch (XmlException xe) {
+ throw new TikaException("Broken OOXML file", xe);
+ }
+ parts.add(slidePart);
+
+ // If it has drawings, return those too
+ try {
+ for (PackageRelationship rel : slidePart.getRelationshipsByType(XSLFRelation.VML_DRAWING.getRelation())) {
+ if (rel.getTargetMode() == TargetMode.INTERNAL) {
+ PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
+ parts.add(rel.getPackage().getPart(relName));
+ }
+ }
+ } catch (InvalidFormatException e) {
+ throw new TikaException("Broken OOXML file", e);
+ }
+ }
+ }
+ return parts;
+ }
+}