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