Home » lucene-3.0.1-src » org.apache.lucene.queryParser.core.nodes » [javadoc | source]

    1   package org.apache.lucene.queryParser.core.nodes;
    2   
    3   /**
    4    * Licensed to the Apache Software Foundation (ASF) under one or more
    5    * contributor license agreements.  See the NOTICE file distributed with
    6    * this work for additional information regarding copyright ownership.
    7    * The ASF licenses this file to You under the Apache License, Version 2.0
    8    * (the "License"); you may not use this file except in compliance with
    9    * the License.  You may obtain a copy of the License at
   10    *
   11    *     http://www.apache.org/licenses/LICENSE-2.0
   12    *
   13    * Unless required by applicable law or agreed to in writing, software
   14    * distributed under the License is distributed on an "AS IS" BASIS,
   15    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16    * See the License for the specific language governing permissions and
   17    * limitations under the License.
   18    */
   19   
   20   import java.util.Locale;
   21   
   22   import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
   23   import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax.Type;
   24   
   25   /**
   26    * A {@link FieldQueryNode} represents a element that contains field/text tuple
   27    */
   28   public class FieldQueryNode extends QueryNodeImpl implements TextableQueryNode,
   29       FieldableNode {
   30   
   31     private static final long serialVersionUID = 3634521145130758265L;
   32   
   33     /**
   34      * The term's field
   35      */
   36     protected CharSequence field;
   37   
   38     /**
   39      * The term's text.
   40      */
   41     protected CharSequence text;
   42   
   43     /**
   44      * The term's begin position.
   45      */
   46     protected int begin;
   47   
   48     /**
   49      * The term's end position.
   50      */
   51     protected int end;
   52   
   53     /**
   54      * The term's position increment.
   55      */
   56     protected int positionIncrement;
   57   
   58     /**
   59      * @param field
   60      *          - field name
   61      * @param text
   62      *          - value
   63      * @param begin
   64      *          - position in the query string
   65      * @param end
   66      *          - position in the query string
   67      */
   68     public FieldQueryNode(CharSequence field, CharSequence text, int begin,
   69         int end) {
   70       this.field = field;
   71       this.text = text;
   72       this.begin = begin;
   73       this.end = end;
   74       this.setLeaf(true);
   75   
   76     }
   77   
   78     protected CharSequence getTermEscaped(EscapeQuerySyntax escaper) {
   79       return escaper.escape(this.text, Locale.getDefault(), Type.NORMAL);
   80     }
   81   
   82     protected CharSequence getTermEscapeQuoted(EscapeQuerySyntax escaper) {
   83       return escaper.escape(this.text, Locale.getDefault(), Type.STRING);
   84     }
   85   
   86     public CharSequence toQueryString(EscapeQuerySyntax escaper) {
   87       if (isDefaultField(this.field)) {
   88         return getTermEscaped(escaper);
   89       } else {
   90         return this.field + ":" + getTermEscaped(escaper);
   91       }
   92     }
   93   
   94     @Override
   95     public String toString() {
   96       return "<field start='" + this.begin + "' end='" + this.end + "' field='"
   97           + this.field + "' text='" + this.text + "'/>";
   98     }
   99   
  100     /**
  101      * @return the term
  102      */
  103     public String getTextAsString() {
  104       if (this.text == null)
  105         return null;
  106       else
  107         return this.text.toString();
  108     }
  109   
  110     /**
  111      * returns null if the field was not specified in the query string
  112      * 
  113      * @return the field
  114      */
  115     public String getFieldAsString() {
  116       if (this.field == null)
  117         return null;
  118       else
  119         return this.field.toString();
  120     }
  121   
  122     public int getBegin() {
  123       return this.begin;
  124     }
  125   
  126     public void setBegin(int begin) {
  127       this.begin = begin;
  128     }
  129   
  130     public int getEnd() {
  131       return this.end;
  132     }
  133   
  134     public void setEnd(int end) {
  135       this.end = end;
  136     }
  137   
  138     public CharSequence getField() {
  139       return this.field;
  140     }
  141   
  142     public void setField(CharSequence field) {
  143       this.field = field;
  144     }
  145   
  146     public int getPositionIncrement() {
  147       return this.positionIncrement;
  148     }
  149   
  150     public void setPositionIncrement(int pi) {
  151       this.positionIncrement = pi;
  152     }
  153   
  154     /**
  155      * Returns the term.
  156      * 
  157      * @return The "original" form of the term.
  158      */
  159     public CharSequence getText() {
  160       return this.text;
  161     }
  162   
  163     /**
  164      * @param text
  165      *          the text to set
  166      */
  167     public void setText(CharSequence text) {
  168       this.text = text;
  169     }
  170   
  171     @Override
  172     public FieldQueryNode cloneTree() throws CloneNotSupportedException {
  173       FieldQueryNode fqn = (FieldQueryNode) super.cloneTree();
  174       fqn.begin = this.begin;
  175       fqn.end = this.end;
  176       fqn.field = this.field;
  177       fqn.text = this.text;
  178       fqn.positionIncrement = this.positionIncrement;
  179       fqn.toQueryStringIgnoreFields = this.toQueryStringIgnoreFields;
  180   
  181       return fqn;
  182   
  183     }
  184   
  185   }

Home » lucene-3.0.1-src » org.apache.lucene.queryParser.core.nodes » [javadoc | source]