Coverage Report - org.apache.tapestry.html.Shell
 
Classes in this File Line Coverage Branch Coverage Complexity
Shell
25% 
33% 
1.818
 
 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.commons.lang.StringUtils;
 18  
 import org.apache.hivemind.HiveMind;
 19  
 import org.apache.tapestry.*;
 20  
 import org.apache.tapestry.coerce.ValueConverter;
 21  
 import org.apache.tapestry.engine.IEngineService;
 22  
 import org.apache.tapestry.engine.ILink;
 23  
 import org.apache.tapestry.services.ResponseBuilder;
 24  
 import org.apache.tapestry.spec.IApplicationSpecification;
 25  
 
 26  
 import java.util.ArrayList;
 27  
 import java.util.Date;
 28  
 import java.util.Iterator;
 29  
 import java.util.List;
 30  
 
 31  
 /**
 32  
  * Component for creating a standard 'shell' for a page, which comprises the <html> and
 33  
  * &lt;head&gt; portions of the page. [ <a
 34  
  * href="../../../../../ComponentReference/Shell.html">Component Reference </a>]
 35  
  * <p>
 36  
  * Specifically does <em>not</em> provide a &lt;body&gt; tag, that is usually accomplished using a
 37  
  * {@link Body}&nbsp; component.
 38  
  * 
 39  
  * @author Howard Lewis Ship
 40  
  */
 41  
 
 42  5
 public abstract class Shell extends AbstractComponent
 43  
 {
 44  
     public static final String SHELL_ATTRIBUTE = "org.apache.tapestry.html.Shell";
 45  
     
 46  1
     private static final String GENERATOR_CONTENT = "Tapestry Application Framework, version " + Tapestry.VERSION;
 47  
 
 48  
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 49  
     {
 50  1
         TapestryUtils.storeUniqueAttribute(cycle, SHELL_ATTRIBUTE, this);
 51  
         
 52  1
         long startTime = System.currentTimeMillis();
 53  1
         boolean rewinding = cycle.isRewinding();
 54  1
         boolean dynamic = getBuilder().isDynamic();
 55  
 
 56  1
         if (!rewinding && !dynamic)
 57  
         {
 58  0
             writeDocType(writer, cycle);
 59  
 
 60  0
             IPage page = getPage();
 61  
 
 62  0
             if (!isDisableTapestryMeta()) {
 63  
 
 64  0
                 writer.comment("Application: " + getApplicationSpecification().getName());
 65  
 
 66  0
                 writer.comment("Page: " + page.getPageName());
 67  0
                 writer.comment("Generated: " + new Date());
 68  
             }
 69  
 
 70  0
             writer.begin("html");
 71  0
             writer.println();
 72  0
             writer.begin("head");
 73  0
             writer.println();
 74  
 
 75  0
             if (!isDisableTapestryMeta())
 76  0
                 writeMetaTag(writer, "name", "generator", GENERATOR_CONTENT);
 77  
 
 78  0
             if (isDisableCaching())
 79  0
                 writeMetaTag(writer, "http-equiv", "content", "no-cache");
 80  
 
 81  0
             if (getRenderContentType())
 82  0
                 writeMetaTag(writer, "http-equiv", "Content-Type", writer.getContentType());
 83  
 
 84  0
             writeRefresh(writer, cycle);
 85  
 
 86  0
             if (getRenderBaseTag())
 87  0
                 getBaseTagWriter().render(writer, cycle);
 88  
 
 89  0
             writer.begin("title");
 90  
 
 91  0
             writer.print(getTitle(), getRaw());
 92  0
             writer.end(); // title
 93  0
             writer.println();
 94  
 
 95  0
             IRender delegate = getDelegate();
 96  
 
 97  0
             if (delegate != null)
 98  0
                 delegate.render(writer, cycle);
 99  
 
 100  0
             IRender ajaxDelegate = getAjaxDelegate();
 101  
 
 102  0
             if (ajaxDelegate != null)
 103  0
                 ajaxDelegate.render(writer, cycle);
 104  
             
 105  0
             IAsset stylesheet = getStylesheet();
 106  
 
 107  0
             if (stylesheet != null)
 108  0
                 writeStylesheetLink(writer, cycle, stylesheet);
 109  
 
 110  0
             Iterator i = (Iterator) getValueConverter().coerceValue(getStylesheets(), Iterator.class);
 111  
 
 112  0
             while (i.hasNext())
 113  
             {
 114  0
                 stylesheet = (IAsset) i.next();
 115  
 
 116  0
                 writeStylesheetLink(writer, cycle, stylesheet);
 117  
             }
 118  
         }
 119  
 
 120  
         // Render the body, the actual page content
 121  
 
 122  1
         IMarkupWriter nested = !dynamic ? writer.getNestedWriter() : writer;
 123  
 
 124  1
         renderBody(nested, cycle);
 125  
 
 126  1
         if (!rewinding)
 127  
         {
 128  0
             List relations = getRelations();
 129  0
             if (relations != null)
 130  0
                 writeRelations(writer, relations); 
 131  
             
 132  0
             StringBuffer additionalContent = getContentBuffer();
 133  0
             if (additionalContent != null)
 134  0
                 writer.printRaw(additionalContent.toString());
 135  
             
 136  0
             writer.end(); // head
 137  
         }
 138  
         
 139  1
         if (!dynamic)
 140  1
             nested.close();
 141  
         
 142  1
         if (!rewinding && !dynamic)
 143  
         {
 144  0
             writer.end(); // html
 145  0
             writer.println();
 146  
 
 147  0
             if (!isDisableTapestryMeta()) {
 148  
                 
 149  0
                 long endTime = System.currentTimeMillis();
 150  
 
 151  0
                 writer.comment("Render time: ~ " + (endTime - startTime) + " ms");     
 152  
             }
 153  
         }
 154  
 
 155  1
     }
 156  
 
 157  
     protected void cleanupAfterRender(IRequestCycle cycle)
 158  
     {
 159  1
         super.cleanupAfterRender(cycle);
 160  
 
 161  1
         cycle.removeAttribute(SHELL_ATTRIBUTE);
 162  1
     }    
 163  
     
 164  
     private void writeDocType(IMarkupWriter writer, IRequestCycle cycle)
 165  
     {
 166  
         // This is the real code
 167  0
         String doctype = getDoctype();
 168  0
         if (HiveMind.isNonBlank(doctype))
 169  
         {
 170  0
             writer.printRaw("<!DOCTYPE " + doctype + ">");
 171  0
             writer.println();
 172  
         }
 173  0
     }
 174  
 
 175  
     private void writeStylesheetLink(IMarkupWriter writer, IRequestCycle cycle, IAsset stylesheet)
 176  
     {
 177  0
         writer.beginEmpty("link");
 178  0
         writer.attribute("rel", "stylesheet");
 179  0
         writer.attribute("type", "text/css");
 180  0
         writer.attribute("href", stylesheet.buildURL());
 181  0
         writer.println();
 182  0
     }
 183  
     
 184  
     private void writeRefresh(IMarkupWriter writer, IRequestCycle cycle)
 185  
     {
 186  0
         int refresh = getRefresh();
 187  
 
 188  0
         if (refresh <= 0)
 189  0
             return;
 190  
 
 191  
         // Here comes the tricky part ... have to assemble a complete URL
 192  
         // for the current page.
 193  
 
 194  0
         IEngineService pageService = getPageService();
 195  0
         String pageName = getPage().getPageName();
 196  
 
 197  0
         ILink link = pageService.getLink(false, pageName);
 198  
 
 199  0
         StringBuffer buffer = new StringBuffer();
 200  0
         buffer.append(refresh);
 201  0
         buffer.append("; URL=");
 202  0
         buffer.append(StringUtils.replace(link.getAbsoluteURL(), "&amp;", "&"));
 203  
 
 204  0
         writeMetaTag(writer, "http-equiv", "Refresh", buffer.toString());
 205  0
     }
 206  
     
 207  
     private void writeMetaTag(IMarkupWriter writer, String key, String value, String content)
 208  
     {
 209  0
         writer.beginEmpty("meta");
 210  0
         writer.attribute(key, value);
 211  0
         writer.attribute("content", content);
 212  0
         writer.println();
 213  0
     }
 214  
     
 215  
     private void writeRelations(IMarkupWriter writer, List relations)
 216  
     {
 217  0
         Iterator i = relations.iterator();
 218  0
         while (i.hasNext())
 219  
         {
 220  0
             RelationBean relationBean = (RelationBean) i.next();
 221  0
             if (relationBean != null)
 222  0
                 writeRelation(writer, relationBean);
 223  0
         }
 224  0
     }
 225  
     
 226  
     private void writeRelation(IMarkupWriter writer, RelationBean relationBean)
 227  
     {
 228  0
             writer.beginEmpty("link");
 229  0
             writeAttributeIfNotNull(writer, "rel", relationBean.getRel());
 230  0
             writeAttributeIfNotNull(writer, "rev", relationBean.getRev());            
 231  0
             writeAttributeIfNotNull(writer, "type", relationBean.getType());
 232  0
             writeAttributeIfNotNull(writer, "media", relationBean.getMedia());
 233  0
             writeAttributeIfNotNull(writer, "title", relationBean.getTitle());
 234  0
             writeAttributeIfNotNull(writer, "href", relationBean.getHref());
 235  0
             writer.println();
 236  0
     }    
 237  
     
 238  
     private void writeAttributeIfNotNull(IMarkupWriter writer, String name, String value)
 239  
     {
 240  0
         if (value != null)
 241  0
             writer.attribute(name, value);
 242  0
     }
 243  
     
 244  
     /**
 245  
      * Retrieves the {@link Shell} that was stored into the request
 246  
      * cycle. This allows components wrapped by the {@link Shell} to
 247  
      * locate it and access the services it provides.
 248  
      * 
 249  
      * @since 4.1.1
 250  
      */
 251  
     public static Shell get(IRequestCycle cycle)
 252  
     {
 253  2
         return (Shell) cycle.getAttribute(SHELL_ATTRIBUTE);
 254  
     }    
 255  
     
 256  
     /**
 257  
      * Adds a relation (stylesheets, favicon, e.t.c.) to the page.
 258  
      *
 259  
      * @since 4.1.1
 260  
      */
 261  
     public void addRelation(RelationBean relation)
 262  
     {
 263  2
         List relations = getRelations();
 264  2
         if (relations == null)
 265  1
             relations = new ArrayList();
 266  
         
 267  2
         if (!relations.contains(relation))
 268  1
             relations.add(relation);
 269  
         
 270  2
         setRelations(relations);             
 271  2
     }
 272  
 
 273  
     /**
 274  
      * Include additional content in the header of a page.
 275  
      * 
 276  
      * @param content 
 277  
      *
 278  
      * @since 4.1.1
 279  
      */
 280  
     public void includeAdditionalContent(String content)
 281  
     {
 282  2
         if (HiveMind.isBlank(content))
 283  1
             return;
 284  
         
 285  1
         StringBuffer buffer = getContentBuffer();
 286  
         
 287  1
         if (buffer == null)
 288  0
             buffer = new StringBuffer();
 289  
         
 290  1
         buffer.append(content);
 291  
         
 292  1
         setContentBuffer(buffer);
 293  1
     }
 294  
     
 295  
     public abstract boolean isDisableCaching();
 296  
     
 297  
     public abstract IRender getAjaxDelegate();
 298  
     
 299  
     public abstract IRender getDelegate();
 300  
     
 301  
     public abstract int getRefresh();
 302  
 
 303  
     public abstract IAsset getStylesheet();
 304  
 
 305  
     public abstract Object getStylesheets();
 306  
 
 307  
     public abstract String getTitle();
 308  
 
 309  
     public abstract String getDoctype();
 310  
 
 311  
     public abstract boolean getRenderContentType();
 312  
 
 313  
     public abstract boolean isDisableTapestryMeta();
 314  
 
 315  
     public abstract ResponseBuilder getBuilder();
 316  
 
 317  
     /** @since 4.0 */
 318  
     public abstract ValueConverter getValueConverter();
 319  
 
 320  
     /** @since 4.0 */
 321  
 
 322  
     public abstract IEngineService getPageService();
 323  
 
 324  
     /** @since 4.0 */
 325  
 
 326  
     public abstract IApplicationSpecification getApplicationSpecification();
 327  
 
 328  
     /** @since 4.0 */
 329  
 
 330  
     public abstract IRender getBaseTagWriter();
 331  
     
 332  
     /** @since 4.0.1 */
 333  
     
 334  
     public abstract boolean getRenderBaseTag();
 335  
     
 336  
     /** @since 4.0.3 */
 337  
     
 338  
     public abstract boolean getRaw();
 339  
     
 340  
     /** @since 4.1.1 */
 341  
     
 342  
     public abstract List getRelations();
 343  
     
 344  
     /** @since 4.1.1 */
 345  
     
 346  
     public abstract void setRelations(List relations);
 347  
     
 348  
     /** @since 4.1.1 */
 349  
     
 350  
     public abstract StringBuffer getContentBuffer();
 351  
     
 352  
     /** @since 4.1.1 */
 353  
     
 354  
     public abstract void setContentBuffer(StringBuffer buffer);    
 355  
 
 356  
 }