MtasCQLQParser.java.html 9.42 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>MtasCQLQParser.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.solr.search</a> &gt; <span class="el_source">MtasCQLQParser.java</span></div><h1>MtasCQLQParser.java</h1><pre class="source lang-java linenums">package mtas.solr.search;

import java.io.BufferedReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import mtas.parser.cql.MtasCQLParser;
import mtas.search.spans.util.MtasSpanQuery;
import org.apache.lucene.search.Query;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import org.apache.solr.search.SyntaxError;

/**
 * The Class MtasCQLQParser.
 */
public class MtasCQLQParser extends QParser {

  /** The Constant MTAS_CQL_QPARSER_FIELD. */
  public static final String MTAS_CQL_QPARSER_FIELD = &quot;field&quot;;

  /** The Constant MTAS_CQL_QPARSER_QUERY. */
  public static final String MTAS_CQL_QPARSER_QUERY = &quot;query&quot;;

  /** The Constant MTAS_CQL_QPARSER_IGNORE. */
  public static final String MTAS_CQL_QPARSER_IGNORE = &quot;ignore&quot;;

  /** The Constant MTAS_CQL_QPARSER_MAXIMUM_IGNORE_LENGTH. */
  public static final String MTAS_CQL_QPARSER_MAXIMUM_IGNORE_LENGTH = &quot;maximumIgnoreLength&quot;;

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

  /** The field. */
<span class="fc" id="L39">  String field = null;</span>

  /** The cql. */
<span class="fc" id="L42">  String cql = null;</span>

  /** The ignore query. */
<span class="fc" id="L45">  String ignoreQuery = null;</span>

  /** The maximum ignore length. */
<span class="fc" id="L48">  Integer maximumIgnoreLength = null;</span>

  /** The default prefix. */
<span class="fc" id="L51">  String defaultPrefix = null;</span>

  /** The variables. */
<span class="fc" id="L54">  HashMap&lt;String, String[]&gt; variables = null;</span>

  /**
   * Instantiates a new mtas CQLQ parser.
   *
   * @param qstr the qstr
   * @param localParams the local params
   * @param params the params
   * @param req the req
   */
  public MtasCQLQParser(String qstr, SolrParams localParams, SolrParams params,
      SolrQueryRequest req) {
<span class="fc" id="L66">    super(qstr, localParams, params, req);</span>

<span class="pc bpc" id="L68" title="1 of 2 branches missed.">    if ((localParams.getParams(MTAS_CQL_QPARSER_FIELD) != null)</span>
<span class="pc bpc" id="L69" title="1 of 2 branches missed.">        &amp;&amp; (localParams.getParams(MTAS_CQL_QPARSER_FIELD).length == 1)) {</span>
<span class="fc" id="L70">      field = localParams.getParams(MTAS_CQL_QPARSER_FIELD)[0];</span>
    }
<span class="pc bpc" id="L72" title="1 of 2 branches missed.">    if ((localParams.getParams(MTAS_CQL_QPARSER_QUERY) != null)</span>
<span class="pc bpc" id="L73" title="1 of 2 branches missed.">        &amp;&amp; (localParams.getParams(MTAS_CQL_QPARSER_QUERY).length == 1)) {</span>
<span class="fc" id="L74">      cql = localParams.getParams(MTAS_CQL_QPARSER_QUERY)[0];</span>
    }
<span class="pc bpc" id="L76" title="1 of 2 branches missed.">    if ((localParams.getParams(MTAS_CQL_QPARSER_IGNORE) != null)</span>
<span class="nc bnc" id="L77" title="All 2 branches missed.">        &amp;&amp; (localParams.getParams(MTAS_CQL_QPARSER_IGNORE).length == 1)) {</span>
<span class="nc" id="L78">      ignoreQuery = localParams.getParams(MTAS_CQL_QPARSER_IGNORE)[0];</span>
    }
<span class="pc bpc" id="L80" title="1 of 2 branches missed.">    if ((localParams.getParams(MTAS_CQL_QPARSER_MAXIMUM_IGNORE_LENGTH) != null)</span>
        &amp;&amp; (localParams
<span class="nc bnc" id="L82" title="All 2 branches missed.">            .getParams(MTAS_CQL_QPARSER_MAXIMUM_IGNORE_LENGTH).length == 1)) {</span>
      try {
<span class="nc" id="L84">        maximumIgnoreLength = Integer.parseInt(</span>
<span class="nc" id="L85">            localParams.getParams(MTAS_CQL_QPARSER_MAXIMUM_IGNORE_LENGTH)[0]);</span>
<span class="nc" id="L86">      } catch (NumberFormatException e) {</span>
<span class="nc" id="L87">        maximumIgnoreLength = null;</span>
<span class="nc" id="L88">      }</span>
    }
<span class="pc bpc" id="L90" title="1 of 2 branches missed.">    if ((localParams.getParams(MTAS_CQL_QPARSER_PREFIX) != null)</span>
<span class="nc bnc" id="L91" title="All 2 branches missed.">        &amp;&amp; (localParams.getParams(MTAS_CQL_QPARSER_PREFIX).length == 1)) {</span>
<span class="nc" id="L92">      defaultPrefix = localParams.getParams(MTAS_CQL_QPARSER_PREFIX)[0];</span>
    }
<span class="fc" id="L94">    variables = new HashMap&lt;&gt;();</span>
<span class="fc" id="L95">    Iterator&lt;String&gt; it = localParams.getParameterNamesIterator();</span>
<span class="fc bfc" id="L96" title="All 2 branches covered.">    while (it.hasNext()) {</span>
<span class="fc" id="L97">      String item = it.next();</span>
<span class="pc bpc" id="L98" title="1 of 2 branches missed.">      if (item.startsWith(&quot;variable_&quot;)) {</span>
<span class="nc bnc" id="L99" title="All 2 branches missed.">        if (localParams.getParams(item).length == 0</span>
<span class="nc bnc" id="L100" title="All 2 branches missed.">            || (localParams.getParams(item).length == 1</span>
<span class="nc bnc" id="L101" title="All 2 branches missed.">                &amp;&amp; localParams.getParams(item)[0].isEmpty())) {</span>
<span class="nc" id="L102">          variables.put(item.substring(9), new String[0]);</span>
        } else {
<span class="nc" id="L104">          ArrayList&lt;String&gt; list = new ArrayList&lt;&gt;();</span>
<span class="nc bnc" id="L105" title="All 2 branches missed.">          for (int i = 0; i &lt; localParams.getParams(item).length; i++) {</span>
<span class="nc" id="L106">            String[] subList = localParams.getParams(item)[i]</span>
<span class="nc" id="L107">                .split(&quot;(?&lt;!\\\\),&quot;);</span>
<span class="nc bnc" id="L108" title="All 2 branches missed.">            for (int j = 0; j &lt; subList.length; j++) {</span>
<span class="nc" id="L109">              list.add(subList[j].replace(&quot;\\,&quot;, &quot;,&quot;).replace(&quot;\\\\&quot;, &quot;\\&quot;));</span>
            }
          }
<span class="nc" id="L112">          variables.put(item.substring(9),</span>
<span class="nc" id="L113">              list.toArray(new String[list.size()]));</span>
        }
      }
<span class="fc" id="L116">    }    </span>
<span class="fc" id="L117">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.solr.search.QParser#parse()
   */
  @Override
  public Query parse() throws SyntaxError {
<span class="pc bpc" id="L126" title="1 of 2 branches missed.">    if (field == null) {</span>
<span class="nc" id="L127">      throw new SyntaxError(&quot;no &quot; + MTAS_CQL_QPARSER_FIELD);</span>
<span class="pc bpc" id="L128" title="1 of 2 branches missed.">    } else if (cql == null) {</span>
<span class="nc" id="L129">      throw new SyntaxError(&quot;no &quot; + MTAS_CQL_QPARSER_QUERY);</span>
    } else {
<span class="fc" id="L131">      MtasSpanQuery q = null;</span>
<span class="fc" id="L132">      MtasSpanQuery iq = null;</span>
<span class="pc bpc" id="L133" title="1 of 2 branches missed.">      if (ignoreQuery != null) {</span>
<span class="nc" id="L134">        Reader ignoreReader = new BufferedReader(new StringReader(ignoreQuery));</span>
<span class="nc" id="L135">        MtasCQLParser ignoreParser = new MtasCQLParser(ignoreReader);</span>
        try {
<span class="nc" id="L137">          iq = ignoreParser.parse(field, null, null, null, null);</span>
<span class="nc" id="L138">        } catch (mtas.parser.cql.TokenMgrError</span>
            | mtas.parser.cql.ParseException e) {
<span class="nc" id="L140">          throw new SyntaxError(e);</span>
<span class="nc" id="L141">        }</span>
      }
<span class="fc" id="L143">      Reader queryReader = new BufferedReader(new StringReader(cql));</span>
<span class="fc" id="L144">      MtasCQLParser queryParser = new MtasCQLParser(queryReader);</span>
      try {
<span class="fc" id="L146">        q = queryParser.parse(field, defaultPrefix, variables, iq,</span>
            maximumIgnoreLength);
<span class="nc" id="L148">      } catch (mtas.parser.cql.TokenMgrError</span>
          | mtas.parser.cql.ParseException e) {
<span class="nc" id="L150">        throw new SyntaxError(e);</span>
<span class="fc" id="L151">      }</span>
<span class="fc" id="L152">      return q;</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>