pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / processors / RemoveEmptyNonLeafQueryNodeProcessor.java
1 package org.apache.lucene.queryParser.standard.processors;
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.LinkedList;
21 import java.util.List;
22
23 import org.apache.lucene.queryParser.core.QueryNodeException;
24 import org.apache.lucene.queryParser.core.nodes.GroupQueryNode;
25 import org.apache.lucene.queryParser.core.nodes.MatchNoDocsQueryNode;
26 import org.apache.lucene.queryParser.core.nodes.ModifierQueryNode;
27 import org.apache.lucene.queryParser.core.nodes.QueryNode;
28 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl;
29
30 /**
31  * This processor removes every {@link QueryNode} that is not a leaf and has not
32  * children. If after processing the entire tree the root node is not a leaf and
33  * has no children, a {@link MatchNoDocsQueryNode} object is returned. <br/>
34  * <br/>
35  * This processor is used at the end of a pipeline to avoid invalid query node
36  * tree structures like a {@link GroupQueryNode} or {@link ModifierQueryNode}
37  * with no children. <br/>
38  * 
39  * @see QueryNode
40  * @see MatchNoDocsQueryNode
41  */
42 public class RemoveEmptyNonLeafQueryNodeProcessor extends
43     QueryNodeProcessorImpl {
44
45   private LinkedList<QueryNode> childrenBuffer = new LinkedList<QueryNode>();
46
47   public RemoveEmptyNonLeafQueryNodeProcessor() {
48     // empty constructor
49   }
50
51   @Override
52   public QueryNode process(QueryNode queryTree) throws QueryNodeException {
53     queryTree = super.process(queryTree);
54
55     if (!queryTree.isLeaf()) {
56
57       List<QueryNode> children = queryTree.getChildren();
58
59       if (children == null || children.size() == 0) {
60         return new MatchNoDocsQueryNode();
61       }
62
63     }
64
65     return queryTree;
66
67   }
68
69   @Override
70   protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
71
72     return node;
73
74   }
75
76   @Override
77   protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
78
79     return node;
80
81   }
82
83   @Override
84   protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
85       throws QueryNodeException {
86
87     try {
88
89       for (QueryNode child : children) {
90
91         if (!child.isLeaf()) {
92
93           List<QueryNode> grandChildren = child.getChildren();
94
95           if (grandChildren != null && grandChildren.size() > 0) {
96             this.childrenBuffer.add(child);
97           }
98
99         } else {
100           this.childrenBuffer.add(child);
101         }
102
103       }
104
105       children.clear();
106       children.addAll(this.childrenBuffer);
107
108     } finally {
109       this.childrenBuffer.clear();
110     }
111
112     return children;
113
114   }
115
116 }