1 package org.apache.lucene.queryParser.standard.processors;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.util.LinkedList;
21 import java.util.List;
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;
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/>
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/>
40 * @see MatchNoDocsQueryNode
42 public class RemoveEmptyNonLeafQueryNodeProcessor extends
43 QueryNodeProcessorImpl {
45 private LinkedList<QueryNode> childrenBuffer = new LinkedList<QueryNode>();
47 public RemoveEmptyNonLeafQueryNodeProcessor() {
52 public QueryNode process(QueryNode queryTree) throws QueryNodeException {
53 queryTree = super.process(queryTree);
55 if (!queryTree.isLeaf()) {
57 List<QueryNode> children = queryTree.getChildren();
59 if (children == null || children.size() == 0) {
60 return new MatchNoDocsQueryNode();
70 protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
77 protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
84 protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
85 throws QueryNodeException {
89 for (QueryNode child : children) {
91 if (!child.isLeaf()) {
93 List<QueryNode> grandChildren = child.getChildren();
95 if (grandChildren != null && grandChildren.size() > 0) {
96 this.childrenBuffer.add(child);
100 this.childrenBuffer.add(child);
106 children.addAll(this.childrenBuffer);
109 this.childrenBuffer.clear();