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 1999-2002 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 package com.sun.dhcpmgr.data;
  29 
  30 import java.util.Vector;
  31 import java.util.Enumeration;
  32 
  33 public class AsciiOptionValue extends OptionValue {
  34     private String name;
  35     private String value;
  36     private boolean valid;
  37 
  38     // Serialization id for this class
  39     static final long serialVersionUID = -4937655446360683504L;
  40     
  41     protected AsciiOptionValue(String name) {
  42         this.name = name;
  43         value = null;
  44         valid = false;
  45     }
  46     
  47     public String getName() {
  48         return name;
  49     }
  50     
  51     public String getValue() {
  52         // Before we return the value, we go through and escape special chars.
  53         StringBuffer retValue = new StringBuffer();
  54         char [] c = value.toCharArray();
  55         for (int i = 0; i < c.length; ++i) {
  56             if (c[i] == '\\' || c[i] == '"') {
  57                 retValue.append('\\');
  58             }
  59             retValue.append(c[i]);
  60         }
  61         return retValue.toString();
  62     }
  63     
  64     public void setValue(Object value) throws ValidationException {
  65         // Find option in option definition table in order to validate the data
  66         Option option = OptionsTable.getTable().get(name);
  67         if (option == null) {
  68             Object [] args = { name };
  69             throwException("invalid_option", args);
  70         }
  71         if (value instanceof String) {
  72             String newValue = (String)value;
  73             // Either quoted, or not, but must balance
  74             if (newValue.startsWith("\"") ^ newValue.endsWith("\"")) { 
  75                 Object [] args = { name,
  76                     Option.getTypeDhcptabString(option.getType()) };
  77                 throwException("invalid_option_value", args);
  78             }
  79             if (newValue.startsWith("\"")) {
  80                 newValue = newValue.substring(1, newValue.length() - 1);
  81             }
  82             if (newValue.length() == 0) {
  83                 // Empty strings are not acceptable
  84                 Object [] args = { name,
  85                     Option.getTypeDhcptabString(option.getType()) };
  86                 throwException("invalid_option_value", args);
  87             }
  88             // Check that the resulting length is OK
  89             if ((option.getMaximum() != 0)
  90                     && (newValue.length() > option.getMaximum())) {
  91                 Object [] args = { name,
  92                     Integer.toString(option.getMaximum()) };
  93                 throwException("invalid_option_maximum", args);
  94             }
  95             this.value = newValue;
  96             valid = true;
  97         } else if (value instanceof Vector) {
  98             /*
  99              * We generate the value by creating a blank-separated list of
 100              * tokens; each token is the product of a toString() on the
 101              * vector's elements.
 102              */
 103             StringBuffer b = new StringBuffer();
 104             Enumeration en = ((Vector)value).elements();
 105             while (en.hasMoreElements()) {
 106                 if (b.length() != 0) {
 107                     b.append(' ');
 108                 }
 109                 b.append(en.nextElement().toString());
 110             }
 111             setValue(b.toString());
 112         } else {
 113             // Anything else should just tell us what it looks like as a string.
 114             setValue(value.toString());
 115         }
 116     }
 117     
 118     public String toString() {
 119         return (getName() + "=\"" + getValue() + "\"");
 120     }
 121     
 122     public boolean isValid() {
 123         return valid;
 124     }
 125     
 126     public Object clone() {
 127         AsciiOptionValue v = new AsciiOptionValue(name);
 128         v.value = value;
 129         v.valid = valid;
 130         return v;
 131     }
 132 }