MtasCharFilterFactory.java.html 13.6 KB
<?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> &gt; <a href="index.source.html" class="el_package">mtas.analysis.util</a> &gt; <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 = &quot;type&quot;;

  /** The Constant ARGUMENT_PREFIX. */
  public static final String ARGUMENT_PREFIX = &quot;prefix&quot;;

  /** The Constant ARGUMENT_POSTFIX. */
  public static final String ARGUMENT_POSTFIX = &quot;postfix&quot;;

  /** The Constant ARGUMENT_CONFIG. */
  public static final String ARGUMENT_CONFIG = &quot;config&quot;;

  /** The Constant ARGUMENT_DEFAULT. */
  public static final String ARGUMENT_DEFAULT = &quot;default&quot;;

  /** The Constant VALUE_TYPE_URL. */
  public static final String VALUE_TYPE_URL = &quot;url&quot;;

  /** The Constant VALUE_TYPE_FILE. */
  public static final String VALUE_TYPE_FILE = &quot;file&quot;;

  /** 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&lt;String, MtasConfiguration&gt; 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&lt;String, String&gt; 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&lt;String, String&gt; 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 &amp;&amp; configArgument != null) {</span>
<span class="nc" id="L92">      throw new IOException(this.getClass().getName() + &quot; can't have both &quot;</span>
          + ARGUMENT_TYPE + &quot; and &quot; + ARGUMENT_CONFIG);
<span class="pc bpc" id="L94" title="1 of 4 branches missed.">    } else if (typeArgument == null &amp;&amp; prefixArgument != null) {</span>
<span class="nc" id="L95">      throw new IOException(this.getClass().getName() + &quot; can't have &quot;</span>
          + ARGUMENT_PREFIX + &quot; without &quot; + ARGUMENT_TYPE);
<span class="pc bpc" id="L97" title="1 of 4 branches missed.">    } else if (typeArgument == null &amp;&amp; postfixArgument != null) {</span>
<span class="nc" id="L98">      throw new IOException(this.getClass().getName() + &quot; can't have &quot;</span>
          + ARGUMENT_POSTFIX + &quot; without &quot; + ARGUMENT_TYPE);
<span class="pc bpc" id="L100" title="1 of 4 branches missed.">    } else if (configArgument == null &amp;&amp; defaultArgument != null) {</span>
<span class="nc" id="L101">      throw new IOException(this.getClass().getName() + &quot; can't have &quot;</span>
          + ARGUMENT_DEFAULT + &quot; without &quot; + ARGUMENT_CONFIG);
<span class="pc bpc" id="L103" title="1 of 4 branches missed.">    } else if (typeArgument == null &amp;&amp; configArgument == null) {</span>
<span class="nc" id="L104">      throw new IOException(this.getClass().getName() + &quot; should have &quot;</span>
          + ARGUMENT_TYPE + &quot; or &quot; + 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 &amp;&amp; configs == null) {</span>
<span class="pc bpc" id="L118" title="1 of 4 branches missed.">      if (typeArgument == null &amp;&amp; configArgument == null) {</span>
<span class="nc" id="L119">        throw new IOException(&quot;no configuration&quot;);</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>
                  &quot;problem loading configurations from &quot; + 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 &amp;&amp; configs.size() &gt; 0) {</span>
<span class="pc bpc" id="L174" title="3 of 4 branches missed.">      if (configuration == null &amp;&amp; defaultArgument == null) {</span>
<span class="nc" id="L175">        throw new IOException(&quot;no (default)configuration&quot;);</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>
              &quot;default configuration &quot; + defaultArgument + &quot; not available&quot;);
        }
      } 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(&quot;configuration &quot; + configuration</span>
                  + &quot; not found and default configuration &quot; + defaultArgument
                  + &quot; not available&quot;);
            }
          } else {
<span class="nc" id="L195">            throw new IOException(&quot;configuration &quot; + configuration</span>
                + &quot; not available and no default configuration&quot;);
          }
        } 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(&quot;no configuration&quot;);</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>