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

import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.FlagsAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.util.Attribute;
import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.AttributeSource.State;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.schema.PreAnalyzedField;
import org.apache.solr.schema.PreAnalyzedField.ParseResult;
import org.apache.solr.schema.PreAnalyzedField.PreAnalyzedParser;
import mtas.solr.update.processor.MtasUpdateRequestProcessorResultItem;
import mtas.solr.update.processor.MtasUpdateRequestProcessorResultReader;

/**
 * The Class MtasPreAnalyzedParser.
 */
<span class="fc" id="L30">public class MtasPreAnalyzedParser implements PreAnalyzedParser {</span>

  /** The log. */
<span class="fc" id="L33">  private static Log log = LogFactory.getLog(MtasPreAnalyzedParser.class);</span>

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.solr.schema.PreAnalyzedField.PreAnalyzedParser#parse(java.io.
   * Reader, org.apache.lucene.util.AttributeSource)
   */
  @Override
  public ParseResult parse(Reader reader, AttributeSource parent)
      throws IOException {
<span class="fc" id="L45">    ParseResult res = new ParseResult();   </span>
    
    // get MtasUpdateRequestProcessorResult
<span class="fc" id="L48">    StringBuilder sb = new StringBuilder();</span>
<span class="fc" id="L49">    char[] buf = new char[128];</span>
    int cnt;
<span class="fc bfc" id="L51" title="All 2 branches covered.">    while ((cnt = reader.read(buf)) &gt; 0) {</span>
<span class="fc" id="L52">      sb.append(buf, 0, cnt);</span>
    }
    Iterator&lt;MtasUpdateRequestProcessorResultItem&gt; iterator;
    
    try {
<span class="fc" id="L57">      MtasUpdateRequestProcessorResultReader result = new MtasUpdateRequestProcessorResultReader(</span>
<span class="fc" id="L58">          sb.toString());</span>
<span class="fc" id="L59">      iterator = result.getIterator();</span>
<span class="pc bpc" id="L60" title="2 of 4 branches missed.">      if (iterator != null &amp;&amp; iterator.hasNext()) {</span>
<span class="fc" id="L61">        res.str = result.getStoredStringValue();</span>
<span class="fc" id="L62">        res.bin = result.getStoredBinValue();      </span>
      } else {
<span class="nc" id="L64">        res.str = null;</span>
<span class="nc" id="L65">        res.bin = null;</span>
<span class="nc" id="L66">        result.close();</span>
<span class="nc" id="L67">        return res;</span>
      }

<span class="fc" id="L70">      parent.clearAttributes();</span>
<span class="fc bfc" id="L71" title="All 2 branches covered.">      while (iterator.hasNext()) {</span>
<span class="fc" id="L72">        MtasUpdateRequestProcessorResultItem item = iterator.next();</span>
<span class="pc bpc" id="L73" title="1 of 2 branches missed.">        if (item.tokenTerm != null) {</span>
<span class="fc" id="L74">          CharTermAttribute catt = parent.addAttribute(CharTermAttribute.class);</span>
<span class="fc" id="L75">          catt.append(item.tokenTerm);</span>
        }
<span class="pc bpc" id="L77" title="1 of 2 branches missed.">        if (item.tokenFlags != null) {</span>
<span class="nc" id="L78">          FlagsAttribute flags = parent.addAttribute(FlagsAttribute.class);</span>
<span class="nc" id="L79">          flags.setFlags(item.tokenFlags);</span>
        }
<span class="fc bfc" id="L81" title="All 2 branches covered.">        if (item.tokenPosIncr != null) {</span>
<span class="fc" id="L82">          PositionIncrementAttribute patt = parent</span>
<span class="fc" id="L83">              .addAttribute(PositionIncrementAttribute.class);</span>
<span class="fc" id="L84">          patt.setPositionIncrement(item.tokenPosIncr);</span>
        }
<span class="pc bpc" id="L86" title="1 of 2 branches missed.">        if (item.tokenPayload != null) {</span>
<span class="fc" id="L87">          PayloadAttribute p = parent.addAttribute(PayloadAttribute.class);</span>
<span class="fc" id="L88">          p.setPayload(new BytesRef(item.tokenPayload));</span>
        }
<span class="pc bpc" id="L90" title="2 of 4 branches missed.">        if (item.tokenOffsetStart != null &amp;&amp; item.tokenOffsetEnd != null) {</span>
<span class="fc" id="L91">          OffsetAttribute offset = parent.addAttribute(OffsetAttribute.class);</span>
<span class="fc" id="L92">          offset.setOffset(item.tokenOffsetStart, item.tokenOffsetEnd);</span>
        }
        // capture state and add to result
<span class="fc" id="L95">        State state = parent.captureState();</span>
<span class="fc" id="L96">        res.states.add(state.clone());       </span>
        // reset for reuse
<span class="fc" id="L98">        parent.clearAttributes();</span>
<span class="fc" id="L99">      }</span>
<span class="nc" id="L100">    } catch (IOException e) {</span>
      //ignore
<span class="nc" id="L102">      log.debug(e);           </span>
<span class="fc" id="L103">    }</span>
<span class="fc" id="L104">    return res;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.solr.schema.PreAnalyzedField.PreAnalyzedParser#toFormattedString
   * (org.apache.lucene.document.Field)
   */
  @Override
  public String toFormattedString(Field f) throws IOException {    
<span class="nc" id="L116">    return this.getClass().getName() + &quot; &quot; + f.name();</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>