001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.script;
016    
017    import org.apache.hivemind.Resource;
018    import org.apache.tapestry.IComponent;
019    import org.apache.tapestry.IRequestCycle;
020    import org.apache.tapestry.IScriptProcessor;
021    import org.apache.tapestry.coerce.ValueConverter;
022    import org.apache.tapestry.services.ExpressionEvaluator;
023    
024    import java.util.Map;
025    
026    /**
027     * The result of executing a script, the session is used during the parsing
028     * process as well. Following
029     * {@link org.apache.tapestry.IScript#execute(IComponent, org.apache.tapestry.IRequestCycle, org.apache.tapestry.IScriptProcessor, java.util.Map)},
030     * the session provides access to output symbols as well as the body and
031     * initialization blocks created by the script tokens.
032     *
033     * @author Howard Lewis Ship
034     * @since 0.2.9
035     */
036    
037    public class ScriptSessionImpl implements ScriptSession
038    {
039    
040        private IRequestCycle _cycle;
041    
042        private IScriptProcessor _processor;
043    
044        private Resource _scriptTemplateResource;
045    
046        private Map _symbols;
047    
048        /** @since 4.0 */
049        private ExpressionEvaluator _evaluator;
050    
051        /** @since 4.0 */
052        private ValueConverter _valueConverter;
053    
054        private IComponent _component;
055    
056        public ScriptSessionImpl(Resource scriptTemplateResource,
057                                 IRequestCycle cycle, IScriptProcessor processor,
058                                 ExpressionEvaluator evaluator, ValueConverter valueConverter,
059                                 Map symbols)
060        {
061            _scriptTemplateResource = scriptTemplateResource;
062            _cycle = cycle;
063            _processor = processor;
064            _symbols = symbols;
065            _evaluator = evaluator;
066            _valueConverter = valueConverter;
067        }
068    
069        public ScriptSessionImpl(Resource scriptTemplateResource,
070                                 IComponent component,
071                                 IRequestCycle cycle, IScriptProcessor processor,
072                                 ExpressionEvaluator evaluator, ValueConverter valueConverter,
073                                 Map symbols)
074        {
075            _scriptTemplateResource = scriptTemplateResource;
076            _component = component;
077            _cycle = cycle;
078            _processor = processor;
079            _symbols = symbols;
080            _evaluator = evaluator;
081            _valueConverter = valueConverter;
082        }
083    
084        public Object evaluate(String expression)
085        {
086            return _evaluator.read(_symbols, expression); //_evaluator.read(_symbols, expression);
087        }
088    
089        public Object evaluate(String expression, Class desiredType)
090        {
091            Object raw = evaluate(expression);
092    
093            return _valueConverter.coerceValue(raw, desiredType);
094        }
095    
096        public Resource getScriptTemplateResource()
097        {
098            return _scriptTemplateResource;
099        }
100    
101        public Map getSymbols()
102        {
103            return _symbols;
104        }
105    
106        public IRequestCycle getRequestCycle()
107        {
108            return _cycle;
109        }
110    
111        public void addBodyScript(String script)
112        {
113            addBodyScript(_component, script);
114        }
115    
116        /**
117         * {@inheritDoc}
118         */
119        public boolean isBodyScriptAllowed(IComponent target)
120        {
121            return _processor.isBodyScriptAllowed(target);
122        }
123    
124        /**
125         * {@inheritDoc}
126         */
127        public boolean isExternalScriptAllowed(IComponent target)
128        {
129            return _processor.isExternalScriptAllowed(target);
130        }
131    
132        /**
133         * {@inheritDoc}
134         */
135        public boolean isInitializationScriptAllowed(IComponent target)
136        {
137            return _processor.isInitializationScriptAllowed(target);
138        }
139    
140        public void addBodyScript(IComponent target, String script)
141        {
142            if (_processor.isBodyScriptAllowed(target))
143                _processor.addBodyScript(target, script);
144        }
145    
146        public void addExternalScript(Resource resource)
147        {
148            addExternalScript(_component, resource);
149        }
150    
151        public void addExternalScript(IComponent target, Resource resource)
152        {
153            if (_processor.isExternalScriptAllowed(target))
154                _processor.addExternalScript(target, resource);
155        }
156    
157        public void addInitializationScript(String script)
158        {
159            addInitializationScript(_component, script);
160        }
161    
162        public void addInitializationScript(IComponent target, String script)
163        {
164            if (_processor.isInitializationScriptAllowed(target))
165                _processor.addInitializationScript(target, script);
166        }
167    
168        public void addScriptAfterInitialization(IComponent target, String script)
169        {
170            if (_processor.isInitializationScriptAllowed(target))
171                _processor.addScriptAfterInitialization(target, script);
172        }
173    
174        public String getUniqueString(String baseValue)
175        {
176            return _processor.getUniqueString(baseValue);
177        }
178    
179        public String toString()
180        {
181            StringBuffer buffer = new StringBuffer();
182    
183            buffer.append("ScriptSession[");
184            buffer.append(_scriptTemplateResource);
185            buffer.append(']');
186    
187            return buffer.toString();
188        }
189    }