Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 312   Methods: 22
NCLOC: 240   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
UrlValidator.java 55% 59.8% 54.5% 57.9%
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  0
     public String toString(IFormComponent field, Object value)
 54   
     {
 55  0
         if (value == null)
 56  0
             return null;
 57   
 
 58  0
         return value.toString();
 59   
     }
 60   
 
 61  6
     public Object toObject(IFormComponent field, String input) throws ValidatorException
 62   
     {
 63  6
         if (checkRequired(field, input))
 64  0
             return null;
 65   
 
 66  6
         if (_minimumLength > 0 && input.length() < _minimumLength)
 67  2
             throw new ValidatorException(buildMinimumLengthMessage(field),
 68   
                     ValidationConstraint.MINIMUM_WIDTH);
 69   
 
 70  4
         if (!isValidUrl(input))
 71  2
             throw new ValidatorException(buildInvalidUrlFormatMessage(field),
 72   
                     ValidationConstraint.URL_FORMAT);
 73   
 
 74  2
         if (!isAllowedProtocol(input))
 75   
         {
 76  1
             throw new ValidatorException(buildDisallowedProtocolMessage(field),
 77   
                     ValidationConstraint.DISALLOWED_PROTOCOL);
 78   
         }
 79   
 
 80  1
         return input;
 81   
     }
 82   
 
 83  0
     public int getMinimumLength()
 84   
     {
 85  0
         return _minimumLength;
 86   
     }
 87   
 
 88  2
     public void setMinimumLength(int minimumLength)
 89   
     {
 90  2
         _minimumLength = minimumLength;
 91   
     }
 92   
 
 93  0
     public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
 94   
             IRequestCycle cycle)
 95   
     {
 96  0
         if (!isClientScriptingEnabled())
 97  0
             return;
 98   
 
 99  0
         Map symbols = new HashMap();
 100   
 
 101  0
         if (isRequired())
 102  0
             symbols.put("requiredMessage", buildRequiredMessage(field)); //$NON-NLS-1$
 103   
 
 104  0
         if (_minimumLength > 0)
 105  0
             symbols.put("minimumLengthMessage", //$NON-NLS-1$
 106   
                     buildMinimumLengthMessage(field));
 107   
 
 108  0
         symbols.put("urlFormatMessage", buildInvalidUrlFormatMessage(field)); //$NON-NLS-1$
 109   
 
 110  0
         symbols.put("urlDisallowedProtocolMessage", //$NON-NLS-1$
 111   
                 buildDisallowedProtocolMessage(field));
 112   
 
 113  0
         symbols.put("urlRegexpProtocols", buildUrlRegexpProtocols()); //$NON-NLS-1$
 114   
 
 115  0
         processValidatorScript(_scriptPath, cycle, field, symbols);
 116   
     }
 117   
 
 118  0
     private String buildUrlRegexpProtocols()
 119   
     {
 120  0
         if (_allowedProtocols == null)
 121   
         {
 122  0
             return null;
 123   
         }
 124  0
         String regexp = "/("; //$NON-NLS-1$
 125  0
         Iterator iter = _allowedProtocols.iterator();
 126  0
         while (iter.hasNext())
 127   
         {
 128  0
             String protocol = (String) iter.next();
 129  0
             regexp += protocol;
 130  0
             if (iter.hasNext())
 131   
             {
 132  0
                 regexp += "|"; //$NON-NLS-1$
 133   
             }
 134   
         }
 135  0
         regexp += "):///"; //$NON-NLS-1$
 136  0
         return regexp;
 137   
     }
 138   
 
 139  0
     public String getScriptPath()
 140   
     {
 141  0
         return _scriptPath;
 142   
     }
 143   
 
 144  0
     public void setScriptPath(String scriptPath)
 145   
     {
 146  0
         _scriptPath = scriptPath;
 147   
     }
 148   
 
 149  4
     protected boolean isValidUrl(String url)
 150   
     {
 151  4
         boolean bIsValid;
 152  4
         try
 153   
         {
 154  4
             new URL(url);
 155  2
             bIsValid = true;
 156   
         }
 157   
         catch (MalformedURLException mue)
 158   
         {
 159  2
             bIsValid = false;
 160   
         }
 161  4
         return bIsValid;
 162   
     }
 163   
 
 164  2
     protected boolean isAllowedProtocol(String url)
 165   
     {
 166  2
         boolean bIsAllowed = false;
 167  2
         if (_allowedProtocols != null)
 168   
         {
 169  1
             URL oUrl;
 170  1
             try
 171   
             {
 172  1
                 oUrl = new URL(url);
 173   
             }
 174   
             catch (MalformedURLException e)
 175   
             {
 176  0
                 return false;
 177   
             }
 178  1
             String actualProtocol = oUrl.getProtocol();
 179  1
             Iterator iter = _allowedProtocols.iterator();
 180  1
             while (iter.hasNext())
 181   
             {
 182  2
                 String protocol = (String) iter.next();
 183  2
                 if (protocol.equals(actualProtocol))
 184   
                 {
 185  0
                     bIsAllowed = true;
 186  0
                     break;
 187   
                 }
 188   
             }
 189   
         }
 190   
         else
 191   
         {
 192  1
             bIsAllowed = true;
 193   
         }
 194  2
         return bIsAllowed;
 195   
     }
 196   
 
 197  0
     public String getInvalidUrlFormatMessage()
 198   
     {
 199  0
         return _invalidUrlFormatMessage;
 200   
     }
 201   
 
 202  0
     public String getMinimumLengthMessage()
 203   
     {
 204  0
         return _minimumLengthMessage;
 205   
     }
 206   
 
 207  1
     public void setInvalidUrlFormatMessage(String string)
 208   
     {
 209  1
         _invalidUrlFormatMessage = string;
 210   
     }
 211   
 
 212  0
     public String getDisallowedProtocolMessage()
 213   
     {
 214  0
         return _disallowedProtocolMessage;
 215   
     }
 216   
 
 217  0
     public void setDisallowedProtocolMessage(String string)
 218   
     {
 219  0
         _disallowedProtocolMessage = string;
 220   
     }
 221   
 
 222  1
     public void setMinimumLengthMessage(String string)
 223   
     {
 224  1
         _minimumLengthMessage = string;
 225   
     }
 226   
 
 227  2
     protected String buildMinimumLengthMessage(IFormComponent field)
 228   
     {
 229  2
         String pattern = getPattern(_minimumLengthMessage, "field-too-short", //$NON-NLS-1$
 230   
                 field.getPage().getLocale());
 231   
 
 232  2
         return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName());
 233   
     }
 234   
 
 235  2
     protected String buildInvalidUrlFormatMessage(IFormComponent field)
 236   
     {
 237  2
         String pattern = getPattern(_invalidUrlFormatMessage, "invalid-url-format", //$NON-NLS-1$
 238   
                 field.getPage().getLocale());
 239   
 
 240  2
         return formatString(pattern, field.getDisplayName());
 241   
     }
 242   
 
 243  1
     protected String buildDisallowedProtocolMessage(IFormComponent field)
 244   
     {
 245  1
         if (_allowedProtocols == null)
 246   
         {
 247  0
             return null;
 248   
         }
 249  1
         String pattern = getPattern(_disallowedProtocolMessage, "disallowed-protocol", //$NON-NLS-1$
 250   
                 field.getPage().getLocale());
 251   
 
 252  1
         String allowedProtocols = ""; //$NON-NLS-1$
 253  1
         Iterator iter = _allowedProtocols.iterator();
 254  1
         while (iter.hasNext())
 255   
         {
 256  2
             String protocol = (String) iter.next();
 257  2
             if (!allowedProtocols.equals("")) { //$NON-NLS-1$
 258  1
                 if (iter.hasNext())
 259   
                 {
 260  0
                     allowedProtocols += ", "; //$NON-NLS-1$
 261   
                 }
 262   
                 else
 263   
                 {
 264  1
                     allowedProtocols += " or "; //$NON-NLS-1$
 265   
                 }
 266   
             }
 267  2
             allowedProtocols += protocol;
 268   
         }
 269   
 
 270  1
         return formatString(pattern, allowedProtocols);
 271   
     }
 272   
 
 273  5
     protected String getPattern(String override, String key, Locale locale)
 274   
     {
 275  5
         if (override != null)
 276  2
             return override;
 277   
 
 278  3
         ResourceBundle strings;
 279  3
         String string;
 280  3
         try
 281   
         {
 282  3
             strings = ResourceBundle.getBundle("net.sf.cendil.tapestry.valid.ValidationStrings", //$NON-NLS-1$
 283   
                     locale);
 284  0
             string = strings.getString(key);
 285   
         }
 286   
         catch (Exception exc)
 287   
         {
 288  3
             strings = ResourceBundle.getBundle("org.apache.tapestry.valid.ValidationStrings", //$NON-NLS-1$
 289   
                     locale);
 290  3
             string = strings.getString(key);
 291   
         }
 292   
 
 293  3
         return string;
 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   
 }