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 java.util.Map;
018    
019    import org.apache.hivemind.Resource;
020    import org.apache.tapestry.IRequestCycle;
021    import org.apache.tapestry.IScriptProcessor;
022    import org.apache.tapestry.coerce.ValueConverter;
023    import org.apache.tapestry.services.ExpressionEvaluator;
024    
025    /**
026     * The result of executing a script, the session is used during the parsing process as well.
027     * Following
028     * {@link org.apache.tapestry.IScript#execute(org.apache.tapestry.IRequestCycle, org.apache.tapestry.IScriptProcessor, java.util.Map)},
029     * the session provides access to output symbols as well as the body and initialization blocks
030     * created by the script tokens.
031     * 
032     * @author Howard Lewis Ship
033     * @since 0.2.9
034     */
035    
036    public class ScriptSessionImpl implements ScriptSession
037    {
038        private IRequestCycle _cycle;
039    
040        private IScriptProcessor _processor;
041    
042        private Resource _scriptTemplateResource;
043    
044        private Map _symbols;
045    
046        /** @since 4.0 */
047        private ExpressionEvaluator _evaluator;
048    
049        /** @since 4.0 */
050        private ValueConverter _valueConverter;
051    
052        public ScriptSessionImpl(Resource scriptTemplateResource, IRequestCycle cycle,
053                IScriptProcessor processor, ExpressionEvaluator evaluator,
054                ValueConverter valueConverter, Map symbols)
055        {
056            _scriptTemplateResource = scriptTemplateResource;
057            _cycle = cycle;
058            _processor = processor;
059            _symbols = symbols;
060            _evaluator = evaluator;
061            _valueConverter = valueConverter;
062        }
063    
064        public Object evaluate(String expression)
065        {
066            return _evaluator.read(_symbols, expression);
067        }
068    
069        public Object evaluate(String expression, Class desiredType)
070        {
071            Object raw = evaluate(expression);
072    
073            return _valueConverter.coerceValue(raw, desiredType);
074        }
075    
076        public Resource getScriptTemplateResource()
077        {
078            return _scriptTemplateResource;
079        }
080    
081        public Map getSymbols()
082        {
083            return _symbols;
084        }
085    
086        public IRequestCycle getRequestCycle()
087        {
088            return _cycle;
089        }
090    
091        public void addBodyScript(String script)
092        {
093            _processor.addBodyScript(script);
094        }
095    
096        public void addExternalScript(Resource resource)
097        {
098            _processor.addExternalScript(resource);
099        }
100    
101        public void addInitializationScript(String script)
102        {
103            _processor.addInitializationScript(script);
104        }
105    
106        public String getUniqueString(String baseValue)
107        {
108            return _processor.getUniqueString(baseValue);
109        }
110    
111        public String toString()
112        {
113            StringBuffer buffer = new StringBuffer();
114    
115            buffer.append("ScriptSession[");
116            buffer.append(_scriptTemplateResource);
117            buffer.append(']');
118    
119            return buffer.toString();
120        }
121    }