svn-gvsig-desktop / tags / v1_9_Build_1222 / install / IzPack / sample / src / IPValidator.java @ 44116
History | View | Annotate | Download (3.14 KB)
1 |
/*
|
---|---|
2 |
* $Id: IPValidator.java 5819 2006-06-14 07:29:09Z cesar $
|
3 |
* Copyright (C) 2003 Elmar Grom
|
4 |
*
|
5 |
* File : IPValidator.java
|
6 |
* Description : Sample implementation of a rule validator
|
7 |
* Author's email : elmar@grom.net
|
8 |
* Author's Website : http://www.izforge.com
|
9 |
*
|
10 |
* This program is free software; you can redistribute it and/or
|
11 |
* modify it under the terms of the GNU General Public License
|
12 |
* as published by the Free Software Foundation; either version 2
|
13 |
* of the License, or any later version.
|
14 |
*
|
15 |
* This program is distributed in the hope that it will be useful,
|
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 |
* GNU General Public License for more details.
|
19 |
*
|
20 |
* You should have received a copy of the GNU General Public License
|
21 |
* along with this program; if not, write to the Free Software
|
22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
23 |
*/
|
24 |
|
25 |
package com.izforge.izpack.sample; |
26 |
|
27 |
import com.izforge.izpack.panels.*; |
28 |
|
29 |
/*---------------------------------------------------------------------------*/
|
30 |
/**
|
31 |
* This class represents a simple validator for IP addresses to demonstrate
|
32 |
* the implementation of a rule validator that cooperates with the
|
33 |
* <code>RuleInputField</code> used in the <code>UserInputPanel</code>
|
34 |
*
|
35 |
* @version 0.0.1 / 02/19/03
|
36 |
* @author Elmar Grom
|
37 |
*/
|
38 |
/*---------------------------------------------------------------------------*/
|
39 |
public class IPValidator implements Validator |
40 |
{ |
41 |
/*--------------------------------------------------------------------------*/
|
42 |
/**
|
43 |
* Validates the contend of a <code>RuleInputField</code>. The test is
|
44 |
* intended for a rule input field composed of four sub-fields. The
|
45 |
* combination of their individual content is assumed to represent an IP
|
46 |
* address.
|
47 |
*
|
48 |
* @param client the client object using the services of this validator.
|
49 |
*
|
50 |
* @return <code>true</code> if the validation passes, otherwise <code>false</code>.
|
51 |
*/
|
52 |
/*--------------------------------------------------------------------------*/
|
53 |
public boolean validate (ProcessingClient client) |
54 |
{ |
55 |
// ----------------------------------------------------
|
56 |
// verify that there are actually four sub-fields. A
|
57 |
// different number would indicate that we are not
|
58 |
// connected with the RuleInputField that we expect
|
59 |
// ----------------------------------------------------
|
60 |
if (client.getNumFields () != 4) |
61 |
{ |
62 |
return (false); |
63 |
} |
64 |
|
65 |
// ----------------------------------------------------
|
66 |
// test each field to make sure it actually contains
|
67 |
// an integer and the value of the integer is beween
|
68 |
// 0 and 255.
|
69 |
// ----------------------------------------------------
|
70 |
boolean isIP = true; |
71 |
|
72 |
for (int i = 0; i < 4; i++) |
73 |
{ |
74 |
int value;
|
75 |
|
76 |
try
|
77 |
{ |
78 |
value = Integer.parseInt (client.getFieldContents (i));
|
79 |
if ((value < 0) || (value > 255)) |
80 |
{ |
81 |
isIP = false;
|
82 |
} |
83 |
} |
84 |
catch (Throwable exception) |
85 |
{ |
86 |
isIP = false;
|
87 |
} |
88 |
} |
89 |
|
90 |
return (isIP);
|
91 |
} |
92 |
} |
93 |
/*---------------------------------------------------------------------------*/
|