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.trie;
17  
18  
19  import static net.sf.eos.config.ConfigurationKey.Type.CLASSNAME;
20  import net.sf.eos.analyzer.TokenizerException;
21  import net.sf.eos.config.Configuration;
22  import net.sf.eos.config.ConfigurationKey;
23  import net.sf.eos.config.FactoryMethod;
24  
25  import java.io.InputStream;
26  
27  public abstract class AbstractTrieLoader<K, V> implements TrieLoader<K, V> {
28  
29      @SuppressWarnings("nls")
30      @ConfigurationKey(type=CLASSNAME,
31              description="Configuration key of the trie loader"
32                          + " factory.")
33      public final static String TRIE_LOADER_IMPL_CONFIG_NAME =
34          "net.sf.eos.trie.AbstractTrieLoader.impl";
35  
36      /**
37       * Default {@code TrieLoader} is {@link XmlTrieLoader}.
38       * @return a loader
39       * @throws TokenizerException
40       */
41      @FactoryMethod(key=TRIE_LOADER_IMPL_CONFIG_NAME,
42                     implementation=XmlTrieLoader.class)
43      public final static TrieLoader newInstance() throws TokenizerException
44      {
45          final Configuration config = new Configuration();
46          return newInstance(config);
47      }
48  
49      @FactoryMethod(key=TRIE_LOADER_IMPL_CONFIG_NAME,
50                     implementation=XmlTrieLoader.class)
51      public final static TrieLoader newInstance(final Configuration config)
52              throws TokenizerException {
53  
54          final Thread t = Thread.currentThread();
55          ClassLoader classLoader = t.getContextClassLoader();
56          if (classLoader == null) {
57              classLoader = AbstractTrieLoader.class.getClassLoader();
58          }
59  
60          final String clazzName =
61              config.get(TRIE_LOADER_IMPL_CONFIG_NAME,
62                         XmlTrieLoader.class.getName());
63  
64          try {
65              final Class<? extends TrieLoader> clazz = 
66                  (Class<? extends TrieLoader>) 
67                      Class.forName(clazzName, true, classLoader);
68              try {
69  
70                  final TrieLoader loader = clazz.newInstance();
71                  return loader;
72  
73              } catch (final InstantiationException e) {
74                  throw new TokenizerException(e);
75              } catch (final IllegalAccessException e) {
76                  throw new TokenizerException(e);
77              }
78          } catch (final ClassNotFoundException e) {
79              throw new TokenizerException(e);
80          }
81      }
82  
83      public abstract void loadTrie(final InputStream trieData,
84                                    final Trie<K, V> trie)
85              throws Exception;
86  }