pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / core / processors / RemoveDeletedQueryNodesProcessor.java
1 package org.apache.lucene.queryParser.core.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.Iterator;
21 import java.util.List;
22
23 import org.apache.lucene.queryParser.core.QueryNodeException;
24 import org.apache.lucene.queryParser.core.nodes.DeletedQueryNode;
25 import org.apache.lucene.queryParser.core.nodes.MatchNoDocsQueryNode;
26 import org.apache.lucene.queryParser.core.nodes.QueryNode;
27
28 /**
29  * A {@link QueryNodeProcessorPipeline} class removes every instance of
30  * {@link DeletedQueryNode} from a query node tree. If the resulting root node
31  * is a {@link DeletedQueryNode}, {@link MatchNoDocsQueryNode} is returned.
32  * 
33  */
34 public class RemoveDeletedQueryNodesProcessor extends QueryNodeProcessorImpl {
35
36   public RemoveDeletedQueryNodesProcessor() {
37     // empty constructor
38   }
39
40   @Override
41   public QueryNode process(QueryNode queryTree) throws QueryNodeException {
42     queryTree = super.process(queryTree);
43
44     if (queryTree instanceof DeletedQueryNode
45         && !(queryTree instanceof MatchNoDocsQueryNode)) {
46
47       return new MatchNoDocsQueryNode();
48
49     }
50
51     return queryTree;
52
53   }
54
55   @Override
56   protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
57
58     if (!node.isLeaf()) {
59       List<QueryNode> children = node.getChildren();
60       boolean removeBoolean = false;
61
62       if (children == null || children.size() == 0) {
63         removeBoolean = true;
64
65       } else {
66         removeBoolean = true;
67
68         for (Iterator<QueryNode> it = children.iterator(); it.hasNext();) {
69
70           if (!(it.next() instanceof DeletedQueryNode)) {
71             removeBoolean = false;
72             break;
73
74           }
75
76         }
77
78       }
79
80       if (removeBoolean) {
81         return new DeletedQueryNode();
82       }
83
84     }
85
86     return node;
87
88   }
89
90   @Override
91   protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
92       throws QueryNodeException {
93
94     for (int i = 0; i < children.size(); i++) {
95
96       if (children.get(i) instanceof DeletedQueryNode) {
97         children.remove(i--);
98       }
99
100     }
101
102     return children;
103
104   }
105
106   @Override
107   protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
108
109     return node;
110
111   }
112
113 }