1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.custom.urlvalidator;
21
22 import javax.faces.application.FacesMessage;
23 import javax.faces.component.UIComponent;
24 import javax.faces.context.FacesContext;
25 import javax.faces.validator.ValidatorException;
26
27 import org.apache.myfaces.validator.ValidatorBase;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public abstract class AbstractUrlValidator extends ValidatorBase {
43
44
45
46
47 public static final String VALIDATOR_ID = "org.apache.myfaces.validator.Url";
48
49
50
51
52 public static final String URL_MESSAGE_ID = "org.apache.myfaces.Url.INVALID";
53
54 public AbstractUrlValidator(){
55
56 }
57
58
59
60
61
62 public void validate(
63 FacesContext facesContext,
64 UIComponent uiComponent,
65 Object value)
66 throws ValidatorException {
67
68
69 if (facesContext == null) throw new NullPointerException("facesContext");
70 if (uiComponent == null) throw new NullPointerException("uiComponent");
71
72 if (value == null)
73 {
74 return;
75 }
76
77 org.apache.commons.validator.UrlValidator urlValidator = initValidator();
78
79 if (!urlValidator.isValid(value.toString())) {
80 Object[] args = {value.toString()};
81 throw new ValidatorException(getFacesMessage(URL_MESSAGE_ID, args));
82 }
83
84 }
85
86 private org.apache.commons.validator.UrlValidator initValidator()
87 {
88 int options = 0;
89
90 if (isAllow2Slashes())
91 {
92 options = options | org.apache.commons.validator.UrlValidator.ALLOW_2_SLASHES;
93 }
94
95 if (isAllowAllSchemas())
96 {
97 options = options | org.apache.commons.validator.UrlValidator.ALLOW_ALL_SCHEMES;
98 }
99
100 String [] schemesList = getSchemesList();
101 org.apache.commons.validator.UrlValidator urlValidator = null;
102 if (schemesList == null){
103 urlValidator = new
104 org.apache.commons.validator.UrlValidator(options);
105 }
106 else
107 {
108 urlValidator = new
109 org.apache.commons.validator.UrlValidator(schemesList,options);
110 }
111 return urlValidator;
112 }
113
114 private String[] getSchemesList(){
115 if (getSchemes() == null)
116 {
117 return null;
118 }
119 String [] list = getSchemes().split(",");
120 String [] resp = new String [list.length];
121
122 for (int i = 0; i < list.length; i++)
123 {
124 resp[i] = list[i].trim();
125 }
126 return resp;
127 }
128
129 public abstract void setSchemes(String _schemes);
130
131
132
133
134
135
136
137
138
139
140 public abstract String getSchemes();
141
142 public abstract void setAllow2Slashes(boolean _allow2Slashes);
143
144
145
146
147
148
149
150 public abstract boolean isAllow2Slashes();
151
152 public abstract void setAllowAllSchemas(boolean _allowAllSchemas);
153
154
155
156
157
158
159
160
161 public abstract boolean isAllowAllSchemas();
162
163 }