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