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.Hashtable;
   22   import java.util.List;
   23   import java.util.Map;
   24   import java.util.ResourceBundle;
   25   
   26   import org.apache.lucene.messages.NLS;
   27   import org.apache.lucene.queryParser.core.messages.QueryParserMessages;
   28   
   29   /**
   30    * A {@link QueryNodeImpl} is the default implementation of the interface
   31    * {@link QueryNode}
   32    */
   33   public abstract class QueryNodeImpl implements QueryNode, Cloneable {
   34   
   35     private static final long serialVersionUID = 5569870883474845989L;
   36   
   37     /* index default field */
   38     // TODO remove PLAINTEXT_FIELD_NAME replacing it with configuration APIs
   39     public static final String PLAINTEXT_FIELD_NAME = "_plain";
   40   
   41     private boolean isLeaf = true;
   42   
   43     private Hashtable<CharSequence, Object> tags = new Hashtable<CharSequence, Object>();
   44   
   45     private List<QueryNode> clauses = null;
   46   
   47     protected void allocate() {
   48   
   49       if (this.clauses == null) {
   50         this.clauses = new ArrayList<QueryNode>();
   51   
   52       } else {
   53         this.clauses.clear();
   54       }
   55   
   56     }
   57   
   58     public final void add(QueryNode child) {
   59   
   60       if (isLeaf() || this.clauses == null || child == null) {
   61         throw new IllegalArgumentException(NLS
   62             .getLocalizedMessage(QueryParserMessages.NODE_ACTION_NOT_SUPPORTED));
   63       }
   64   
   65       this.clauses.add(child);
   66       ((QueryNodeImpl) child).setParent(this);
   67   
   68     }
   69   
   70     public final void add(List<QueryNode> children) {
   71   
   72       if (isLeaf() || this.clauses == null) {
   73         throw new IllegalArgumentException(NLS
   74             .getLocalizedMessage(QueryParserMessages.NODE_ACTION_NOT_SUPPORTED));
   75       }
   76   
   77       for (QueryNode child : getChildren()) {
   78         add(child);
   79       }
   80   
   81     }
   82   
   83     public boolean isLeaf() {
   84       return this.isLeaf;
   85     }
   86   
   87     public final void set(List<QueryNode> children) {
   88   
   89       if (isLeaf() || this.clauses == null) {
   90         ResourceBundle bundle = ResourceBundle
   91             .getBundle("org.apache.lucene.queryParser.messages.QueryParserMessages");
   92         String message = bundle.getObject("Q0008E.NODE_ACTION_NOT_SUPPORTED")
   93             .toString();
   94   
   95         throw new IllegalArgumentException(message);
   96   
   97       }
   98   
   99       // reset parent value
  100       for (QueryNode child : children) {
  101   
  102         ((QueryNodeImpl) child).setParent(null);
  103   
  104       }
  105   
  106       // allocate new children list
  107       allocate();
  108   
  109       // add new children and set parent
  110       for (QueryNode child : children) {
  111         add(child);
  112       }
  113     }
  114   
  115     public QueryNode cloneTree() throws CloneNotSupportedException {
  116       QueryNodeImpl clone = (QueryNodeImpl) super.clone();
  117       clone.isLeaf = this.isLeaf;
  118   
  119       // Reset all tags
  120       clone.tags = new Hashtable<CharSequence, Object>();
  121   
  122       // copy children
  123       if (this.clauses != null) {
  124         List<QueryNode> localClauses = new ArrayList<QueryNode>();
  125         for (QueryNode clause : this.clauses) {
  126           localClauses.add(clause.cloneTree());
  127         }
  128         clone.clauses = localClauses;
  129       }
  130   
  131       return clone;
  132     }
  133   
  134     @Override
  135     public Object clone() throws CloneNotSupportedException {
  136       return cloneTree();
  137     }
  138   
  139     protected void setLeaf(boolean isLeaf) {
  140       this.isLeaf = isLeaf;
  141     }
  142   
  143     /**
  144      * @return a List for QueryNode object. Returns null, for nodes that do not
  145      *         contain children. All leaf Nodes return null.
  146      */
  147     public final List<QueryNode> getChildren() {
  148       if (isLeaf() || this.clauses == null) {
  149         return null;
  150       }
  151       return this.clauses;
  152     }
  153   
  154     public void setTag(CharSequence tagName, Object value) {
  155       this.tags.put(tagName.toString().toLowerCase(), value);
  156     }
  157   
  158     public void unsetTag(CharSequence tagName) {
  159       this.tags.remove(tagName.toString().toLowerCase());
  160     }
  161   
  162     public boolean containsTag(CharSequence tagName) {
  163       return this.tags.containsKey(tagName.toString().toLowerCase());
  164     }
  165   
  166     public Object getTag(CharSequence tagName) {
  167       return this.tags.get(tagName.toString().toLowerCase());
  168     }
  169   
  170     private QueryNode parent = null;
  171   
  172     private void setParent(QueryNode parent) {
  173       this.parent = parent;
  174     }
  175   
  176     public QueryNode getParent() {
  177       return this.parent;
  178     }
  179   
  180     protected boolean isRoot() {
  181       return getParent() == null;
  182     }
  183   
  184     /**
  185      * If set to true the the method toQueryString will not write field names
  186      */
  187     protected boolean toQueryStringIgnoreFields = false;
  188   
  189     /**
  190      * This method is use toQueryString to detect if fld is the default field
  191      * 
  192      * @param fld
  193      *          - field name
  194      * @return true if fld is the default field
  195      */
  196     protected boolean isDefaultField(CharSequence fld) {
  197       if (this.toQueryStringIgnoreFields)
  198         return true;
  199       if (fld == null)
  200         return true;
  201       if (QueryNodeImpl.PLAINTEXT_FIELD_NAME.equals(fld.toString()))
  202         return true;
  203       return false;
  204     }
  205   
  206     /**
  207      * Every implementation of this class should return pseudo xml like this:
  208      * 
  209      * For FieldQueryNode: <field start='1' end='2' field='subject' text='foo'/>
  210      * 
  211      * @see org.apache.lucene.queryParser.core.nodes.QueryNode#toString()
  212      */
  213     @Override
  214     public String toString() {
  215       return super.toString();
  216     }
  217   
  218     /**
  219      * @see org.apache.lucene.queryParser.core.nodes.QueryNode#getTag(CharSequence)
  220      * @return a Map with all tags for this QueryNode
  221      */
  222     @SuppressWarnings( { "unchecked" })
  223     public Map<CharSequence, Object> getTags() {
  224       return (Map<CharSequence, Object>) this.tags.clone();
  225     }
  226   
  227   } // end class QueryNodeImpl

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