KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > script > el > ParsedExpression


1 /*
2  * Copyright 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  * $Header:$
17  */

18 package org.apache.beehive.netui.script.el;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.beehive.netui.util.logging.Logger;
26
27 /**
28  *
29  */

30 public class ParsedExpression {
31
32     private static Logger LOGGER = Logger.getInstance(ParsedExpression.class);
33
34     /* cache the debug status; this needs to be _fast_ */
35     private static final boolean DEBUG_ENABLED = LOGGER.isDebugEnabled();
36     private static final String JavaDoc EMPTY_STRING = "";
37
38     private ArrayList JavaDoc/*<Term>*/ _terms = new ArrayList JavaDoc/*<Term>*/(3);
39     private boolean _isExpression = false;
40     private boolean _containsExpression = false;
41     private ExpressionTerm _atomicExpression = null;
42     private Term[] _termArray = null;
43     private String JavaDoc _exprStr;
44
45     public void seal() {
46         _termArray = new Term[_terms.size()];
47
48         InternalStringBuilder buf = new InternalStringBuilder();
49         for(int i = 0; i < _terms.size(); i++) {
50             Term t = (Term)_terms.get(i);
51             t.seal();
52
53             if(t instanceof ExpressionTerm) {
54                 if(_terms.size() == 1) {
55                     _atomicExpression = (ExpressionTerm)_terms.get(0);
56                     _isExpression = true;
57                 }
58                 _containsExpression = true;
59             } else if(t instanceof LiteralTerm) {
60                 String JavaDoc lit = t.getExpressionString();
61                 if(lit != null && lit.indexOf("{") > -1)
62                     _containsExpression = true;
63             }
64
65             _termArray[i] = (Term)_terms.get(i);
66             buf.append(t.getExpressionString());
67         }
68         _exprStr = buf.toString();
69     }
70
71     public boolean isExpression() {
72         return _isExpression;
73     }
74
75     public boolean containsExpression() {
76         return _containsExpression;
77     }
78
79     public void addTerm(Term term) {
80         _terms.add(term);
81     }
82
83     public int getTokenCount() {
84         return _terms.size();
85     }
86
87     public Term getTerm(int i) {
88         assert _termArray != null;
89         assert i > 0 && i < _termArray.length;
90
91         return _termArray[i];
92     }
93
94     public ExpressionTerm getAtomicExpressionTerm() {
95         return _atomicExpression;
96     }
97
98     public Object JavaDoc evaluate(NetUIVariableResolver vr) {
99         if(DEBUG_ENABLED)
100             LOGGER.debug("evaluate expression: " + _exprStr);
101
102         if(_isExpression) {
103             if(DEBUG_ENABLED)
104                 LOGGER.debug("atoimc expression");
105
106             return _atomicExpression.evaluate(vr);
107         } else {
108             InternalStringBuilder buf = new InternalStringBuilder();
109
110             for(int i = 0; i < _terms.size(); i++) {
111                 if(DEBUG_ENABLED)
112                     LOGGER.debug("term[" + i + "]: " + _termArray[i].getClass().getName() +
113                         " expression string: " + _termArray[i].getExpressionString());
114
115                 Object JavaDoc result = _termArray[i].evaluate(vr);
116
117                 buf.append(result != null ? result.toString() : EMPTY_STRING);
118             }
119
120             return buf.toString();
121         }
122     }
123
124     public void update(Object JavaDoc value, NetUIVariableResolver vr) {
125         if(!_isExpression) {
126             String JavaDoc msg = "The expression can not be updated because it is not atomic.";
127             if(LOGGER.isErrorEnabled())
128                 LOGGER.error(msg);
129             throw new RuntimeException JavaDoc(msg);
130         }
131
132         _atomicExpression.update(value, vr);
133     }
134
135     public String JavaDoc changeContext(String JavaDoc oldContext, String JavaDoc newContext, Object JavaDoc index) {
136         if(!_isExpression) {
137             String JavaDoc msg = "The expression can not change context because it is not atomic.";
138
139             if(LOGGER.isErrorEnabled())
140                 LOGGER.error(msg);
141
142             throw new RuntimeException JavaDoc(msg);
143         }
144
145         return _atomicExpression.changeContext(oldContext, newContext, index);
146     }
147
148     public String JavaDoc qualify(String JavaDoc contextName) {
149         /* todo: could check to see if first term is literal */
150
151         return "{" + contextName + "." + getExpressionString() + "}";
152     }
153
154     // only call on atomic expressions
155
public String JavaDoc getExpressionString() {
156         if(_isExpression)
157             return _atomicExpression.getExpressionString();
158         else
159             return _exprStr;
160     }
161
162     public String JavaDoc toString() {
163         InternalStringBuilder builder = new InternalStringBuilder();
164         for(Iterator JavaDoc i = _terms.iterator(); i.hasNext();)
165         {
166             Term term = (Term) i.next();
167             builder.append(term.toString());
168         }
169         return builder.toString();
170     }
171 }
172
Popular Tags