From 6f5beef8018df29e6f6d8483d7015632ee706b7d Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Wed, 17 Aug 2011 11:06:42 +0000 Subject: [PATCH] do not throw exception if ExtractorFactory is available in classpath, but xbean.jar not git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1158626 13f79535-47bb-0310-9956-ffa450edef68 --- .../hwpf/converter/WordToTextConverter.java | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java index f3f921f5b..5998a2b90 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java @@ -48,12 +48,16 @@ import org.apache.poi.hwpf.usermodel.TableRow; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.Entry; import org.apache.poi.util.Beta; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; import org.w3c.dom.Document; import org.w3c.dom.Element; @Beta public class WordToTextConverter extends AbstractWordConverter { + private static final POILogger logger = POILogFactory + .getLogger( WordToTextConverter.class ); public static String getText( DirectoryNode root ) throws Exception { @@ -325,7 +329,10 @@ public class WordToTextConverter extends AbstractWordConverter return false; DirectoryNode directoryNode = (DirectoryNode) entry; - // even if no ExtractorFactory in classpath + /* + * even if there is no ExtractorFactory in classpath, still support + * included Word's objects + */ if ( directoryNode.hasEntry( "WordDocument" ) ) { String text = WordToTextConverter.getText( (DirectoryNode) entry ); @@ -335,14 +342,27 @@ public class WordToTextConverter extends AbstractWordConverter return true; } + Object extractor; try { Class cls = Class .forName( "org.apache.poi.extractor.ExtractorFactory" ); Method createExtractor = cls.getMethod( "createExtractor", DirectoryNode.class ); - Object extractor = createExtractor.invoke( null, directoryNode ); + extractor = createExtractor.invoke( null, directoryNode ); + } + catch ( Error exc ) + { + // no extractor in classpath + logger.log( POILogger.WARN, "There is an OLE object entry '", + entry.getName(), + "', but there is no text extractor for this object type ", + "or text extractor factory is not available: ", "" + exc ); + return false; + } + try + { Method getText = extractor.getClass().getMethod( "getText" ); String text = (String) getText.invoke( extractor ); @@ -351,12 +371,13 @@ public class WordToTextConverter extends AbstractWordConverter + UNICODECHAR_ZERO_WIDTH_SPACE ) ); return true; } - catch ( ClassNotFoundException exc ) + catch ( Exception exc ) { - // no extractor in classpath + logger.log( POILogger.ERROR, + "Unable to extract text from OLE entry '", entry.getName(), + "': ", exc, exc ); + return false; } - - return false; } @Override