Coverage Report - org.apache.tapestry.portlet.PortletLink
 
Classes in this File Line Coverage Branch Coverage Complexity
PortletLink
0%
0/32
0%
0/10
1.8
 
 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.portlet;
 16  
 
 17  
 import javax.portlet.PortletURL;
 18  
 
 19  
 import org.apache.hivemind.util.Defense;
 20  
 import org.apache.tapestry.engine.ILink;
 21  
 import org.apache.tapestry.util.QueryParameterMap;
 22  
 
 23  
 /**
 24  
  * Wrapper around {@link javax.portlet.PortletURL}.
 25  
  * 
 26  
  * @author Howard M. Lewis Ship
 27  
  * @since 4.0
 28  
  */
 29  
 public class PortletLink implements ILink
 30  
 {
 31  
 
 32  
     private final PortletURL _portletURL;
 33  
 
 34  
     private final QueryParameterMap _parameters;
 35  
 
 36  
     public PortletLink(PortletURL portletURL, QueryParameterMap parameters)
 37  0
     {
 38  0
         Defense.notNull(portletURL, "portletURL");
 39  0
         Defense.notNull(parameters, "parameters");
 40  
 
 41  0
         _portletURL = portletURL;
 42  0
         _parameters = parameters;
 43  0
     }
 44  
 
 45  
     public String getURL()
 46  
     {
 47  0
         return getURL(null, true);
 48  
     }
 49  
 
 50  
     public String getURL(String anchor, boolean includeParameters)
 51  
     {
 52  0
         if (includeParameters) loadParameters();
 53  
 
 54  0
         String url = _portletURL.toString();
 55  
 
 56  0
         url = unencode(url);
 57  
 
 58  0
         if (anchor != null) url = url + "#" + anchor;
 59  
 
 60  0
         return url;
 61  
     }
 62  
 
 63  
     /**
 64  
      * The PortletURL class returns a url that's already XML-escaped, ready for
 65  
      * inclusion directly into the response stream. However, the IMarkupWriter
 66  
      * expects to do that encoding too ... and double encoding is bad. So we
 67  
      * back out the most likely encoding (convert '&' to just '&').
 68  
      */
 69  
 
 70  
     private String unencode(String url)
 71  
     {
 72  0
         StringBuffer buffer = new StringBuffer(url.length());
 73  0
         String text = url;
 74  
 
 75  
         while(true)
 76  
         {
 77  0
             int ampx = text.indexOf("&");
 78  
 
 79  0
             if (ampx < 0) break;
 80  
 
 81  
             // Take up to and including the '&'
 82  
 
 83  0
             buffer.append(text.substring(0, ampx + 1));
 84  
 
 85  0
             text = text.substring(ampx + 5);
 86  0
         }
 87  
 
 88  0
         buffer.append(text);
 89  
 
 90  0
         return buffer.toString();
 91  
     }
 92  
 
 93  
     private void loadParameters()
 94  
     {
 95  0
         String[] names = _parameters.getParameterNames();
 96  
 
 97  0
         for(int i = 0; i < names.length; i++)
 98  
         {
 99  0
             String name = names[i];
 100  0
             String[] values = _parameters.getParameterValues(name);
 101  
 
 102  0
             if (values != null) _portletURL.setParameter(name, values);
 103  
         }
 104  0
     }
 105  
 
 106  
     public String getURL(String scheme, String server, int port, String anchor,
 107  
             boolean includeParameters)
 108  
     {
 109  
         // Ignore scheme, server and port ... those are under the control of the
 110  
         // portlet container.
 111  
 
 112  0
         return getURL(anchor, includeParameters);
 113  
     }
 114  
 
 115  
     public String getAbsoluteURL()
 116  
     {
 117  0
         throw new UnsupportedOperationException(PortletMessages
 118  
                 .unsupportedMethod("getAbsoluteURL"));
 119  
     }
 120  
 
 121  
     public String getAbsoluteURL(String scheme, String server, int port,
 122  
             String anchor, boolean includeParameters)
 123  
     {
 124  0
         throw new UnsupportedOperationException(PortletMessages
 125  
                 .unsupportedMethod("getAbsoluteURL"));
 126  
     }
 127  
 
 128  
     public String[] getParameterNames()
 129  
     {
 130  0
         return _parameters.getParameterNames();
 131  
     }
 132  
 
 133  
     public String[] getParameterValues(String name)
 134  
     {
 135  0
         return _parameters.getParameterValues(name);
 136  
     }
 137  
 
 138  
 }