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 2002 Sun Microsystems, Inc. All rights reserved. 26 * Use is subject to license terms. 27 */ 28 29 package com.sun.dhcpmgr.data.qualifier; 30 31 import java.lang.reflect.Array; 32 import java.util.ArrayList; 33 import java.util.StringTokenizer; 34 35 /** 36 * An an implementation of the qualifier type that provides an array of 37 * qualifier types. The element qualifer type is contained within the array 38 * qualifier type. 39 */ 40 public class QualifierArray extends QualifierTypeImpl { 41 42 /** 43 * The default String of characters that delimit elements in a String 44 * representation of the array when it is parsed. 45 */ 46 public static final String DEFAULT_PARSE_DELIM = ", "; 47 48 /** 49 * The default String of characters that delimit elements in a String 50 * representation of the array when it is formatted. 51 */ 52 public static final String DEFAULT_FORMAT_DELIM = ","; 53 54 /** 55 * The type of the arrays elements. 56 */ 57 protected QualifierType type; 58 59 /** 60 * The String of characters that delimit elements in a String 61 * representation of the array when it is parsed. 62 */ 63 protected String parseDelim = DEFAULT_PARSE_DELIM; 64 65 /** 66 * The String of characters that delimit elements in a String 67 * representation of the array when it is formatted. 68 */ 69 protected String formatDelim = DEFAULT_FORMAT_DELIM; 70 71 private QualifierArray() {} 72 73 /** 74 * Construct an array qualifier type. 75 * 76 * @param type 77 * The qualifier type of the arrays elements. 78 */ 79 public QualifierArray(QualifierType type) { 80 this(type, DEFAULT_PARSE_DELIM, DEFAULT_FORMAT_DELIM); 81 } 82 83 /** 84 * Construct an array qualifier type. 85 * 86 * @param type 87 * The qualifier type of the arrays elements. 88 * @param delim 89 * The String of characters that delimit elements in a String 90 * representation of the array. 91 */ 92 public QualifierArray(QualifierType type, 93 String parseDelim, 94 String formatDelim) { 95 this.type = type; 96 this.parseDelim = parseDelim; 97 this.formatDelim = formatDelim; 98 } 99 100 /** 101 * Get the arrays element qualifier type. 102 * 103 * @areturn 104 * The qualifier type of the arrays elements. 105 */ 106 public QualifierType getElementType() { 107 return type; 108 } 109 110 /** 111 * Determine if the given value is a legal value for this type. The 112 * element delimiters are the default or those supplied during 113 * construction of the QualifierArray. 114 * 115 * @param value 116 * The value to test. 117 * @return 118 * Returns a Java type containing the parse value if legal, otherwise 119 * null is returned if the value was illegal. 120 */ 121 public Object parseValue(String value) { 122 return parseValue(value, parseDelim); 123 } 124 125 /** 126 * Determine if the given value is a legal value for this type. The 127 * element delimiters provided override the use of the defaults or 128 * those supplied during the construction of the QualifierArray. 129 * 130 * @param value 131 * The value to test. 132 * @param parseDelim 133 * The String of characters that delimit elements in a String 134 * representation of the array. 135 * @return 136 * Returns a Java type containing the parse value if legal, otherwise 137 * null is returned if the value was illegal. 138 */ 139 public Object parseValue(String value, String parseDelim) { 140 if (value == null) { 141 return null; 142 } 143 144 StringTokenizer tokenizer = new StringTokenizer(value, parseDelim); 145 ArrayList elements = new ArrayList(); 146 147 while (tokenizer.hasMoreTokens()) { 148 String token = tokenizer.nextToken(); 149 Object object = type.parseValue(token); 150 151 if (object == null) { 152 return null; 153 } 154 155 elements.add(object); 156 } 157 158 return elements.toArray( 159 (Object[])Array.newInstance(type.getJavaType(), elements.size())); 160 } 161 162 /** 163 * Format the given string if it is a legal value for this type. The 164 * element delimiters are the default or those supplied during 165 * construction of the QualifierArray. 166 * 167 * @param value 168 * The value to format. 169 * @return 170 * Returns a string containing the formatted value if legal, otherwise 171 * null is returned if the value was illegal. 172 */ 173 public String formatValue(String value) { 174 return formatValue(value, parseDelim, formatDelim); 175 } 176 177 /** 178 * Format the given string if it is a legal value for this type. The 179 * element delimiters provided override the use of the defaults or 180 * those supplied during the construction of the QualifierArray. 181 * 182 * @param value 183 * The value to format. 184 * @param parseDelim 185 * The String of characters that delimit elements in a String 186 * representation of the array when it is parsed. 187 * @param formatDelim 188 * The String of characters that delimit elements in a String 189 * representation of the array when it is formatted. 190 * @return 191 * Returns a string containing the formatted value if legal, otherwise 192 * null is returned if the value was illegal. 193 */ 194 public String formatValue(String value, 195 String parseDelim, 196 String formatDelim) { 197 198 if (value == null) { 199 return null; 200 } 201 202 value = value.trim(); 203 204 StringTokenizer tokenizer = new StringTokenizer(value, parseDelim); 205 StringBuffer string = new StringBuffer(); 206 207 while (tokenizer.hasMoreTokens()) { 208 String token = tokenizer.nextToken(); 209 token = type.formatValue(token); 210 211 if (token == null) { 212 return null; 213 } 214 215 string.append(token); 216 217 if (tokenizer.hasMoreTokens()) { 218 string.append(formatDelim); 219 } 220 } 221 222 return string.toString(); 223 } 224 225 /** 226 * Get the String containing the characters that delimit elements in 227 * a String representation of the array when it is parsed. 228 * 229 * @return 230 * Returns a String containing the characters that delimit elements in 231 * a String representation of the array when it is parsed. 232 */ 233 public String getParseDelimiters() { 234 return parseDelim; 235 } 236 237 /** 238 * Get the String containing the characters that delimit elements in 239 * a String representation of the array when it is formatted. 240 * 241 * @return 242 * Returns a String containing the characters that delimit elements in 243 * a String representation of the array when it is formatted. 244 */ 245 public String getFormatDelimiters() { 246 return formatDelim; 247 } 248 249 public Class getJavaType() { 250 return java.lang.reflect.Array.class; 251 } 252 253 public String toString() { 254 return "[L" + type.getClass().getName() + ";"; 255 } 256 257 }