Coverage Report - org.apache.tapestry.html.Relation
 
Classes in this File Line Coverage Branch Coverage Complexity
Relation
0%
0/39
0%
0/24
2.273
 
 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  0
 public abstract class Relation extends AbstractComponent
 36  
 {
 37  
     /**
 38  
      * {@inheritDoc}
 39  
      */
 40  
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 41  
     {
 42  0
         if (!cycle.isRewinding())
 43  
         {
 44  0
             Shell shell = Shell.get(cycle);
 45  
 
 46  0
             if (shell == null)
 47  0
                 throw new ApplicationRuntimeException(
 48  
                         HTMLMessages.shellComponentRequired(),
 49  
                         this.getLocation(), null);
 50  
 
 51  0
             if (getUseBody() && getHref() == null)
 52  
             {
 53  0
                 renderStyleTag(shell, writer, cycle);
 54  
             }
 55  
             else
 56  
             {
 57  0
                 renderLinkTag(shell, writer, cycle);
 58  
             }
 59  
         }
 60  0
     }
 61  
 
 62  
     protected void renderLinkTag(Shell shell, IMarkupWriter writer, IRequestCycle cycle)
 63  
     {
 64  0
         Object href = getHref();
 65  0
         boolean ok = (href instanceof String) || (href instanceof IAsset);
 66  0
         if (!ok)
 67  0
             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  
         // see if nothing (or nowhere) to include
 92  0
         if (getBody() == null || writer.getContentType() == null)
 93  
         {
 94  0
             return;
 95  
         }
 96  
 
 97  0
         StringWriter sWriter = new StringWriter();
 98  0
         IMarkupWriter nested = getMarkupWriterSource().newMarkupWriter(new PrintWriter(sWriter),
 99  
                                                                        new ContentType(writer.getContentType()));
 100  
 
 101  0
         nested.begin("style");
 102  0
         nested.attribute("type", "text/css");
 103  
 
 104  0
         if (getMedia()!=null)
 105  0
             nested.attribute("media", getMedia());
 106  0
         if (getTitle()!=null)
 107  0
             nested.attribute("title", getTitle());
 108  
 
 109  0
         renderBody(nested, cycle);
 110  0
         nested.close();
 111  
 
 112  0
         shell.includeAdditionalContent(sWriter.toString());
 113  0
     }
 114  
 
 115  
     public abstract boolean getUseBody();
 116  
 
 117  
     public abstract Object getHref();
 118  
 
 119  
     public abstract String getRel();
 120  
 
 121  
     public abstract String getRev();
 122  
 
 123  
     public abstract String getType();
 124  
 
 125  
     public abstract String getTitle();
 126  
 
 127  
     public abstract String getMedia();
 128  
 
 129  
     /* injected */
 130  
     public abstract MarkupWriterSource getMarkupWriterSource();
 131  
 
 132  
 }