01: /*
02: * WorkingURLParameterType.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.io.IOException;
23: import java.net.MalformedURLException;
24: import java.net.URL;
25:
26: import org.naturalcli.IParameterType;
27:
28: /**
29: * The class implements an URL which is validated parameter type.
30: *
31: * @see java.net.URL
32: * @author Ferran Busquets
33: *
34: */
35: public class WorkingURLParameterType implements IParameterType {
36:
37: /* (non-Javadoc)
38: * @see org.naturalcli.IParameterType#getParameterTypeName()
39: */
40: @Override
41: public String getParameterTypeName() {
42: return "working_url";
43: }
44:
45: /* (non-Javadoc)
46: * @see org.naturalcli.IParameterType#validateParameter(java.lang.String)
47: */
48: @Override
49: public boolean validateParameter(String value) {
50: try {
51: new URL(value).openConnection();
52: return true;
53: } catch (MalformedURLException e) {
54: return false;
55: } catch (IOException e) {
56: return false;
57: }
58: }
59:
60: /* (non-Javadoc)
61: * @see org.naturalcli.IParameterType#validationMessage(java.lang.String)
62: */
63: @Override
64: public String validationMessage(String value) {
65: return this .validateParameter(value) ? null
66: : "Not working URL.";
67: }
68:
69: /* (non-Javadoc)
70: * @see org.naturalcli.IParameterType#convertParameterValue(java.lang.String)
71: */
72: @Override
73: public Object convertParameterValue(String strRepresentation) {
74: try {
75: return new URL(strRepresentation);
76: } catch (MalformedURLException e) {
77: return null;
78: }
79: }
80:
81: }
|