Coverage Report - org.apache.tapestry.html.Script
 
Classes in this File Line Coverage Branch Coverage Complexity
Script
0%
0/43
0%
0/22
2.455
 
 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 org.apache.hivemind.ApplicationRuntimeException;
 18  
 import org.apache.hivemind.Location;
 19  
 import org.apache.hivemind.Resource;
 20  
 import org.apache.hivemind.util.Defense;
 21  
 import org.apache.tapestry.*;
 22  
 import org.apache.tapestry.asset.AssetSource;
 23  
 import org.apache.tapestry.engine.IScriptSource;
 24  
 
 25  
 import java.util.HashMap;
 26  
 import java.util.Iterator;
 27  
 import java.util.Map;
 28  
 
 29  
 /**
 30  
  * Works with the {@link Body}component to add a script (and perhaps some
 31  
  * initialization) to the HTML response. [ <a
 32  
  * href="../../../../../components/general/script.html">Component Reference
 33  
  * </a>]
 34  
  * 
 35  
  * @author Howard Lewis Ship
 36  
  */
 37  
 
 38  0
 public abstract class Script extends AbstractComponent
 39  
 {
 40  
     /**
 41  
      * A Map of input and output symbols visible to the body of the Script.
 42  
      * 
 43  
      * @since 2.2
 44  
      */
 45  
 
 46  
     private Map _symbols;
 47  
 
 48  
     /**
 49  
      * Injected.
 50  
      * 
 51  
      * @since 4.0
 52  
      */
 53  
 
 54  
     public abstract IScriptSource getScriptSource();
 55  
     
 56  
     /**
 57  
      * Constructs the symbols {@link Map}. This starts with the contents of the
 58  
      * symbols parameter (if specified) to which is added any informal
 59  
      * parameters. If both a symbols parameter and informal parameters are
 60  
      * bound, then a copy of the symbols parameter's value is made (that is, the
 61  
      * {@link Map}provided by the symbols parameter is read, but not modified).
 62  
      */
 63  
 
 64  
     private Map getInputSymbols()
 65  
     {
 66  0
         Map result = new HashMap();
 67  
 
 68  0
         Map baseSymbols = getBaseSymbols();
 69  
 
 70  0
         if (baseSymbols != null) result.putAll(baseSymbols);
 71  
 
 72  
         // Now, iterate through all the binding names (which includes both
 73  
         // formal and informal parmeters). Skip the formal ones and
 74  
         // access the informal ones.
 75  
 
 76  0
         Iterator i = getBindingNames().iterator();
 77  0
         while(i.hasNext())
 78  
         {
 79  0
             String bindingName = (String) i.next();
 80  
 
 81  
             // Skip formal parameters
 82  
 
 83  0
             if (getSpecification().getParameter(bindingName) != null) continue;
 84  
 
 85  0
             IBinding binding = getBinding(bindingName);
 86  
 
 87  0
             Object value = binding.getObject();
 88  
 
 89  0
             result.put(bindingName, value);
 90  0
         }
 91  
 
 92  0
         return result;
 93  
     }
 94  
 
 95  
     /**
 96  
      * Gets the {@link IScript}for the correct script.
 97  
      */
 98  
 
 99  
     private IScript getParsedScript()
 100  
     {
 101  0
         IAsset scriptAsset = getScriptAsset();
 102  0
         String scriptPath = getScriptPath();
 103  
 
 104  
         // only one of the two is allowed
 105  0
         if (scriptAsset != null && scriptPath != null)
 106  0
             throw new ApplicationRuntimeException(HTMLMessages.multiAssetParameterError(getBinding("scriptAsset"),
 107  
                             getBinding("script")));
 108  
 
 109  0
         if (scriptPath == null && scriptAsset == null)
 110  0
             throw new ApplicationRuntimeException(HTMLMessages.noScriptPathError());
 111  
 
 112  0
         IScriptSource source = getScriptSource();
 113  
         
 114  0
         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  0
             Resource rootLocation = getContainer().getSpecification().getSpecificationLocation();
 121  
 
 122  0
             scriptAsset = getAssetSource().findAsset(rootLocation, getContainer().getSpecification(), scriptPath, getPage().getLocale(), getScriptLocation());
 123  
         }
 124  
 
 125  0
         Defense.notNull(scriptAsset, "script");
 126  
 
 127  
         try
 128  
         {
 129  0
             return source.getScript(scriptAsset.getResourceLocation());
 130  
         }
 131  0
         catch (RuntimeException ex)
 132  
         {
 133  0
             throw new ApplicationRuntimeException(ex.getMessage(), this, getScriptLocation(), ex);
 134  
         }
 135  
 
 136  
     }
 137  
 
 138  
     Location getScriptLocation()
 139  
     {
 140  0
         Location location = null;
 141  
 
 142  0
         if (getBinding("script")!=null)
 143  0
             location = getBinding("script").getLocation();
 144  0
         else if (getBinding("scriptAsset")!=null)
 145  0
             location = getBinding("scriptAsset").getLocation();
 146  
 
 147  0
         return location;
 148  
     }
 149  
 
 150  
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 151  
     {
 152  0
         if (!cycle.isRewinding())
 153  
         {
 154  0
             PageRenderSupport pageRenderSupport = TapestryUtils
 155  
                     .getPageRenderSupport(cycle, this);
 156  
             
 157  0
             _symbols = getInputSymbols();
 158  
             
 159  0
             getParsedScript().execute(this, cycle, pageRenderSupport, _symbols);
 160  
         }
 161  
 
 162  
         // Render the body of the Script;
 163  0
         renderBody(writer, cycle);
 164  0
     }
 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  0
         return _symbols;
 188  
     }
 189  
 
 190  
     protected void cleanupAfterRender(IRequestCycle cycle)
 191  
     {
 192  0
         _symbols = null;
 193  
 
 194  0
         super.cleanupAfterRender(cycle);
 195  0
     }
 196  
 
 197  
 }