MtasCharFilterFactory.java.html
13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MtasCharFilterFactory.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MTAS</a> > <a href="index.source.html" class="el_package">mtas.analysis.util</a> > <span class="el_source">MtasCharFilterFactory.java</span></div><h1>MtasCharFilterFactory.java</h1><pre class="source lang-java linenums">package mtas.analysis.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.util.CharFilterFactory;
import org.apache.lucene.analysis.util.ResourceLoader;
import org.apache.lucene.analysis.util.ResourceLoaderAware;
import org.apache.solr.core.SolrResourceLoader;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
/**
* A factory for creating MtasCharFilter objects.
*/
public class MtasCharFilterFactory extends CharFilterFactory
implements ResourceLoaderAware {
/** The Constant log. */
<span class="fc" id="L22"> private static final Log log = LogFactory.getLog(MtasCharFilterFactory.class);</span>
/** The Constant ARGUMENT_TYPE. */
public static final String ARGUMENT_TYPE = "type";
/** The Constant ARGUMENT_PREFIX. */
public static final String ARGUMENT_PREFIX = "prefix";
/** The Constant ARGUMENT_POSTFIX. */
public static final String ARGUMENT_POSTFIX = "postfix";
/** The Constant ARGUMENT_CONFIG. */
public static final String ARGUMENT_CONFIG = "config";
/** The Constant ARGUMENT_DEFAULT. */
public static final String ARGUMENT_DEFAULT = "default";
/** The Constant VALUE_TYPE_URL. */
public static final String VALUE_TYPE_URL = "url";
/** The Constant VALUE_TYPE_FILE. */
public static final String VALUE_TYPE_FILE = "file";
/** The config argument. */
String configArgument;
/** The default argument. */
String defaultArgument;
/** The type argument. */
String typeArgument;
/** The prefix argument. */
String prefixArgument;
/** The postfix argument. */
String postfixArgument;
/** The configs. */
<span class="fc" id="L61"> private HashMap<String, MtasConfiguration> configs = null;</span>
/** The config. */
<span class="fc" id="L64"> private MtasConfiguration config = null;</span>
/**
* Instantiates a new mtas char filter factory.
*
* @param args the args
* @throws IOException Signals that an I/O exception has occurred.
*/
public MtasCharFilterFactory(Map<String, String> args) throws IOException {
<span class="fc" id="L73"> this(args, null);</span>
<span class="fc" id="L74"> }</span>
/**
* Instantiates a new mtas char filter factory.
*
* @param args the args
* @param resourceLoader the resource loader
* @throws IOException Signals that an I/O exception has occurred.
*/
public MtasCharFilterFactory(Map<String, String> args,
ResourceLoader resourceLoader) throws IOException {
<span class="fc" id="L85"> super(args);</span>
<span class="fc" id="L86"> typeArgument = get(args, ARGUMENT_TYPE);</span>
<span class="fc" id="L87"> prefixArgument = get(args, ARGUMENT_PREFIX);</span>
<span class="fc" id="L88"> postfixArgument = get(args, ARGUMENT_POSTFIX);</span>
<span class="fc" id="L89"> configArgument = get(args, ARGUMENT_CONFIG);</span>
<span class="fc" id="L90"> defaultArgument = get(args, ARGUMENT_DEFAULT);</span>
<span class="pc bpc" id="L91" title="1 of 4 branches missed."> if (typeArgument != null && configArgument != null) {</span>
<span class="nc" id="L92"> throw new IOException(this.getClass().getName() + " can't have both "</span>
+ ARGUMENT_TYPE + " and " + ARGUMENT_CONFIG);
<span class="pc bpc" id="L94" title="1 of 4 branches missed."> } else if (typeArgument == null && prefixArgument != null) {</span>
<span class="nc" id="L95"> throw new IOException(this.getClass().getName() + " can't have "</span>
+ ARGUMENT_PREFIX + " without " + ARGUMENT_TYPE);
<span class="pc bpc" id="L97" title="1 of 4 branches missed."> } else if (typeArgument == null && postfixArgument != null) {</span>
<span class="nc" id="L98"> throw new IOException(this.getClass().getName() + " can't have "</span>
+ ARGUMENT_POSTFIX + " without " + ARGUMENT_TYPE);
<span class="pc bpc" id="L100" title="1 of 4 branches missed."> } else if (configArgument == null && defaultArgument != null) {</span>
<span class="nc" id="L101"> throw new IOException(this.getClass().getName() + " can't have "</span>
+ ARGUMENT_DEFAULT + " without " + ARGUMENT_CONFIG);
<span class="pc bpc" id="L103" title="1 of 4 branches missed."> } else if (typeArgument == null && configArgument == null) {</span>
<span class="nc" id="L104"> throw new IOException(this.getClass().getName() + " should have "</span>
+ ARGUMENT_TYPE + " or " + ARGUMENT_CONFIG);
}
<span class="fc" id="L107"> init(resourceLoader);</span>
<span class="fc" id="L108"> }</span>
/**
* Inits the.
*
* @param resourceLoader the resource loader
* @throws IOException Signals that an I/O exception has occurred.
*/
private void init(ResourceLoader resourceLoader) throws IOException {
<span class="pc bpc" id="L117" title="1 of 4 branches missed."> if (config == null && configs == null) {</span>
<span class="pc bpc" id="L118" title="1 of 4 branches missed."> if (typeArgument == null && configArgument == null) {</span>
<span class="nc" id="L119"> throw new IOException("no configuration");</span>
} else {
<span class="fc bfc" id="L121" title="All 2 branches covered."> if (typeArgument != null) {</span>
<span class="fc" id="L122"> config = new MtasConfiguration();</span>
<span class="fc" id="L123"> config.attributes.put(MtasConfiguration.CHARFILTER_CONFIGURATION_TYPE,</span>
typeArgument);
<span class="fc" id="L125"> config.attributes.put(</span>
MtasConfiguration.CHARFILTER_CONFIGURATION_PREFIX,
prefixArgument);
<span class="fc" id="L128"> config.attributes.put(</span>
MtasConfiguration.CHARFILTER_CONFIGURATION_POSTFIX,
postfixArgument);
}
<span class="fc bfc" id="L132" title="All 2 branches covered."> if (configArgument != null) {</span>
<span class="fc bfc" id="L133" title="All 2 branches covered."> if (resourceLoader != null) {</span>
try {
<span class="fc" id="L135"> configs = MtasConfiguration.readMtasCharFilterConfigurations(</span>
resourceLoader, configArgument);
<span class="nc" id="L137"> } catch (IOException e) {</span>
<span class="nc" id="L138"> throw new IOException(</span>
"problem loading configurations from " + configArgument, e);
<span class="fc" id="L140"> }</span>
}
}
}
}
<span class="fc" id="L145"> }</span>
/*
* (non-Javadoc)
*
* @see
* org.apache.lucene.analysis.util.CharFilterFactory#create(java.io.Reader)
*/
@Override
public Reader create(Reader input) {
<span class="fc" id="L155"> String configuration = null;</span>
try {
<span class="fc" id="L157"> return create(input, configuration);</span>
<span class="nc" id="L158"> } catch (IOException e) {</span>
<span class="nc" id="L159"> log.debug(e);</span>
<span class="nc" id="L160"> return null;</span>
}
}
/**
* Creates the.
*
* @param input the input
* @param configuration the configuration
* @return the reader
* @throws IOException Signals that an I/O exception has occurred.
*/
public Reader create(Reader input, String configuration) throws IOException {
<span class="pc bpc" id="L173" title="1 of 4 branches missed."> if (configs != null && configs.size() > 0) {</span>
<span class="pc bpc" id="L174" title="3 of 4 branches missed."> if (configuration == null && defaultArgument == null) {</span>
<span class="nc" id="L175"> throw new IOException("no (default)configuration");</span>
<span class="pc bpc" id="L176" title="1 of 2 branches missed."> } else if (configuration == null) {</span>
<span class="nc bnc" id="L177" title="All 2 branches missed."> if (configs.get(defaultArgument) != null) {</span>
<span class="nc" id="L178"> return create(input, configs.get(defaultArgument));</span>
} else {
<span class="nc" id="L180"> throw new IOException(</span>
"default configuration " + defaultArgument + " not available");
}
} else {
<span class="fc" id="L184"> MtasConfiguration config = configs.get(configuration);</span>
<span class="pc bpc" id="L185" title="1 of 2 branches missed."> if (config == null) {</span>
<span class="nc bnc" id="L186" title="All 2 branches missed."> if (defaultArgument != null) {</span>
<span class="nc bnc" id="L187" title="All 2 branches missed."> if (configs.get(defaultArgument) != null) {</span>
<span class="nc" id="L188"> return create(input, configs.get(defaultArgument));</span>
} else {
<span class="nc" id="L190"> throw new IOException("configuration " + configuration</span>
+ " not found and default configuration " + defaultArgument
+ " not available");
}
} else {
<span class="nc" id="L195"> throw new IOException("configuration " + configuration</span>
+ " not available and no default configuration");
}
} else {
<span class="fc" id="L199"> return create(input, config);</span>
}
}
<span class="pc bpc" id="L202" title="1 of 2 branches missed."> } else if (config != null) {</span>
<span class="fc" id="L203"> return create(input, config);</span>
} else {
<span class="nc" id="L205"> throw new IOException("no configuration");</span>
}
}
/**
* Creates the.
*
* @param input the input
* @param config the config
* @return the reader
* @throws IOException Signals that an I/O exception has occurred.
*/
public Reader create(Reader input, MtasConfiguration config)
throws IOException {
<span class="fc" id="L220"> MtasFetchData fetchData = new MtasFetchData(input);</span>
<span class="fc" id="L221"> if (config.attributes</span>
<span class="pc bpc" id="L222" title="1 of 2 branches missed."> .containsKey(MtasConfiguration.CHARFILTER_CONFIGURATION_TYPE)) {</span>
<span class="fc" id="L223"> if (config.attributes.get(MtasConfiguration.CHARFILTER_CONFIGURATION_TYPE)</span>
<span class="pc bpc" id="L224" title="1 of 2 branches missed."> .equals(VALUE_TYPE_URL)) {</span>
try {
<span class="nc" id="L226"> return fetchData.getUrl(</span>
config.attributes
<span class="nc" id="L228"> .get(MtasConfiguration.CHARFILTER_CONFIGURATION_PREFIX),</span>
config.attributes
<span class="nc" id="L230"> .get(MtasConfiguration.CHARFILTER_CONFIGURATION_POSTFIX));</span>
<span class="nc" id="L231"> } catch (MtasParserException e) {</span>
<span class="nc" id="L232"> log.debug(e);</span>
<span class="nc" id="L233"> throw new IOException(e.getMessage());</span>
}
<span class="fc" id="L235"> } else if (config.attributes</span>
<span class="fc" id="L236"> .get(MtasConfiguration.CHARFILTER_CONFIGURATION_TYPE)</span>
<span class="pc bpc" id="L237" title="1 of 2 branches missed."> .equals(VALUE_TYPE_FILE)) {</span>
try {
<span class="fc" id="L239"> return fetchData.getFile(</span>
config.attributes
<span class="fc" id="L241"> .get(MtasConfiguration.CHARFILTER_CONFIGURATION_PREFIX),</span>
config.attributes
<span class="fc" id="L243"> .get(MtasConfiguration.CHARFILTER_CONFIGURATION_POSTFIX));</span>
<span class="nc" id="L244"> } catch (MtasParserException e) {</span>
<span class="nc" id="L245"> throw new IOException(e);</span>
}
} else {
<span class="nc" id="L248"> return fetchData.getDefault();</span>
}
} else {
<span class="nc" id="L251"> return fetchData.getDefault();</span>
}
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.analysis.util.ResourceLoaderAware#inform(org.apache.
* lucene.analysis.util.ResourceLoader)
*/
@Override
public void inform(ResourceLoader loader) throws IOException {
<span class="fc" id="L263"> init(loader);</span>
<span class="fc" id="L264"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html>