1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.eos.analyzer;
17
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import net.sf.eos.EosException;
23 import net.sf.eos.config.Configuration;
24 import net.sf.eos.config.ConfigurationKey;
25 import static net.sf.eos.config.ConfigurationKey.Type.CLASSNAME;
26 import net.sf.eos.config.Configured;
27 import net.sf.eos.config.FactoryMethod;
28
29 import java.util.Arrays;
30 import java.util.List;
31
32
33
34
35
36
37
38 public abstract class TextBuilder extends Configured {
39
40
41 private static final Log LOG = LogFactory.getLog(TextBuilder.class);
42
43
44
45 @SuppressWarnings("nls")
46 @ConfigurationKey(type=CLASSNAME,
47 description="Instances are used to create a new text "
48 + "thru Token concationation.")
49 public final static String TEXT_BUILDER_IMPL_CONFIG_NAME =
50 "net.sf.eos.analyzer.TextBuilder.impl";
51
52
53
54
55
56 @SuppressWarnings("nls")
57 public static final TextBuilder SPACE_BUILDER = new SpaceBuilder();
58
59
60
61
62
63 public static final class SpaceBuilder extends TextBuilder {
64 @SuppressWarnings("nls")
65 public final static String SPACE = new String(new char[] {0x20});
66 @Override
67 public CharSequence buildText(final List<Token> tokens) {
68 final StringBuilder sb = new StringBuilder();
69 for (final Token token : tokens) {
70 final CharSequence text = token.getTokenText();
71 sb.append(text);
72 sb.append(SPACE);
73 }
74 return sb.length() > 0 ? sb.subSequence(0, sb.length() - 1) : "";
75 }
76 @Override
77 public CharSequence buildText(final Token... tokens) {
78 return buildText(Arrays.asList(tokens));
79 }
80 @Override
81 public CharSequence buildText(final CharSequence... seq) {
82 final StringBuilder sb = new StringBuilder();
83 for (final CharSequence cs : seq) {
84 sb.append(cs);
85 sb.append(SPACE);
86 }
87 return sb.length() > 0 ? sb.subSequence(0, sb.length() - 1) : "";
88 }
89 };
90
91
92
93
94
95
96
97
98
99
100
101
102 @SuppressWarnings("nls")
103 @FactoryMethod(key=TEXT_BUILDER_IMPL_CONFIG_NAME,
104 implementation=SpaceBuilder.class)
105 public final static TextBuilder newInstance(final Configuration config)
106 throws EosException {
107
108 final Thread t = Thread.currentThread();
109 ClassLoader classLoader = t.getContextClassLoader();
110 if (classLoader == null) {
111 classLoader = TextBuilder.class.getClassLoader();
112 }
113
114 final String clazzName = config.get(TEXT_BUILDER_IMPL_CONFIG_NAME,
115 SPACE_BUILDER.getClass().getName());
116 if (clazzName.equals(SPACE_BUILDER.getClass().getName())) {
117 return SPACE_BUILDER;
118 }
119
120 try {
121 final Class<? extends TextBuilder> clazz =
122 (Class<? extends TextBuilder>)
123 Class.forName(clazzName, true, classLoader);
124 try {
125
126 final TextBuilder textBuilder = clazz.newInstance();
127 textBuilder.configure(config);
128 if (LOG.isDebugEnabled()) {
129 LOG.debug("TextBuilder instance: "
130 + textBuilder.getClass().getName());
131 }
132 return textBuilder;
133
134 } catch (final InstantiationException e) {
135 throw new TokenizerException(e);
136 } catch (final IllegalAccessException e) {
137 throw new TokenizerException(e);
138 }
139 } catch (final ClassNotFoundException e) {
140 throw new TokenizerException(e);
141 }
142 }
143
144
145
146
147
148
149
150 public abstract CharSequence buildText(final List<Token> tokens);
151
152
153
154
155
156
157
158 public abstract CharSequence buildText(final Token... tokens);
159
160
161
162
163
164
165
166 public abstract CharSequence buildText(final CharSequence... seq);
167 }