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