1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
39
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 }