|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AbstractToken.java | 100% | 94.4% | 100% | 96.7% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.tapestry.script;
|
|
16 |
|
|
17 |
import java.util.ArrayList;
|
|
18 |
import java.util.Iterator;
|
|
19 |
import java.util.List;
|
|
20 |
|
|
21 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
22 |
import org.apache.hivemind.Location;
|
|
23 |
import org.apache.tapestry.services.ExpressionEvaluator;
|
|
24 |
|
|
25 |
/**
|
|
26 |
* Base class for creating tokens which may contain other tokens.
|
|
27 |
*
|
|
28 |
* @author Howard Lewis Ship
|
|
29 |
* @since 0.2.9
|
|
30 |
*/
|
|
31 |
|
|
32 |
abstract class AbstractToken implements IScriptToken |
|
33 |
{ |
|
34 |
private List _tokens;
|
|
35 |
|
|
36 |
private Location _location;
|
|
37 |
|
|
38 | 166 |
protected AbstractToken(Location location)
|
39 |
{ |
|
40 | 166 |
_location = location; |
41 |
} |
|
42 |
|
|
43 | 6 |
public Location getLocation()
|
44 |
{ |
|
45 | 6 |
return _location;
|
46 |
} |
|
47 |
|
|
48 | 146 |
public void addToken(IScriptToken token) |
49 |
{ |
|
50 | 146 |
if (_tokens == null) |
51 | 50 |
_tokens = new ArrayList();
|
52 |
|
|
53 | 146 |
_tokens.add(token); |
54 |
} |
|
55 |
|
|
56 |
/**
|
|
57 |
* Invokes {@link IScriptToken#write(StringBuffer,ScriptSession)}on each child token (if there
|
|
58 |
* are any).
|
|
59 |
*/
|
|
60 |
|
|
61 | 59 |
protected void writeChildren(StringBuffer buffer, ScriptSession session) |
62 |
{ |
|
63 | 59 |
if (_tokens == null) |
64 | 1 |
return;
|
65 |
|
|
66 | 58 |
Iterator i = _tokens.iterator(); |
67 |
|
|
68 | 58 |
while (i.hasNext())
|
69 |
{ |
|
70 | 201 |
IScriptToken token = (IScriptToken) i.next(); |
71 |
|
|
72 | 201 |
token.write(buffer, session); |
73 |
} |
|
74 |
} |
|
75 |
|
|
76 |
/**
|
|
77 |
* Evaluates the expression against the session's symbols, using
|
|
78 |
* {@link ExpressionEvaluator#read(Object, String)}and returns the result.
|
|
79 |
*/
|
|
80 | 34 |
protected Object evaluate(String expression, ScriptSession session)
|
81 |
{ |
|
82 |
|
|
83 | 34 |
try
|
84 |
{ |
|
85 | 34 |
return session.evaluate(expression);
|
86 |
} |
|
87 |
catch (Exception ex)
|
|
88 |
{ |
|
89 | 0 |
throw new ApplicationRuntimeException(ex.getMessage(), _location, ex); |
90 |
} |
|
91 |
} |
|
92 |
|
|
93 |
/**
|
|
94 |
* Evaluates an expression and coerces the result to a boolean.
|
|
95 |
*
|
|
96 |
* @since 4.0
|
|
97 |
*/
|
|
98 |
|
|
99 | 16 |
protected boolean evaluateBoolean(String expression, ScriptSession session) |
100 |
{ |
|
101 | 16 |
try
|
102 |
{ |
|
103 | 16 |
Boolean b = (Boolean) session.evaluate(expression, Boolean.class);
|
104 |
|
|
105 | 15 |
return b.booleanValue();
|
106 |
} |
|
107 |
catch (Exception ex)
|
|
108 |
{ |
|
109 | 1 |
throw new ApplicationRuntimeException(ex.getMessage(), _location, ex); |
110 |
} |
|
111 |
} |
|
112 |
} |
|