View Javadoc

1   /* Copyright (c) 2008 Sascha Kohlmann
2    *
3    * This program is free software: you can redistribute it and/or modify
4    * it under the terms of the GNU Affero General Public License as published by
5    * the Free Software Foundation, either version 3 of the License, or
6    * (at your option) any later version.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Affero General Public License for more details.
12   *
13   * You should have received a copy of the GNU Affero General Public License
14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15   */
16  package net.sf.eos.analyzer;
17  
18  
19  import static net.sf.eos.config.ConfigurationKey.Type.CLASSNAME;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  // import net.sf.eos.Provider;
25  import net.sf.eos.Supplier;
26  import net.sf.eos.config.Configuration;
27  import net.sf.eos.config.ConfigurationKey;
28  import net.sf.eos.config.Configured;
29  import net.sf.eos.config.FactoryMethod;
30  import net.sf.eos.medline.MedlineTokenizerSupplier;
31  
32  /**
33   * Support class for {@link ResettableTokenizer}.
34   * @author Sascha Kohlmann
35   * @see ResettableTokenFilter
36   */
37  public abstract class TokenizerSupplier extends Configured
38          implements Supplier<ResettableTokenizer> {
39  
40      /** For logging. */
41      private static final Log LOG =
42          LogFactory.getLog(TokenizerSupplier.class.getName());
43  
44      /** The configuration key name for the classname of the provider.
45       * @see #newInstance(Configuration) */
46      @SuppressWarnings("nls")
47      @ConfigurationKey(type=CLASSNAME,
48                              description="Provider supports the creation of stacked \n"
49                                          + "Tokenizers following the decoration "
50                                          + "pattern.")
51      public final static String TOKENIZER_PROVIDER_IMPL_CONFIG_NAME =
52          "net.sf.eos.analyzer.TokenizerProvider.impl";
53  
54      /**
55       * Creates a new instance. The
56       * {@code Configuration} must contain a key
57       * {@link #TOKENIZER_PROVIDER_IMPL_CONFIG_NAME} of a provider implementation. There 
58       * is no default implementation.
59       * @param config the configuration
60       * @return a new instance
61       * @throws TokenizerException if it is not possible to instantiate an instance
62       */
63      @SuppressWarnings("nls")
64      @FactoryMethod(key=TOKENIZER_PROVIDER_IMPL_CONFIG_NAME,
65                     implementation=MedlineTokenizerSupplier.class)
66      public final static TokenizerSupplier newInstance(final Configuration config)
67              throws TokenizerException {
68  
69          final Thread t = Thread.currentThread();
70          ClassLoader classLoader = t.getContextClassLoader();
71          if (classLoader == null) {
72              classLoader = TokenizerSupplier.class.getClassLoader();
73          }
74  
75          final String clazzName = config.get(TOKENIZER_PROVIDER_IMPL_CONFIG_NAME,
76                                              MedlineTokenizerSupplier.class.getName());
77  
78          try {
79              final Class<? extends TokenizerSupplier> clazz = 
80                  (Class<? extends TokenizerSupplier>) 
81                      Class.forName(clazzName, true, classLoader);
82              try {
83  
84                  final TokenizerSupplier builder = clazz.newInstance();
85                  builder.configure(config);
86                  if (LOG.isDebugEnabled()) {
87                      LOG.debug("TokenizerProvider instance: "
88                                 + builder.getClass().getName());
89                  }
90                  return builder;
91  
92              } catch (final InstantiationException e) {
93                  throw new TokenizerException(e);
94              } catch (final IllegalAccessException e) {
95                  throw new TokenizerException(e);
96              }
97          } catch (final ClassNotFoundException e) {
98              throw new TokenizerException(e);
99          }
100     }
101 
102     /**
103      * Creates a new instance.
104      * @return a new instance
105      * @throws TokenizerException if an error occurs
106      */
107     public abstract ResettableTokenizer get();
108 }