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.ArrayList;
   21   import java.util.List;
   22   
   23   import org.apache.lucene.messages.MessageImpl;
   24   import org.apache.lucene.queryParser.core.QueryNodeError;
   25   import org.apache.lucene.queryParser.core.messages.QueryParserMessages;
   26   import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
   27   
   28   /**
   29    * A {@link ModifierQueryNode} indicates the modifier value (+,-,?,NONE) for
   30    * each term on the query string. For example "+t1 -t2 t3" will have a tree of:
   31    * <blockquote>
   32    * &lt;BooleanQueryNode&gt; &lt;ModifierQueryNode modifier="MOD_REQ"&gt; &lt;t1/&gt;
   33    * &lt;/ModifierQueryNode&gt; &lt;ModifierQueryNode modifier="MOD_NOT"&gt; &lt;t2/&gt;
   34    * &lt;/ModifierQueryNode&gt; &lt;t3/&gt; &lt;/BooleanQueryNode&gt;
   35    * </blockquote>
   36    */
   37   public class ModifierQueryNode extends QueryNodeImpl {
   38   
   39     private static final long serialVersionUID = -391209837953928169L;
   40   
   41     public enum Modifier {
   42       MOD_NONE, MOD_NOT, MOD_REQ;
   43   
   44       @Override
   45       public String toString() {
   46         switch (this) {
   47         case MOD_NONE:
   48           return "MOD_NONE";
   49         case MOD_NOT:
   50           return "MOD_NOT";
   51         case MOD_REQ:
   52           return "MOD_REQ";
   53         }
   54         // this code is never executed
   55         return "MOD_DEFAULT";
   56       }
   57   
   58       public String toDigitString() {
   59         switch (this) {
   60         case MOD_NONE:
   61           return "";
   62         case MOD_NOT:
   63           return "-";
   64         case MOD_REQ:
   65           return "+";
   66         }
   67         // this code is never executed
   68         return "";
   69       }
   70   
   71       public String toLargeString() {
   72         switch (this) {
   73         case MOD_NONE:
   74           return "";
   75         case MOD_NOT:
   76           return "NOT ";
   77         case MOD_REQ:
   78           return "+";
   79         }
   80         // this code is never executed
   81         return "";
   82       }
   83     }
   84   
   85     private Modifier modifier = Modifier.MOD_NONE;
   86   
   87     /**
   88      * Used to store the modifier value on the original query string
   89      * 
   90      * @param query
   91      *          - QueryNode subtree
   92      * @param mod
   93      *          - Modifier Value
   94      */
   95     public ModifierQueryNode(QueryNode query, Modifier mod) {
   96       if (query == null) {
   97         throw new QueryNodeError(new MessageImpl(
   98             QueryParserMessages.PARAMETER_VALUE_NOT_SUPPORTED, "query", "null"));
   99       }
  100   
  101       allocate();
  102       setLeaf(false);
  103       add(query);
  104       this.modifier = mod;
  105     }
  106   
  107     public QueryNode getChild() {
  108       return getChildren().get(0);
  109     }
  110   
  111     public Modifier getModifier() {
  112       return this.modifier;
  113     }
  114   
  115     @Override
  116     public String toString() {
  117       return "<modifier operation='" + this.modifier.toString() + "'>" + "\n"
  118           + getChild().toString() + "\n</modifier>";
  119     }
  120   
  121     public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) {
  122       if (getChild() == null)
  123         return "";
  124   
  125       String leftParenthensis = "";
  126       String rightParenthensis = "";
  127   
  128       if (getChild() != null && getChild() instanceof ModifierQueryNode) {
  129         leftParenthensis = "(";
  130         rightParenthensis = ")";
  131       }
  132   
  133       if (getChild() instanceof BooleanQueryNode) {
  134         return this.modifier.toLargeString() + leftParenthensis
  135             + getChild().toQueryString(escapeSyntaxParser) + rightParenthensis;
  136       } else {
  137         return this.modifier.toDigitString() + leftParenthensis
  138             + getChild().toQueryString(escapeSyntaxParser) + rightParenthensis;
  139       }
  140     }
  141   
  142     @Override
  143     public QueryNode cloneTree() throws CloneNotSupportedException {
  144       ModifierQueryNode clone = (ModifierQueryNode) super.cloneTree();
  145   
  146       clone.modifier = this.modifier;
  147   
  148       return clone;
  149     }
  150   
  151     /**
  152      * @param child
  153      */
  154     public void setChild(QueryNode child) {
  155       List<QueryNode> list = new ArrayList<QueryNode>();
  156       list.add(child);
  157       this.set(list);
  158     }
  159   
  160   }

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