Clover coverage report - Code Coverage for tapestry release 4.0.2
Coverage timestamp: Thu Apr 13 2006 10:52:06 EDT
file stats: LOC: 183   Methods: 5
NCLOC: 85   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Script.java 85.7% 86.1% 80% 85.5%
coverage coverage
 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.html;
 16   
 17    import java.util.HashMap;
 18    import java.util.Iterator;
 19    import java.util.Map;
 20   
 21    import org.apache.hivemind.ApplicationRuntimeException;
 22    import org.apache.hivemind.Resource;
 23    import org.apache.tapestry.AbstractComponent;
 24    import org.apache.tapestry.IAsset;
 25    import org.apache.tapestry.IBinding;
 26    import org.apache.tapestry.IMarkupWriter;
 27    import org.apache.tapestry.IRequestCycle;
 28    import org.apache.tapestry.IScript;
 29    import org.apache.tapestry.PageRenderSupport;
 30    import org.apache.tapestry.TapestryUtils;
 31    import org.apache.tapestry.engine.IScriptSource;
 32   
 33    /**
 34    * Works with the {@link Body}component to add a script (and perhaps some initialization) to the
 35    * HTML response. [ <a href="../../../../../ComponentReference/Script.html">Component Reference
 36    * </a>]
 37    *
 38    * @author Howard Lewis Ship
 39    */
 40   
 41    public abstract class Script extends AbstractComponent
 42    {
 43    /**
 44    * Injected
 45    *
 46    * @since 4.0
 47    */
 48   
 49    public abstract IScriptSource getScriptSource();
 50   
 51    /**
 52    * A Map of input and output symbols visible to the body of the Script.
 53    *
 54    * @since 2.2
 55    */
 56   
 57    private Map _symbols;
 58   
 59    /**
 60    * Constructs the symbols {@link Map}. This starts with the contents of the symbols parameter
 61    * (if specified) to which is added any informal parameters. If both a symbols parameter and
 62    * informal parameters are bound, then a copy of the symbols parameter's value is made (that is,
 63    * the {@link Map}provided by the symbols parameter is read, but not modified).
 64    */
 65   
 66  5 private Map getInputSymbols()
 67    {
 68  5 Map result = new HashMap();
 69   
 70  5 Map baseSymbols = getBaseSymbols();
 71   
 72  5 if (baseSymbols != null)
 73  2 result.putAll(baseSymbols);
 74   
 75    // Now, iterate through all the binding names (which includes both
 76    // formal and informal parmeters). Skip the formal ones and
 77    // access the informal ones.
 78   
 79  5 Iterator i = getBindingNames().iterator();
 80  5 while (i.hasNext())
 81    {
 82  1 String bindingName = (String) i.next();
 83   
 84    // Skip formal parameters
 85   
 86  1 if (getSpecification().getParameter(bindingName) != null)
 87  0 continue;
 88   
 89  1 IBinding binding = getBinding(bindingName);
 90   
 91  1 Object value = binding.getObject();
 92   
 93  1 result.put(bindingName, value);
 94    }
 95   
 96  5 return result;
 97    }
 98   
 99    /**
 100    * Gets the {@link IScript}for the correct script.
 101    */
 102   
 103  5 private IScript getParsedScript()
 104    {
 105  5 IAsset scriptAsset = getScriptAsset();
 106  5 String scriptPath = getScriptPath();
 107   
 108    //only one of the two is allowed
 109  5 if (scriptAsset != null && scriptPath != null)
 110  1 throw new ApplicationRuntimeException(HTMLMessages.multiAssetParameterError(getBinding("scriptAsset"),
 111    getBinding("scriptPath")));
 112   
 113  4 if (scriptPath == null && scriptAsset == null)
 114  0 throw new ApplicationRuntimeException(HTMLMessages.noScriptPathError());
 115   
 116  4 IScriptSource source = getScriptSource();
 117   
 118  4 Resource scriptLocation = null;
 119  4 if (scriptPath != null) {
 120   
 121    // If the script path is relative, it should be relative to the Script component's
 122    // container (i.e., relative to a page in the application).
 123   
 124  3 Resource rootLocation = getContainer().getSpecification().getSpecificationLocation();
 125  3 scriptLocation = rootLocation.getRelativeResource(scriptPath);
 126    } else
 127  1 scriptLocation = scriptAsset.getResourceLocation();
 128   
 129  4 try
 130    {
 131  4 return source.getScript(scriptLocation);
 132    }
 133    catch (RuntimeException ex)
 134    {
 135  0 throw new ApplicationRuntimeException(ex.getMessage(), this, getBinding("script")
 136    .getLocation(), ex);
 137    }
 138   
 139    }
 140   
 141  6 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 142    {
 143  6 if (!cycle.isRewinding())
 144    {
 145  5 PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
 146   
 147  5 _symbols = getInputSymbols();
 148   
 149  5 getParsedScript().execute(cycle, pageRenderSupport, _symbols);
 150    }
 151   
 152    // Render the body of the Script;
 153  5 renderBody(writer, cycle);
 154    }
 155   
 156    public abstract String getScriptPath();
 157   
 158    public abstract IAsset getScriptAsset();
 159   
 160    // Parameter
 161   
 162    public abstract Map getBaseSymbols();
 163   
 164    /**
 165    * Returns the complete set of symbols (input and output) from the script execution. This is
 166    * visible to the body of the Script, but is cleared after the Script finishes rendering.
 167    *
 168    * @since 2.2
 169    */
 170   
 171  2 public Map getSymbols()
 172    {
 173  2 return _symbols;
 174    }
 175   
 176  0 protected void cleanupAfterRender(IRequestCycle cycle)
 177    {
 178  0 _symbols = null;
 179   
 180  0 super.cleanupAfterRender(cycle);
 181    }
 182   
 183    }