Clover coverage report - Code Coverage for tapestry release 4.0-beta-2
Coverage timestamp: Sat Jul 9 2005 22:02:17 EDT
file stats: LOC: 312   Methods: 23
NCLOC: 234   Classes: 1
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
UrlValidator.java 55% 57.6% 52.2% 56.1%
coverage coverage
 1    // Copyright 2004, 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.valid;
 16   
 17    import java.net.MalformedURLException;
 18    import java.net.URL;
 19    import java.util.Collection;
 20    import java.util.HashMap;
 21    import java.util.Iterator;
 22    import java.util.Locale;
 23    import java.util.Map;
 24    import java.util.ResourceBundle;
 25    import java.util.Vector;
 26   
 27    import org.apache.hivemind.util.PropertyUtils;
 28    import org.apache.tapestry.IMarkupWriter;
 29    import org.apache.tapestry.IRequestCycle;
 30    import org.apache.tapestry.form.IFormComponent;
 31    import org.apache.tapestry.util.StringSplitter;
 32   
 33    /**
 34    * @since 3.0
 35    */
 36    public class UrlValidator extends BaseValidator
 37    {
 38    private int _minimumLength;
 39   
 40    private String _minimumLengthMessage;
 41   
 42    private String _invalidUrlFormatMessage;
 43   
 44    private String _disallowedProtocolMessage;
 45   
 46    private Collection _allowedProtocols;
 47   
 48    private String _scriptPath = "/org/apache/tapestry/valid/UrlValidator.script"; //$NON-NLS-1$
 49   
 50  6 public UrlValidator()
 51    {
 52    }
 53   
 54    /**
 55    * Initializes the UrlValidator with properties defined by the initializer.
 56    *
 57    * @since 4.0
 58    */
 59   
 60  0 public UrlValidator(String initializer)
 61    {
 62  0 super(initializer);
 63    }
 64   
 65  0 public String toString(IFormComponent field, Object value)
 66    {
 67  0 if (value == null)
 68  0 return null;
 69   
 70  0 return value.toString();
 71    }
 72   
 73  6 public Object toObject(IFormComponent field, String input) throws ValidatorException
 74    {
 75  6 if (checkRequired(field, input))
 76  0 return null;
 77   
 78  6 if (_minimumLength > 0 && input.length() < _minimumLength)
 79  2 throw new ValidatorException(buildMinimumLengthMessage(field),
 80    ValidationConstraint.MINIMUM_WIDTH);
 81   
 82  4 if (!isValidUrl(input))
 83  2 throw new ValidatorException(buildInvalidUrlFormatMessage(field),
 84    ValidationConstraint.URL_FORMAT);
 85   
 86  2 if (!isAllowedProtocol(input))
 87    {
 88  1 throw new ValidatorException(buildDisallowedProtocolMessage(field),
 89    ValidationConstraint.DISALLOWED_PROTOCOL);
 90    }
 91   
 92  1 return input;
 93    }
 94   
 95  0 public int getMinimumLength()
 96    {
 97  0 return _minimumLength;
 98    }
 99   
 100  2 public void setMinimumLength(int minimumLength)
 101    {
 102  2 _minimumLength = minimumLength;
 103    }
 104   
 105  0 public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
 106    IRequestCycle cycle)
 107    {
 108  0 if (!isClientScriptingEnabled())
 109  0 return;
 110   
 111  0 Map symbols = new HashMap();
 112   
 113  0 if (isRequired())
 114  0 symbols.put("requiredMessage", buildRequiredMessage(field)); //$NON-NLS-1$
 115   
 116  0 if (_minimumLength > 0)
 117  0 symbols.put("minimumLengthMessage", //$NON-NLS-1$
 118    buildMinimumLengthMessage(field));
 119   
 120  0 symbols.put("urlFormatMessage", buildInvalidUrlFormatMessage(field)); //$NON-NLS-1$
 121   
 122  0 symbols.put("urlDisallowedProtocolMessage", //$NON-NLS-1$
 123    buildDisallowedProtocolMessage(field));
 124   
 125  0 symbols.put("urlRegexpProtocols", buildUrlRegexpProtocols()); //$NON-NLS-1$
 126   
 127  0 processValidatorScript(_scriptPath, cycle, field, symbols);
 128    }
 129   
 130  0 private String buildUrlRegexpProtocols()
 131    {
 132  0 if (_allowedProtocols == null)
 133    {
 134  0 return null;
 135    }
 136  0 String regexp = "/("; //$NON-NLS-1$
 137  0 Iterator iter = _allowedProtocols.iterator();
 138  0 while (iter.hasNext())
 139    {
 140  0 String protocol = (String) iter.next();
 141  0 regexp += protocol;
 142  0 if (iter.hasNext())
 143    {
 144  0 regexp += "|"; //$NON-NLS-1$
 145    }
 146    }
 147  0 regexp += "):///"; //$NON-NLS-1$
 148  0 return regexp;
 149    }
 150   
 151  0 public String getScriptPath()
 152    {
 153  0 return _scriptPath;
 154    }
 155   
 156  0 public void setScriptPath(String scriptPath)
 157    {
 158  0 _scriptPath = scriptPath;
 159    }
 160   
 161  4 protected boolean isValidUrl(String url)
 162    {
 163  4 boolean bIsValid;
 164  4 try
 165    {
 166  4 new URL(url);
 167  2 bIsValid = true;
 168    }
 169    catch (MalformedURLException mue)
 170    {
 171  2 bIsValid = false;
 172    }
 173  4 return bIsValid;
 174    }
 175   
 176  2 protected boolean isAllowedProtocol(String url)
 177    {
 178  2 boolean bIsAllowed = false;
 179  2 if (_allowedProtocols != null)
 180    {
 181  1 URL oUrl;
 182  1 try
 183    {
 184  1 oUrl = new URL(url);
 185    }
 186    catch (MalformedURLException e)
 187    {
 188  0 return false;
 189    }
 190  1 String actualProtocol = oUrl.getProtocol();
 191  1 Iterator iter = _allowedProtocols.iterator();
 192  1 while (iter.hasNext())
 193    {
 194  2 String protocol = (String) iter.next();
 195  2 if (protocol.equals(actualProtocol))
 196    {
 197  0 bIsAllowed = true;
 198  0 break;
 199    }
 200    }
 201    }
 202    else
 203    {
 204  1 bIsAllowed = true;
 205    }
 206  2 return bIsAllowed;
 207    }
 208   
 209  0 public String getInvalidUrlFormatMessage()
 210    {
 211  0 return _invalidUrlFormatMessage;
 212    }
 213   
 214  0 public String getMinimumLengthMessage()
 215    {
 216  0 return _minimumLengthMessage;
 217    }
 218   
 219  1 public void setInvalidUrlFormatMessage(String string)
 220    {
 221  1 _invalidUrlFormatMessage = string;
 222    }
 223   
 224  0 public String getDisallowedProtocolMessage()
 225    {
 226  0 return _disallowedProtocolMessage;
 227    }
 228   
 229  0 public void setDisallowedProtocolMessage(String string)
 230    {
 231  0 _disallowedProtocolMessage = string;
 232    }
 233   
 234  1 public void setMinimumLengthMessage(String string)
 235    {
 236  1 _minimumLengthMessage = string;
 237    }
 238   
 239  2 protected String buildMinimumLengthMessage(IFormComponent field)
 240    {
 241  2 String pattern = getPattern(_minimumLengthMessage, "field-too-short", //$NON-NLS-1$
 242    field.getPage().getLocale());
 243   
 244  2 return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName());
 245    }
 246   
 247  2 protected String buildInvalidUrlFormatMessage(IFormComponent field)
 248    {
 249  2 String pattern = getPattern(_invalidUrlFormatMessage, "invalid-url-format", //$NON-NLS-1$
 250    field.getPage().getLocale());
 251   
 252  2 return formatString(pattern, field.getDisplayName());
 253    }
 254   
 255  1 protected String buildDisallowedProtocolMessage(IFormComponent field)
 256    {
 257  1 if (_allowedProtocols == null)
 258    {
 259  0 return null;
 260    }
 261  1 String pattern = getPattern(_disallowedProtocolMessage, "disallowed-protocol", //$NON-NLS-1$
 262    field.getPage().getLocale());
 263   
 264  1 String allowedProtocols = ""; //$NON-NLS-1$
 265  1 Iterator iter = _allowedProtocols.iterator();
 266  1 while (iter.hasNext())
 267    {
 268  2 String protocol = (String) iter.next();
 269  2 if (!allowedProtocols.equals("")) { //$NON-NLS-1$
 270  1 if (iter.hasNext())
 271    {
 272  0 allowedProtocols += ", "; //$NON-NLS-1$
 273    }
 274    else
 275    {
 276  1 allowedProtocols += " or "; //$NON-NLS-1$
 277    }
 278    }
 279  2 allowedProtocols += protocol;
 280    }
 281   
 282  1 return formatString(pattern, allowedProtocols);
 283    }
 284   
 285  5 protected String getPattern(String override, String key, Locale locale)
 286    {
 287  5 if (override != null)
 288  2 return override;
 289   
 290  3 ResourceBundle strings = ResourceBundle.getBundle(
 291    "org.apache.tapestry.valid.ValidationStrings",
 292    locale);
 293  3 return strings.getString(key);
 294    }
 295   
 296    /**
 297    * @param protocols
 298    * comma separated list of allowed protocols
 299    */
 300  1 public void setAllowedProtocols(String protocols)
 301    {
 302  1 StringSplitter spliter = new StringSplitter(',');
 303    //String[] aProtocols = protocols.split(","); //$NON-NLS-1$
 304  1 String[] aProtocols = spliter.splitToArray(protocols); //$NON-NLS-1$
 305  1 _allowedProtocols = new Vector();
 306  1 for (int i = 0; i < aProtocols.length; i++)
 307    {
 308  2 _allowedProtocols.add(aProtocols[i]);
 309    }
 310    }
 311   
 312    }