xref: /onnv-gate/usr/src/lib/libslp/javalib/com/sun/slp/AttributeString.java (revision 7298:b69e27387f74)
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 //  AttributeString.java: Model an Attribute value string.
280Sstevel@tonic-gate //  Author:           James Kempf
290Sstevel@tonic-gate //  Created On:       Wed Apr  8 10:40:03 1998
300Sstevel@tonic-gate //  Last Modified By: James Kempf
310Sstevel@tonic-gate //  Last Modified On: Wed Jul 29 15:21:32 1998
320Sstevel@tonic-gate //  Update Count:     16
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 import java.io.*;
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /**
410Sstevel@tonic-gate  * The AttributeString class embodies the SLP lower cased, space compressed
420Sstevel@tonic-gate  * string matching rules. It precomputes an
430Sstevel@tonic-gate  * efficient string for matching, squeezing out whitespace and lower casing.
440Sstevel@tonic-gate  * The toString() method returns the original string. Note that it does
450Sstevel@tonic-gate  * NOT handle pattern wildcard matching.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * @author James Kempf
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate class AttributeString extends Object {
510Sstevel@tonic-gate 
520Sstevel@tonic-gate     String string;		// the original string.
530Sstevel@tonic-gate     String cstring;		// whitespace separated, lower cased parts.
540Sstevel@tonic-gate     Locale locale;		// the locale in which this string was created.
550Sstevel@tonic-gate 
560Sstevel@tonic-gate     // Create an attribute string. Use the passed in locale to determine
570Sstevel@tonic-gate     //  the lower casing rules.
580Sstevel@tonic-gate 
AttributeString(String str, Locale nlocale)590Sstevel@tonic-gate     AttributeString(String str, Locale nlocale) {
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	string = str;
620Sstevel@tonic-gate 	locale = nlocale;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	cstring = parse(str, nlocale);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate     }
670Sstevel@tonic-gate 
680Sstevel@tonic-gate     // Parse the string into whitespace separated, lower cased parts.
690Sstevel@tonic-gate 
parse(String str, Locale nlocale)700Sstevel@tonic-gate     private String parse(String str, Locale nlocale) {
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	StringBuffer buf = new StringBuffer();
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	StringTokenizer tk =
750Sstevel@tonic-gate 	    new StringTokenizer(str, ServiceLocationAttribute.WHITESPACE);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	while (tk.hasMoreTokens()) {
780Sstevel@tonic-gate 	    buf.append(tk.nextToken().toLowerCase(nlocale));
790Sstevel@tonic-gate 	    buf.append(ServiceLocationAttribute.SPACE);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	}
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	return buf.toString().trim();
840Sstevel@tonic-gate     }
850Sstevel@tonic-gate 
860Sstevel@tonic-gate     //
870Sstevel@tonic-gate     // Comparison operations.
880Sstevel@tonic-gate     //
890Sstevel@tonic-gate 
900Sstevel@tonic-gate     // For compatibility with AttributePattern.
910Sstevel@tonic-gate 
match(AttributeString str)920Sstevel@tonic-gate     public boolean match(AttributeString str) {
930Sstevel@tonic-gate 	return equals(str);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate     }
960Sstevel@tonic-gate 
lessEqual(AttributeString str)970Sstevel@tonic-gate     public boolean lessEqual(AttributeString str) {
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	return (cstring.compareTo(str.cstring) <= 0);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate     }
1020Sstevel@tonic-gate 
greaterEqual(AttributeString str)1030Sstevel@tonic-gate     public boolean greaterEqual(AttributeString str) {
1040Sstevel@tonic-gate 	return (cstring.compareTo(str.cstring) >= 0);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate     }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate     //
1090Sstevel@tonic-gate     // Object overrides.
1100Sstevel@tonic-gate     //
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate     /**
1130Sstevel@tonic-gate      * Return true if obj pattern matches with this string.
1140Sstevel@tonic-gate      */
1150Sstevel@tonic-gate 
equals(Object obj)1160Sstevel@tonic-gate     public boolean equals(Object obj) {
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (obj == this) {
1190Sstevel@tonic-gate 	    return true;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if (!(obj instanceof AttributeString)) {
1240Sstevel@tonic-gate 	    return false;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	return cstring.equals(((AttributeString)obj).cstring);
1290Sstevel@tonic-gate     }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate     /**
1320Sstevel@tonic-gate      * Return the original string.
1330Sstevel@tonic-gate      */
1340Sstevel@tonic-gate 
toString()1350Sstevel@tonic-gate     public String toString() {
1360Sstevel@tonic-gate 	return string;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate     }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate     /**
1410Sstevel@tonic-gate      * Hash on the computed string.
1420Sstevel@tonic-gate      */
1430Sstevel@tonic-gate 
hashCode()1440Sstevel@tonic-gate     public int hashCode() {
1450Sstevel@tonic-gate 	return cstring.toString().hashCode();
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate     }
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate }
150