From fbff3e557bbee0882a9b6492cad0e87ace030477 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 29 Jul 2010 11:57:08 +0000 Subject: Refactor to make it easier to tell which content types each POIXMLTextExtractor handles git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@980414 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/extractor/ExtractorFactory.java | 69 +++++++++++----------- 1 file changed, 34 insertions(+), 35 deletions(-) (limited to 'src/ooxml/java/org/apache/poi/extractor') diff --git a/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java b/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java index 7ccb9f9297..4864714f49 100644 --- a/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java +++ b/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java @@ -52,6 +52,7 @@ import org.apache.poi.poifs.filesystem.Entry; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.xslf.XSLFSlideShow; import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor; +import org.apache.poi.xslf.usermodel.XSLFRelation; import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor; import org.apache.poi.xssf.extractor.XSSFExcelExtractor; import org.apache.poi.xssf.usermodel.XSSFRelation; @@ -155,42 +156,40 @@ public class ExtractorFactory { } public static POIXMLTextExtractor createExtractor(OPCPackage pkg) throws IOException, OpenXML4JException, XmlException { - PackageRelationshipCollection core = - pkg.getRelationshipsByType(CORE_DOCUMENT_REL); - if(core.size() != 1) { - throw new IllegalArgumentException("Invalid OOXML Package received - expected 1 core document, found " + core.size()); - } - - PackagePart corePart = pkg.getPart(core.getRelationship(0)); - if (corePart.getContentType().equals(XSSFRelation.WORKBOOK.getContentType()) || - corePart.getContentType().equals(XSSFRelation.MACRO_TEMPLATE_WORKBOOK.getContentType()) || - corePart.getContentType().equals(XSSFRelation.MACRO_ADDIN_WORKBOOK.getContentType()) || - corePart.getContentType().equals(XSSFRelation.TEMPLATE_WORKBOOK.getContentType()) || - corePart.getContentType().equals(XSSFRelation.MACROS_WORKBOOK.getContentType())) { - if(getPreferEventExtractor()) { - return new XSSFEventBasedExcelExtractor(pkg); - } else { - return new XSSFExcelExtractor(pkg); - } - } - - if(corePart.getContentType().equals(XWPFRelation.DOCUMENT.getContentType()) || - corePart.getContentType().equals(XWPFRelation.TEMPLATE.getContentType()) || - corePart.getContentType().equals(XWPFRelation.MACRO_DOCUMENT.getContentType()) || - corePart.getContentType().equals(XWPFRelation.MACRO_TEMPLATE_DOCUMENT.getContentType()) ) { - return new XWPFWordExtractor(pkg); - } - - if(corePart.getContentType().equals(XSLFSlideShow.MAIN_CONTENT_TYPE) || - corePart.getContentType().equals(XSLFSlideShow.MACRO_CONTENT_TYPE) || - corePart.getContentType().equals(XSLFSlideShow.MACRO_TEMPLATE_CONTENT_TYPE) || - corePart.getContentType().equals(XSLFSlideShow.PRESENTATIONML_CONTENT_TYPE) || - corePart.getContentType().equals(XSLFSlideShow.PRESENTATIONML_TEMPLATE_CONTENT_TYPE) || - corePart.getContentType().equals(XSLFSlideShow.PRESENTATION_MACRO_CONTENT_TYPE)) { - return new XSLFPowerPointExtractor(pkg); - } + PackageRelationshipCollection core = + pkg.getRelationshipsByType(CORE_DOCUMENT_REL); + if(core.size() != 1) { + throw new IllegalArgumentException("Invalid OOXML Package received - expected 1 core document, found " + core.size()); + } - throw new IllegalArgumentException("No supported documents found in the OOXML package (found "+corePart.getContentType()+")"); + PackagePart corePart = pkg.getPart(core.getRelationship(0)); + + // Is it XSSF? + for(XSSFRelation rel : XSSFExcelExtractor.SUPPORTED_TYPES) { + if(corePart.getContentType().equals(rel.getContentType())) { + if(getPreferEventExtractor()) { + return new XSSFEventBasedExcelExtractor(pkg); + } else { + return new XSSFExcelExtractor(pkg); + } + } + } + + // Is it XWPF? + for(XWPFRelation rel : XWPFWordExtractor.SUPPORTED_TYPES) { + if(corePart.getContentType().equals(rel.getContentType())) { + return new XWPFWordExtractor(pkg); + } + } + + // Is it XSLF? + for(XSLFRelation rel : XSLFPowerPointExtractor.SUPPORTED_TYPES) { + if(corePart.getContentType().equals(rel.getContentType())) { + return new XSLFPowerPointExtractor(pkg); + } + } + + throw new IllegalArgumentException("No supported documents found in the OOXML package (found "+corePart.getContentType()+")"); } public static POIOLE2TextExtractor createExtractor(POIFSFileSystem fs) throws IOException { -- cgit v1.2.3