1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * ident        "%Z%%M% %I%     %E% SMI"
  24  *
  25  * Copyright (c) 2001 by Sun Microsystems, Inc.
  26  * All rights reserved.
  27  */
  28 package com.sun.dhcpmgr.cli.dhcpconfig;
  29 
  30 import com.sun.dhcpmgr.data.IPAddress;
  31 import com.sun.dhcpmgr.data.ValidationException;
  32 
  33 import java.util.Vector;
  34 import java.util.Enumeration;
  35 import java.util.StringTokenizer;
  36 import java.text.MessageFormat;
  37 
  38 /**
  39  * Class that builds a vector of IPAddress objects.
  40  */
  41 public class IPAddressList extends Vector {
  42 
  43     /**
  44      * Construct a IPAddressList from a String list of IP addresses
  45      * and/or host names.
  46      * @param addresses a String list of IP addresses
  47      */
  48     public IPAddressList(String addresses) throws ValidationException {
  49 
  50         removeAllElements();
  51 
  52         if (addresses == null) {
  53             return;
  54         }
  55 
  56         StringTokenizer st = new StringTokenizer(addresses, ",");
  57         while (st.hasMoreTokens()) {
  58             String address = st.nextToken();
  59             try {
  60                 addElement(new IPAddress(address.trim()));
  61             } catch (ValidationException e) {
  62                 Object [] args = new Object[1];
  63                 args[0] = address;
  64                 MessageFormat form = new MessageFormat(
  65                     ResourceStrings.getString("invalid_ip_address"));
  66                 throw new ValidationException(form.format(args));
  67             }
  68         }
  69     } // constructor
  70 
  71     /**
  72      * Construct a IPAddressList from an array of IPAddress objects.
  73      * @param addresses array of IPAddress objects
  74      */
  75     public IPAddressList(IPAddress [] addresses) {
  76 
  77         removeAllElements();
  78 
  79         if (addresses == null) {
  80             return;
  81         }
  82 
  83         for (int i = 0; i < addresses.length; i++) {
  84             addElement(addresses[i]);
  85         }
  86     } // constructor
  87 
  88     /**
  89      * Returns an array of IP Addresses.
  90      * @return an array of IP Addresses.
  91      */
  92     public IPAddress [] toIPAddressArray() {
  93         return ((IPAddress [])toArray(new IPAddress[size()]));
  94     } // toIPAddressArray
  95 
  96     /**
  97      * Returns a comma separated list of IP Addresses.
  98      * @return a comma separated list of IP Addresses.
  99      */
 100     public String toString() {
 101         StringBuffer b = new StringBuffer();
 102         Enumeration en = elements();
 103         while (en.hasMoreElements()) {
 104             if (b.length() != 0) {
 105                 b.append(',');
 106             }
 107             b.append(((IPAddress)en.nextElement()).getHostAddress());
 108         }
 109         return b.toString();
 110     } // toString
 111 
 112 } // IPAddressList