add cause to exceptions, log exceptions that are caught and suppressed

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753030 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-07-17 08:41:52 +00:00
parent 1cf76af9e8
commit 0025b16e3e

View File

@ -50,6 +50,9 @@ import org.apache.poi.poifs.filesystem.OPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.OfficeXmlFileException; import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.util.NotImplemented;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.xdgf.extractor.XDGFVisioExtractor; import org.apache.poi.xdgf.extractor.XDGFVisioExtractor;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor; import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xslf.usermodel.XSLFRelation; import org.apache.poi.xslf.usermodel.XSLFRelation;
@ -72,6 +75,8 @@ import org.apache.xmlbeans.XmlException;
*/ */
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
public class ExtractorFactory { public class ExtractorFactory {
private static final POILogger logger = POILogFactory.getLogger(ExtractorFactory.class);
public static final String CORE_DOCUMENT_REL = PackageRelationshipTypes.CORE_DOCUMENT; public static final String CORE_DOCUMENT_REL = PackageRelationshipTypes.CORE_DOCUMENT;
protected static final String VISIO_DOCUMENT_REL = PackageRelationshipTypes.VISIO_CORE_DOCUMENT; protected static final String VISIO_DOCUMENT_REL = PackageRelationshipTypes.VISIO_CORE_DOCUMENT;
protected static final String STRICT_DOCUMENT_REL = PackageRelationshipTypes.STRICT_CORE_DOCUMENT; protected static final String STRICT_DOCUMENT_REL = PackageRelationshipTypes.STRICT_CORE_DOCUMENT;
@ -208,11 +213,12 @@ public class ExtractorFactory {
} }
// Grab the core document part, and try to identify from that // Grab the core document part, and try to identify from that
PackagePart corePart = pkg.getPart(core.getRelationship(0)); final PackagePart corePart = pkg.getPart(core.getRelationship(0));
final String contentType = corePart.getContentType();
// Is it XSSF? // Is it XSSF?
for (XSSFRelation rel : XSSFExcelExtractor.SUPPORTED_TYPES) { for (XSSFRelation rel : XSSFExcelExtractor.SUPPORTED_TYPES) {
if (corePart.getContentType().equals(rel.getContentType())) { if ( rel.getContentType().equals( contentType ) ) {
if (getPreferEventExtractor()) { if (getPreferEventExtractor()) {
return new XSSFEventBasedExcelExtractor(pkg); return new XSSFEventBasedExcelExtractor(pkg);
} }
@ -222,24 +228,24 @@ public class ExtractorFactory {
// Is it XWPF? // Is it XWPF?
for (XWPFRelation rel : XWPFWordExtractor.SUPPORTED_TYPES) { for (XWPFRelation rel : XWPFWordExtractor.SUPPORTED_TYPES) {
if (corePart.getContentType().equals(rel.getContentType())) { if ( rel.getContentType().equals( contentType ) ) {
return new XWPFWordExtractor(pkg); return new XWPFWordExtractor(pkg);
} }
} }
// Is it XSLF? // Is it XSLF?
for (XSLFRelation rel : XSLFPowerPointExtractor.SUPPORTED_TYPES) { for (XSLFRelation rel : XSLFPowerPointExtractor.SUPPORTED_TYPES) {
if (corePart.getContentType().equals(rel.getContentType())) { if ( rel.getContentType().equals( contentType ) ) {
return new XSLFPowerPointExtractor(pkg); return new XSLFPowerPointExtractor(pkg);
} }
} }
// special handling for SlideShow-Theme-files, // special handling for SlideShow-Theme-files,
if (XSLFRelation.THEME_MANAGER.getContentType().equals(corePart.getContentType())) { if (XSLFRelation.THEME_MANAGER.getContentType().equals(contentType)) {
return new XSLFPowerPointExtractor(new XSLFSlideShow(pkg)); return new XSLFPowerPointExtractor(new XSLFSlideShow(pkg));
} }
throw new IllegalArgumentException("No supported documents found in the OOXML package (found "+corePart.getContentType()+")"); throw new IllegalArgumentException("No supported documents found in the OOXML package (found "+contentType+")");
} catch (IOException e) { } catch (IOException e) {
// ensure that we close the package again if there is an error opening it, however // ensure that we close the package again if there is an error opening it, however
@ -328,6 +334,7 @@ public class ExtractorFactory {
} }
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
logger.log(POILogger.INFO, "Ignoring FileNotFoundException while extracting Word document", e.getLocalizedMessage());
// ignored here // ignored here
} }
//} else if(ext instanceof PowerPointExtractor) { //} else if(ext instanceof PowerPointExtractor) {
@ -351,23 +358,24 @@ public class ExtractorFactory {
return new POITextExtractor[0]; return new POITextExtractor[0];
} }
ArrayList<POITextExtractor> e = new ArrayList<POITextExtractor>(); ArrayList<POITextExtractor> textExtractors = new ArrayList<POITextExtractor>();
for (Entry dir : dirs) { for (Entry dir : dirs) {
e.add(createExtractor((DirectoryNode) dir)); textExtractors.add(createExtractor((DirectoryNode) dir));
} }
for (InputStream nonPOIF : nonPOIFS) { for (InputStream nonPOIF : nonPOIFS) {
try { try {
e.add(createExtractor(nonPOIF)); textExtractors.add(createExtractor(nonPOIF));
} catch (IllegalArgumentException ie) { } catch (IllegalArgumentException e) {
// Ignore, just means it didn't contain // Ignore, just means it didn't contain
// a format we support as yet // a format we support as yet
} catch (XmlException xe) { logger.log(POILogger.INFO, "Format not supported yet", e.getLocalizedMessage());
throw new IOException(xe.getMessage()); } catch (XmlException e) {
} catch (OpenXML4JException oe) { throw new IOException(e.getMessage(), e);
throw new IOException(oe.getMessage()); } catch (OpenXML4JException e) {
throw new IOException(e.getMessage(), e);
} }
} }
return e.toArray(new POITextExtractor[e.size()]); return textExtractors.toArray(new POITextExtractor[textExtractors.size()]);
} }
/** /**
@ -377,6 +385,7 @@ public class ExtractorFactory {
* empty array. Otherwise, you'll get one open * empty array. Otherwise, you'll get one open
* {@link POITextExtractor} for each embedded file. * {@link POITextExtractor} for each embedded file.
*/ */
@NotImplemented
@SuppressWarnings("UnusedParameters") @SuppressWarnings("UnusedParameters")
public static POITextExtractor[] getEmbededDocsTextExtractors(POIXMLTextExtractor ext) { public static POITextExtractor[] getEmbededDocsTextExtractors(POIXMLTextExtractor ext) {
throw new IllegalStateException("Not yet supported"); throw new IllegalStateException("Not yet supported");