You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by co...@apache.org on 2012/07/13 19:37:05 UTC

svn commit: r1361308 - in /opennlp/trunk/opennlp-tools/src: main/java/opennlp/tools/postag/ main/java/opennlp/tools/sentdetect/ main/java/opennlp/tools/tokenize/ main/java/opennlp/tools/util/ test/java/opennlp/tools/postag/ test/java/opennlp/tools/sent...

Author: colen
Date: Fri Jul 13 17:37:05 2012
New Revision: 1361308

URL: http://svn.apache.org/viewvc?rev=1361308&view=rev
Log:
OPENNLP-500: Each ToolFactory now uses the ExtensionLoader to instantiate subclasses, and implements a protected init method that takes the required arguments. To make it easier to instantiate the tool factory from api we kept the constructor that takes the same arguments, internally it calls the init method.

Modified:
    opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerFactory.java
    opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceDetectorFactory.java
    opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/tokenize/TokenizerFactory.java
    opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java
    opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/POSTaggerFactoryTest.java
    opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DummySentenceDetectorFactory.java
    opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/tokenize/DummyTokenizerFactory.java

Modified: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerFactory.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerFactory.java?rev=1361308&r1=1361307&r2=1361308&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerFactory.java (original)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerFactory.java Fri Jul 13 17:37:05 2012
@@ -23,7 +23,6 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.lang.reflect.Constructor;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Map;
@@ -34,6 +33,7 @@ import opennlp.tools.dictionary.Dictiona
 import opennlp.tools.util.BaseToolFactory;
 import opennlp.tools.util.InvalidFormatException;
 import opennlp.tools.util.SequenceValidator;
+import opennlp.tools.util.ext.ExtensionLoader;
 import opennlp.tools.util.model.ArtifactSerializer;
 import opennlp.tools.util.model.UncloseableInputStream;
 
@@ -64,6 +64,10 @@ public class POSTaggerFactory extends Ba
    */
   public POSTaggerFactory(Dictionary ngramDictionary,
       TagDictionary posDictionary) {
+    this.init(ngramDictionary, posDictionary);
+  }
+  
+  protected void init(Dictionary ngramDictionary, TagDictionary posDictionary) {
     this.ngramDictionary = ngramDictionary;
     this.posDictionary = posDictionary;
   }
@@ -223,31 +227,19 @@ public class POSTaggerFactory extends Ba
       // will create the default factory
       return new POSTaggerFactory(ngramDictionary, posDictionary);
     }
-    POSTaggerFactory theFactory = null;
-    Class<? extends BaseToolFactory> factoryClass = loadSubclass(subclassName);
-    if (factoryClass != null) {
-      try {
-        Constructor<?> constructor = null;
-        constructor = factoryClass.getConstructor(Dictionary.class,
-            TagDictionary.class);
-        theFactory = (POSTaggerFactory) constructor.newInstance(
-            ngramDictionary, posDictionary);
-      } catch (NoSuchMethodException e) {
-        String msg = "Could not instantiate the "
-            + subclassName
-            + ". The mandatory constructor (Dictionary, TagDictionary) is missing.";
-        System.err.println(msg);
-        throw new IllegalArgumentException(msg);
-      } catch (Exception e) {
-        String msg = "Could not instantiate the "
-            + subclassName
-            + ". The constructor (Dictionary, TagDictionary) throw an exception.";
-        System.err.println(msg);
-        e.printStackTrace();
-        throw new InvalidFormatException(msg);
-      }
+    try {
+      POSTaggerFactory theFactory = ExtensionLoader.instantiateExtension(
+          POSTaggerFactory.class, subclassName);
+      theFactory.init(ngramDictionary, posDictionary);
+      return theFactory;
+    } catch (Exception e) {
+      String msg = "Could not instantiate the " + subclassName
+          + ". The initialization throw an exception.";
+      System.err.println(msg);
+      e.printStackTrace();
+      throw new InvalidFormatException(msg, e);
     }
-    return theFactory;
+
   }
 
   public TagDictionary createEmptyTagDictionary() {

Modified: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceDetectorFactory.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceDetectorFactory.java?rev=1361308&r1=1361307&r2=1361308&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceDetectorFactory.java (original)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceDetectorFactory.java Fri Jul 13 17:37:05 2012
@@ -17,7 +17,6 @@
 
 package opennlp.tools.sentdetect;
 
-import java.lang.reflect.Constructor;
 import java.util.Collections;
 import java.util.Map;
 import java.util.Set;
@@ -26,6 +25,7 @@ import opennlp.tools.dictionary.Dictiona
 import opennlp.tools.sentdetect.lang.Factory;
 import opennlp.tools.util.BaseToolFactory;
 import opennlp.tools.util.InvalidFormatException;
+import opennlp.tools.util.ext.ExtensionLoader;
 
 /**
  * The factory that provides SentenceDetecor default implementations and
@@ -59,6 +59,11 @@ public class SentenceDetectorFactory ext
    */
   public SentenceDetectorFactory(String languageCode, boolean useTokenEnd,
       Dictionary abbreviationDictionary, char[] eosCharacters) {
+    this.init(languageCode, useTokenEnd, abbreviationDictionary, eosCharacters);
+  }
+  
+  protected void init(String languageCode, boolean useTokenEnd,
+      Dictionary abbreviationDictionary, char[] eosCharacters) {
     this.languageCode = languageCode;
     this.useTokenEnd = useTokenEnd;
     this.eosCharacters = eosCharacters;
@@ -116,31 +121,19 @@ public class SentenceDetectorFactory ext
       return new SentenceDetectorFactory(languageCode, useTokenEnd,
           abbreviationDictionary, eosCharacters);
     }
-    SentenceDetectorFactory theFactory = null;
-    Class<? extends BaseToolFactory> factoryClass = loadSubclass(subclassName);
-    if (factoryClass != null) {
-      try {
-        Constructor<?> constructor = null;
-        constructor = factoryClass.getConstructor(String.class, boolean.class,
-            Dictionary.class, char[].class);
-        theFactory = (SentenceDetectorFactory) constructor.newInstance(
-            languageCode, useTokenEnd, abbreviationDictionary, eosCharacters);
-      } catch (NoSuchMethodException e) {
-        String msg = "Could not instantiate the "
-            + subclassName
-            + ". The mandatory constructor (String, boolean, Dictionary, char[])) is missing.";
-        System.err.println(msg);
-        throw new IllegalArgumentException(msg);
-      } catch (Exception e) {
-        String msg = "Could not instantiate the "
-            + subclassName
-            + ". The constructor (String, boolean, Dictionary, char[]) throw an exception.";
-        System.err.println(msg);
-        e.printStackTrace();
-        throw new InvalidFormatException(msg);
-      }
+    try {
+      SentenceDetectorFactory theFactory = ExtensionLoader
+          .instantiateExtension(SentenceDetectorFactory.class, subclassName);
+      theFactory.init(languageCode, useTokenEnd, abbreviationDictionary,
+          eosCharacters);
+      return theFactory;
+    } catch (Exception e) {
+      String msg = "Could not instantiate the " + subclassName
+          + ". The initialization throw an exception.";
+      System.err.println(msg);
+      e.printStackTrace();
+      throw new InvalidFormatException(msg, e);
     }
-    return theFactory;
   }
 
   public char[] getEOSCharacters() {

Modified: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/tokenize/TokenizerFactory.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/tokenize/TokenizerFactory.java?rev=1361308&r1=1361307&r2=1361308&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/tokenize/TokenizerFactory.java (original)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/tokenize/TokenizerFactory.java Fri Jul 13 17:37:05 2012
@@ -17,7 +17,6 @@
 
 package opennlp.tools.tokenize;
 
-import java.lang.reflect.Constructor;
 import java.util.Collections;
 import java.util.Map;
 import java.util.Set;
@@ -27,7 +26,7 @@ import opennlp.tools.dictionary.Dictiona
 import opennlp.tools.tokenize.lang.Factory;
 import opennlp.tools.util.BaseToolFactory;
 import opennlp.tools.util.InvalidFormatException;
-import opennlp.tools.util.model.ArtifactProvider;
+import opennlp.tools.util.ext.ExtensionLoader;
 
 /**
  * The factory that provides {@link Tokenizer} default implementations and
@@ -69,6 +68,12 @@ public class TokenizerFactory extends Ba
   public TokenizerFactory(String languageCode,
       Dictionary abbreviationDictionary, boolean useAlphaNumericOptimization,
       Pattern alphaNumericPattern) {
+    this.init(languageCode, abbreviationDictionary,
+        useAlphaNumericOptimization, alphaNumericPattern);
+  }
+  
+  protected void init(String languageCode, Dictionary abbreviationDictionary,
+      boolean useAlphaNumericOptimization, Pattern alphaNumericPattern) {
     this.languageCode = languageCode;
     this.useAlphaNumericOptimization = useAlphaNumericOptimization;
     this.alphaNumericPattern = alphaNumericPattern;
@@ -135,32 +140,19 @@ public class TokenizerFactory extends Ba
       return new TokenizerFactory(languageCode, abbreviationDictionary,
           useAlphaNumericOptimization, alphaNumericPattern);
     }
-    TokenizerFactory theFactory = null;
-    Class<? extends BaseToolFactory> factoryClass = loadSubclass(subclassName);
-    if (factoryClass != null) {
-      try {
-        Constructor<?> constructor = null;
-        constructor = factoryClass.getConstructor(String.class,
-            Dictionary.class, boolean.class, Pattern.class);
-        theFactory = (TokenizerFactory) constructor.newInstance(languageCode,
-            abbreviationDictionary, useAlphaNumericOptimization,
-            alphaNumericPattern);
-      } catch (NoSuchMethodException e) {
-        String msg = "Could not instantiate the "
-            + subclassName
-            + ". The mandatory constructor (String, Dictionary, boolean, Pattern) is missing.";
-        System.err.println(msg);
-        throw new IllegalArgumentException(msg);
-      } catch (Exception e) {
-        String msg = "Could not instantiate the "
-            + subclassName
-            + ". The constructor (String, Dictionary, boolean, Pattern) throw an exception.";
-        System.err.println(msg);
-        e.printStackTrace();
-        throw new InvalidFormatException(msg);
-      }
+    try {
+      TokenizerFactory theFactory = ExtensionLoader.instantiateExtension(
+          TokenizerFactory.class, subclassName);
+      theFactory.init(languageCode, abbreviationDictionary,
+          useAlphaNumericOptimization, alphaNumericPattern);
+      return theFactory;
+    } catch (Exception e) {
+      String msg = "Could not instantiate the " + subclassName
+          + ". The initialization throw an exception.";
+      System.err.println(msg);
+      e.printStackTrace();
+      throw new InvalidFormatException(msg, e);
     }
-    return theFactory;
   }
 
   /**

Modified: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java?rev=1361308&r1=1361307&r2=1361308&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java (original)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java Fri Jul 13 17:37:05 2012
@@ -47,7 +47,7 @@ public abstract class BaseToolFactory {
  /**
   * Initializes the ToolFactory with an artifact provider.
   */
-  public void init(ArtifactProvider artifactProvider) {
+  protected void init(ArtifactProvider artifactProvider) {
     this.artifactProvider = artifactProvider;
   }
 
@@ -137,24 +137,4 @@ public abstract class BaseToolFactory {
     }
     return theFactory;
   }
-  
-  @SuppressWarnings("unchecked")
-  protected
-  static Class<? extends BaseToolFactory> loadSubclass(
-      String factoryName) throws InvalidFormatException {
-    Class<? extends BaseToolFactory> factoryClass = null;
-    try {
-      factoryClass = (Class<? extends BaseToolFactory>) Class
-          .forName(factoryName);
-    } catch (ClassNotFoundException e) {
-      throw new NoClassDefFoundError(
-          "Could not find the factory class in the classpath: " + factoryName);
-    } catch (ClassCastException e) {
-      throw new InvalidFormatException(
-          "The factory class does not extend BaseToolFactory: " + factoryName,
-          e);
-    }
-    return factoryClass;
-  }
-  
 }

Modified: opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/POSTaggerFactoryTest.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/POSTaggerFactoryTest.java?rev=1361308&r1=1361307&r2=1361308&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/POSTaggerFactoryTest.java (original)
+++ opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/POSTaggerFactoryTest.java Fri Jul 13 17:37:05 2012
@@ -117,7 +117,7 @@ public class POSTaggerFactoryTest {
     BaseToolFactory.create("X", null);
   }
 
-  @Test(expected = NoClassDefFoundError.class)
+  @Test(expected = InvalidFormatException.class)
   public void testCreateWithInvalidName2() throws InvalidFormatException {
     POSTaggerFactory.create("X", null, null);
   }
@@ -127,7 +127,7 @@ public class POSTaggerFactoryTest {
     BaseToolFactory.create(Object.class.getCanonicalName(), null);
   }
 
-  @Test(expected = IllegalArgumentException.class)
+  @Test(expected = InvalidFormatException.class)
   public void testCreateWithHierarchy2() throws InvalidFormatException {
     POSTaggerFactory.create(this.getClass().getCanonicalName(), null, null);
   }

Modified: opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DummySentenceDetectorFactory.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DummySentenceDetectorFactory.java?rev=1361308&r1=1361307&r2=1361308&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DummySentenceDetectorFactory.java (original)
+++ opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DummySentenceDetectorFactory.java Fri Jul 13 17:37:05 2012
@@ -37,7 +37,13 @@ public class DummySentenceDetectorFactor
   
   public DummySentenceDetectorFactory(String languageCode, boolean useTokenEnd,
       Dictionary abbreviationDictionary, char[] eosCharacters) {
-    super(languageCode, useTokenEnd, null, eosCharacters);
+    super(languageCode, useTokenEnd, abbreviationDictionary, eosCharacters);
+  }
+  
+  @Override
+  protected void init(String languageCode, boolean useTokenEnd,
+      Dictionary abbreviationDictionary, char[] eosCharacters) {
+    super.init(languageCode, useTokenEnd, abbreviationDictionary, eosCharacters);
     this.dict = new DummyDictionary(abbreviationDictionary);
   }
 

Modified: opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/tokenize/DummyTokenizerFactory.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/tokenize/DummyTokenizerFactory.java?rev=1361308&r1=1361307&r2=1361308&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/tokenize/DummyTokenizerFactory.java (original)
+++ opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/tokenize/DummyTokenizerFactory.java Fri Jul 13 17:37:05 2012
@@ -41,6 +41,13 @@ public class DummyTokenizerFactory exten
       Pattern alphaNumericPattern) {
     super(languageCode, abbreviationDictionary, useAlphaNumericOptimization,
         alphaNumericPattern);
+  }
+  
+  @Override
+  protected void init(String languageCode, Dictionary abbreviationDictionary,
+      boolean useAlphaNumericOptimization, Pattern alphaNumericPattern) {
+    super.init(languageCode, abbreviationDictionary, useAlphaNumericOptimization,
+        alphaNumericPattern);
     this.dict = new DummyDictionary(abbreviationDictionary);
   }