Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
AbstractToken |
|
| 2.6666666666666665;2.667 |
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 org.apache.hivemind.ApplicationRuntimeException; |
|
18 | import org.apache.hivemind.Location; |
|
19 | ||
20 | import java.util.ArrayList; |
|
21 | import java.util.Iterator; |
|
22 | import java.util.List; |
|
23 | ||
24 | /** |
|
25 | * Base class for creating tokens which may contain other tokens. |
|
26 | * |
|
27 | * @author Howard Lewis Ship |
|
28 | * @since 0.2.9 |
|
29 | */ |
|
30 | ||
31 | abstract class AbstractToken implements IScriptToken |
|
32 | { |
|
33 | private List _tokens; |
|
34 | ||
35 | private Location _location; |
|
36 | ||
37 | protected AbstractToken(Location location) |
|
38 | 89 | { |
39 | 89 | _location = location; |
40 | 89 | } |
41 | ||
42 | public Location getLocation() |
|
43 | { |
|
44 | 6 | return _location; |
45 | } |
|
46 | ||
47 | public void addToken(IScriptToken token) |
|
48 | { |
|
49 | 71 | if (_tokens == null) |
50 | 39 | _tokens = new ArrayList(); |
51 | ||
52 | 71 | _tokens.add(token); |
53 | 71 | } |
54 | ||
55 | /** |
|
56 | * Invokes {@link IScriptToken#write(StringBuffer,ScriptSession)}on each child token (if there |
|
57 | * are any). |
|
58 | */ |
|
59 | ||
60 | protected void writeChildren(StringBuffer buffer, ScriptSession session) |
|
61 | { |
|
62 | 42 | if (_tokens == null) |
63 | 1 | return; |
64 | ||
65 | 41 | Iterator i = _tokens.iterator(); |
66 | ||
67 | 118 | while (i.hasNext()) |
68 | { |
|
69 | 79 | IScriptToken token = (IScriptToken) i.next(); |
70 | ||
71 | 79 | token.write(buffer, session); |
72 | 77 | } |
73 | 39 | } |
74 | ||
75 | /** |
|
76 | * Evaluates the expression against the session's symbols, using |
|
77 | * {@link org.apache.tapestry.services.ExpressionEvaluator#read(Object, String)} and returns the result. |
|
78 | */ |
|
79 | protected Object evaluate(String expression, ScriptSession session) |
|
80 | { |
|
81 | ||
82 | try |
|
83 | { |
|
84 | 6 | return session.evaluate(expression); |
85 | } |
|
86 | 0 | catch (Exception ex) |
87 | { |
|
88 | 0 | throw new ApplicationRuntimeException(ex.getMessage(), _location, ex); |
89 | } |
|
90 | } |
|
91 | ||
92 | /** |
|
93 | * Evaluates an expression and coerces the result to a boolean. |
|
94 | * |
|
95 | * @since 4.0 |
|
96 | */ |
|
97 | ||
98 | protected boolean evaluateBoolean(String expression, ScriptSession session) |
|
99 | { |
|
100 | try |
|
101 | { |
|
102 | 5 | Boolean b = (Boolean) session.evaluate(expression, Boolean.class); |
103 | ||
104 | 3 | return b.booleanValue(); |
105 | } |
|
106 | 1 | catch (Exception ex) |
107 | { |
|
108 | 1 | throw new ApplicationRuntimeException(ex.getMessage(), _location, ex); |
109 | } |
|
110 | } |
|
111 | } |