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