01: /*
02: * URLParameterType.java
03: *
04: * Copyright (C) 2007 Ferran Busquets
05: *
06: * This program is free software: you can redistribute it and/or modify
07: * it under the terms of the GNU General Public License as published by
08: * the Free Software Foundation, either version 3 of the License, or
09: * any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program. If not, see <http://www.gnu.org/licenses/>.
18: *
19: */
20: package org.naturalcli.parameters;
21:
22: import java.net.MalformedURLException;
23: import java.net.URL;
24:
25: import org.naturalcli.IParameterType;
26:
27: /**
28: * The class implements an URL parameter type.
29: * @author Ferran Busquets
30: *
31: */
32: public class URLParameterType implements IParameterType {
33:
34: /* (non-Javadoc)
35: * @see org.naturalcli.IParameterType#getParameterTypeName()
36: */
37: @Override
38: public String getParameterTypeName() {
39: return "url";
40: }
41:
42: /* (non-Javadoc)
43: * @see org.naturalcli.IParameterType#validateParameter(java.lang.String)
44: */
45: @Override
46: public boolean validateParameter(String value) {
47: try {
48: new URL(value);
49: return true;
50: } catch (MalformedURLException e) {
51: return false;
52: }
53: }
54:
55: /* (non-Javadoc)
56: * @see org.naturalcli.IParameterType#validationMessage(java.lang.String)
57: */
58: @Override
59: public String validationMessage(String value) {
60: return this .validateParameter(value) ? null : "Malformed URL.";
61: }
62:
63: /* (non-Javadoc)
64: * @see org.naturalcli.IParameterType#convertParameterValue(java.lang.String)
65: */
66: @Override
67: public Object convertParameterValue(String strRepresentation) {
68: try {
69: return new URL(strRepresentation);
70: } catch (MalformedURLException e) {
71: return null;
72: }
73: }
74:
75: }
|