MtasSolrResultUtil.java.html 26.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
<?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>MtasSolrResultUtil.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.handler.component.util</a> &gt; <span class="el_source">MtasSolrResultUtil.java</span></div><h1>MtasSolrResultUtil.java</h1><pre class="source lang-java linenums">package mtas.solr.handler.component.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.Base64;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;

import mtas.codec.util.DataCollector;
import mtas.codec.util.CodecComponent.GroupHit;
import mtas.codec.util.collector.MtasDataItem;
import mtas.parser.cql.MtasCQLParser;
import mtas.parser.cql.TokenMgrError;
import mtas.search.spans.util.MtasSpanQuery;
import mtas.solr.handler.component.MtasSolrSearchComponent;

/**
 * The Class MtasSolrResultUtil.
 */
public class MtasSolrResultUtil {

  /** The Constant log. */
<span class="fc" id="L44">  private static final Log log = LogFactory.getLog(MtasSolrResultUtil.class);</span>

  /** The Constant QUERY_TYPE_CQL. */
  public static final String QUERY_TYPE_CQL = &quot;cql&quot;;

  /** The Constant patternKeyStartGrouphit. */
<span class="fc" id="L50">  public static final Pattern patternKeyStartGrouphit = Pattern</span>
<span class="fc" id="L51">      .compile(&quot;^&quot; + GroupHit.KEY_START);</span>

  /**
   * Instantiates a new mtas solr result util.
   */
<span class="nc" id="L56">  private MtasSolrResultUtil() {</span>
    // do nothing
<span class="nc" id="L58">  }</span>

  /**
   * Rewrite.
   *
   * @param al the al
   * @param searchComponent the search component
   * @throws IOException Signals that an I/O exception has occurred.
   */
  @SuppressWarnings({ &quot;unchecked&quot;, &quot;rawtypes&quot; })
  public static void rewrite(ArrayList&lt;?&gt; al,
      MtasSolrSearchComponent searchComponent) throws IOException {
<span class="fc bfc" id="L70" title="All 2 branches covered.">    for (int i = 0; i &lt; al.size(); i++) {</span>
<span class="pc bpc" id="L71" title="1 of 2 branches missed.">      if (al.get(i) instanceof NamedList) {</span>
<span class="fc" id="L72">        rewrite((NamedList) al.get(i), searchComponent);</span>
<span class="nc bnc" id="L73" title="All 2 branches missed.">      } else if (al.get(i) instanceof ArrayList) {</span>
<span class="nc" id="L74">        rewrite((ArrayList) al.get(i), searchComponent);</span>
      }
    }
<span class="fc" id="L77">  }</span>

  /**
   * Rewrite.
   *
   * @param nl the nl
   * @param searchComponent the search component
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public static void rewrite(NamedList&lt;Object&gt; nl,
      MtasSolrSearchComponent searchComponent) throws IOException {
<span class="fc" id="L88">    rewrite(nl, searchComponent, true);</span>
<span class="fc" id="L89">  }</span>

  /**
   * Rewrite.
   *
   * @param nl the nl
   * @param searchComponent the search component
   * @param doCollapse the do collapse
   * @throws IOException Signals that an I/O exception has occurred.
   */
  @SuppressWarnings({ &quot;rawtypes&quot;, &quot;unchecked&quot; })
  private static void rewrite(NamedList&lt;Object&gt; nl,
      MtasSolrSearchComponent searchComponent, boolean doCollapse)
      throws IOException {
<span class="fc" id="L103">    boolean showDebugInfo = false;</span>
<span class="fc" id="L104">    HashMap&lt;String, NamedList&lt;Object&gt;&gt; collapseNamedList = new HashMap&lt;&gt;();</span>
<span class="fc" id="L105">    int length = nl.size();</span>
<span class="fc bfc" id="L106" title="All 2 branches covered.">    for (int i = 0; i &lt; length; i++) {</span>
<span class="pc bpc" id="L107" title="1 of 2 branches missed.">      if (nl.getVal(i) instanceof NamedList) {</span>
<span class="nc" id="L108">        NamedList o = (NamedList) nl.getVal(i);</span>
<span class="nc" id="L109">        rewrite(o, searchComponent, true);</span>
<span class="nc" id="L110">        nl.setVal(i, o);</span>
<span class="pc bfc" id="L111" title="All 2 branches covered.">      } else if (nl.getVal(i) instanceof ArrayList) {</span>
<span class="fc" id="L112">        ArrayList o = (ArrayList) nl.getVal(i);</span>
<span class="fc" id="L113">        rewrite(o, searchComponent);</span>
<span class="fc" id="L114">        nl.setVal(i, o);</span>
<span class="pc bpc" id="L115" title="1 of 2 branches missed.">      } else if (nl.getVal(i) instanceof MtasDataItem) {</span>
<span class="nc" id="L116">        MtasDataItem dataItem = (MtasDataItem) nl.getVal(i);</span>
<span class="nc" id="L117">        nl.setVal(i, dataItem.rewrite(showDebugInfo));</span>
<span class="pc bfc" id="L118" title="All 2 branches covered.">      } else if (nl.getVal(i) instanceof MtasSolrCollectionResult) {</span>
<span class="fc" id="L119">        MtasSolrCollectionResult o = (MtasSolrCollectionResult) nl.getVal(i);</span>
<span class="fc" id="L120">        collapseNamedList.put(nl.getName(i), o.rewrite(searchComponent));</span>
<span class="fc bfc" id="L121" title="All 2 branches covered.">      } else if (nl.getVal(i) instanceof MtasSolrMtasResult) {</span>
<span class="fc" id="L122">        MtasSolrMtasResult o = (MtasSolrMtasResult) nl.getVal(i);</span>
<span class="fc" id="L123">        if (o.dataCollector.getCollectorType()</span>
<span class="fc bfc" id="L124" title="All 2 branches covered.">            .equals(DataCollector.COLLECTOR_TYPE_LIST)) {</span>
<span class="fc bfc" id="L125" title="All 2 branches covered.">          if (!o.dataCollector.withTotal()) {</span>
<span class="fc" id="L126">            NamedList&lt;Object&gt; nnl = o.getNamedList(showDebugInfo);</span>
<span class="fc bfc" id="L127" title="All 2 branches covered.">            for (int j = 0; j &lt; nnl.size(); j++) {</span>
<span class="pc bpc" id="L128" title="1 of 2 branches missed.">              if (nnl.getVal(j) != null</span>
<span class="pc bpc" id="L129" title="1 of 2 branches missed.">                  &amp;&amp; nnl.getVal(j) instanceof MtasDataItem) {</span>
<span class="nc" id="L130">                MtasDataItem mdi = (MtasDataItem) nnl.getVal(j);</span>
<span class="nc" id="L131">                mdi.rewrite(showDebugInfo);</span>
<span class="nc" id="L132">                nnl.setVal(j, mdi);</span>
              }
            }
            // System.out.println(&quot;rewrite -- &quot;+nnl);
<span class="fc" id="L136">            nl.setVal(i, rewriteToArray(nnl));</span>
            // System.out.println(&quot;rewrite! &quot;+nl.getVal(i));
<span class="fc" id="L138">          } else {</span>
<span class="fc" id="L139">            NamedList&lt;Object&gt; tmpResponse = new SimpleOrderedMap&lt;&gt;();</span>
<span class="fc" id="L140">            tmpResponse.add(nl.getName(i) + &quot;Total&quot;, o.dataCollector.getSize());</span>
<span class="fc" id="L141">            NamedList&lt;Object&gt; nnl = o.getNamedList(showDebugInfo);</span>
<span class="fc bfc" id="L142" title="All 2 branches covered.">            for (int j = 0; j &lt; nnl.size(); j++) {</span>
<span class="pc bpc" id="L143" title="1 of 2 branches missed.">              if (nnl.getVal(j) != null</span>
<span class="pc bpc" id="L144" title="1 of 2 branches missed.">                  &amp;&amp; nnl.getVal(j) instanceof MtasDataItem) {</span>
<span class="nc" id="L145">                MtasDataItem mdi = (MtasDataItem) nnl.getVal(j);</span>
<span class="nc" id="L146">                mdi.rewrite(showDebugInfo);</span>
<span class="nc" id="L147">                nnl.setVal(j, mdi);</span>
              }
            }
<span class="fc" id="L150">            tmpResponse.add(nl.getName(i), rewriteToArray(nnl));</span>
<span class="fc" id="L151">            nl.setVal(i, null);</span>
<span class="fc" id="L152">            collapseNamedList.put(nl.getName(i), tmpResponse);</span>
<span class="fc" id="L153">          }</span>
<span class="fc" id="L154">        } else if (o.dataCollector.getCollectorType()</span>
<span class="pc bpc" id="L155" title="1 of 2 branches missed.">            .equals(DataCollector.COLLECTOR_TYPE_DATA)) {</span>
<span class="fc" id="L156">          NamedList&lt;Object&gt; nnl = o.getData(showDebugInfo);</span>
<span class="fc bfc" id="L157" title="All 2 branches covered.">          if (nnl.size() &gt; 0) {</span>
<span class="fc" id="L158">            rewrite(nnl, searchComponent);</span>
<span class="fc" id="L159">            collapseNamedList.put(nl.getName(i), nnl);</span>
<span class="fc" id="L160">            nl.setVal(i, nnl);</span>
          } else {
<span class="fc" id="L162">            nl.setVal(i, null);</span>
          }
        }
      }
    }
    // collapse
<span class="pc bpc" id="L168" title="1 of 4 branches missed.">    if (doCollapse &amp;&amp; collapseNamedList.size() &gt; 0) {</span>
<span class="fc bfc" id="L169" title="All 2 branches covered.">      for (String key : collapseNamedList.keySet()) {</span>
<span class="fc" id="L170">        nl.remove(key);</span>
<span class="fc" id="L171">      }</span>
<span class="fc bfc" id="L172" title="All 2 branches covered.">      for (NamedList&lt;Object&gt; items : collapseNamedList.values()) {</span>
<span class="fc" id="L173">        nl.addAll(items);</span>
<span class="fc" id="L174">      }</span>
    }
<span class="fc" id="L176">  }</span>

  /**
   * Rewrite to array.
   *
   * @param nnl the nnl
   * @return the array list
   */
  private static ArrayList&lt;NamedList&lt;Object&gt;&gt; rewriteToArray(
      NamedList&lt;Object&gt; nnl) {
<span class="fc" id="L186">    ArrayList&lt;NamedList&lt;Object&gt;&gt; al = new ArrayList&lt;&gt;();</span>
    String key;
<span class="fc" id="L188">    Iterator&lt;Entry&lt;String, Object&gt;&gt; it = nnl.iterator();</span>
<span class="fc bfc" id="L189" title="All 2 branches covered.">    while (it.hasNext()) {</span>
<span class="fc" id="L190">      Entry&lt;String, Object&gt; entry = it.next();</span>
<span class="fc" id="L191">      NamedList&lt;Object&gt; item = (NamedList&lt;Object&gt;) entry.getValue();</span>
<span class="fc" id="L192">      key = entry.getKey();</span>
<span class="pc bpc" id="L193" title="1 of 2 branches missed.">      if (key.startsWith(GroupHit.KEY_START)) {</span>
<span class="nc" id="L194">        StringBuilder newKey = new StringBuilder(&quot;&quot;);</span>
<span class="nc" id="L195">        item.add(&quot;group&quot;, GroupHit.keyToObject(key, newKey));</span>
<span class="nc" id="L196">        item.add(&quot;key&quot;, newKey.toString().trim());</span>
<span class="nc" id="L197">      } else {</span>
<span class="fc" id="L198">        item.add(&quot;key&quot;, key);</span>
      }
<span class="fc" id="L200">      al.add(item);</span>
<span class="fc" id="L201">    }</span>
<span class="fc" id="L202">    return al;</span>
  }

  /**
   * Rewrite merge list.
   *
   * @param key the key
   * @param subKey the sub key
   * @param snl the snl
   * @param tnl the tnl
   */
  @SuppressWarnings({ &quot;unchecked&quot;, &quot;unused&quot; })
  private static void rewriteMergeList(String key, String subKey,
      NamedList&lt;Object&gt; snl, NamedList&lt;Object&gt; tnl) {
<span class="nc bnc" id="L216" title="All 2 branches missed.">    for (int i = 0; i &lt; tnl.size(); i++) {</span>
<span class="nc" id="L217">      Object item = snl.get(tnl.getName(i));</span>
<span class="nc bnc" id="L218" title="All 4 branches missed.">      if (item != null &amp;&amp; tnl.getVal(i) instanceof NamedList) {</span>
<span class="nc" id="L219">        NamedList&lt;Object&gt; tnnl = (NamedList&lt;Object&gt;) tnl.getVal(i);</span>
<span class="nc" id="L220">        Object o = tnnl.get(key);</span>
        NamedList&lt;Object&gt; tnnnl;
<span class="nc bnc" id="L222" title="All 4 branches missed.">        if (o != null &amp;&amp; o instanceof NamedList) {</span>
<span class="nc" id="L223">          tnnnl = (NamedList&lt;Object&gt;) o;</span>
        } else {
<span class="nc" id="L225">          tnnnl = new SimpleOrderedMap&lt;&gt;();</span>
<span class="nc" id="L226">          tnnl.add(key, tnnnl);</span>
        }
<span class="nc" id="L228">        tnnnl.add(subKey, item);</span>
      }
    }
<span class="nc" id="L231">  }</span>

  /**
   * Rewrite merge data.
   *
   * @param key the key
   * @param subKey the sub key
   * @param snl the snl
   * @param tnl the tnl
   */
  @SuppressWarnings({ &quot;unused&quot;, &quot;unchecked&quot; })
  private static void rewriteMergeData(String key, String subKey,
      NamedList&lt;Object&gt; snl, NamedList&lt;Object&gt; tnl) {
<span class="nc bnc" id="L244" title="All 2 branches missed.">    if (snl != null) {</span>
<span class="nc" id="L245">      Object o = tnl.get(key);</span>
      NamedList&lt;Object&gt; tnnnl;
<span class="nc bnc" id="L247" title="All 4 branches missed.">      if (o != null &amp;&amp; o instanceof NamedList) {</span>
<span class="nc" id="L248">        tnnnl = (NamedList&lt;Object&gt;) o;</span>
      } else {
<span class="nc" id="L250">        tnnnl = new SimpleOrderedMap&lt;&gt;();</span>
<span class="nc" id="L251">        tnl.add(key, tnnnl);</span>
      }
<span class="nc" id="L253">      tnnnl.add(subKey, snl);</span>
    }
<span class="nc" id="L255">  }</span>

  /**
   * Encode.
   *
   * @param o the o
   * @return the string
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public static String encode(Object o) throws IOException {
<span class="pc bpc" id="L265" title="1 of 2 branches missed.">    if (o instanceof Serializable) {</span>
<span class="fc" id="L266">      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();</span>
      ObjectOutputStream objectOutputStream;
      try {
<span class="fc" id="L269">        objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);</span>
<span class="fc" id="L270">        objectOutputStream.writeObject(o);</span>
<span class="fc" id="L271">        objectOutputStream.close();</span>
<span class="fc" id="L272">        byte[] byteArray = byteArrayOutputStream.toByteArray();</span>
<span class="fc" id="L273">        return Base64.byteArrayToBase64(byteArray);</span>
<span class="nc" id="L274">      } catch (IOException e) {</span>
<span class="nc" id="L275">        e.printStackTrace();</span>
<span class="nc" id="L276">        log.error(e);</span>
<span class="nc" id="L277">        return null;</span>
      }
    } else {
<span class="nc" id="L280">      throw new IOException(&quot;no serializable object&quot;);</span>
    }
  }

  /**
   * Decode.
   *
   * @param s the s
   * @return the object
   */
  static Object decode(String s) {
<span class="fc" id="L291">    byte[] bytes = Base64.base64ToByteArray(s);</span>
    ObjectInputStream objectInputStream;
    try {
<span class="fc" id="L294">      objectInputStream = new ObjectInputStream(</span>
          new ByteArrayInputStream(bytes));
<span class="fc" id="L296">      return objectInputStream.readObject();</span>
<span class="nc" id="L297">    } catch (IOException | ClassNotFoundException e) {</span>
<span class="nc" id="L298">      log.error(e);</span>
<span class="nc" id="L299">      return null;</span>
    }
  }

  /**
   * Decode.
   *
   * @param l the l
   * @return the array list
   */
  @SuppressWarnings({ &quot;rawtypes&quot;, &quot;unchecked&quot; })
  static ArrayList decode(ArrayList l) {
<span class="fc bfc" id="L311" title="All 2 branches covered.">    for (int i = 0; i &lt; l.size(); i++) {</span>
<span class="pc bpc" id="L312" title="1 of 2 branches missed.">      if (l.get(i) instanceof NamedList) {</span>
<span class="fc" id="L313">        l.set(i, decode((NamedList) l.get(i)));</span>
<span class="nc bnc" id="L314" title="All 2 branches missed.">      } else if (l.get(i) instanceof ArrayList) {</span>
<span class="nc" id="L315">        l.set(i, decode((ArrayList) l.get(i)));</span>
      }
    }
<span class="fc" id="L318">    return l;</span>
  }

  /**
   * Decode.
   *
   * @param nl the nl
   * @return the named list
   */
  @SuppressWarnings({ &quot;rawtypes&quot;, &quot;unchecked&quot; })
  static NamedList&lt;Object&gt; decode(NamedList&lt;Object&gt; nl) {
<span class="fc bfc" id="L329" title="All 2 branches covered.">    for (int i = 0; i &lt; nl.size(); i++) {</span>
<span class="fc" id="L330">      String key = nl.getName(i);</span>
<span class="fc" id="L331">      Object o = nl.getVal(i);</span>
<span class="fc bfc" id="L332" title="All 2 branches covered.">      if (key.matches(&quot;^_encoded_.*$&quot;)) {</span>
<span class="pc bpc" id="L333" title="1 of 2 branches missed.">        if (o instanceof String) {</span>
<span class="fc" id="L334">          Object decodedObject = decode((String) nl.getVal(i));</span>
<span class="fc" id="L335">          String decodedKey = key.replaceFirst(&quot;^_encoded_&quot;, &quot;&quot;);</span>
<span class="pc bpc" id="L336" title="1 of 2 branches missed.">          if (decodedKey.equals(&quot;&quot;)) {</span>
<span class="nc" id="L337">            decodedKey = &quot;_&quot; + decodedObject.getClass().getSimpleName() + &quot;_&quot;;</span>
          }
<span class="fc" id="L339">          nl.setName(i, decodedKey);</span>
<span class="fc" id="L340">          nl.setVal(i, decodedObject);</span>
<span class="pc bnc" id="L341" title="All 2 branches missed.">        } else if (o instanceof NamedList) {</span>
<span class="nc" id="L342">          NamedList nl2 = (NamedList) o;</span>
<span class="nc bnc" id="L343" title="All 2 branches missed.">          for (int j = 0; j &lt; nl2.size(); j++) {</span>
<span class="nc bnc" id="L344" title="All 2 branches missed.">            if (nl2.getVal(j) instanceof String) {</span>
<span class="nc" id="L345">              nl2.setVal(j, decode((String) nl2.getVal(j)));</span>
            }
          }
<span class="nc" id="L348">        } else {</span>
          // System.out.println(&quot;unknown type &quot; +
          // o.getClass().getCanonicalName());
        }
      } else {
<span class="pc bpc" id="L353" title="1 of 2 branches missed.">        if (o instanceof NamedList) {</span>
<span class="nc" id="L354">          nl.setVal(i, decode((NamedList&lt;Object&gt;) o));</span>
<span class="fc bfc" id="L355" title="All 2 branches covered.">        } else if (o instanceof ArrayList) {</span>
<span class="fc" id="L356">          nl.setVal(i, decode((ArrayList&lt;Object&gt;) o));</span>
        }
      }
    }
<span class="fc" id="L360">    return nl;</span>
  }

  /**
   * Gets the ids from parameters.
   *
   * @param params the params
   * @param prefix the prefix
   * @return the ids from parameters
   */
  public static SortedSet&lt;String&gt; getIdsFromParameters(SolrParams params,
      String prefix) {
<span class="fc" id="L372">    SortedSet&lt;String&gt; ids = new TreeSet&lt;&gt;();</span>
<span class="fc" id="L373">    Iterator&lt;String&gt; it = params.getParameterNamesIterator();</span>
<span class="fc" id="L374">    Pattern pattern = Pattern</span>
<span class="fc" id="L375">        .compile(&quot;^&quot; + Pattern.quote(prefix) + &quot;\\.([^\\.]+)(\\..*|$)&quot;);</span>
<span class="fc bfc" id="L376" title="All 2 branches covered.">    while (it.hasNext()) {</span>
<span class="fc" id="L377">      String item = it.next();</span>
<span class="fc" id="L378">      Matcher m = pattern.matcher(item);</span>
<span class="fc bfc" id="L379" title="All 2 branches covered.">      if (m.matches()) {</span>
<span class="fc" id="L380">        ids.add(m.group(1));</span>
      }
<span class="fc" id="L382">    }</span>
<span class="fc" id="L383">    return ids;</span>
  }

  /**
   * Compare and check.
   *
   * @param list the list
   * @param original the original
   * @param nameNew the name new
   * @param nameOriginal the name original
   * @param unique the unique
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public static void compareAndCheck(String[] list, String[] original,
      String nameNew, String nameOriginal, Boolean unique) throws IOException {
<span class="pc bpc" id="L398" title="1 of 2 branches missed.">    if (list != null) {</span>
<span class="pc bpc" id="L399" title="1 of 2 branches missed.">      if (list.length != original.length) {</span>
<span class="nc" id="L400">        throw new IOException(</span>
            &quot;unequal size &quot; + nameNew + &quot; and &quot; + nameOriginal);
      }
<span class="fc bfc" id="L403" title="All 2 branches covered.">      if (unique) {</span>
<span class="fc" id="L404">        Set&lt;String&gt; set = new HashSet&lt;&gt;();</span>
<span class="fc bfc" id="L405" title="All 2 branches covered.">        for (int i = 0; i &lt; list.length; i++) {</span>
<span class="fc" id="L406">          set.add(list[i]);</span>
        }
<span class="pc bpc" id="L408" title="1 of 2 branches missed.">        if (set.size() &lt; list.length) {</span>
<span class="nc" id="L409">          throw new IOException(&quot;duplicate &quot; + nameNew);</span>
        }
      }
    }
<span class="fc" id="L413">  }</span>

  /**
   * Construct query.
   *
   * @param queryValue the query value
   * @param queryType the query type
   * @param queryPrefix the query prefix
   * @param queryVariables the query variables
   * @param field the field
   * @param queryIgnore the query ignore
   * @param maximumIgnoreLength the maximum ignore length
   * @return the mtas span query
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public static MtasSpanQuery constructQuery(String queryValue,
      String queryType, String queryPrefix,
      HashMap&lt;String, String[]&gt; queryVariables, String field,
      String queryIgnore, Integer maximumIgnoreLength) throws IOException {
<span class="pc bpc" id="L432" title="2 of 4 branches missed.">    if (queryType == null || queryType.isEmpty()) {</span>
<span class="nc" id="L433">      throw new IOException(&quot;no (valid) type for query &quot; + queryValue);</span>
<span class="pc bpc" id="L434" title="2 of 4 branches missed.">    } else if (queryValue == null || queryValue.isEmpty()) {</span>
<span class="nc" id="L435">      throw new IOException(&quot;no (valid) value for &quot; + queryType + &quot; query&quot;);</span>
    }
<span class="fc" id="L437">    MtasSpanQuery ignore = null;</span>
<span class="pc bpc" id="L438" title="1 of 2 branches missed.">    if (queryIgnore != null) {</span>
<span class="nc" id="L439">      Reader queryIgnoreReader = new BufferedReader(</span>
          new StringReader(queryIgnore));
<span class="nc bnc" id="L441" title="All 2 branches missed.">      if (queryType.equals(QUERY_TYPE_CQL)) {</span>
<span class="nc" id="L442">        MtasCQLParser ip = new MtasCQLParser(queryIgnoreReader);</span>
        try {
<span class="nc" id="L444">          ignore = ip.parse(field, null, null, null, null);</span>
<span class="nc" id="L445">        } catch (mtas.parser.cql.ParseException e) {</span>
<span class="nc" id="L446">          throw new IOException(&quot;couldn't parse &quot; + queryType + &quot; query &quot;</span>
<span class="nc" id="L447">              + queryIgnore + &quot; (&quot; + e.getMessage() + &quot;)&quot;, e);</span>
<span class="nc" id="L448">        } catch (TokenMgrError e) {</span>
<span class="nc" id="L449">          throw new IOException(&quot;couldn't parse &quot; + queryType + &quot; query &quot;</span>
<span class="nc" id="L450">              + queryIgnore + &quot; (&quot; + e.getMessage() + &quot;)&quot;, e);</span>
<span class="nc" id="L451">        }</span>
<span class="nc" id="L452">      } else {</span>
<span class="nc" id="L453">        throw new IOException(</span>
            &quot;unknown queryType &quot; + queryType + &quot; for query &quot; + queryValue);
      }
    }
<span class="fc" id="L457">    Reader queryValueReader = new BufferedReader(new StringReader(queryValue));</span>
<span class="pc bpc" id="L458" title="1 of 2 branches missed.">    if (queryType.equals(QUERY_TYPE_CQL)) {</span>
<span class="fc" id="L459">      MtasCQLParser qp = new MtasCQLParser(queryValueReader);</span>
      try {
<span class="fc" id="L461">        return qp.parse(field, queryPrefix, queryVariables, ignore,</span>
            maximumIgnoreLength);
<span class="nc" id="L463">      } catch (mtas.parser.cql.ParseException e) {</span>
<span class="nc" id="L464">        throw new IOException(&quot;couldn't parse &quot; + queryType + &quot; query &quot;</span>
<span class="nc" id="L465">            + queryValue + &quot; (&quot; + e.getMessage() + &quot;)&quot;, e);</span>
<span class="nc" id="L466">      } catch (TokenMgrError e) {</span>
<span class="nc" id="L467">        throw new IOException(&quot;couldn't parse &quot; + queryType + &quot; query &quot;</span>
<span class="nc" id="L468">            + queryValue + &quot; (&quot; + e.getMessage() + &quot;)&quot;, e);</span>
      }
    } else {
<span class="nc" id="L471">      throw new IOException(</span>
          &quot;unknown queryType &quot; + queryType + &quot; for query &quot; + queryValue);
    }
  }

}
</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>