]> source.dussan.org Git - poi.git/commitdiff
do not throw exception if ExtractorFactory is available in classpath, but xbean.jar not
authorSergey Vladimirov <sergey@apache.org>
Wed, 17 Aug 2011 11:06:42 +0000 (11:06 +0000)
committerSergey Vladimirov <sergey@apache.org>
Wed, 17 Aug 2011 11:06:42 +0000 (11:06 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1158626 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java

index f3f921f5b73b2074b29625f779229c2fe8fd9ea6..5998a2b90d2b56e3440e8e25916406ed9664c68c 100644 (file)
@@ -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