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