MtasCQLQParser.java.html 9.08 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="L40">  String field = null;</span>

  /** The cql. */
<span class="fc" id="L43">  String cql = null;</span>
  
  /** The ignore query. */
<span class="fc" id="L46">  String ignoreQuery = null;</span>
  
  /** The maximum ignore length. */
<span class="fc" id="L49">  Integer maximumIgnoreLength = null;</span>

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

  /** The variables. */
<span class="fc" id="L55">  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="L67">    super(qstr, localParams, params, req);</span>

<span class="pc bpc" id="L69" title="1 of 2 branches missed.">    if ((localParams.getParams(MTAS_CQL_QPARSER_FIELD) != null)</span>
<span class="pc bpc" id="L70" title="1 of 2 branches missed.">        &amp;&amp; (localParams.getParams(MTAS_CQL_QPARSER_FIELD).length == 1)) {</span>
<span class="fc" id="L71">      field = localParams.getParams(MTAS_CQL_QPARSER_FIELD)[0];</span>
    }
<span class="pc bpc" id="L73" title="1 of 2 branches missed.">    if ((localParams.getParams(MTAS_CQL_QPARSER_QUERY) != null)</span>
<span class="pc bpc" id="L74" title="1 of 2 branches missed.">        &amp;&amp; (localParams.getParams(MTAS_CQL_QPARSER_QUERY).length == 1)) {</span>
<span class="fc" id="L75">      cql = localParams.getParams(MTAS_CQL_QPARSER_QUERY)[0];</span>
    }
<span class="pc bpc" id="L77" title="1 of 2 branches missed.">    if ((localParams.getParams(MTAS_CQL_QPARSER_IGNORE) != null)</span>
<span class="nc bnc" id="L78" title="All 2 branches missed.">        &amp;&amp; (localParams.getParams(MTAS_CQL_QPARSER_IGNORE).length == 1)) {</span>
<span class="nc" id="L79">      ignoreQuery = localParams.getParams(MTAS_CQL_QPARSER_IGNORE)[0];</span>
    }
<span class="pc bpc" id="L81" title="1 of 2 branches missed.">    if ((localParams.getParams(MTAS_CQL_QPARSER_MAXIMUM_IGNORE_LENGTH) != null)</span>
<span class="nc bnc" id="L82" title="All 2 branches missed.">        &amp;&amp; (localParams.getParams(MTAS_CQL_QPARSER_MAXIMUM_IGNORE_LENGTH).length == 1)) {</span>
      try {
<span class="nc" id="L84">        maximumIgnoreLength = Integer.parseInt(localParams.getParams(MTAS_CQL_QPARSER_MAXIMUM_IGNORE_LENGTH)[0]);</span>
<span class="nc" id="L85">      } catch (NumberFormatException e) {</span>
<span class="nc" id="L86">        maximumIgnoreLength = null;</span>
<span class="nc" id="L87">      }</span>
    }
<span class="pc bpc" id="L89" title="1 of 2 branches missed.">    if ((localParams.getParams(MTAS_CQL_QPARSER_PREFIX) != null)</span>
        &amp;&amp; (localParams
<span class="nc bnc" id="L91" title="All 2 branches missed.">            .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 6 branches missed.">        if(localParams.getParams(item).length==0 || (localParams.getParams(item).length==1 &amp;&amp; localParams.getParams(item)[0].isEmpty())) {</span>
<span class="nc" id="L100">          variables.put(item.substring(9),new String[0]);</span>
        } else {
<span class="nc" id="L102">          ArrayList&lt;String&gt; list = new ArrayList&lt;&gt;();</span>
<span class="nc bnc" id="L103" title="All 2 branches missed.">          for(int i=0; i&lt;localParams.getParams(item).length; i++) {</span>
<span class="nc" id="L104">            String[] subList = localParams.getParams(item)[i].split(&quot;(?&lt;!\\\\),&quot;);</span>
<span class="nc bnc" id="L105" title="All 2 branches missed.">            for(int j=0; j&lt;subList.length; j++) {</span>
<span class="nc" id="L106">              list.add(subList[j].replace(&quot;\\,&quot;, &quot;,&quot;).replace(&quot;\\\\&quot;, &quot;\\&quot;));</span>
            }
          }
<span class="nc" id="L109">          variables.put(item.substring(9), list.toArray(new String[list.size()]));</span>
        }
      }
<span class="fc" id="L112">    }    </span>
<span class="fc" id="L113">  }</span>

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