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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 1999 by Sun Microsystems, Inc. 23 * All rights reserved. 24 * 25 */ 26 27 // AttributeDescriptor.java: Describes an SLP attribute. 28 // Author: James Kempf 29 // Created On: Thu Jun 19 10:38:01 1997 30 // Last Modified By: James Kempf 31 // Last Modified On: Tue Jun 2 13:29:08 1998 32 // Update Count: 29 33 // 34 35 package com.sun.slp; 36 37 import java.util.*; 38 39 40 /** 41 * The instances of the AttributeDescriptor class 42 * return information on a particular service location attribute. This 43 * information is primarily for GUI tools. Programmatic attribute 44 * verification should be done through the ServiceLocationAttributeVerifier. 45 * 46 * @author James Kempf 47 * 48 */ 49 50 class AttributeDescriptor 51 extends Object 52 implements ServiceLocationAttributeDescriptor { 53 54 // Indicates byte array type. 55 56 private static final String JAVA_OPAQUE_TYPE = "[B"; 57 58 private String id = ""; 59 private String valueType = ""; 60 private String description = ""; 61 private Vector allowedValues = new Vector(); 62 private Vector defaultValues = new Vector(); 63 private boolean isMultivalued = false; 64 private boolean isOptional = false; 65 private boolean requiresExplicitMatch = false; 66 private boolean isLiteral = false; 67 private boolean isKeyword = false; 68 69 /** 70 * Return the attribute's id. 71 * 72 * @return A String with the attribute's id. 73 */ 74 getId()75 final public String getId() { 76 return id; 77 } 78 79 /** 80 * Return the fully qualified Java type of the attribute. SLP types 81 * are translated into Java types as follows: 82 * 83 * STRING java.lang.String 84 * INTEGER java.lang.Integer 85 * BOOLEAN java.lang.Boolean 86 * OPAQUE [B (i.e. array of byte, byte[]); 87 * KEYWORD null string, "" 88 * 89 * @return A String containing the Java type name for the attribute values. 90 */ 91 getValueType()92 final public String getValueType() { 93 return valueType; 94 } 95 96 /** 97 * Return attribute's help text. 98 * 99 * @return A String containing the attribute's help text. 100 */ 101 getDescription()102 final public String getDescription() { 103 return description; 104 } 105 106 /** 107 * Return an Enumeration of allowed values for the attribute type. 108 * For keyword attributes returns null. For no allowed values 109 * (i.e. unrestricted) returns an empty Enumeration. Small memory 110 * implementations may want to parse values on demand rather 111 * than at the time the descriptor is created. 112 * 113 * @return An Enumeration of allowed values for the attribute or 114 * null if the attribute is keyword. 115 */ 116 getAllowedValues()117 final public Enumeration getAllowedValues() { 118 119 if (getIsKeyword()) { 120 return null; 121 } else { 122 return allowedValues.elements(); 123 } 124 } 125 126 127 /** 128 * Return an Enumeration of default values for the attribute type. 129 * For keyword attributes returns null. For no allowed values 130 * (i.e. unrestricted) returns an empty Enumeration. Small memory 131 * implementations may want to parse values on demand rather 132 * than at the time the descriptor is created. 133 * 134 * @return An Enumeration of default values for the attribute or 135 * null if the attribute is keyword. 136 */ 137 getDefaultValues()138 final public Enumeration getDefaultValues() { 139 140 if (getIsKeyword()) { 141 return null; 142 } else { 143 return defaultValues.elements(); 144 } 145 } 146 147 /** 148 * Returns true if the "M" flag is set. 149 * 150 * @return True if the "M" flag is set. 151 */ 152 getIsMultivalued()153 final public boolean getIsMultivalued() { 154 return isMultivalued; 155 } 156 157 /** 158 * Returns true if the "O" flag is set. 159 * 160 * @return True if the "O" flag is set. 161 */ 162 getIsOptional()163 final public boolean getIsOptional() { 164 return isOptional; 165 } 166 167 /** 168 * Returns true if the "X" flag is set. 169 * 170 * @return True if the "X" flag is set. 171 */ 172 getRequiresExplicitMatch()173 final public boolean getRequiresExplicitMatch() { 174 return requiresExplicitMatch; 175 } 176 177 /** 178 * Returns true if the "L" flag is set. 179 * 180 * @return True if the "L" flag is set. 181 */ 182 getIsLiteral()183 final public boolean getIsLiteral() { 184 return isLiteral; 185 } 186 187 /** 188 * Returns true if the attribute is a keyword attribute. 189 * 190 * @return True if the attribute is a keyword attribute 191 */ 192 getIsKeyword()193 final public boolean getIsKeyword() { 194 return isKeyword; 195 } 196 197 // 198 // Package private interface for setting properties. 199 // 200 201 /** 202 * Set the attribute's id. 203 * 204 * @param nid New id string 205 */ 206 setId(String nid)207 void setId(String nid) { 208 id = nid; 209 } 210 211 /** 212 * Set the fully qualified Java type of the attribute. We don't check 213 * the argument here, assuming that the caller has taken care of it. 214 * 215 * @param nvt New value type. 216 */ 217 setValueType(String nvt)218 void setValueType(String nvt) { 219 valueType = nvt; 220 } 221 222 /** 223 * Set attribute's help text. 224 * 225 * @param ndes A String containing the attribute's help text. 226 */ 227 setDescription(String ndes)228 void setDescription(String ndes) { 229 description = ndes; 230 } 231 232 /** 233 * Set the allowed values for an attribute. 234 * 235 * @param nnv A vector of allowed values for the attribute. 236 */ 237 setAllowedValues(Vector nnv)238 void setAllowedValues(Vector nnv) { 239 allowedValues = nnv; 240 } 241 242 243 /** 244 * Set the default values for an attribute. 245 * 246 * @param nnv A vector of default values for the attribute. 247 */ 248 setDefaultValues(Vector nnv)249 void setDefaultValues(Vector nnv) { 250 defaultValues = nnv; 251 } 252 253 /** 254 * Set the isMultivalued flag. 255 * 256 * @param flag New multivalued flag. 257 */ 258 setIsMultivalued(boolean flag)259 void setIsMultivalued(boolean flag) { 260 isMultivalued = flag; 261 } 262 263 /** 264 * Set the isOptional flag. 265 * 266 * @param flag New optional flag. 267 */ 268 setIsOptional(boolean flag)269 void setIsOptional(boolean flag) { 270 isOptional = flag; 271 } 272 273 /** 274 * Set the requiresExplicitMatch flag. 275 * 276 * @param flag New explicit match flag. 277 */ 278 setRequiresExplicitMatch(boolean flag)279 void setRequiresExplicitMatch(boolean flag) { 280 requiresExplicitMatch = flag; 281 } 282 283 /** 284 * Set the isLiteral flag. 285 * 286 * @param flag New literal flag. 287 */ 288 setIsLiteral(boolean flag)289 void setIsLiteral(boolean flag) { 290 isLiteral = flag; 291 } 292 293 /** 294 * Set the keyword attribute flag. 295 * 296 * @param flag New keyword attribute flag. 297 */ 298 setIsKeyword(boolean flag)299 void setIsKeyword(boolean flag) { 300 isKeyword = flag; 301 } 302 303 /** 304 * Format a string with the id and all the fields. 305 * 306 */ 307 toString()308 public String toString() { 309 310 String ret = ""; 311 312 ret += "\nid:" + id + "\n"; 313 ret += "valueType:" + valueType + "\n"; 314 ret += "description:" + description + "\n"; 315 ret += 316 "defaultValues:" + 317 (defaultValues == null ? "<null>": 318 (valueType.equals(JAVA_OPAQUE_TYPE) ? 319 formatByteArrays(defaultValues) : defaultValues.toString())) + 320 "\n"; 321 ret += 322 "allowedValues:" + 323 (allowedValues == null ? "<null>": 324 (valueType.equals(JAVA_OPAQUE_TYPE) ? 325 formatByteArrays(allowedValues) : allowedValues.toString())) + 326 "\n"; 327 ret += "isMultivalued:" + (isMultivalued ? "true":"false") + "\n"; 328 ret += "isOptional:" + (isOptional ? "true":"false") + "\n"; 329 ret += "requiresExplicitMatch:" + 330 (requiresExplicitMatch ? "true":"false") + "\n"; 331 ret += "isLiteral:" + (isLiteral ? "true":"false") + "\n"; 332 ret += "isKeyword:" + (isKeyword ? "true":"false") + "\n\n"; 333 334 return ret; 335 } 336 337 // Formats an array of bytes for opaque, rather than just the address. 338 formatByteArrays(Vector arrays)339 private String formatByteArrays(Vector arrays) { 340 int i, n = arrays.size(); 341 StringBuffer ret = new StringBuffer(); 342 343 ret.append("["); 344 345 for (i = 0; i < n; i++) { 346 byte array[] = (byte[])arrays.elementAt(i); 347 348 ret.append("{ "); 349 350 int j, m = array.length; 351 352 for (j = 0; j < m; j++) { 353 ret.append("0x"); 354 ret.append(Integer.toHexString((int)array[j]&0xFF)); 355 ret.append(j == m - 1 ? " } " : ","); 356 } 357 358 ret.append(i == n - 1 ? "":" , "); 359 } 360 361 ret.append("]"); 362 363 return ret.toString(); 364 } 365 366 } 367