Clover coverage report - Code Coverage for tapestry release 4.0-beta-2
Coverage timestamp: Sat Jul 9 2005 22:02:17 EDT
file stats: LOC: 256   Methods: 11
NCLOC: 146   Classes: 1
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
PageRenderSupportImpl.java 96.4% 100% 100% 99.1%
coverage coverage
 1    // Copyright 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.util;
 16   
 17    import java.util.ArrayList;
 18    import java.util.HashMap;
 19    import java.util.List;
 20    import java.util.Map;
 21   
 22    import org.apache.hivemind.Locatable;
 23    import org.apache.hivemind.Location;
 24    import org.apache.hivemind.Resource;
 25    import org.apache.hivemind.util.Defense;
 26    import org.apache.tapestry.IAsset;
 27    import org.apache.tapestry.IMarkupWriter;
 28    import org.apache.tapestry.IRequestCycle;
 29    import org.apache.tapestry.PageRenderSupport;
 30    import org.apache.tapestry.Tapestry;
 31    import org.apache.tapestry.asset.AssetFactory;
 32    import org.apache.tapestry.asset.PrivateAsset;
 33   
 34    /**
 35    * Implementation of {@link org.apache.tapestry.PageRenderSupport}. The
 36    * {@link org.apache.tapestry.html.Body} component uses an instance of this class.
 37    *
 38    * @author Howard M. Lewis Ship
 39    * @since 4.0
 40    */
 41    public class PageRenderSupportImpl implements Locatable, PageRenderSupport
 42    {
 43    private final AssetFactory _assetFactory;
 44   
 45    private final Location _location;
 46   
 47    // Lines that belong inside the onLoad event handler for the <body> tag.
 48    private StringBuffer _initializationScript;
 49   
 50    // Any other scripting desired
 51   
 52    private StringBuffer _bodyScript;
 53   
 54    // Contains text lines related to image initializations
 55   
 56    private StringBuffer _imageInitializations;
 57   
 58    /**
 59    * Map of URLs to Strings (preloaded image references).
 60    */
 61   
 62    private Map _imageMap;
 63   
 64    /**
 65    * List of included scripts. Values are Strings.
 66    *
 67    * @since 1.0.5
 68    */
 69   
 70    private List _externalScripts;
 71   
 72    private final IdAllocator _idAllocator;
 73   
 74    private final String _preloadName;
 75   
 76  84 public PageRenderSupportImpl(AssetFactory assetFactory, String namespace, Location location)
 77    {
 78  84 Defense.notNull(assetFactory, "assetService");
 79   
 80  84 _assetFactory = assetFactory;
 81  84 _location = location;
 82  84 _idAllocator = new IdAllocator(namespace);
 83   
 84  84 _preloadName = (namespace.equals("") ? "tapestry" : namespace) + "_preload";
 85    }
 86   
 87    /**
 88    * Returns the location, which may be used in error messages. In practical terms, this is the
 89    * location of the {@link org.apache.tapestry.html.Body}&nbsp;component.
 90    */
 91   
 92  1 public Location getLocation()
 93    {
 94  1 return _location;
 95    }
 96   
 97  4 public String getPreloadedImageReference(String URL)
 98    {
 99  4 if (_imageMap == null)
 100  2 _imageMap = new HashMap();
 101   
 102  4 String reference = (String) _imageMap.get(URL);
 103   
 104  4 if (reference == null)
 105    {
 106  3 int count = _imageMap.size();
 107  3 String varName = _preloadName + "[" + count + "]";
 108  3 reference = varName + ".src";
 109   
 110  3 if (_imageInitializations == null)
 111  2 _imageInitializations = new StringBuffer();
 112   
 113  3 _imageInitializations.append(" ");
 114  3 _imageInitializations.append(varName);
 115  3 _imageInitializations.append(" = new Image();\n");
 116  3 _imageInitializations.append(" ");
 117  3 _imageInitializations.append(reference);
 118  3 _imageInitializations.append(" = \"");
 119  3 _imageInitializations.append(URL);
 120  3 _imageInitializations.append("\";\n");
 121   
 122  3 _imageMap.put(URL, reference);
 123    }
 124   
 125  4 return reference;
 126    }
 127   
 128  3 public void addBodyScript(String script)
 129    {
 130  3 if (_bodyScript == null)
 131  3 _bodyScript = new StringBuffer(script.length());
 132   
 133  3 _bodyScript.append(script);
 134    }
 135   
 136  41 public void addInitializationScript(String script)
 137    {
 138  41 if (_initializationScript == null)
 139  32 _initializationScript = new StringBuffer(script.length() + 1);
 140   
 141  41 _initializationScript.append(script);
 142  41 _initializationScript.append('\n');
 143    }
 144   
 145  42 public void addExternalScript(Resource scriptLocation)
 146    {
 147  42 if (_externalScripts == null)
 148  32 _externalScripts = new ArrayList();
 149   
 150  42 if (_externalScripts.contains(scriptLocation))
 151  9 return;
 152   
 153    // Record the Resource so we don't include it twice.
 154   
 155  33 _externalScripts.add(scriptLocation);
 156   
 157    }
 158   
 159  9 public String getUniqueString(String baseValue)
 160    {
 161  9 return _idAllocator.allocateId(baseValue);
 162    }
 163   
 164  26 private void writeExternalScripts(IMarkupWriter writer, IRequestCycle cycle)
 165    {
 166  26 int count = Tapestry.size(_externalScripts);
 167  26 for (int i = 0; i < count; i++)
 168    {
 169  27 Resource scriptLocation = (Resource) _externalScripts.get(i);
 170   
 171  27 IAsset asset = _assetFactory.createAsset(scriptLocation, null);
 172   
 173  27 String url = asset.buildURL(cycle);
 174   
 175    // Note: important to use begin(), not beginEmpty(), because browser don't
 176    // interpret <script .../> properly.
 177   
 178  27 writer.begin("script");
 179  27 writer.attribute("language", "JavaScript");
 180  27 writer.attribute("type", "text/javascript");
 181  27 writer.attribute("src", url);
 182  27 writer.end();
 183  27 writer.println();
 184    }
 185    }
 186   
 187    /**
 188    * Writes a single large JavaScript block containing:
 189    * <ul>
 190    * <li>Any image initializations (via {@link #getPreloadedImageReference(String)})
 191    * <li>Any included scripts (via {@link #addExternalScript(Resource)})
 192    * <li>Any contributions (via {@link #addBodyScript(String)})
 193    * </ul>
 194    *
 195    * @see #writeInitializationScript(IMarkupWriter)
 196    */
 197   
 198  73 public void writeBodyScript(IMarkupWriter writer, IRequestCycle cycle)
 199    {
 200  73 if (!Tapestry.isEmpty(_externalScripts))
 201  26 writeExternalScripts(writer, cycle);
 202   
 203  73 if (!(any(_bodyScript) || any(_imageInitializations)))
 204  69 return;
 205   
 206  4 writer.begin("script");
 207  4 writer.attribute("language", "JavaScript");
 208  4 writer.attribute("type", "text/javascript");
 209  4 writer.printRaw("<!--");
 210   
 211  4 if (any(_imageInitializations))
 212    {
 213  2 writer.printRaw("\n\nvar " + _preloadName + " = new Array();\n");
 214  2 writer.printRaw("if (document.images)\n");
 215  2 writer.printRaw("{\n");
 216  2 writer.printRaw(_imageInitializations.toString());
 217  2 writer.printRaw("}\n");
 218    }
 219   
 220  4 if (any(_bodyScript))
 221    {
 222  3 writer.printRaw("\n\n");
 223  3 writer.printRaw(_bodyScript.toString());
 224    }
 225   
 226  4 writer.printRaw("\n\n// -->");
 227  4 writer.end();
 228    }
 229   
 230    /**
 231    * Writes any image initializations; this should be invoked at the end of the render, after all
 232    * the related HTML will have already been streamed to the client and parsed by the web browser.
 233    * Earlier versions of Tapestry uses a <code>window.onload</code> event handler.
 234    */
 235   
 236  70 public void writeInitializationScript(IMarkupWriter writer)
 237    {
 238  70 if (!any(_initializationScript))
 239  44 return;
 240   
 241  26 writer.begin("script");
 242  26 writer.attribute("language", "JavaScript");
 243  26 writer.attribute("type", "text/javascript");
 244  26 writer.printRaw("<!--\n");
 245   
 246  26 writer.printRaw(_initializationScript.toString());
 247   
 248  26 writer.printRaw("\n// -->");
 249  26 writer.end();
 250    }
 251   
 252  221 private boolean any(StringBuffer buffer)
 253    {
 254  221 return buffer != null && buffer.length() > 0;
 255    }
 256    }