Coverage Report - org.apache.tapestry.html.Relation
 
Classes in this File Line Coverage Branch Coverage Complexity
Relation
31% 
56% 
2
 
 1  
 // Copyright Aug 2, 2006 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  
 package org.apache.tapestry.html;
 15  
 
 16  
 import org.apache.hivemind.ApplicationRuntimeException;
 17  
 import org.apache.tapestry.AbstractComponent;
 18  
 import org.apache.tapestry.IAsset;
 19  
 import org.apache.tapestry.IMarkupWriter;
 20  
 import org.apache.tapestry.IRequestCycle;
 21  
 import org.apache.tapestry.markup.MarkupWriterSource;
 22  
 import org.apache.tapestry.util.ContentType;
 23  
 
 24  
 import java.io.PrintWriter;
 25  
 import java.io.StringWriter;
 26  
 
 27  
 /**
 28  
  * Works with the {@link Shell} component to define and append a 
 29  
  * relationship between documents (typically a stylesheet) to 
 30  
  * the HTML response. 
 31  
  *
 32  
  * @author Andreas Andreou
 33  
  * @since 4.1.1
 34  
  */
 35  3
 public abstract class Relation extends AbstractComponent
 36  
 {
 37  
     /**
 38  
      * {@inheritDoc}
 39  
      */
 40  
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 41  
     {
 42  3
         if (!cycle.isRewinding())
 43  
         {
 44  2
             Shell shell = Shell.get(cycle);
 45  
 
 46  2
             if (shell == null)
 47  1
                 throw new ApplicationRuntimeException(
 48  
                         HTMLMessages.shellComponentRequired(),
 49  
                         this.getLocation(), null);
 50  
 
 51  1
             if (getUseBody() && getHref() == null)
 52  
             {
 53  0
                 renderStyleTag(shell, writer, cycle);
 54  
             }
 55  
             else
 56  
             {
 57  1
                 renderLinkTag(shell, writer, cycle);
 58  
             }
 59  
         }
 60  1
     }
 61  
 
 62  
     protected void renderLinkTag(Shell shell, IMarkupWriter writer, IRequestCycle cycle)
 63  
     {
 64  1
         Object href = getHref();
 65  1
         boolean ok = (href instanceof String) || (href instanceof IAsset);
 66  1
         if (!ok)
 67  1
             throw new ApplicationRuntimeException(HTMLMessages.stringOrIAssetExpected(), getLocation(), null);
 68  
 
 69  
         String url;
 70  0
         if (href instanceof String)
 71  
         {
 72  0
             url = (String) href;
 73  
         }
 74  
         else
 75  
         {
 76  0
             url = ((IAsset)href).buildURL();
 77  
         }
 78  
 
 79  0
         RelationBean bean = new RelationBean();
 80  0
         bean.setHref(url);
 81  0
         bean.setMedia(getMedia());
 82  0
         bean.setRel(getRel());
 83  0
         bean.setRev(getRev());
 84  0
         bean.setTitle(getTitle());
 85  0
         bean.setType(getType());
 86  0
         shell.addRelation(bean);
 87  0
     }
 88  
 
 89  
     protected void renderStyleTag(Shell shell, IMarkupWriter writer, IRequestCycle cycle)
 90  
     {
 91  0
         if (getBody() == null) //nothing to include
 92  
         {
 93  0
             return;
 94  
         }
 95  
 
 96  0
         StringWriter sWriter = new StringWriter();
 97  0
         IMarkupWriter nested = getMarkupWriterSource().newMarkupWriter(new PrintWriter(sWriter),
 98  
                                                                        new ContentType(writer.getContentType()));
 99  
 
 100  0
         nested.begin("style");
 101  0
         nested.attribute("type", "text/css");
 102  
 
 103  0
         if (getMedia()!=null)
 104  0
             nested.attribute("media", getMedia());
 105  0
         if (getTitle()!=null)
 106  0
             nested.attribute("title", getTitle());
 107  
 
 108  0
         renderBody(nested, cycle);
 109  0
         nested.close();
 110  
 
 111  0
         shell.includeAdditionalContent(sWriter.toString());
 112  0
     }
 113  
 
 114  
     public abstract boolean getUseBody();
 115  
 
 116  
     public abstract Object getHref();
 117  
 
 118  
     public abstract String getRel();
 119  
 
 120  
     public abstract String getRev();
 121  
 
 122  
     public abstract String getType();
 123  
 
 124  
     public abstract String getTitle();
 125  
 
 126  
     public abstract String getMedia();
 127  
 
 128  
     /* injected */
 129  
     public abstract MarkupWriterSource getMarkupWriterSource();
 130  
 
 131  
 }