001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
006 * 
007 * Copyright 2003 Elmar Grom
008 * 
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *     
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package   com.izforge.izpack.sample;
023
024import com.izforge.izpack.panels.ProcessingClient;
025import com.izforge.izpack.panels.Validator;
026
027/*---------------------------------------------------------------------------*/
028/**
029 * This class represents a simple validator for IP addresses to demonstrate
030 * the implementation of a rule validator that cooperates with the
031 * <code>RuleInputField</code> used in the <code>UserInputPanel</code>
032 *
033 * @version  0.0.1 / 02/19/03
034 * @author   Elmar Grom
035 */
036/*---------------------------------------------------------------------------*/
037public class IPValidator implements Validator
038{
039 /*--------------------------------------------------------------------------*/
040 /**
041  * Validates the contend of a <code>RuleInputField</code>. The test is
042  * intended for a rule input field composed of four sub-fields. The
043  * combination of their individual content is assumed to represent an IP
044  * address.
045  *
046  * @param     client   the client object using the services of this validator.
047  *
048  * @return    <code>true</code> if the validation passes, otherwise <code>false</code>.
049  */
050 /*--------------------------------------------------------------------------*/
051  public boolean validate (ProcessingClient client)
052  {
053    // ----------------------------------------------------
054    // verify that there are actually four sub-fields. A
055    // different number would indicate that we are not
056    // connected with the RuleInputField that we expect
057    // ----------------------------------------------------
058    if (client.getNumFields () != 4)
059    {
060      return (false);
061    }
062    
063    // ----------------------------------------------------
064    // test each field to make sure it actually contains
065    // an integer and the value of the integer is beween
066    // 0 and 255.
067    // ----------------------------------------------------
068    boolean isIP = true;
069    
070    for (int i = 0; i < 4; i++)
071    {
072      int value;
073      
074      try
075      {
076        value = Integer.parseInt (client.getFieldContents (i));
077        if ((value < 0) || (value > 255))
078        {
079          isIP = false;
080        }
081      }
082      catch (Throwable exception)
083      {
084        isIP = false;
085      }
086    }
087    
088    return (isIP);
089  }
090}
091/*---------------------------------------------------------------------------*/