KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > exp > parser > AggregateConditionNode


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.exp.parser;
21
22 import org.apache.commons.collections.Transformer;
23 import org.apache.cayenne.exp.ExpressionException;
24
25 /**
26  * Superclass of aggregated conditional nodes such as NOT, AND, OR. Performs
27  * extra checks on parent and child expressions to validate conditions that
28  * are not addressed in the Cayenne expressions grammar.
29  *
30  * @since 1.1
31  * @author Andrus Adamchik
32  */

33 public abstract class AggregateConditionNode extends SimpleNode {
34     AggregateConditionNode(int i) {
35         super(i);
36     }
37
38     protected boolean pruneNodeForPrunedChild(Object JavaDoc prunedChild) {
39         return false;
40     }
41
42     protected Object JavaDoc transformExpression(Transformer transformer) {
43         Object JavaDoc transformed = super.transformExpression(transformer);
44
45         if (!(transformed instanceof AggregateConditionNode)) {
46             return transformed;
47         }
48         
49         AggregateConditionNode condition = (AggregateConditionNode) transformed;
50
51         // prune itself if the transformation resulted in
52
// no children or a single child
53
switch (condition.getOperandCount()) {
54             case 1 :
55                 return condition.getOperand(0);
56             case 0 :
57                 return PRUNED_NODE;
58             default :
59                 return condition;
60         }
61     }
62
63     public void jjtSetParent(Node n) {
64         // this is a check that we can't handle properly
65
// in the grammar... do it here...
66

67         // disallow non-aggregated condition parents...
68
if (!(n instanceof AggregateConditionNode)) {
69             String JavaDoc label =
70                 (n instanceof SimpleNode)
71                     ? ((SimpleNode) n).expName()
72                     : String.valueOf(n);
73             throw new ExpressionException(expName() + ": invalid parent - " + label);
74         }
75
76         super.jjtSetParent(n);
77     }
78
79     public void jjtAddChild(Node n, int i) {
80         // this is a check that we can't handle properly
81
// in the grammar... do it here...
82

83         // only allow conditional nodes...no scalars
84
if (!(n instanceof ConditionNode) && !(n instanceof AggregateConditionNode)) {
85             String JavaDoc label =
86                 (n instanceof SimpleNode)
87                     ? ((SimpleNode) n).expName()
88                     : String.valueOf(n);
89             throw new ExpressionException(expName() + ": invalid child - " + label);
90         }
91
92         super.jjtAddChild(n, i);
93     }
94 }
95
Popular Tags