10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7298SMark.J.Nelson@Sun.COM  * Common Development and Distribution License (the "License").
6*7298SMark.J.Nelson@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
220Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
230Sstevel@tonic-gate  * All rights reserved.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate //  AttributeDescriptor.java: Describes an SLP attribute.
280Sstevel@tonic-gate //  Author:           James Kempf
290Sstevel@tonic-gate //  Created On:       Thu Jun 19 10:38:01 1997
300Sstevel@tonic-gate //  Last Modified By: James Kempf
310Sstevel@tonic-gate //  Last Modified On: Tue Jun  2 13:29:08 1998
320Sstevel@tonic-gate //  Update Count:     29
330Sstevel@tonic-gate //
340Sstevel@tonic-gate 
350Sstevel@tonic-gate package com.sun.slp;
360Sstevel@tonic-gate 
370Sstevel@tonic-gate import java.util.*;
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /**
410Sstevel@tonic-gate  * The instances of the AttributeDescriptor class
420Sstevel@tonic-gate  * return information on a particular service location attribute. This
430Sstevel@tonic-gate  * information is primarily for GUI tools. Programmatic attribute
440Sstevel@tonic-gate  * verification should be done through the ServiceLocationAttributeVerifier.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * @author James Kempf
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate class AttributeDescriptor
510Sstevel@tonic-gate     extends Object
520Sstevel@tonic-gate     implements ServiceLocationAttributeDescriptor {
530Sstevel@tonic-gate 
540Sstevel@tonic-gate     // Indicates byte array type.
550Sstevel@tonic-gate 
560Sstevel@tonic-gate     private static final String JAVA_OPAQUE_TYPE = "[B";
570Sstevel@tonic-gate 
580Sstevel@tonic-gate     private String id = "";
590Sstevel@tonic-gate     private String valueType = "";
600Sstevel@tonic-gate     private String description = "";
610Sstevel@tonic-gate     private Vector allowedValues = new Vector();
620Sstevel@tonic-gate     private Vector defaultValues = new Vector();
630Sstevel@tonic-gate     private boolean isMultivalued = false;
640Sstevel@tonic-gate     private boolean isOptional = false;
650Sstevel@tonic-gate     private boolean requiresExplicitMatch = false;
660Sstevel@tonic-gate     private boolean isLiteral = false;
670Sstevel@tonic-gate     private boolean isKeyword = false;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate     /**
700Sstevel@tonic-gate      * Return the attribute's id.
710Sstevel@tonic-gate      *
720Sstevel@tonic-gate      * @return A String with the attribute's id.
730Sstevel@tonic-gate      */
740Sstevel@tonic-gate 
getId()750Sstevel@tonic-gate     final public String getId() {
760Sstevel@tonic-gate 	return id;
770Sstevel@tonic-gate     }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate     /**
800Sstevel@tonic-gate      * Return the fully qualified Java type of the attribute. SLP types
810Sstevel@tonic-gate      * are translated into Java types as follows:
820Sstevel@tonic-gate      *
830Sstevel@tonic-gate      *	STRING		java.lang.String
840Sstevel@tonic-gate      *	INTEGER		java.lang.Integer
850Sstevel@tonic-gate      *	BOOLEAN		java.lang.Boolean
860Sstevel@tonic-gate      *	OPAQUE		[B (i.e. array of byte, byte[]);
870Sstevel@tonic-gate      *	KEYWORD		null string, ""
880Sstevel@tonic-gate      *
890Sstevel@tonic-gate      * @return A String containing the Java type name for the attribute values.
900Sstevel@tonic-gate      */
910Sstevel@tonic-gate 
getValueType()920Sstevel@tonic-gate     final public String getValueType() {
930Sstevel@tonic-gate 	return valueType;
940Sstevel@tonic-gate     }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate     /**
970Sstevel@tonic-gate      * Return attribute's help text.
980Sstevel@tonic-gate      *
990Sstevel@tonic-gate      * @return A String containing the attribute's help text.
1000Sstevel@tonic-gate      */
1010Sstevel@tonic-gate 
getDescription()1020Sstevel@tonic-gate     final public String getDescription() {
1030Sstevel@tonic-gate 	return description;
1040Sstevel@tonic-gate     }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate     /**
1070Sstevel@tonic-gate      * Return an Enumeration of allowed values for the attribute type.
1080Sstevel@tonic-gate      * For keyword attributes returns null. For no allowed values
1090Sstevel@tonic-gate      * (i.e. unrestricted) returns an empty Enumeration. Small memory
1100Sstevel@tonic-gate      * implementations may want to parse values on demand rather
1110Sstevel@tonic-gate      * than at the time the descriptor is created.
1120Sstevel@tonic-gate      *
1130Sstevel@tonic-gate      * @return An Enumeration of allowed values for the attribute or
1140Sstevel@tonic-gate      *         null if the attribute is keyword.
1150Sstevel@tonic-gate      */
1160Sstevel@tonic-gate 
getAllowedValues()1170Sstevel@tonic-gate     final public Enumeration getAllowedValues() {
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if (getIsKeyword()) {
1200Sstevel@tonic-gate 	    return null;
1210Sstevel@tonic-gate 	} else {
1220Sstevel@tonic-gate 	    return allowedValues.elements();
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate     }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate     /**
1280Sstevel@tonic-gate      * Return an Enumeration of default values for the attribute type.
1290Sstevel@tonic-gate      * For keyword attributes returns null. For no allowed values
1300Sstevel@tonic-gate      * (i.e. unrestricted) returns an empty Enumeration. Small memory
1310Sstevel@tonic-gate      * implementations may want to parse values on demand rather
1320Sstevel@tonic-gate      * than at the time the descriptor is created.
1330Sstevel@tonic-gate      *
1340Sstevel@tonic-gate      * @return An Enumeration of default values for the attribute or
1350Sstevel@tonic-gate      *	      null if the attribute is keyword.
1360Sstevel@tonic-gate      */
1370Sstevel@tonic-gate 
getDefaultValues()1380Sstevel@tonic-gate     final public Enumeration getDefaultValues() {
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if (getIsKeyword()) {
1410Sstevel@tonic-gate 	    return null;
1420Sstevel@tonic-gate 	} else {
1430Sstevel@tonic-gate 	    return defaultValues.elements();
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate     }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate     /**
1480Sstevel@tonic-gate      * Returns true if the "M" flag is set.
1490Sstevel@tonic-gate      *
1500Sstevel@tonic-gate      * @return True if the "M" flag is set.
1510Sstevel@tonic-gate      */
1520Sstevel@tonic-gate 
getIsMultivalued()1530Sstevel@tonic-gate     final public boolean getIsMultivalued() {
1540Sstevel@tonic-gate 	return isMultivalued;
1550Sstevel@tonic-gate     }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate     /**
1580Sstevel@tonic-gate      * Returns true if the "O" flag is set.
1590Sstevel@tonic-gate      *
1600Sstevel@tonic-gate      * @return True if the "O" flag is set.
1610Sstevel@tonic-gate      */
1620Sstevel@tonic-gate 
getIsOptional()1630Sstevel@tonic-gate     final public boolean getIsOptional() {
1640Sstevel@tonic-gate 	return isOptional;
1650Sstevel@tonic-gate     }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate     /**
1680Sstevel@tonic-gate      * Returns true if the "X" flag is set.
1690Sstevel@tonic-gate      *
1700Sstevel@tonic-gate      * @return True if the "X" flag is set.
1710Sstevel@tonic-gate      */
1720Sstevel@tonic-gate 
getRequiresExplicitMatch()1730Sstevel@tonic-gate     final public boolean getRequiresExplicitMatch() {
1740Sstevel@tonic-gate 	return requiresExplicitMatch;
1750Sstevel@tonic-gate     }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate     /**
1780Sstevel@tonic-gate      * Returns true if the "L" flag is set.
1790Sstevel@tonic-gate      *
1800Sstevel@tonic-gate      * @return True if the "L" flag is set.
1810Sstevel@tonic-gate      */
1820Sstevel@tonic-gate 
getIsLiteral()1830Sstevel@tonic-gate     final public boolean getIsLiteral() {
1840Sstevel@tonic-gate 	return isLiteral;
1850Sstevel@tonic-gate     }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate     /**
1880Sstevel@tonic-gate      * Returns true if the attribute is a keyword attribute.
1890Sstevel@tonic-gate      *
1900Sstevel@tonic-gate      * @return True if the attribute is a keyword attribute
1910Sstevel@tonic-gate      */
1920Sstevel@tonic-gate 
getIsKeyword()1930Sstevel@tonic-gate     final public boolean getIsKeyword() {
1940Sstevel@tonic-gate 	return isKeyword;
1950Sstevel@tonic-gate     }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate     //
1980Sstevel@tonic-gate     // Package private interface for setting properties.
1990Sstevel@tonic-gate     //
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate     /**
2020Sstevel@tonic-gate      * Set the attribute's id.
2030Sstevel@tonic-gate      *
2040Sstevel@tonic-gate      * @param nid New id string
2050Sstevel@tonic-gate      */
2060Sstevel@tonic-gate 
setId(String nid)2070Sstevel@tonic-gate     void setId(String nid) {
2080Sstevel@tonic-gate 	id = nid;
2090Sstevel@tonic-gate     }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate     /**
2120Sstevel@tonic-gate      * Set the fully qualified Java type of the attribute. We don't check
2130Sstevel@tonic-gate      * the argument here, assuming that the caller has taken care of it.
2140Sstevel@tonic-gate      *
2150Sstevel@tonic-gate      * @param nvt New value type.
2160Sstevel@tonic-gate      */
2170Sstevel@tonic-gate 
setValueType(String nvt)2180Sstevel@tonic-gate     void setValueType(String nvt) {
2190Sstevel@tonic-gate 	valueType = nvt;
2200Sstevel@tonic-gate     }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate     /**
2230Sstevel@tonic-gate      * Set attribute's help text.
2240Sstevel@tonic-gate      *
2250Sstevel@tonic-gate      * @param ndes A String containing the attribute's help text.
2260Sstevel@tonic-gate      */
2270Sstevel@tonic-gate 
setDescription(String ndes)2280Sstevel@tonic-gate     void setDescription(String ndes) {
2290Sstevel@tonic-gate 	description = ndes;
2300Sstevel@tonic-gate     }
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate     /**
2330Sstevel@tonic-gate      * Set the allowed values for an attribute.
2340Sstevel@tonic-gate      *
2350Sstevel@tonic-gate      * @param nnv A vector of allowed values for the attribute.
2360Sstevel@tonic-gate      */
2370Sstevel@tonic-gate 
setAllowedValues(Vector nnv)2380Sstevel@tonic-gate     void setAllowedValues(Vector nnv) {
2390Sstevel@tonic-gate 	allowedValues = nnv;
2400Sstevel@tonic-gate     }
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate     /**
2440Sstevel@tonic-gate      * Set the default values for an attribute.
2450Sstevel@tonic-gate      *
2460Sstevel@tonic-gate      * @param nnv A vector of default values for the attribute.
2470Sstevel@tonic-gate      */
2480Sstevel@tonic-gate 
setDefaultValues(Vector nnv)2490Sstevel@tonic-gate     void setDefaultValues(Vector nnv) {
2500Sstevel@tonic-gate 	defaultValues = nnv;
2510Sstevel@tonic-gate     }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate     /**
2540Sstevel@tonic-gate      * Set the isMultivalued flag.
2550Sstevel@tonic-gate      *
2560Sstevel@tonic-gate      * @param flag New multivalued flag.
2570Sstevel@tonic-gate      */
2580Sstevel@tonic-gate 
setIsMultivalued(boolean flag)2590Sstevel@tonic-gate     void setIsMultivalued(boolean flag) {
2600Sstevel@tonic-gate 	isMultivalued = flag;
2610Sstevel@tonic-gate     }
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate     /**
2640Sstevel@tonic-gate      * Set the isOptional flag.
2650Sstevel@tonic-gate      *
2660Sstevel@tonic-gate      * @param flag New optional flag.
2670Sstevel@tonic-gate      */
2680Sstevel@tonic-gate 
setIsOptional(boolean flag)2690Sstevel@tonic-gate     void setIsOptional(boolean flag) {
2700Sstevel@tonic-gate 	isOptional = flag;
2710Sstevel@tonic-gate     }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate     /**
2740Sstevel@tonic-gate      * Set the requiresExplicitMatch flag.
2750Sstevel@tonic-gate      *
2760Sstevel@tonic-gate      * @param flag New explicit match flag.
2770Sstevel@tonic-gate      */
2780Sstevel@tonic-gate 
setRequiresExplicitMatch(boolean flag)2790Sstevel@tonic-gate     void setRequiresExplicitMatch(boolean flag) {
2800Sstevel@tonic-gate 	requiresExplicitMatch = flag;
2810Sstevel@tonic-gate     }
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate     /**
2840Sstevel@tonic-gate      * Set the isLiteral flag.
2850Sstevel@tonic-gate      *
2860Sstevel@tonic-gate      * @param flag New literal flag.
2870Sstevel@tonic-gate      */
2880Sstevel@tonic-gate 
setIsLiteral(boolean flag)2890Sstevel@tonic-gate     void setIsLiteral(boolean flag) {
2900Sstevel@tonic-gate 	isLiteral = flag;
2910Sstevel@tonic-gate     }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate     /**
2940Sstevel@tonic-gate      * Set the keyword attribute flag.
2950Sstevel@tonic-gate      *
2960Sstevel@tonic-gate      * @param flag New keyword attribute flag.
2970Sstevel@tonic-gate      */
2980Sstevel@tonic-gate 
setIsKeyword(boolean flag)2990Sstevel@tonic-gate     void setIsKeyword(boolean flag) {
3000Sstevel@tonic-gate 	isKeyword = flag;
3010Sstevel@tonic-gate     }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate     /**
3040Sstevel@tonic-gate      * Format a string with the id and all the fields.
3050Sstevel@tonic-gate      *
3060Sstevel@tonic-gate      */
3070Sstevel@tonic-gate 
toString()3080Sstevel@tonic-gate     public String toString() {
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	String ret = "";
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	ret += "\nid:" + id + "\n";
3130Sstevel@tonic-gate 	ret += "valueType:" + valueType + "\n";
3140Sstevel@tonic-gate 	ret += "description:" + description + "\n";
3150Sstevel@tonic-gate 	ret +=
3160Sstevel@tonic-gate 	    "defaultValues:" +
3170Sstevel@tonic-gate 	    (defaultValues == null ? "<null>":
3180Sstevel@tonic-gate 	    (valueType.equals(JAVA_OPAQUE_TYPE) ?
3190Sstevel@tonic-gate 	    formatByteArrays(defaultValues) : defaultValues.toString())) +
3200Sstevel@tonic-gate 	    "\n";
3210Sstevel@tonic-gate 	ret +=
3220Sstevel@tonic-gate 	    "allowedValues:" +
3230Sstevel@tonic-gate 	    (allowedValues == null ? "<null>":
3240Sstevel@tonic-gate 	    (valueType.equals(JAVA_OPAQUE_TYPE) ?
3250Sstevel@tonic-gate 	    formatByteArrays(allowedValues) : allowedValues.toString())) +
3260Sstevel@tonic-gate 	    "\n";
3270Sstevel@tonic-gate 	ret += "isMultivalued:" + (isMultivalued ? "true":"false") + "\n";
3280Sstevel@tonic-gate 	ret += "isOptional:" + (isOptional ? "true":"false") + "\n";
3290Sstevel@tonic-gate 	ret += "requiresExplicitMatch:" +
3300Sstevel@tonic-gate 	    (requiresExplicitMatch ? "true":"false") + "\n";
3310Sstevel@tonic-gate 	ret += "isLiteral:" + (isLiteral ? "true":"false") + "\n";
3320Sstevel@tonic-gate 	ret += "isKeyword:" + (isKeyword ? "true":"false") + "\n\n";
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	return ret;
3350Sstevel@tonic-gate     }
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate     // Formats an array of bytes for opaque, rather than just the address.
3380Sstevel@tonic-gate 
formatByteArrays(Vector arrays)3390Sstevel@tonic-gate     private String formatByteArrays(Vector arrays) {
3400Sstevel@tonic-gate 	int i, n = arrays.size();
3410Sstevel@tonic-gate 	StringBuffer ret = new StringBuffer();
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	ret.append("[");
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
3460Sstevel@tonic-gate 	    byte array[] = (byte[])arrays.elementAt(i);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	    ret.append("{ ");
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	    int j, m = array.length;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	    for (j = 0; j < m; j++) {
3530Sstevel@tonic-gate 		ret.append("0x");
3540Sstevel@tonic-gate 		ret.append(Integer.toHexString((int)array[j]&0xFF));
3550Sstevel@tonic-gate 		ret.append(j == m - 1 ? " } " : ",");
3560Sstevel@tonic-gate 	    }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	    ret.append(i == n - 1 ? "":" , ");
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	ret.append("]");
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	return ret.toString();
3640Sstevel@tonic-gate     }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate }
367