<?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>MtasTokenizerFactory.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">MtasTokenizerFactory.java</span></div><h1>MtasTokenizerFactory.java</h1><pre class="source lang-java linenums">package mtas.analysis.util; import mtas.analysis.MtasTokenizer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.util.ResourceLoader; import org.apache.lucene.analysis.util.ResourceLoaderAware; import org.apache.lucene.analysis.util.TokenizerFactory; import org.apache.lucene.util.AttributeFactory; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * A factory for creating MtasTokenizer objects. */ public class MtasTokenizerFactory extends TokenizerFactory implements ResourceLoaderAware { /** The Constant log. */ <span class="fc" id="L23"> private static final Log log = LogFactory.getLog(MtasTokenizerFactory.class);</span> /** The Constant ARGUMENT_CONFIGFILE. */ public static final String ARGUMENT_CONFIGFILE = "configFile"; /** The Constant ARGUMENT_CONFIG. */ public static final String ARGUMENT_CONFIG = "config"; /** The Constant ARGUMENT_DEFAULT. */ public static final String ARGUMENT_DEFAULT = "default"; /** The config argument. */ private String configArgument; /** The default argument. */ private String defaultArgument; /** The config file argument. */ private String configFileArgument; /** The configs. */ <span class="fc" id="L44"> private HashMap<String, MtasConfiguration> configs = null;</span> /** The config. */ <span class="fc" id="L47"> private MtasConfiguration config = null;</span> /** * Instantiates a new mtas tokenizer factory. * * @param args the args * @throws IOException Signals that an I/O exception has occurred. */ public MtasTokenizerFactory(Map<String, String> args) throws IOException { <span class="fc" id="L56"> this(args, null);</span> <span class="fc" id="L57"> }</span> /** * Instantiates a new mtas tokenizer factory. * * @param args the args * @param resourceLoader the resource loader * @throws IOException Signals that an I/O exception has occurred. */ public MtasTokenizerFactory(Map<String, String> args, ResourceLoader resourceLoader) throws IOException { <span class="fc" id="L68"> super(args);</span> <span class="fc" id="L69"> configFileArgument = get(args, ARGUMENT_CONFIGFILE);</span> <span class="fc" id="L70"> configArgument = get(args, ARGUMENT_CONFIG);</span> <span class="fc" id="L71"> defaultArgument = get(args, ARGUMENT_DEFAULT);</span> <span class="pc bpc" id="L72" title="1 of 4 branches missed."> if (configFileArgument != null && configArgument != null) {</span> <span class="nc" id="L73"> throw new IOException(this.getClass().getName() + " can't have both "</span> + ARGUMENT_CONFIGFILE + " and " + ARGUMENT_CONFIG); <span class="pc bpc" id="L75" title="1 of 4 branches missed."> } else if (configArgument == null && defaultArgument != null) {</span> <span class="nc" id="L76"> throw new IOException(this.getClass().getName() + " can't have "</span> + ARGUMENT_DEFAULT + " without " + ARGUMENT_CONFIG); <span class="pc bpc" id="L78" title="1 of 4 branches missed."> } else if (configFileArgument == null && configArgument == null) {</span> <span class="nc" id="L79"> throw new IOException(this.getClass().getName() + " should have "</span> + ARGUMENT_CONFIGFILE + " or " + ARGUMENT_CONFIG); } <span class="fc" id="L82"> init(resourceLoader);</span> <span class="fc" id="L83"> }</span> /* * (non-Javadoc) * * @see * org.apache.lucene.analysis.util.TokenizerFactory#create(org.apache.lucene. * util.AttributeFactory) */ @Override public MtasTokenizer create(AttributeFactory factory) { <span class="fc" id="L94"> MtasTokenizer tokenizer = null;</span> try { <span class="fc" id="L96"> tokenizer = create(factory, null);</span> <span class="nc" id="L97"> } catch (IOException e) {</span> <span class="nc" id="L98"> log.error(e);</span> <span class="fc" id="L99"> }</span> <span class="fc" id="L100"> return tokenizer;</span> } /** * Creates the. * * @param configuration the configuration * @return the mtas tokenizer * @throws IOException Signals that an I/O exception has occurred. */ public MtasTokenizer create(String configuration) throws IOException { <span class="fc" id="L111"> return create(TokenStream.DEFAULT_TOKEN_ATTRIBUTE_FACTORY, configuration);</span> } /** * Creates the. * * @param factory the factory * @param configuration the configuration * @return the mtas tokenizer * @throws IOException Signals that an I/O exception has occurred. */ public MtasTokenizer create(AttributeFactory factory, String configuration) throws IOException { <span class="pc bpc" id="L124" title="1 of 4 branches missed."> if (configs != null && configs.size() > 0) {</span> <span class="pc bpc" id="L125" title="3 of 4 branches missed."> if (configuration == null && defaultArgument == null) {</span> <span class="nc" id="L126"> throw new IOException("no (default)configuration");</span> <span class="pc bpc" id="L127" title="1 of 2 branches missed."> } else if (configuration == null) {</span> <span class="nc bnc" id="L128" title="All 2 branches missed."> if (configs.get(defaultArgument) != null) {</span> <span class="nc" id="L129"> return new MtasTokenizer(factory, configs.get(defaultArgument));</span> } else { <span class="nc" id="L131"> throw new IOException(</span> "default configuration " + defaultArgument + " not available"); } } else { <span class="fc" id="L135"> MtasConfiguration config = configs.get(configuration);</span> <span class="pc bpc" id="L136" title="1 of 2 branches missed."> if (config == null) {</span> <span class="nc bnc" id="L137" title="All 2 branches missed."> if (defaultArgument != null) {</span> <span class="nc bnc" id="L138" title="All 2 branches missed."> if (configs.get(defaultArgument) != null) {</span> <span class="nc" id="L139"> return new MtasTokenizer(factory, configs.get(defaultArgument));</span> } else { <span class="nc" id="L141"> throw new IOException("configuration " + configuration</span> + " not found and default configuration " + defaultArgument + " not available"); } } else { <span class="nc" id="L146"> throw new IOException("configuration " + configuration</span> + " not available and no default configuration"); } } else { <span class="fc" id="L150"> return new MtasTokenizer(factory, config);</span> } } <span class="pc bpc" id="L153" title="1 of 2 branches missed."> } else if (config != null) {</span> <span class="fc" id="L154"> return new MtasTokenizer(factory, config);</span> } else { <span class="nc" id="L156"> throw new IOException("no configuration");</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="L167" title="2 of 4 branches missed."> if (config == null && configs == null) {</span> <span class="fc bfc" id="L168" title="All 2 branches covered."> if (resourceLoader == null) {</span> <span class="fc" id="L169"> return;</span> <span class="pc bpc" id="L170" title="1 of 4 branches missed."> } else if (configFileArgument == null && configArgument == null) {</span> <span class="nc" id="L171"> throw new IOException("no configuration");</span> } else { <span class="fc bfc" id="L173" title="All 2 branches covered."> if (configFileArgument != null) {</span> try { <span class="fc" id="L175"> config = MtasConfiguration.readConfiguration(</span> <span class="fc" id="L176"> resourceLoader.openResource(configFileArgument));</span> <span class="nc" id="L177"> } catch (IOException e) {</span> <span class="nc" id="L178"> throw new IOException(</span> "Problem loading configuration from " + configFileArgument, e); <span class="fc" id="L180"> }</span> } <span class="fc bfc" id="L182" title="All 2 branches covered."> if (configArgument != null) {</span> try { <span class="fc" id="L184"> configs = MtasConfiguration.readMtasTokenizerConfigurations(</span> resourceLoader, configArgument); <span class="nc" id="L186"> } catch (IOException e) {</span> <span class="nc" id="L187"> throw new IOException(</span> "Problem loading configurations from " + configArgument, e); <span class="fc" id="L189"> }</span> } } } <span class="fc" id="L193"> }</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="L203"> init(loader);</span> <span class="fc" id="L204"> }</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>