MtasConfiguration.java.html
20.4 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?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>MtasConfiguration.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">MtasConfiguration.java</span></div><h1>MtasConfiguration.java</h1><pre class="source lang-java linenums">package mtas.analysis.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.util.ResourceLoader;
/**
* The Class MtasConfiguration.
*/
public class MtasConfiguration {
/** The Constant log. */
<span class="fc" id="L26"> private static final Log log = LogFactory.getLog(MtasConfiguration.class);</span>
/** The Constant CONFIGURATIONS_MTAS. */
public static final String CONFIGURATIONS_MTAS = "mtas";
/** The Constant CONFIGURATIONS_CONFIGURATIONS. */
public static final String CONFIGURATIONS_CONFIGURATIONS = "configurations";
/** The Constant CONFIGURATIONS_CONFIGURATION. */
public static final String CONFIGURATIONS_CONFIGURATION = "configuration";
/** The Constant CONFIGURATIONS_CONFIGURATION_NAME. */
public static final String CONFIGURATIONS_CONFIGURATION_NAME = "name";
/** The Constant TOKENIZER_CONFIGURATION_FILE. */
public static final String TOKENIZER_CONFIGURATION_FILE = "file";
/** The Constant CHARFILTER_CONFIGURATION_TYPE. */
public static final String CHARFILTER_CONFIGURATION_TYPE = "type";
/** The Constant CHARFILTER_CONFIGURATION_PREFIX. */
public static final String CHARFILTER_CONFIGURATION_PREFIX = "prefix";
/** The Constant CHARFILTER_CONFIGURATION_POSTFIX. */
public static final String CHARFILTER_CONFIGURATION_POSTFIX = "postfix";
/** The name. */
public String name;
/** The attributes. */
public HashMap<String, String> attributes;
/** The children. */
public List<MtasConfiguration> children;
/** The parent. */
public MtasConfiguration parent;
/**
* Instantiates a new mtas configuration.
*/
<span class="fc" id="L67"> public MtasConfiguration() {</span>
<span class="fc" id="L68"> name = null;</span>
<span class="fc" id="L69"> attributes = new HashMap<String, String>();</span>
<span class="fc" id="L70"> children = new ArrayList<MtasConfiguration>();</span>
<span class="fc" id="L71"> parent = null;</span>
<span class="fc" id="L72"> }</span>
/**
* Read configurations.
*
* @param resourceLoader the resource loader
* @param configFile the config file
* @param className the class name
* @return the hash map
* @throws IOException Signals that an I/O exception has occurred.
*/
private static HashMap<String, HashMap<String, String>> readConfigurations(
ResourceLoader resourceLoader, String configFile, String className)
throws IOException {
<span class="fc" id="L86"> HashMap<String, HashMap<String, String>> configs = null;</span>
<span class="fc" id="L87"> InputStream reader = resourceLoader.openResource(configFile);</span>
// parse xml
<span class="fc" id="L89"> XMLInputFactory factory = XMLInputFactory.newInstance();</span>
try {
<span class="fc" id="L91"> XMLStreamReader streamReader = factory.createXMLStreamReader(reader);</span>
<span class="fc" id="L92"> String currentElement = null;</span>
<span class="fc" id="L93"> ArrayList<String> currentElements = new ArrayList<String>();</span>
QName qname;
<span class="fc" id="L95"> boolean skipCurrentConfigurations = false;</span>
try {
<span class="fc" id="L97"> int event = streamReader.getEventType();</span>
while (true) {
<span class="pc bpc" id="L99" title="1 of 6 branches missed."> switch (event) {</span>
case XMLStreamConstants.START_DOCUMENT:
<span class="pc bpc" id="L101" title="1 of 2 branches missed."> if (!streamReader.getCharacterEncodingScheme().equals("UTF-8")) {</span>
<span class="nc" id="L102"> throw new IOException("XML not UTF-8 encoded");</span>
}
break;
case XMLStreamConstants.END_DOCUMENT:
<span class="fc" id="L106"> break;</span>
case XMLStreamConstants.SPACE:
<span class="nc" id="L108"> break;</span>
case XMLStreamConstants.START_ELEMENT:
// get data
<span class="fc" id="L111"> qname = streamReader.getName();</span>
<span class="fc bfc" id="L112" title="All 2 branches covered."> if (configs == null) {</span>
<span class="pc bpc" id="L113" title="1 of 2 branches missed."> if (qname.getLocalPart().equals(CONFIGURATIONS_MTAS)) {</span>
<span class="fc" id="L114"> configs = new HashMap<String, HashMap<String, String>>();</span>
} else {
<span class="nc" id="L116"> throw new IOException("no Mtas Configurations File");</span>
}
<span class="pc bpc" id="L118" title="1 of 2 branches missed."> } else if (currentElement != null</span>
<span class="fc bfc" id="L119" title="All 2 branches covered."> && currentElement.equals(CONFIGURATIONS_MTAS)) {</span>
<span class="pc bpc" id="L120" title="1 of 2 branches missed."> if (qname.getLocalPart().equals(CONFIGURATIONS_CONFIGURATIONS)) {</span>
<span class="fc" id="L121"> skipCurrentConfigurations = true;</span>
<span class="pc bpc" id="L122" title="1 of 2 branches missed."> if (className != null) {</span>
<span class="fc bfc" id="L123" title="All 2 branches covered."> for (int i = 0; i < streamReader.getAttributeCount(); i++) {</span>
<span class="pc bpc" id="L124" title="1 of 2 branches missed."> if (streamReader.getAttributeLocalName(i).equals("type")) {</span>
<span class="fc bfc" id="L125" title="All 2 branches covered."> if (streamReader.getAttributeValue(i).equals(className)) {</span>
<span class="fc" id="L126"> skipCurrentConfigurations = false;</span>
}
}
}
}
} else {
<span class="nc" id="L132"> throw new IOException("unexpected " + qname.getLocalPart());</span>
}
<span class="pc bpc" id="L134" title="1 of 2 branches missed."> } else if (currentElement != null</span>
<span class="pc bpc" id="L135" title="1 of 4 branches missed."> && currentElement.equals(CONFIGURATIONS_CONFIGURATIONS)</span>
&& !skipCurrentConfigurations) {
<span class="pc bpc" id="L137" title="1 of 2 branches missed."> if (qname.getLocalPart().equals(CONFIGURATIONS_CONFIGURATION)) {</span>
<span class="fc" id="L138"> String configurationName = null;</span>
<span class="fc" id="L139"> HashMap<String, String> configurationValues = new HashMap<String, String>();</span>
<span class="fc bfc" id="L140" title="All 2 branches covered."> for (int i = 0; i < streamReader.getAttributeCount(); i++) {</span>
<span class="fc" id="L141"> if (streamReader.getAttributeLocalName(i)</span>
<span class="fc bfc" id="L142" title="All 2 branches covered."> .equals(CONFIGURATIONS_CONFIGURATION_NAME)) {</span>
<span class="fc" id="L143"> configurationName = streamReader.getAttributeValue(i);</span>
} else {
<span class="fc" id="L145"> configurationValues.put(</span>
<span class="fc" id="L146"> streamReader.getAttributeLocalName(i),</span>
<span class="fc" id="L147"> streamReader.getAttributeValue(i));</span>
}
}
<span class="pc bpc" id="L150" title="1 of 2 branches missed."> if (configurationName != null) {</span>
<span class="fc" id="L151"> configs.put(configurationName, configurationValues);</span>
} else {
<span class="nc" id="L153"> throw new IOException("configuration without "</span>
+ CONFIGURATIONS_CONFIGURATION_NAME);
}
<span class="fc" id="L156"> } else {</span>
<span class="nc" id="L157"> throw new IOException("unexpected tag " + qname.getLocalPart());</span>
}
}
<span class="fc" id="L160"> currentElement = qname.getLocalPart();</span>
<span class="fc" id="L161"> currentElements.add(currentElement);</span>
<span class="fc" id="L162"> break;</span>
case XMLStreamConstants.END_ELEMENT:
<span class="pc bpc" id="L164" title="1 of 2 branches missed."> if (currentElement != null</span>
<span class="fc bfc" id="L165" title="All 2 branches covered."> && currentElement.equals(CONFIGURATIONS_CONFIGURATIONS)) {</span>
<span class="fc" id="L166"> skipCurrentConfigurations = false;</span>
}
<span class="fc" id="L168"> int i = currentElements.size();</span>
<span class="fc" id="L169"> currentElements.remove((i - 1));</span>
<span class="fc bfc" id="L170" title="All 2 branches covered."> if (i > 1) {</span>
<span class="fc" id="L171"> currentElement = currentElements.get(i - 2);</span>
} else {
<span class="fc" id="L173"> currentElement = null;</span>
}
<span class="fc" id="L175"> break;</span>
case XMLStreamConstants.CHARACTERS:
break;
}
<span class="fc bfc" id="L179" title="All 2 branches covered."> if (!streamReader.hasNext()) {</span>
<span class="fc" id="L180"> break;</span>
}
<span class="fc" id="L182"> event = streamReader.next();</span>
}
} finally {
<span class="pc" id="L185"> streamReader.close();</span>
<span class="fc" id="L186"> }</span>
<span class="nc" id="L187"> } catch (XMLStreamException e) {</span>
<span class="nc" id="L188"> log.debug(e);</span>
<span class="fc" id="L189"> }</span>
<span class="fc" id="L190"> return configs;</span>
}
/**
* Read mtas char filter configurations.
*
* @param resourceLoader the resource loader
* @param configFile the config file
* @return the hash map
* @throws IOException Signals that an I/O exception has occurred.
*/
public static HashMap<String, MtasConfiguration> readMtasCharFilterConfigurations(
ResourceLoader resourceLoader, String configFile) throws IOException {
<span class="fc" id="L203"> HashMap<String, HashMap<String, String>> configs = readConfigurations(</span>
<span class="fc" id="L204"> resourceLoader, configFile, MtasCharFilterFactory.class.getName());</span>
<span class="pc bpc" id="L205" title="1 of 2 branches missed."> if (configs == null) {</span>
<span class="nc" id="L206"> throw new IOException("no configurations");</span>
} else {
<span class="fc" id="L208"> HashMap<String, MtasConfiguration> result = new HashMap<String, MtasConfiguration>();</span>
<span class="fc bfc" id="L209" title="All 2 branches covered."> for (Entry<String, HashMap<String, String>> entry : configs.entrySet()) {</span>
<span class="fc" id="L210"> HashMap<String, String> config = entry.getValue();</span>
<span class="pc bpc" id="L211" title="1 of 2 branches missed."> if (config.containsKey(CHARFILTER_CONFIGURATION_TYPE)) {</span>
<span class="fc" id="L212"> MtasConfiguration item = new MtasConfiguration();</span>
<span class="fc" id="L213"> item.attributes.put(CHARFILTER_CONFIGURATION_TYPE,</span>
<span class="fc" id="L214"> config.get(CHARFILTER_CONFIGURATION_TYPE));</span>
<span class="fc" id="L215"> item.attributes.put(CHARFILTER_CONFIGURATION_PREFIX,</span>
<span class="fc" id="L216"> config.get(CHARFILTER_CONFIGURATION_PREFIX));</span>
<span class="fc" id="L217"> item.attributes.put(CHARFILTER_CONFIGURATION_POSTFIX,</span>
<span class="fc" id="L218"> config.get(CHARFILTER_CONFIGURATION_POSTFIX));</span>
<span class="fc" id="L219"> result.put(entry.getKey(), item);</span>
<span class="fc" id="L220"> } else {</span>
<span class="nc" id="L221"> throw new IOException("configuration " + entry.getKey() + " has no "</span>
+ CHARFILTER_CONFIGURATION_TYPE);
}
<span class="fc" id="L224"> }</span>
<span class="fc" id="L225"> return result;</span>
}
}
/**
* Read mtas tokenizer configurations.
*
* @param resourceLoader the resource loader
* @param configFile the config file
* @return the hash map
* @throws IOException Signals that an I/O exception has occurred.
*/
public static HashMap<String, MtasConfiguration> readMtasTokenizerConfigurations(
ResourceLoader resourceLoader, String configFile) throws IOException {
<span class="fc" id="L239"> HashMap<String, HashMap<String, String>> configs = readConfigurations(</span>
<span class="fc" id="L240"> resourceLoader, configFile, MtasTokenizerFactory.class.getName());</span>
<span class="pc bpc" id="L241" title="1 of 2 branches missed."> if (configs == null) {</span>
<span class="nc" id="L242"> throw new IOException("no configurations");</span>
} else {
<span class="fc" id="L244"> HashMap<String, MtasConfiguration> result = new HashMap<String, MtasConfiguration>();</span>
<span class="fc bfc" id="L245" title="All 2 branches covered."> for (Entry<String, HashMap<String, String>> entry : configs.entrySet()) {</span>
<span class="fc" id="L246"> HashMap<String, String> config = entry.getValue();</span>
<span class="pc bpc" id="L247" title="1 of 2 branches missed."> if (config.containsKey(TOKENIZER_CONFIGURATION_FILE)) {</span>
<span class="fc" id="L248"> result.put(entry.getKey(), readConfiguration(resourceLoader</span>
<span class="fc" id="L249"> .openResource(config.get(TOKENIZER_CONFIGURATION_FILE))));</span>
} else {
<span class="nc" id="L251"> throw new IOException("configuration " + entry.getKey() + " has no "</span>
+ TOKENIZER_CONFIGURATION_FILE);
}
<span class="fc" id="L254"> }</span>
<span class="fc" id="L255"> return result;</span>
}
}
/**
* Read configuration.
*
* @param reader the reader
* @return the mtas configuration
* @throws IOException Signals that an I/O exception has occurred.
*/
public static MtasConfiguration readConfiguration(InputStream reader)
throws IOException {
<span class="fc" id="L268"> MtasConfiguration currentConfig = null;</span>
// parse xml
<span class="fc" id="L270"> XMLInputFactory factory = XMLInputFactory.newInstance();</span>
try {
<span class="fc" id="L272"> XMLStreamReader streamReader = factory.createXMLStreamReader(reader);</span>
QName qname;
try {
<span class="fc" id="L275"> int event = streamReader.getEventType();</span>
while (true) {
<span class="pc bpc" id="L277" title="1 of 5 branches missed."> switch (event) {</span>
case XMLStreamConstants.START_DOCUMENT:
<span class="pc bpc" id="L279" title="1 of 2 branches missed."> if (!streamReader.getCharacterEncodingScheme().equals("UTF-8")) {</span>
<span class="nc" id="L280"> throw new IOException("XML not UTF-8 encoded");</span>
}
break;
case XMLStreamConstants.END_DOCUMENT:
case XMLStreamConstants.SPACE:
<span class="nc" id="L285"> break;</span>
case XMLStreamConstants.START_ELEMENT:
// get data
<span class="fc" id="L288"> qname = streamReader.getName();</span>
<span class="fc bfc" id="L289" title="All 2 branches covered."> if (currentConfig == null) {</span>
<span class="pc bpc" id="L290" title="1 of 2 branches missed."> if (qname.getLocalPart().equals("mtas")) {</span>
<span class="fc" id="L291"> currentConfig = new MtasConfiguration();</span>
} else {
<span class="nc" id="L293"> throw new IOException("no Mtas Configuration");</span>
}
} else {
<span class="fc" id="L296"> MtasConfiguration parentConfig = currentConfig;</span>
<span class="fc" id="L297"> currentConfig = new MtasConfiguration();</span>
<span class="fc" id="L298"> parentConfig.children.add(currentConfig);</span>
<span class="fc" id="L299"> currentConfig.parent = parentConfig;</span>
<span class="fc" id="L300"> currentConfig.name = qname.getLocalPart();</span>
<span class="fc bfc" id="L301" title="All 2 branches covered."> for (int i = 0; i < streamReader.getAttributeCount(); i++) {</span>
<span class="fc" id="L302"> currentConfig.attributes.put(</span>
<span class="fc" id="L303"> streamReader.getAttributeLocalName(i),</span>
<span class="fc" id="L304"> streamReader.getAttributeValue(i));</span>
}
}
<span class="fc" id="L307"> break;</span>
case XMLStreamConstants.END_ELEMENT:
<span class="fc bfc" id="L309" title="All 2 branches covered."> if (currentConfig.parent == null) {</span>
<span class="fc" id="L310"> return currentConfig;</span>
} else {
<span class="fc" id="L312"> currentConfig = currentConfig.parent;</span>
}
<span class="fc" id="L314"> break;</span>
case XMLStreamConstants.CHARACTERS:
break;
}
<span class="pc bpc" id="L318" title="1 of 2 branches missed."> if (!streamReader.hasNext()) {</span>
<span class="nc" id="L319"> break;</span>
}
<span class="fc" id="L321"> event = streamReader.next();</span>
}
} finally {
<span class="pc" id="L324"> streamReader.close();</span>
<span class="nc" id="L325"> }</span>
<span class="nc" id="L326"> } catch (XMLStreamException e) {</span>
<span class="nc" id="L327"> log.debug(e);</span>
<span class="nc" id="L328"> }</span>
<span class="nc" id="L329"> return null;</span>
}
// public String toString() {
// return toString(0);
// }
//
// private String toString(int indent) {
// String text = "";
// if(name!=null) {
// text+=(indent>0?String.format("%"+indent+"s", ""):"")+"name: "+name+"\n";
// }
// if(attributes!=null) {
// for(String key : attributes.keySet()) {
// text+=(indent>0?String.format("%"+indent+"s", ""):"")+key+":
// "+attributes.get(key)+"\n";
// }
// }
// if(children!=null) {
// for(MtasConfiguration child : children) {
// text+=(indent>0?String.format("%"+indent+"s",
// ""):"")+child.toString(indent+2);
// }
// }
// return text;
// }
}
</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>