org.apache.lucene.misc_2.9.1.v20100421-0704

"");
    }
  }
  
  protected Query getWildcardQuery(String field, String termStr)
    throws ParseException
  {
    if (isPass2ResolvingPhrases) {
      checkPhraseClauseIsForSameField(field);
    }
    return super.getWildcardQuery(field, termStr);
  }
  
  protected Query getRangeQuery(String field, String part1, String part2, boolean inclusive)
    throws ParseException
  {
    if (isPass2ResolvingPhrases) {
      checkPhraseClauseIsForSameField(field);
    }
    return super.getRangeQuery(field, part1, part2, inclusive);
  }
  
  protected Query newRangeQuery(String field, String part1, String part2, boolean inclusive)
  {
    if (isPass2ResolvingPhrases)
    {
      TermRangeQuery rangeQuery = new TermRangeQuery(field, part1, part2, inclusive, inclusive, getRangeCollator());
      
      rangeQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
      return rangeQuery;
    }
    return super.newRangeQuery(field, part1, part2, inclusive);
  }
  
  protected Query getFuzzyQuery(String field, String termStr, float minSimilarity)
    throws ParseException
  {
    if (isPass2ResolvingPhrases) {
      checkPhraseClauseIsForSameField(field);
    }
    return super.getFuzzyQuery(field, termStr, minSimilarity);
  }
  
  static class ComplexPhraseQuery
    extends Query
  {
    String field;
    String phrasedQueryStringContents;
    int slopFactor;
    private Query contents;
    
    public ComplexPhraseQuery(String field, String phrasedQueryStringContents, int slopFactor)
    {
      this.field = field;
      this.phrasedQueryStringContents = phrasedQueryStringContents;
      this.slopFactor = slopFactor;
    }
    
    protected void parsePhraseElements(QueryParser qp)
      throws ParseException
    {
      contents = qp.parse(phrasedQueryStringContents);
    }
    
    public Query rewrite(IndexReader reader)
      throws IOException
    {
      if ((contents instanceof TermQuery)) {
        return contents;
      }
      int numNegatives = 0;
      if (!(contents instanceof BooleanQuery)) {
        throw new IllegalArgumentException("Unknown query type \"" + contents.getClass().getName() + "\" found in phrase query string \"" + phrasedQueryStringContents + "\"");
      }
      BooleanQuery bq = (BooleanQuery)contents;
      BooleanClause[] bclauses = bq.getClauses();
      SpanQuery[] allSpanClauses = new SpanQuery[bclauses.length];
      for (int i = 0; i < bclauses.length; i++)
      {
        Query qc = bclauses[i].getQuery();
        
        qc = qc.rewrite(reader);
        if (bclauses[i].getOccur().equals(BooleanClause.Occur.MUST_NOT)) {
          numNegatives++;
        }
        if ((qc instanceof BooleanQuery))
        {
          ArrayList sc = new ArrayList();
          addComplexPhraseClause(sc, (BooleanQuery)qc);
          if (sc.size() > 0) {
            allSpanClauses[i] = ((SpanQuery)sc.get(0));
          } else {
            allSpanClauses[i] = new SpanTermQuery(new Term(field, "Dummy clause because no terms found - must match nothing"));
          }
        }
        else if ((qc instanceof TermQuery))
        {
          TermQuery tq = (TermQuery)qc;
          allSpanClauses[i] = new SpanTermQuery(tq.getTerm());
        }
        else
        {
          throw new IllegalArgumentException("Unknown query type \"" + qc.getClass().getName() + "\" found in phrase query string \"" + phrasedQueryStringContents + "\"");
        }
      }
      if (numNegatives == 0) {
        return new SpanNearQuery(allSpanClauses, slopFactor, true);
      }
      ArrayList positiveClauses = new ArrayList();
      for (int j = 0; j < allSpanClauses.length; j++) {
        if (!bclauses[j].getOccur().equals(BooleanClause.Occur.MUST_NOT)) {
          positiveClauses.add(allSpanClauses[j]);
        }
      }
      SpanQuery[] includeClauses = (SpanQuery[])positiveClauses.toArray(new SpanQuery[positiveClauses.size()]);
      
      SpanQuery include = null;
      if (includeClauses.length == 1) {
        include = includeClauses[0];
      } else {
        include = new SpanNearQuery(includeClauses, slopFactor + numNegatives, true);
      }
      SpanNearQuery exclude = new SpanNearQuery(allSpanClauses, slopFactor, true);
      
      SpanNotQuery snot = new SpanNotQuery(include, exclude);
      return snot;
    }
    
    private void addComplexPhraseClause(List spanClauses, BooleanQuery qc)
    {
      ArrayList ors = new ArrayList();
      ArrayList nots = new ArrayList();
      BooleanClause[] bclauses = qc.getClauses();
      for (int i = 0; i < bclauses.length; i++)
      {
        Query childQuery = bclauses[i].getQuery();
        
        ArrayList chosenList = ors;
        if (bclauses[i].getOccur() == BooleanClause.Occur.MUST_NOT) {
          chosenList = nots;
        }
        if ((childQuery instanceof TermQuery))
        {
          TermQuery tq = (TermQuery)childQuery;
          SpanTermQuery stq = new SpanTermQuery(tq.getTerm());
          stq.setBoost(tq.getBoost());
          chosenList.add(stq);
        }
        else if ((childQuery instanceof BooleanQuery))
        {
          BooleanQuery cbq = (BooleanQuery)childQuery;
          addComplexPhraseClause(chosenList, cbq);
        }
        else
        {
          throw new IllegalArgumentException("Unknown query type:" + childQuery.getClass().getName());
        }
      }
      if (ors.size() == 0) {
        return;
      }
      SpanOrQuery soq = new SpanOrQuery((SpanQuery[])ors.toArray(new SpanQuery[ors.size()]));
      if (nots.size() == 0)
      {
        spanClauses.add(soq);
      }
      else
      {
        SpanOrQuery snqs = new SpanOrQuery((SpanQuery[])nots.toArray(new SpanQuery[nots.size()]));
        
        SpanNotQuery snq = new SpanNotQuery(soq, snqs);
        spanClauses.add(snq);
      }
    }
    
    public String toString(String field)
    {
      return "\"" + phrasedQueryStringContents + "\"";
    }
    
    public int hashCode()
    {
      int prime = 31;
      int result = 1;
      result = 31 * result + (field == null ? 0 : field.hashCode());
      result = 31 * result + (phrasedQueryStringContents == null ? 0 : phrasedQueryStringContents.hashCode());
      
      result = 31 * result + slopFactor;
      return result;
    }
    
    public boolean equals(Object obj)
    {
      if (this == obj) {
        return true;
      }
      if (obj == null) {
        return false;
      }
      if (getClass() != obj.getClass()) {
        return false;
      }
      ComplexPhraseQuery other = (ComplexPhraseQuery)obj;
      if (field == null)
      {
        if (field != null) {
          return false;
        }
      }
      else if (!field.equals(field)) {
        return false;
      }
      if (phrasedQueryStringContents == null)
      {
        if (phrasedQueryStringContents != null) {
          return false;
        }
      }
      else if (!phrasedQueryStringContents.equals(phrasedQueryStringContents)) {
        return false;
      }
      if (slopFactor != slopFactor) {
        return false;
      }
      return true;
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.queryParser.complexPhrase.ComplexPhraseQueryParser
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.queryParser.precedence;

import java.io.IOException;

public abstract interface CharStream
{
  public abstract char readChar()
    throws IOException;
  
  /**
   * @deprecated
   */
  public abstract int getColumn();
  
  /**
   * @deprecated
   */
  public abstract int getLine();
  
  public abstract int getEndColumn();
  
  public abstract int getEndLine();
  
  public abstract int getBeginColumn();
  
  public abstract int getBeginLine();
  
  public abstract void backup(int paramInt);
  
  public abstract char BeginToken()
    throws IOException;
  
  public abstract String GetImage();
  
  public abstract char[] GetSuffix(int paramInt);
  
  public abstract void Done();
}

/* Location:
 * Qualified Name:     org.apache.lucene.queryParser.precedence.CharStream
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.queryParser.precedence;

import java.io.IOException;
import java.io.PrintStream;
import java.io.Reader;

public final class FastCharStream
  implements CharStream
{
  char[] buffer = null;
  int bufferLength = 0;
  int bufferPosition = 0;
  int tokenStart = 0;
  int bufferStart = 0;
  Reader input;
  
  public FastCharStream(Reader r)
  {
    input = r;
  }
  
  public final char readChar()
    throws IOException
  {
    if (bufferPosition >= bufferLength) {
      refill();
    }
    return buffer[(bufferPosition++)];
  }
  
  private final void refill()
    throws IOException
  {
    int newPosition = bufferLength - tokenStart;
    if (tokenStart == 0)
    {
      if (buffer == null)
      {
        buffer = new char['?'];
      }
      else if (bufferLength == buffer.length)
      {
        char[] newBuffer = new char[buffer.length * 2];
        System.arraycopy(buffer, 0, newBuffer, 0, bufferLength);
        buffer = newBuffer;
      }
    }
    else {
      System.arraycopy(buffer, tokenStart, buffer, 0, newPosition);
    }
    bufferLength = newPosition;
    bufferPosition = newPosition;
    bufferStart += tokenStart;
    tokenStart = 0;
    
    int charsRead = input.read(buffer, newPosition, buffer.length - newPosition);
    if (charsRead == -1) {
      throw new IOException("read past eof");
    }
    bufferLength += charsRead;
  }
  
  public final char BeginToken()
    throws IOException
  {
    tokenStart = bufferPosition;
    return readChar();
  }
  
  public final void backup(int amount)
  {
    bufferPosition -= amount;
  }
  
  public final String GetImage()
  {
    return new String(buffer, tokenStart, bufferPosition - tokenStart);
  }
  
  public final char[] GetSuffix(int len)
  {
    char[] value = new char[len];
    System.arraycopy(buffer, bufferPosition - len, value, 0, len);
    return value;
  }
  
  public final void Done()
  {
    try
    {
      input.close();
    }
    catch (IOException e)
    {
      System.err.println("Caught: " + e + "; ignoring.");
    }
  }
  
  public final int getColumn()
  {
    return bufferStart + bufferPosition;
  }
  
  public final int getLine()
  {
    return 1;
  }
  
  public final int getEndColumn()
  {
    return bufferStart + bufferPosition;
  }
  
  public final int getEndLine()
  {
    return 1;
  }
  
  public final int getBeginColumn()
  {
    return bufferStart + tokenStart;
  }
  
  public final int getBeginLine()
  {
    return 1;
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.queryParser.precedence.FastCharStream
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.queryParser.precedence;

public class ParseException
  extends Exception
{
  protected boolean specialConstructor;
  public Token currentToken;
  public int[][] expectedTokenSequences;
  public String[] tokenImage;
  
  public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String[] tokenImageVal)
  {
    super("");
    specialConstructor = true;
    currentToken = currentTokenVal;
    expectedTokenSequences = expectedTokenSequencesVal;
    tokenImage = tokenImageVal;
  }
  
  public ParseException()
  {
    specialConstructor = false;
  }
  
  public ParseException(String message)
  {
    super(message);
    specialConstructor = false;
  }
  
  public String getMessage()
  {
    if (!specialConstructor) {
      return super.getMessage();
    }
    String expected = "";
    int maxSize = 0;
    for (int i = 0; i < expectedTokenSequences.length; i++)
    {
      if (maxSize < expectedTokenSequences[i].length) {
        maxSize = expectedTokenSequences[i].length;
      }
      for (int j = 0; j < expectedTokenSequences[i].length; j++) {
        expected = expected + tokenImage[expectedTokenSequences[i][j]] + " ";
      }
      if (expectedTokenSequences[i][(expectedTokenSequences[i].length - 1)] != 0) {
        expected = expected + "...";
      }
      expected = expected + eol + "    ";
    }
    String retval = "Encountered \"";
    Token tok = currentToken.next;
    for (int i = 0; i < maxSize; i++)
    {
      if (i != 0) {
        retval = retval + " ";
      }
      if (kind == 0)
      {
        retval = retval + tokenImage[0];
        break;
      }
      retval = retval + add_escapes(image);
      tok = next;
    }
    retval = retval + "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
    retval = retval + "." + eol;
    if (expectedTokenSequences.length == 1) {
      retval = retval + "Was expecting:" + eol + "    ";
    } else {
      retval = retval + "Was expecting one of:" + eol + "    ";
    }
    retval = retval + expected;
    return retval;
  }
  
  protected String eol = System.getProperty("line.separator", "\n");
  
  protected String add_escapes(String str)
  {
    StringBuffer retval = new StringBuffer();
    for (int i = 0; i < str.length(); i++) {
      switch (str.charAt(i))
      {
      case '\000': 
        break;
      case '\b': 
        retval.append("\\b");
        break;
      case '\t': 
        retval.append("\\t");
        break;
      case '\n': 
        retval.append("\\n");
        break;
      case '\f': 
        retval.append("\\f");
        break;
      case '\r': 
        retval.append("\\r");
        break;
      case '"': 
        retval.append("\\\"");
        break;
      case '\'': 
        retval.append("\\'");
        break;
      case '\\': 
        retval.append("\\\\");
        break;
      default: 
        char ch;
        if (((ch = str.charAt(i)) < ' ') || (ch > '~'))
        {
          String s = "0000" + Integer.toString(ch, 16);
          retval.append("\\u" + s.substring(s.length() - 4, s.length()));
        }
        else
        {
          retval.append(ch);
        }
        break;
      }
    }
    return retval.toString();
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.queryParser.precedence.ParseException
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.queryParser.precedence;

class PrecedenceQueryParser$1 {}

/* Location:
 * Qualified Name:     org.apache.lucene.queryParser.precedence.PrecedenceQueryParser.1
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.queryParser.precedence;

final class PrecedenceQueryParser$JJCalls
{
  int gen;
  Token first;
  int arg;
  JJCalls next;
}

/* Location:
 * Qualified Name:     org.apache.lucene.queryParser.precedence.PrecedenceQueryParser.JJCalls
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.queryParser.precedence;

final class PrecedenceQueryParser$LookaheadSuccess
  extends Error
{
  private PrecedenceQueryParser$LookaheadSuccess() {}
  
  PrecedenceQueryParser$LookaheadSuccess(PrecedenceQueryParser.1 x0)
  {
    this();
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.queryParser.precedence.PrecedenceQueryParser.LookaheadSuccess
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.queryParser.precedence;

import org.apache.lucene.util.Parameter;

final class PrecedenceQueryParser$Operator
  extends Parameter
{
  private PrecedenceQueryParser$Operator(String name)
  {
    super(name);
  }
  
  static final Operator OR = new Operator("OR");
  static final Operator AND = new Operator("AND");
}

/* Location:
 * Qualified Name:     org.apache.lucene.queryParser.precedence.PrecedenceQueryParser.Operator
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.queryParser.precedence;

import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Vector;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.DateTools.Resolution;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BooleanQuery.TooManyClauses;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RangeQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.util.Parameter;

public class PrecedenceQueryParser
  implements PrecedenceQueryParserConstants
{
  private static final int CONJ_NONE = 0;
  private static final int CONJ_AND = 1;
  private static final int CONJ_OR = 2;
  private static final int MOD_NONE = 0;
  private static final int MOD_NOT = 10;
  private static final int MOD_REQ = 11;
  public static final Operator AND_OPERATOR = Operator.AND;
  public static final Operator OR_OPERATOR = Operator.OR;
  private Operator operator = OR_OPERATOR;
  boolean lowercaseExpandedTerms = true;
  Analyzer analyzer;
  String field;
  int phraseSlop = 0;
  float fuzzyMinSim = 0.5F;
  int fuzzyPrefixLength = 0;
  Locale locale = Locale.getDefault();
  public PrecedenceQueryParserTokenManager token_source;
  public Token token;
  public Token jj_nt;
  private int jj_ntk;
  private Token jj_scanpos;
  private Token jj_lastpos;
  private int jj_la;
  
  static final class Operator
    extends Parameter
  {
    private Operator(String name)
    {
      super();
    }
    
    static final Operator OR = new Operator("OR");
    static final Operator AND = new Operator("AND");
  }
  
  public PrecedenceQueryParser(String f, Analyzer a)
  {
    this(new FastCharStream(new StringReader("")));
    analyzer = a;
    field = f;
  }
  
  public Query parse(String expression)
    throws ParseException
  {
    if ((expression == null) || (expression.trim().length() == 0)) {
      return new BooleanQuery();
    }
    ReInit(new FastCharStream(new StringReader(expression)));
    try
    {
      Query query = Query(field);
      return query != null ? query : new BooleanQuery();
    }
    catch (TokenMgrError tme)
    {
      throw new ParseException(tme.getMessage());
    }
    catch (BooleanQuery.TooManyClauses tmc)
    {
      throw new ParseException("Too many boolean clauses");
    }
  }
  
  public Analyzer getAnalyzer()
  {
    return analyzer;
  }
  
  public String getField()
  {
    return field;
  }
  
  public float getFuzzyMinSim()
  {
    return fuzzyMinSim;
  }
  
  public void setFuzzyMinSim(float fuzzyMinSim)
  {
    this.fuzzyMinSim = fuzzyMinSim;
  }
  
  public int getFuzzyPrefixLength()
  {
    return fuzzyPrefixLength;
  }
  
  public void setFuzzyPrefixLength(int fuzzyPrefixLength)
  {
    this.fuzzyPrefixLength = fuzzyPrefixLength;
  }
  
  public void setPhraseSlop(int phraseSlop)
  {
    this.phraseSlop = phraseSlop;
  }
  
  public int getPhraseSlop()
  {
    return phraseSlop;
  }
  
  public void setDefaultOperator(Operator op)
  {
    operator = op;
  }
  
  public Operator getDefaultOperator()
  {
    return operator;
  }
  
  public void setLowercaseExpandedTerms(boolean lowercaseExpandedTerms)
  {
    this.lowercaseExpandedTerms = lowercaseExpandedTerms;
  }
  
  public boolean getLowercaseExpandedTerms()
  {
    return lowercaseExpandedTerms;
  }
  
  public void setLocale(Locale locale)
  {
    this.locale = locale;
  }
  
  public Locale getLocale()
  {
    return locale;
  }
  
  /**
   * @deprecated
   */
  protected void addClause(Vector clauses, int conj, int modifier, Query q)
  {
    addClause(clauses, conj, modifier, q);
  }
  
  protected void addClause(List clauses, int conj, int modifier, Query q)
  {
    if ((clauses.size() > 0) && (conj == 1))
    {
      BooleanClause c = (BooleanClause)clauses.get(clauses.size() - 1);
      if (!c.isProhibited()) {
        c.setOccur(BooleanClause.Occur.MUST);
      }
    }
    if ((clauses.size() > 0) && (operator == AND_OPERATOR) && (conj == 2))
    {
      BooleanClause c = (BooleanClause)clauses.get(clauses.size() - 1);
      if (!c.isProhibited()) {
        c.setOccur(BooleanClause.Occur.SHOULD);
      }
    }
    if (q == null) {
      return;
    }
    boolean prohibited;
    boolean required;
    if (operator == OR_OPERATOR)
    {
      boolean prohibited = modifier == 10;
      boolean required = modifier == 11;
      if ((conj == 1) && (!prohibited)) {
        required = true;
      }
    }
    else
    {
      prohibited = modifier == 10;
      required = (!prohibited) && (conj != 2);
    }
    if ((required) && (!prohibited)) {
      clauses.add(new BooleanClause(q, BooleanClause.Occur.MUST));
    } else if ((!required) && (!prohibited)) {
      clauses.add(new BooleanClause(q, BooleanClause.Occur.SHOULD));
    } else if ((!required) && (prohibited)) {
      clauses.add(new BooleanClause(q, BooleanClause.Occur.MUST_NOT));
    } else {
      throw new RuntimeException("Clause cannot be both required and prohibited");
    }
  }
  
  protected Query getFieldQuery(String field, String queryText)
    throws ParseException
  {
    TokenStream source = analyzer.tokenStream(field, new StringReader(queryText));
    List list = new ArrayList();
    org.apache.lucene.analysis.Token reusableToken = new org.apache.lucene.analysis.Token();
    
    int positionCount = 0;
    boolean severalTokensAtSamePosition = false;
    org.apache.lucene.analysis.Token nextToken;
    for (;;)
    {
      try
      {
        nextToken = source.next(reusableToken);
      }
      catch (IOException e)
      {
        nextToken = null;
      }
      if (nextToken == null) {
        break;
      }
      list.add(nextToken.clone());
      if (nextToken.getPositionIncrement() == 1) {
        positionCount++;
      } else {
        severalTokensAtSamePosition = true;
      }
    }
    try
    {
      source.close();
    }
    catch (IOException e) {}
    if (list.size() == 0) {
      return null;
    }
    if (list.size() == 1)
    {
      nextToken = (org.apache.lucene.analysis.Token)list.get(0);
      return new TermQuery(new Term(field, nextToken.term()));
    }
    if (severalTokensAtSamePosition)
    {
      if (positionCount == 1)
      {
        BooleanQuery q = new BooleanQuery();
        for (int i = 0; i < list.size(); i++)
        {
          nextToken = (org.apache.lucene.analysis.Token)list.get(i);
          TermQuery currentQuery = new TermQuery(new Term(field, nextToken.term()));
          
          q.add(currentQuery, BooleanClause.Occur.SHOULD);
        }
        return q;
      }
      MultiPhraseQuery mpq = new MultiPhraseQuery();
      List multiTerms = new ArrayList();
      for (int i = 0; i < list.size(); i++)
      {
        nextToken = (org.apache.lucene.analysis.Token)list.get(i);
        if ((nextToken.getPositionIncrement() == 1) && (multiTerms.size() > 0))
        {
          mpq.add((Term[])multiTerms.toArray(new Term[0]));
          multiTerms.clear();
        }
        multiTerms.add(new Term(field, nextToken.term()));
      }
      mpq.add((Term[])multiTerms.toArray(new Term[0]));
      return mpq;
    }
    PhraseQuery q = new PhraseQuery();
    q.setSlop(phraseSlop);
    for (int i = 0; i < list.size(); i++) {
      q.add(new Term(field, ((org.apache.lucene.analysis.Token)list.get(i)).term()));
    }
    return q;
  }
  
  protected Query getFieldQuery(String field, String queryText, int slop)
    throws ParseException
  {
    Query query = getFieldQuery(field, queryText);
    if ((query instanceof PhraseQuery)) {
      ((PhraseQuery)query).setSlop(slop);
    }
    if ((query instanceof MultiPhraseQuery)) {
      ((MultiPhraseQuery)query).setSlop(slop);
    }
    return query;
  }
  
  protected Query getRangeQuery(String field, String part1, String part2, boolean inclusive)
    throws ParseException
  {
    if (lowercaseExpandedTerms)
    {
      part1 = part1.toLowerCase();
      part2 = part2.toLowerCase();
    }
    try
    {
      DateFormat df = DateFormat.getDateInstance(3, locale);
      df.setLenient(true);
      Date d1 = df.parse(part1);
      Date d2 = df.parse(part2);
      part1 = DateTools.dateToString(d1, DateTools.Resolution.DAY);
      part2 = DateTools.dateToString(d2, DateTools.Resolution.DAY);
    }
    catch (Exception e) {}
    return new RangeQuery(new Term(field, part1), new Term(field, part2), inclusive);
  }
  
  /**
   * @deprecated
   */
  protected Query getBooleanQuery(Vector clauses)
    throws ParseException
  {
    return getBooleanQuery(clauses, false);
  }
  
  protected Query getBooleanQuery(List clauses)
    throws ParseException
  {
    return getBooleanQuery(clauses, false);
  }
  
  /**
   * @deprecated
   */
  protected Query getBooleanQuery(Vector clauses, boolean disableCoord)
    throws ParseException
  {
    return getBooleanQuery(clauses, disableCoord);
  }
  
  protected Query getBooleanQuery(List clauses, boolean disableCoord)
    throws ParseException
  {
    if ((clauses == null) || (clauses.size() == 0)) {
      return null;
    }
    BooleanQuery query = new BooleanQuery(disableCoord);
    for (int i = 0; i < clauses.size(); i++) {
      query.add((BooleanClause)clauses.get(i));
    }
    return query;
  }
  
  protected Query getWildcardQuery(String field, String termStr)
    throws ParseException
  {
    if (lowercaseExpandedTerms) {
      termStr = termStr.toLowerCase();
    }
    Term t = new Term(field, termStr);
    return new WildcardQuery(t);
  }
  
  protected Query getPrefixQuery(String field, String termStr)
    throws ParseException
  {
    if (lowercaseExpandedTerms) {
      termStr = termStr.toLowerCase();
    }
    Term t = new Term(field, termStr);
    return new PrefixQuery(t);
  }
  
  protected Query getFuzzyQuery(String field, String termStr, float minSimilarity)
    throws ParseException
  {
    if (lowercaseExpandedTerms) {
      termStr = termStr.toLowerCase();
    }
    Term t = new Term(field, termStr);
    return new FuzzyQuery(t, minSimilarity, fuzzyPrefixLength);
  }
  
  private String discardEscapeChar(String input)
  {
    char[] caSource = input.toCharArray();
    char[] caDest = new char[caSource.length];
    int j = 0;
    for (int i = 0; i < caSource.length; i++) {
      if ((caSource[i] != '\\') || ((i > 0) && (caSource[(i - 1)] == '\\'))) {
        caDest[(j++)] = caSource[i];
      }
    }
    return new String(caDest, 0, j);
  }
  
  public static String escape(String s)
  {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < s.length(); i++)
    {
      char c = s.charAt(i);
      if ((c == '\\') || (c == '+') || (c == '-') || (c == '!') || (c == '(') || (c == ')') || (c == ':') || (c == '^') || (c == '[') || (c == ']') || (c == '"') || (c == '{') || (c == '}') || (c == '~') || (c == '*') || (c == '?')) {
        sb.append('\\');
      }
      sb.append(c);
    }
    return sb.toString();
  }
  
  public static void main(String[] args)
    throws Exception
  {
    if (args.length == 0)
    {
      System.out.println("Usage: java org.apache.lucene.queryParser.QueryParser <input>");
      System.exit(0);
    }
    PrecedenceQueryParser qp = new PrecedenceQueryParser("field", new SimpleAnalyzer());
    
    Query q = qp.parse(args[0]);
    System.out.println(q.toString("field"));
  }
  
  public final int Conjunction()
    throws ParseException
  {
    int ret = 0;
    switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
    {
    case 7: 
    case 8: 
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 7: 
        jj_consume_token(7);
        ret = 1;
        break;
      case 8: 
        jj_consume_token(8);
        ret = 2;
        break;
      default: 
        jj_la1[0] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      break;
    default: 
      jj_la1[1] = jj_gen;
    }
    return ret;
  }
  
  public final int Modifier()
    throws ParseException
  {
    int ret = 0;
    switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
    {
    case 9: 
    case 10: 
    case 11: 
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 10: 
        jj_consume_token(10);
        ret = 11;
        break;
      case 11: 
        jj_consume_token(11);
        ret = 10;
        break;
      case 9: 
        jj_consume_token(9);
        ret = 10;
        break;
      default: 
        jj_la1[2] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      break;
    default: 
      jj_la1[3] = jj_gen;
    }
    return ret;
  }
  
  public final Query Query(String field)
    throws ParseException
  {
    List clauses = new ArrayList();
    Query firstQuery = null;
    boolean orPresent = false;
    
    int modifier = Modifier();
    Query q = andExpression(field);
    addClause(clauses, 0, modifier, q);
    if (modifier == 0) {
      firstQuery = q;
    }
    for (;;)
    {
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 8: 
      case 9: 
      case 10: 
      case 11: 
      case 12: 
      case 16: 
      case 17: 
      case 19: 
      case 20: 
      case 21: 
      case 22: 
      case 23: 
        break;
      case 13: 
      case 14: 
      case 15: 
      case 18: 
      default: 
        jj_la1[4] = jj_gen;
        break;
      }
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 8: 
        jj_consume_token(8);
        orPresent = true;
        break;
      default: 
        jj_la1[5] = jj_gen;
      }
      modifier = Modifier();
      q = andExpression(field);
      addClause(clauses, orPresent ? 2 : 0, modifier, q);
    }
    if ((clauses.size() == 1) && (firstQuery != null)) {
      return firstQuery;
    }
    return getBooleanQuery(clauses);
  }
  
  public final Query andExpression(String field)
    throws ParseException
  {
    List clauses = new ArrayList();
    Query firstQuery = null;
    
    Query q = Clause(field);
    addClause(clauses, 0, 0, q);
    firstQuery = q;
    for (;;)
    {
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 7: 
        break;
      default: 
        jj_la1[6] = jj_gen;
        break;
      }
      jj_consume_token(7);
      int modifier = Modifier();
      q = Clause(field);
      addClause(clauses, 1, modifier, q);
    }
    if ((clauses.size() == 1) && (firstQuery != null)) {
      return firstQuery;
    }
    return getBooleanQuery(clauses);
  }
  
  public final Query Clause(String field)
    throws ParseException
  {
    Token fieldToken = null;Token boost = null;
    if (jj_2_1(2))
    {
      fieldToken = jj_consume_token(17);
      jj_consume_token(14);
      field = discardEscapeChar(image);
    }
    Query q;
    switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
    {
    case 16: 
    case 17: 
    case 19: 
    case 20: 
    case 21: 
    case 22: 
    case 23: 
      q = Term(field);
      break;
    case 12: 
      jj_consume_token(12);
      q = Query(field);
      jj_consume_token(13);
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 15: 
        jj_consume_token(15);
        boost = jj_consume_token(23);
        break;
      default: 
        jj_la1[7] = jj_gen;
      }
      break;
    case 13: 
    case 14: 
    case 15: 
    case 18: 
    default: 
      jj_la1[8] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    if (boost != null)
    {
      float f = 1.0F;
      try
      {
        f = Float.valueOf(image).floatValue();
        q.setBoost(f);
      }
      catch (Exception ignored) {}
    }
    return q;
  }
  
  public final Query Term(String field)
    throws ParseException
  {
    Token boost = null;Token fuzzySlop = null;
    boolean prefix = false;
    boolean wildcard = false;
    boolean fuzzy = false;
    Token term;
    Query q;
    Token goop1;
    Token goop2;
    switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
    {
    case 17: 
    case 19: 
    case 20: 
    case 23: 
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 17: 
        term = jj_consume_token(17);
        break;
      case 19: 
        term = jj_consume_token(19);
        prefix = true;
        break;
      case 20: 
        term = jj_consume_token(20);
        wildcard = true;
        break;
      case 23: 
        term = jj_consume_token(23);
        break;
      case 18: 
      case 21: 
      case 22: 
      default: 
        jj_la1[9] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 18: 
        fuzzySlop = jj_consume_token(18);
        fuzzy = true;
        break;
      default: 
        jj_la1[10] = jj_gen;
      }
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 15: 
        jj_consume_token(15);
        boost = jj_consume_token(23);
        switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
        {
        case 18: 
          fuzzySlop = jj_consume_token(18);
          fuzzy = true;
          break;
        default: 
          jj_la1[11] = jj_gen;
        }
        break;
      default: 
        jj_la1[12] = jj_gen;
      }
      String termImage = discardEscapeChar(image);
      Query q;
      if (wildcard)
      {
        q = getWildcardQuery(field, termImage);
      }
      else
      {
        Query q;
        if (prefix)
        {
          q = getPrefixQuery(field, discardEscapeChar(image.substring(0, image.length() - 1)));
        }
        else
        {
          Query q;
          if (fuzzy)
          {
            float fms = fuzzyMinSim;
            try
            {
              fms = Float.valueOf(image.substring(1)).floatValue();
            }
            catch (Exception ignored) {}
            if ((fms < 0.0F) || (fms > 1.0F)) {
              throw new ParseException("Minimum similarity for a FuzzyQuery has to be between 0.0f and 1.0f !");
            }
            q = getFuzzyQuery(field, termImage, fms);
          }
          else
          {
            q = getFieldQuery(field, termImage);
          }
        }
      }
      break;
    case 21: 
      jj_consume_token(21);
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 27: 
        goop1 = jj_consume_token(27);
        break;
      case 26: 
        goop1 = jj_consume_token(26);
        break;
      default: 
        jj_la1[13] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 24: 
        jj_consume_token(24);
        break;
      default: 
        jj_la1[14] = jj_gen;
      }
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 27: 
        goop2 = jj_consume_token(27);
        break;
      case 26: 
        goop2 = jj_consume_token(26);
        break;
      default: 
        jj_la1[15] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      jj_consume_token(25);
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 15: 
        jj_consume_token(15);
        boost = jj_consume_token(23);
        break;
      default: 
        jj_la1[16] = jj_gen;
      }
      if (kind == 26) {
        image = image.substring(1, image.length() - 1);
      } else {
        image = discardEscapeChar(image);
      }
      if (kind == 26) {
        image = image.substring(1, image.length() - 1);
      } else {
        image = discardEscapeChar(image);
      }
      q = getRangeQuery(field, image, image, true);
      break;
    case 22: 
      jj_consume_token(22);
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 31: 
        goop1 = jj_consume_token(31);
        break;
      case 30: 
        goop1 = jj_consume_token(30);
        break;
      default: 
        jj_la1[17] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 28: 
        jj_consume_token(28);
        break;
      default: 
        jj_la1[18] = jj_gen;
      }
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 31: 
        goop2 = jj_consume_token(31);
        break;
      case 30: 
        goop2 = jj_consume_token(30);
        break;
      default: 
        jj_la1[19] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      jj_consume_token(29);
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 15: 
        jj_consume_token(15);
        boost = jj_consume_token(23);
        break;
      default: 
        jj_la1[20] = jj_gen;
      }
      if (kind == 30) {
        image = image.substring(1, image.length() - 1);
      } else {
        image = discardEscapeChar(image);
      }
      if (kind == 30) {
        image = image.substring(1, image.length() - 1);
      } else {
        image = discardEscapeChar(image);
      }
      q = getRangeQuery(field, image, image, false);
      break;
    case 16: 
      term = jj_consume_token(16);
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 18: 
        fuzzySlop = jj_consume_token(18);
        break;
      default: 
        jj_la1[21] = jj_gen;
      }
      switch (jj_ntk == -1 ? jj_ntk() : jj_ntk)
      {
      case 15: 
        jj_consume_token(15);
        boost = jj_consume_token(23);
        break;
      default: 
        jj_la1[22] = jj_gen;
      }
      int s = phraseSlop;
      if (fuzzySlop != null) {
        try
        {
          s = Float.valueOf(image.substring(1)).intValue();
        }
        catch (Exception ignored) {}
      }
      q = getFieldQuery(field, image.substring(1, image.length() - 1), s);
      break;
    case 18: 
    default: 
      jj_la1[23] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    if (boost != null)
    {
      float f = 1.0F;
      try
      {
        f = Float.valueOf(image).floatValue();
      }
      catch (Exception ignored) {}
      if (q != null) {
        q.setBoost(f);
      }
    }
    return q;
  }
  
  private final boolean jj_2_1(int xla)
  {
    jj_la = xla;jj_lastpos = (jj_scanpos = token);
    try
    {
      return !jj_3_1();
    }
    catch (LookaheadSuccess ls)
    {
      return true;
    }
    finally
    {
      jj_save(0, xla);
    }
  }
  
  private final boolean jj_3_1()
  {
    if (jj_scan_token(17)) {
      return true;
    }
    if (jj_scan_token(14)) {
      return true;
    }
    return false;
  }
  
  public boolean lookingAhead = false;
  private boolean jj_semLA;
  private int jj_gen;
  private final int[] jj_la1 = new int[24];
  private static int[] jj_la1_0;
  
  static
  {
    jj_la1_0();
  }
  
  private static void jj_la1_0()
  {
    jj_la1_0 = new int[] { 384, 384, 3584, 3584, 16457472, 256, 128, 32768, 16453632, 10092544, 262144, 262144, 32768, 201326592, 16777216, 201326592, 32768, -1073741824, 268435456, -1073741824, 32768, 262144, 32768, 16449536 };
  }
  
  private final JJCalls[] jj_2_rtns = new JJCalls[1];
  private boolean jj_rescan = false;
  private int jj_gc = 0;
  
  public PrecedenceQueryParser(CharStream stream)
  {
    token_source = new PrecedenceQueryParserTokenManager(stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 24; i++) {
      jj_la1[i] = -1;
    }
    for (int i = 0; i < jj_2_rtns.length; i++) {
      jj_2_rtns[i] = new JJCalls();
    }
  }
  
  public void ReInit(CharStream stream)
  {
    token_source.ReInit(stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 24; i++) {
      jj_la1[i] = -1;
    }
    for (int i = 0; i < jj_2_rtns.length; i++) {
      j
1 2 3 4

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd