KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > valid > UrlValidator


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 JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25 import java.util.Vector JavaDoc;
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 JavaDoc _minimumLengthMessage;
41
42     private String JavaDoc _invalidUrlFormatMessage;
43
44     private String JavaDoc _disallowedProtocolMessage;
45
46     private Collection JavaDoc _allowedProtocols;
47
48     private String JavaDoc _scriptPath = "/org/apache/tapestry/valid/UrlValidator.script"; //$NON-NLS-1$
49

50     public UrlValidator()
51     {
52     }
53
54     /**
55      * Initializes the UrlValidator with properties defined by the initializer.
56      *
57      * @since 4.0
58      */

59
60     public UrlValidator(String JavaDoc initializer)
61     {
62         super(initializer);
63     }
64
65     public String JavaDoc toString(IFormComponent field, Object JavaDoc value)
66     {
67         if (value == null)
68             return null;
69
70         return value.toString();
71     }
72
73     public Object JavaDoc toObject(IFormComponent field, String JavaDoc input) throws ValidatorException
74     {
75         if (checkRequired(field, input))
76             return null;
77
78         if (_minimumLength > 0 && input.length() < _minimumLength)
79             throw new ValidatorException(buildMinimumLengthMessage(field),
80                     ValidationConstraint.MINIMUM_WIDTH);
81
82         if (!isValidUrl(input))
83             throw new ValidatorException(buildInvalidUrlFormatMessage(field),
84                     ValidationConstraint.URL_FORMAT);
85
86         if (!isAllowedProtocol(input))
87         {
88             throw new ValidatorException(buildDisallowedProtocolMessage(field),
89                     ValidationConstraint.DISALLOWED_PROTOCOL);
90         }
91
92         return input;
93     }
94
95     public int getMinimumLength()
96     {
97         return _minimumLength;
98     }
99
100     public void setMinimumLength(int minimumLength)
101     {
102         _minimumLength = minimumLength;
103     }
104
105     public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
106             IRequestCycle cycle)
107     {
108         if (!isClientScriptingEnabled())
109             return;
110
111         Map JavaDoc symbols = new HashMap JavaDoc();
112
113         if (isRequired())
114             symbols.put("requiredMessage", buildRequiredMessage(field)); //$NON-NLS-1$
115

116         if (_minimumLength > 0)
117             symbols.put("minimumLengthMessage", //$NON-NLS-1$
118
buildMinimumLengthMessage(field));
119
120         symbols.put("urlFormatMessage", buildInvalidUrlFormatMessage(field)); //$NON-NLS-1$
121

122         symbols.put("urlDisallowedProtocolMessage", //$NON-NLS-1$
123
buildDisallowedProtocolMessage(field));
124
125         symbols.put("urlRegexpProtocols", buildUrlRegexpProtocols()); //$NON-NLS-1$
126

127         processValidatorScript(_scriptPath, cycle, field, symbols);
128     }
129
130     private String JavaDoc buildUrlRegexpProtocols()
131     {
132         if (_allowedProtocols == null)
133         {
134             return null;
135         }
136         String JavaDoc regexp = "/("; //$NON-NLS-1$
137
Iterator JavaDoc iter = _allowedProtocols.iterator();
138         while (iter.hasNext())
139         {
140             String JavaDoc protocol = (String JavaDoc) iter.next();
141             regexp += protocol;
142             if (iter.hasNext())
143             {
144                 regexp += "|"; //$NON-NLS-1$
145
}
146         }
147         regexp += "):///"; //$NON-NLS-1$
148
return regexp;
149     }
150
151     public String JavaDoc getScriptPath()
152     {
153         return _scriptPath;
154     }
155
156     public void setScriptPath(String JavaDoc scriptPath)
157     {
158         _scriptPath = scriptPath;
159     }
160
161     protected boolean isValidUrl(String JavaDoc url)
162     {
163         boolean bIsValid;
164         try
165         {
166             new URL JavaDoc(url);
167             bIsValid = true;
168         }
169         catch (MalformedURLException JavaDoc mue)
170         {
171             bIsValid = false;
172         }
173         return bIsValid;
174     }
175
176     protected boolean isAllowedProtocol(String JavaDoc url)
177     {
178         boolean bIsAllowed = false;
179         if (_allowedProtocols != null)
180         {
181             URL JavaDoc oUrl;
182             try
183             {
184                 oUrl = new URL JavaDoc(url);
185             }
186             catch (MalformedURLException JavaDoc e)
187             {
188                 return false;
189             }
190             String JavaDoc actualProtocol = oUrl.getProtocol();
191             Iterator JavaDoc iter = _allowedProtocols.iterator();
192             while (iter.hasNext())
193             {
194                 String JavaDoc protocol = (String JavaDoc) iter.next();
195                 if (protocol.equals(actualProtocol))
196                 {
197                     bIsAllowed = true;
198                     break;
199                 }
200             }
201         }
202         else
203         {
204             bIsAllowed = true;
205         }
206         return bIsAllowed;
207     }
208
209     public String JavaDoc getInvalidUrlFormatMessage()
210     {
211         return _invalidUrlFormatMessage;
212     }
213
214     public String JavaDoc getMinimumLengthMessage()
215     {
216         return _minimumLengthMessage;
217     }
218
219     public void setInvalidUrlFormatMessage(String JavaDoc string)
220     {
221         _invalidUrlFormatMessage = string;
222     }
223
224     public String JavaDoc getDisallowedProtocolMessage()
225     {
226         return _disallowedProtocolMessage;
227     }
228
229     public void setDisallowedProtocolMessage(String JavaDoc string)
230     {
231         _disallowedProtocolMessage = string;
232     }
233
234     public void setMinimumLengthMessage(String JavaDoc string)
235     {
236         _minimumLengthMessage = string;
237     }
238
239     protected String JavaDoc buildMinimumLengthMessage(IFormComponent field)
240     {
241         String JavaDoc pattern = getPattern(_minimumLengthMessage, "field-too-short", //$NON-NLS-1$
242
field.getPage().getLocale());
243
244         return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName());
245     }
246
247     protected String JavaDoc buildInvalidUrlFormatMessage(IFormComponent field)
248     {
249         String JavaDoc pattern = getPattern(_invalidUrlFormatMessage, "invalid-url-format", //$NON-NLS-1$
250
field.getPage().getLocale());
251
252         return formatString(pattern, field.getDisplayName());
253     }
254
255     protected String JavaDoc buildDisallowedProtocolMessage(IFormComponent field)
256     {
257         if (_allowedProtocols == null)
258         {
259             return null;
260         }
261         String JavaDoc pattern = getPattern(_disallowedProtocolMessage, "disallowed-protocol", //$NON-NLS-1$
262
field.getPage().getLocale());
263
264         String JavaDoc allowedProtocols = ""; //$NON-NLS-1$
265
Iterator JavaDoc iter = _allowedProtocols.iterator();
266         while (iter.hasNext())
267         {
268             String JavaDoc protocol = (String JavaDoc) iter.next();
269             if (!allowedProtocols.equals("")) { //$NON-NLS-1$
270
if (iter.hasNext())
271                 {
272                     allowedProtocols += ", "; //$NON-NLS-1$
273
}
274                 else
275                 {
276                     allowedProtocols += " or "; //$NON-NLS-1$
277
}
278             }
279             allowedProtocols += protocol;
280         }
281
282         return formatString(pattern, allowedProtocols);
283     }
284
285     protected String JavaDoc getPattern(String JavaDoc override, String JavaDoc key, Locale JavaDoc locale)
286     {
287         if (override != null)
288             return override;
289
290         ResourceBundle JavaDoc strings = ResourceBundle.getBundle(
291                 "org.apache.tapestry.valid.ValidationStrings",
292                 locale);
293         return strings.getString(key);
294     }
295
296     /**
297      * @param protocols
298      * comma separated list of allowed protocols
299      */

300     public void setAllowedProtocols(String JavaDoc protocols)
301     {
302         StringSplitter spliter = new StringSplitter(',');
303         //String[] aProtocols = protocols.split(","); //$NON-NLS-1$
304
String JavaDoc[] aProtocols = spliter.splitToArray(protocols); //$NON-NLS-1$
305
_allowedProtocols = new Vector JavaDoc();
306         for (int i = 0; i < aProtocols.length; i++)
307         {
308             _allowedProtocols.add(aProtocols[i]);
309         }
310     }
311
312 }
Popular Tags