KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > formmodel > ExpressionContextImpl


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

16 package org.apache.cocoon.woody.formmodel;
17
18 import org.outerj.expression.ExpressionContext;
19
20 import java.math.BigDecimal JavaDoc;
21
22 /**
23  * Implementation of the ExpressionContext required for the evaluation of
24  * expressions by xReporter expression interpreter.
25  *
26  * @version $Id: ExpressionContextImpl.java 30932 2004-07-29 17:35:38Z vgritsenko $
27  */

28 public class ExpressionContextImpl implements ExpressionContext {
29     private Widget widget;
30     private boolean referenceChildren;
31
32     public ExpressionContextImpl(Widget widget) {
33         this.widget = widget;
34         this.referenceChildren = false;
35     }
36
37     /**
38      * @param referenceChildren if true, variables will be resolved among the children of the given
39      * container widget, rather than among the siblings of the widget.
40      */

41     public ExpressionContextImpl(Widget widget, boolean referenceChildren) {
42         this.widget = widget;
43         this.referenceChildren = referenceChildren;
44     }
45
46     /**
47      * Variables refer to other widgets.
48      *
49      * <p>The current implementation only allows access to sibling widgets.
50      *
51      * <p>In case the value of a widget is null but the widget is required, then a special
52      * exception will be thrown, the {@link CannotYetResolveWarning}. This is because in
53      * that case, you'll probably want to re-evaluate the expression at a later time (since
54      * the widget is required, it will eventually get a value).
55      *
56      * <p>In case the value of the widget is null but the field is not required, then simply
57      * null is returned. (TODO: a function IsNull() will provided in the expression library
58      * so that expression writers can check for the likely condition where a non-required field
59      * is null).
60      *
61      * <p>If the variable name does not refer to an existing widget, null is returned (TODO: this
62      * behaviour will probably change in the future)
63      */

64     public Object JavaDoc resolveVariable(String JavaDoc name) {
65         // TODO allow to access other widgets instead of only siblings (allow going up with ../ notation or something)
66
Widget widget;
67         if (!referenceChildren)
68             widget = this.widget.getParent().getWidget(name);
69         else
70             widget = this.widget.getWidget(name);
71         if (widget != null) {
72             Object JavaDoc value = widget.getValue();
73
74             if (value == null && widget.isRequired()) {
75                 // the widget currently has not yet a value, but since it is required, it will get a value sooner
76
// or later. Therefore, we throw an exception here indicating that this expression can currenlty
77
// not yet be evaluated, but will be at a later time.
78
throw new CannotYetResolveWarning();
79             }
80
81             // do some type conversions:
82
// * the expression library only knows about BigDecimals as being numbers, so convert Longs first to BigDecimals
83
// * ...
84
if (value instanceof Long JavaDoc)
85                 return new BigDecimal JavaDoc(((Long JavaDoc)value).longValue());
86             else if (value instanceof Integer JavaDoc)
87                 return new BigDecimal JavaDoc(((Integer JavaDoc)value).intValue());
88             else
89                 return value;
90         }
91         return null;
92     }
93
94     public Object JavaDoc get(String JavaDoc s) {
95         return null;
96     }
97
98 }
99
Popular Tags