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.html;
016    
017    import org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.hivemind.Location;
019    import org.apache.hivemind.Resource;
020    import org.apache.hivemind.util.Defense;
021    import org.apache.tapestry.*;
022    import org.apache.tapestry.asset.AssetSource;
023    import org.apache.tapestry.engine.IScriptSource;
024    
025    import java.util.HashMap;
026    import java.util.Iterator;
027    import java.util.Map;
028    
029    /**
030     * Works with the {@link Body}component to add a script (and perhaps some
031     * initialization) to the HTML response. [ <a
032     * href="../../../../../ComponentReference/Script.html">Component Reference
033     * </a>]
034     * 
035     * @author Howard Lewis Ship
036     */
037    
038    public abstract class Script extends AbstractComponent
039    {
040        /**
041         * A Map of input and output symbols visible to the body of the Script.
042         * 
043         * @since 2.2
044         */
045    
046        private Map _symbols;
047    
048        /**
049         * Injected.
050         * 
051         * @since 4.0
052         */
053    
054        public abstract IScriptSource getScriptSource();
055        
056        /**
057         * Constructs the symbols {@link Map}. This starts with the contents of the
058         * symbols parameter (if specified) to which is added any informal
059         * parameters. If both a symbols parameter and informal parameters are
060         * bound, then a copy of the symbols parameter's value is made (that is, the
061         * {@link Map}provided by the symbols parameter is read, but not modified).
062         */
063    
064        private Map getInputSymbols()
065        {
066            Map result = new HashMap();
067    
068            Map baseSymbols = getBaseSymbols();
069    
070            if (baseSymbols != null) result.putAll(baseSymbols);
071    
072            // Now, iterate through all the binding names (which includes both
073            // formal and informal parmeters). Skip the formal ones and
074            // access the informal ones.
075    
076            Iterator i = getBindingNames().iterator();
077            while(i.hasNext())
078            {
079                String bindingName = (String) i.next();
080    
081                // Skip formal parameters
082    
083                if (getSpecification().getParameter(bindingName) != null) continue;
084    
085                IBinding binding = getBinding(bindingName);
086    
087                Object value = binding.getObject();
088    
089                result.put(bindingName, value);
090            }
091    
092            return result;
093        }
094    
095        /**
096         * Gets the {@link IScript}for the correct script.
097         */
098    
099        private IScript getParsedScript()
100        {
101            IAsset scriptAsset = getScriptAsset();
102            String scriptPath = getScriptPath();
103    
104            // only one of the two is allowed
105            if (scriptAsset != null && scriptPath != null)
106                throw new ApplicationRuntimeException(HTMLMessages.multiAssetParameterError(getBinding("scriptAsset"),
107                                getBinding("script")));
108    
109            if (scriptPath == null && scriptAsset == null)
110                throw new ApplicationRuntimeException(HTMLMessages.noScriptPathError());
111    
112            IScriptSource source = getScriptSource();
113            
114            if (scriptPath != null)
115            {
116                // If the script path is relative, it should be relative to the
117                // Script component's
118                // container (i.e., relative to a page in the application).
119    
120                Resource rootLocation = getContainer().getSpecification().getSpecificationLocation();
121    
122                scriptAsset = getAssetSource().findAsset(rootLocation, getContainer().getSpecification(), scriptPath, getPage().getLocale(), getScriptLocation());
123            }
124    
125            Defense.notNull(scriptAsset, "script");
126    
127            try
128            {
129                return source.getScript(scriptAsset.getResourceLocation());
130            }
131            catch (RuntimeException ex)
132            {
133                throw new ApplicationRuntimeException(ex.getMessage(), this, getScriptLocation(), ex);
134            }
135    
136        }
137    
138        Location getScriptLocation()
139        {
140            Location location = null;
141    
142            if (getBinding("script")!=null)
143                location = getBinding("script").getLocation();
144            else if (getBinding("scriptAsset")!=null)
145                location = getBinding("scriptAsset").getLocation();
146    
147            return location;
148        }
149    
150        protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
151        {
152            if (!cycle.isRewinding())
153            {
154                PageRenderSupport pageRenderSupport = TapestryUtils
155                        .getPageRenderSupport(cycle, this);
156                
157                _symbols = getInputSymbols();
158                
159                getParsedScript().execute(this, cycle, pageRenderSupport, _symbols);
160            }
161    
162            // Render the body of the Script;
163            renderBody(writer, cycle);
164        }
165    
166        public abstract String getScriptPath();
167    
168        public abstract IAsset getScriptAsset();
169    
170        // injected
171        public abstract AssetSource getAssetSource();
172    
173        // Parameter
174    
175        public abstract Map getBaseSymbols();
176    
177        /**
178         * Returns the complete set of symbols (input and output) from the script
179         * execution. This is visible to the body of the Script, but is cleared
180         * after the Script finishes rendering.
181         * 
182         * @since 2.2
183         */
184    
185        public Map getSymbols()
186        {
187            return _symbols;
188        }
189    
190        protected void cleanupAfterRender(IRequestCycle cycle)
191        {
192            _symbols = null;
193    
194            super.cleanupAfterRender(cycle);
195        }
196    
197    }