Clover coverage report - Code Coverage for tapestry-portlet release 4.0.1
Coverage timestamp: Fri Mar 31 2006 09:14:58 EST
file stats: LOC: 138   Methods: 10
NCLOC: 78   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PortletLink.java 90% 100% 100% 98.1%
coverage coverage
 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    private final PortletURL _portletURL;
 32   
 33    private final QueryParameterMap _parameters;
 34   
 35  16 public PortletLink(PortletURL portletURL, QueryParameterMap parameters)
 36    {
 37  16 Defense.notNull(portletURL, "portletURL");
 38  16 Defense.notNull(parameters, "parameters");
 39   
 40  16 _portletURL = portletURL;
 41  16 _parameters = parameters;
 42    }
 43   
 44  6 public String getURL()
 45    {
 46  6 return getURL(null, true);
 47    }
 48   
 49  10 public String getURL(String anchor, boolean includeParameters)
 50    {
 51  10 if (includeParameters)
 52  8 loadParameters();
 53   
 54  10 String url = _portletURL.toString();
 55   
 56  10 url = unencode(url);
 57   
 58  10 if (anchor != null)
 59  4 url = url + "#" + anchor;
 60   
 61  10 return url;
 62    }
 63   
 64    /**
 65    * The PortletURL class returns a url that's already XML-escaped, ready for inclusion directly
 66    * into the response stream. However, the IMarkupWriter expects to do that encoding too ... and
 67    * double encoding is bad. So we back out the most likely encoding (convert '&' to just
 68    * '&').
 69    */
 70   
 71  10 private String unencode(String url)
 72    {
 73  10 StringBuffer buffer = new StringBuffer(url.length());
 74  10 String text = url;
 75   
 76  10 while (true)
 77    {
 78  12 int ampx = text.indexOf("&");
 79   
 80  12 if (ampx < 0)
 81  10 break;
 82   
 83    // Take up to and including the '&'
 84   
 85  2 buffer.append(text.substring(0, ampx + 1));
 86   
 87  2 text = text.substring(ampx + 5);
 88    }
 89   
 90  10 buffer.append(text);
 91   
 92  10 return buffer.toString();
 93    }
 94   
 95  8 private void loadParameters()
 96    {
 97  8 String[] names = _parameters.getParameterNames();
 98   
 99  8 for (int i = 0; i < names.length; i++)
 100    {
 101  4 String name = names[i];
 102  4 String[] values = _parameters.getParameterValues(name);
 103   
 104  4 if (values != null)
 105  4 _portletURL.setParameter(name, values);
 106    }
 107    }
 108   
 109  2 public String getURL(String scheme, String server, int port, String anchor,
 110    boolean includeParameters)
 111    {
 112    // Ignore scheme, server and port ... those are under the control of the portlet container.
 113   
 114  2 return getURL(anchor, includeParameters);
 115    }
 116   
 117  2 public String getAbsoluteURL()
 118    {
 119  2 throw new UnsupportedOperationException(PortletMessages.unsupportedMethod("getAbsoluteURL"));
 120    }
 121   
 122  2 public String getAbsoluteURL(String scheme, String server, int port, String anchor,
 123    boolean includeParameters)
 124    {
 125  2 throw new UnsupportedOperationException(PortletMessages.unsupportedMethod("getAbsoluteURL"));
 126    }
 127   
 128  2 public String[] getParameterNames()
 129    {
 130  2 return _parameters.getParameterNames();
 131    }
 132   
 133  2 public String[] getParameterValues(String name)
 134    {
 135  2 return _parameters.getParameterValues(name);
 136    }
 137   
 138    }