KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > script > ScriptSessionImpl


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.Map JavaDoc;
18
19 import org.apache.hivemind.Resource;
20 import org.apache.tapestry.IRequestCycle;
21 import org.apache.tapestry.IScriptProcessor;
22 import org.apache.tapestry.coerce.ValueConverter;
23 import org.apache.tapestry.services.ExpressionEvaluator;
24
25 /**
26  * The result of executing a script, the session is used during the parsing process as well.
27  * Following
28  * {@link org.apache.tapestry.IScript#execute(org.apache.tapestry.IRequestCycle, org.apache.tapestry.IScriptProcessor, java.util.Map)},
29  * the session provides access to output symbols as well as the body and initialization blocks
30  * created by the script tokens.
31  *
32  * @author Howard Lewis Ship
33  * @since 0.2.9
34  */

35
36 public class ScriptSessionImpl implements ScriptSession
37 {
38     private IRequestCycle _cycle;
39
40     private IScriptProcessor _processor;
41
42     private Resource _scriptTemplateResource;
43
44     private Map JavaDoc _symbols;
45
46     /** @since 4.0 */
47     private ExpressionEvaluator _evaluator;
48
49     /** @since 4.0 */
50     private ValueConverter _valueConverter;
51
52     public ScriptSessionImpl(Resource scriptTemplateResource, IRequestCycle cycle,
53             IScriptProcessor processor, ExpressionEvaluator evaluator,
54             ValueConverter valueConverter, Map JavaDoc symbols)
55     {
56         _scriptTemplateResource = scriptTemplateResource;
57         _cycle = cycle;
58         _processor = processor;
59         _symbols = symbols;
60         _evaluator = evaluator;
61         _valueConverter = valueConverter;
62     }
63
64     public Object JavaDoc evaluate(String JavaDoc expression)
65     {
66         return _evaluator.read(_symbols, expression);
67     }
68
69     public Object JavaDoc evaluate(String JavaDoc expression, Class JavaDoc desiredType)
70     {
71         Object JavaDoc raw = evaluate(expression);
72
73         return _valueConverter.coerceValue(raw, desiredType);
74     }
75
76     public Resource getScriptTemplateResource()
77     {
78         return _scriptTemplateResource;
79     }
80
81     public Map JavaDoc getSymbols()
82     {
83         return _symbols;
84     }
85
86     public IRequestCycle getRequestCycle()
87     {
88         return _cycle;
89     }
90
91     public void addBodyScript(String JavaDoc script)
92     {
93         _processor.addBodyScript(script);
94     }
95
96     public void addExternalScript(Resource resource)
97     {
98         _processor.addExternalScript(resource);
99     }
100
101     public void addInitializationScript(String JavaDoc script)
102     {
103         _processor.addInitializationScript(script);
104     }
105
106     public String JavaDoc getUniqueString(String JavaDoc baseValue)
107     {
108         return _processor.getUniqueString(baseValue);
109     }
110
111     public String JavaDoc toString()
112     {
113         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
114
115         buffer.append("ScriptSession[");
116         buffer.append(_scriptTemplateResource);
117         buffer.append(']');
118
119         return buffer.toString();
120     }
121 }
Popular Tags