MtasConfiguration.java.html 21.5 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>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> &gt; <a href="index.source.html" class="el_package">mtas.analysis.util</a> &gt; <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 = &quot;mtas&quot;;

  /** The Constant CONFIGURATIONS_CONFIGURATIONS. */
  public static final String CONFIGURATIONS_CONFIGURATIONS = &quot;configurations&quot;;

  /** The Constant CONFIGURATIONS_CONFIGURATION. */
  public static final String CONFIGURATIONS_CONFIGURATION = &quot;configuration&quot;;

  /** The Constant CONFIGURATIONS_CONFIGURATION_NAME. */
  public static final String CONFIGURATIONS_CONFIGURATION_NAME = &quot;name&quot;;

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

  /** The Constant CHARFILTER_CONFIGURATION_TYPE. */
  public static final String CHARFILTER_CONFIGURATION_TYPE = &quot;type&quot;;

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

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

  /** The name. */
  public String name;

  /** The attributes. */
  public HashMap&lt;String, String&gt; attributes;

  /** The children. */
  public List&lt;MtasConfiguration&gt; 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&lt;String, String&gt;();</span>
<span class="fc" id="L70">    children = new ArrayList&lt;MtasConfiguration&gt;();</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&lt;String, HashMap&lt;String, String&gt;&gt; readConfigurations(
      ResourceLoader resourceLoader, String configFile, String className)
      throws IOException {
<span class="fc" id="L90">    HashMap&lt;String, HashMap&lt;String, String&gt;&gt; configs = null;</span>
<span class="fc" id="L91">    InputStream reader = resourceLoader.openResource(configFile);</span>
    // parse xml
<span class="fc" id="L93">    XMLInputFactory factory = XMLInputFactory.newInstance();</span>
    try {
<span class="fc" id="L95">      XMLStreamReader streamReader = factory.createXMLStreamReader(reader);</span>
<span class="fc" id="L96">      String currentElement = null;</span>
<span class="fc" id="L97">      ArrayList&lt;String&gt; currentElements = new ArrayList&lt;String&gt;();</span>
      QName qname;
<span class="fc" id="L99">      boolean skipCurrentConfigurations = false;</span>
      try {
<span class="fc" id="L101">        int event = streamReader.getEventType();</span>
        while (true) {
<span class="pc bpc" id="L103" title="1 of 6 branches missed.">          switch (event) {</span>
          case XMLStreamConstants.START_DOCUMENT:
<span class="pc bpc" id="L105" title="1 of 2 branches missed.">            if (!streamReader.getCharacterEncodingScheme().equals(&quot;UTF-8&quot;)) {</span>
<span class="nc" id="L106">              throw new IOException(&quot;XML not UTF-8 encoded&quot;);</span>
            }
            break;
          case XMLStreamConstants.END_DOCUMENT:
<span class="fc" id="L110">            break;</span>
          case XMLStreamConstants.SPACE:
<span class="nc" id="L112">            break;</span>
          case XMLStreamConstants.START_ELEMENT:
            // get data
<span class="fc" id="L115">            qname = streamReader.getName();</span>
<span class="fc bfc" id="L116" title="All 2 branches covered.">            if (configs == null) {</span>
<span class="pc bpc" id="L117" title="1 of 2 branches missed.">              if (qname.getLocalPart().equals(CONFIGURATIONS_MTAS)) {</span>
<span class="fc" id="L118">                configs = new HashMap&lt;String, HashMap&lt;String, String&gt;&gt;();</span>
              } else {
<span class="nc" id="L120">                throw new IOException(&quot;no Mtas Configurations File&quot;);</span>
              }
<span class="pc bpc" id="L122" title="1 of 2 branches missed.">            } else if (currentElement != null</span>
<span class="fc bfc" id="L123" title="All 2 branches covered.">                &amp;&amp; currentElement.equals(CONFIGURATIONS_MTAS)) {</span>
<span class="pc bpc" id="L124" title="1 of 2 branches missed.">              if (qname.getLocalPart().equals(CONFIGURATIONS_CONFIGURATIONS)) {</span>
<span class="fc" id="L125">                skipCurrentConfigurations = true;</span>
<span class="pc bpc" id="L126" title="1 of 2 branches missed.">                if (className != null) {</span>
<span class="fc bfc" id="L127" title="All 2 branches covered.">                  for (int i = 0; i &lt; streamReader.getAttributeCount(); i++) {</span>
<span class="pc bpc" id="L128" title="1 of 2 branches missed.">                    if (streamReader.getAttributeLocalName(i).equals(&quot;type&quot;)) {</span>
<span class="fc bfc" id="L129" title="All 2 branches covered.">                      if (streamReader.getAttributeValue(i).equals(className)) {</span>
<span class="fc" id="L130">                        skipCurrentConfigurations = false;</span>
                      }
                    }
                  }
                }
              } else {
<span class="nc" id="L136">                throw new IOException(&quot;unexpected &quot; + qname.getLocalPart());</span>
              }
<span class="pc bpc" id="L138" title="1 of 2 branches missed.">            } else if (currentElement != null</span>
<span class="pc bpc" id="L139" title="1 of 4 branches missed.">                &amp;&amp; currentElement.equals(CONFIGURATIONS_CONFIGURATIONS)</span>
                &amp;&amp; !skipCurrentConfigurations) {
<span class="pc bpc" id="L141" title="1 of 2 branches missed.">              if (qname.getLocalPart().equals(CONFIGURATIONS_CONFIGURATION)) {</span>
<span class="fc" id="L142">                String configurationName = null;</span>
<span class="fc" id="L143">                HashMap&lt;String, String&gt; configurationValues = new HashMap&lt;String, String&gt;();</span>
<span class="fc bfc" id="L144" title="All 2 branches covered.">                for (int i = 0; i &lt; streamReader.getAttributeCount(); i++) {</span>
<span class="fc" id="L145">                  if (streamReader.getAttributeLocalName(i)</span>
<span class="fc bfc" id="L146" title="All 2 branches covered.">                      .equals(CONFIGURATIONS_CONFIGURATION_NAME)) {</span>
<span class="fc" id="L147">                    configurationName = streamReader.getAttributeValue(i);</span>
                  } else {
<span class="fc" id="L149">                    configurationValues.put(</span>
<span class="fc" id="L150">                        streamReader.getAttributeLocalName(i),</span>
<span class="fc" id="L151">                        streamReader.getAttributeValue(i));</span>
                  }
                }
<span class="pc bpc" id="L154" title="1 of 2 branches missed.">                if (configurationName != null) {</span>
<span class="fc" id="L155">                  configs.put(configurationName, configurationValues);</span>
                } else {
<span class="nc" id="L157">                  throw new IOException(&quot;configuration without &quot;</span>
                      + CONFIGURATIONS_CONFIGURATION_NAME);
                }
<span class="fc" id="L160">              } else {</span>
<span class="nc" id="L161">                throw new IOException(&quot;unexpected tag &quot; + qname.getLocalPart());</span>
              }
            }
<span class="fc" id="L164">            currentElement = qname.getLocalPart();</span>
<span class="fc" id="L165">            currentElements.add(currentElement);</span>
<span class="fc" id="L166">            break;</span>
          case XMLStreamConstants.END_ELEMENT:
<span class="pc bpc" id="L168" title="1 of 2 branches missed.">            if (currentElement != null</span>
<span class="fc bfc" id="L169" title="All 2 branches covered.">                &amp;&amp; currentElement.equals(CONFIGURATIONS_CONFIGURATIONS)) {</span>
<span class="fc" id="L170">              skipCurrentConfigurations = false;</span>
            }
<span class="fc" id="L172">            int i = currentElements.size();</span>
<span class="fc" id="L173">            currentElements.remove((i - 1));</span>
<span class="fc bfc" id="L174" title="All 2 branches covered.">            if (i &gt; 1) {</span>
<span class="fc" id="L175">              currentElement = currentElements.get(i - 2);</span>
            } else {
<span class="fc" id="L177">              currentElement = null;</span>
            }
<span class="fc" id="L179">            break;</span>
          case XMLStreamConstants.CHARACTERS:
            break;
          }
<span class="fc bfc" id="L183" title="All 2 branches covered.">          if (!streamReader.hasNext()) {</span>
<span class="fc" id="L184">            break;</span>
          }
<span class="fc" id="L186">          event = streamReader.next();</span>
        }
      } finally {
<span class="pc" id="L189">        streamReader.close();</span>
<span class="fc" id="L190">      }</span>
<span class="nc" id="L191">    } catch (XMLStreamException e) {</span>
<span class="nc" id="L192">      log.debug(e);</span>
<span class="fc" id="L193">    }</span>
<span class="fc" id="L194">    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&lt;String, MtasConfiguration&gt; readMtasCharFilterConfigurations(
      ResourceLoader resourceLoader, String configFile) throws IOException {
<span class="fc" id="L210">    HashMap&lt;String, HashMap&lt;String, String&gt;&gt; configs = readConfigurations(</span>
<span class="fc" id="L211">        resourceLoader, configFile, MtasCharFilterFactory.class.getName());</span>
<span class="pc bpc" id="L212" title="1 of 2 branches missed.">    if (configs == null) {</span>
<span class="nc" id="L213">      throw new IOException(&quot;no configurations&quot;);</span>
    } else {
<span class="fc" id="L215">      HashMap&lt;String, MtasConfiguration&gt; result = new HashMap&lt;String, MtasConfiguration&gt;();</span>
<span class="fc bfc" id="L216" title="All 2 branches covered.">      for (Entry&lt;String, HashMap&lt;String, String&gt;&gt; entry : configs.entrySet()) {</span>
<span class="fc" id="L217">        HashMap&lt;String, String&gt; config = entry.getValue();</span>
<span class="pc bpc" id="L218" title="1 of 2 branches missed.">        if (config.containsKey(CHARFILTER_CONFIGURATION_TYPE)) {</span>
<span class="fc" id="L219">          MtasConfiguration item = new MtasConfiguration();</span>
<span class="fc" id="L220">          item.attributes.put(CHARFILTER_CONFIGURATION_TYPE,</span>
<span class="fc" id="L221">              config.get(CHARFILTER_CONFIGURATION_TYPE));</span>
<span class="fc" id="L222">          item.attributes.put(CHARFILTER_CONFIGURATION_PREFIX,</span>
<span class="fc" id="L223">              config.get(CHARFILTER_CONFIGURATION_PREFIX));</span>
<span class="fc" id="L224">          item.attributes.put(CHARFILTER_CONFIGURATION_POSTFIX,</span>
<span class="fc" id="L225">              config.get(CHARFILTER_CONFIGURATION_POSTFIX));</span>
<span class="fc" id="L226">          result.put(entry.getKey(), item);</span>
<span class="fc" id="L227">        } else {</span>
<span class="nc" id="L228">          throw new IOException(&quot;configuration &quot; + entry.getKey() + &quot; has no &quot;</span>
              + CHARFILTER_CONFIGURATION_TYPE);
        }
<span class="fc" id="L231">      }</span>
<span class="fc" id="L232">      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&lt;String, MtasConfiguration&gt; readMtasTokenizerConfigurations(
      ResourceLoader resourceLoader, String configFile) throws IOException {
<span class="fc" id="L249">    HashMap&lt;String, HashMap&lt;String, String&gt;&gt; configs = readConfigurations(</span>
<span class="fc" id="L250">        resourceLoader, configFile, MtasTokenizerFactory.class.getName());</span>
<span class="pc bpc" id="L251" title="1 of 2 branches missed.">    if (configs == null) {</span>
<span class="nc" id="L252">      throw new IOException(&quot;no configurations&quot;);</span>
    } else {
<span class="fc" id="L254">      HashMap&lt;String, MtasConfiguration&gt; result = new HashMap&lt;String, MtasConfiguration&gt;();</span>
<span class="fc bfc" id="L255" title="All 2 branches covered.">      for (Entry&lt;String, HashMap&lt;String, String&gt;&gt; entry : configs.entrySet()) {</span>
<span class="fc" id="L256">        HashMap&lt;String, String&gt; config = entry.getValue();</span>
<span class="pc bpc" id="L257" title="1 of 2 branches missed.">        if (config.containsKey(TOKENIZER_CONFIGURATION_FILE)) {</span>
<span class="fc" id="L258">          result.put(entry.getKey(), readConfiguration(resourceLoader</span>
<span class="fc" id="L259">              .openResource(config.get(TOKENIZER_CONFIGURATION_FILE))));</span>
        } else {
<span class="nc" id="L261">          throw new IOException(&quot;configuration &quot; + entry.getKey() + &quot; has no &quot;</span>
              + TOKENIZER_CONFIGURATION_FILE);
        }
<span class="fc" id="L264">      }</span>
<span class="fc" id="L265">      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="L280">    MtasConfiguration currentConfig = null;</span>
    // parse xml
<span class="fc" id="L282">    XMLInputFactory factory = XMLInputFactory.newInstance();</span>
    try {
<span class="fc" id="L284">      XMLStreamReader streamReader = factory.createXMLStreamReader(reader);</span>
      QName qname;
      try {
<span class="fc" id="L287">        int event = streamReader.getEventType();</span>
        while (true) {
<span class="pc bpc" id="L289" title="1 of 5 branches missed.">          switch (event) {</span>
          case XMLStreamConstants.START_DOCUMENT:
<span class="pc bpc" id="L291" title="1 of 2 branches missed.">            if (!streamReader.getCharacterEncodingScheme().equals(&quot;UTF-8&quot;)) {</span>
<span class="nc" id="L292">              throw new IOException(&quot;XML not UTF-8 encoded&quot;);</span>
            }
            break;
          case XMLStreamConstants.END_DOCUMENT:
          case XMLStreamConstants.SPACE:
<span class="nc" id="L297">            break;</span>
          case XMLStreamConstants.START_ELEMENT:
            // get data
<span class="fc" id="L300">            qname = streamReader.getName();</span>
<span class="fc bfc" id="L301" title="All 2 branches covered.">            if (currentConfig == null) {</span>
<span class="pc bpc" id="L302" title="1 of 2 branches missed.">              if (qname.getLocalPart().equals(&quot;mtas&quot;)) {</span>
<span class="fc" id="L303">                currentConfig = new MtasConfiguration();</span>
              } else {
<span class="nc" id="L305">                throw new IOException(&quot;no Mtas Configuration&quot;);</span>
              }
            } else {
<span class="fc" id="L308">              MtasConfiguration parentConfig = currentConfig;</span>
<span class="fc" id="L309">              currentConfig = new MtasConfiguration();</span>
<span class="fc" id="L310">              parentConfig.children.add(currentConfig);</span>
<span class="fc" id="L311">              currentConfig.parent = parentConfig;</span>
<span class="fc" id="L312">              currentConfig.name = qname.getLocalPart();</span>
<span class="fc bfc" id="L313" title="All 2 branches covered.">              for (int i = 0; i &lt; streamReader.getAttributeCount(); i++) {</span>
<span class="fc" id="L314">                currentConfig.attributes.put(</span>
<span class="fc" id="L315">                    streamReader.getAttributeLocalName(i),</span>
<span class="fc" id="L316">                    streamReader.getAttributeValue(i));</span>
              }
            }
<span class="fc" id="L319">            break;</span>
          case XMLStreamConstants.END_ELEMENT:
<span class="fc bfc" id="L321" title="All 2 branches covered.">            if (currentConfig.parent == null) {</span>
<span class="fc" id="L322">              return currentConfig;</span>
            } else {
<span class="fc" id="L324">              currentConfig = currentConfig.parent;</span>
            }
<span class="fc" id="L326">            break;</span>
          case XMLStreamConstants.CHARACTERS:
            break;
          }
<span class="pc bpc" id="L330" title="1 of 2 branches missed.">          if (!streamReader.hasNext()) {</span>
<span class="nc" id="L331">            break;</span>
          }
<span class="fc" id="L333">          event = streamReader.next();</span>
        }
      } finally {
<span class="pc" id="L336">        streamReader.close();</span>
<span class="nc" id="L337">      }</span>
<span class="nc" id="L338">    } catch (XMLStreamException e) {</span>
<span class="nc" id="L339">      log.debug(e);</span>
<span class="nc" id="L340">    }</span>
<span class="nc" id="L341">    return null;</span>
  }

  public String toString() {
<span class="nc" id="L345">    return toString(0);</span>
  }

  private String toString(int indent) {
<span class="nc" id="L349">    String text = &quot;&quot;;</span>
<span class="nc bnc" id="L350" title="All 2 branches missed.">    if (name != null) {</span>
<span class="nc bnc" id="L351" title="All 2 branches missed.">      text += (indent &gt; 0 ? String.format(&quot;%&quot; + indent + &quot;s&quot;, &quot;&quot;) : &quot;&quot;)</span>
          + &quot;name: &quot; + name + &quot;\n&quot;;
    }
<span class="nc bnc" id="L354" title="All 2 branches missed.">    if (attributes != null) {</span>
<span class="nc bnc" id="L355" title="All 2 branches missed.">      for (Entry&lt;String,String&gt; entry : attributes.entrySet()) {</span>
<span class="nc bnc" id="L356" title="All 2 branches missed.">        text += (indent &gt; 0 ? String.format(&quot;%&quot; + indent + &quot;s&quot;, &quot;&quot;) : &quot;&quot;) + entry.getKey()</span>
<span class="nc" id="L357">            + &quot;:&quot; + entry.getValue() + &quot;\n&quot;;</span>
<span class="nc" id="L358">      }</span>
    }
<span class="nc bnc" id="L360" title="All 2 branches missed.">    if (children != null) {</span>
<span class="nc bnc" id="L361" title="All 2 branches missed.">      for (MtasConfiguration child : children) {</span>
<span class="nc bnc" id="L362" title="All 2 branches missed.">        text += (indent &gt; 0 ? String.format(&quot;%&quot; + indent + &quot;s&quot;, &quot;&quot;) : &quot;&quot;)</span>
<span class="nc" id="L363">            + child.toString(indent + 2);</span>
<span class="nc" id="L364">      }</span>
    }
<span class="nc" id="L366">    return text;</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>