summaryrefslogtreecommitdiff
path: root/usr/src/lib/libslp/javalib/com/sun/slp/AttributePattern.java
blob: 86da31ab6efcd90cee07e85532b1d9625b09823f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1999 by Sun Microsystems, Inc.
 * All rights reserved.
 *
 */

//  AttributePattern.java: Models a pattern for attribute matching.
//  Author:           James Kempf
//  Created On:       Tue Feb  3 15:26:30 1998
//  Last Modified By: James Kempf
//  Last Modified On: Thu Aug  6 14:33:57 1998
//  Update Count:     19
//

package com.sun.slp;

import java.util.*;
import java.io.*;

/**
 * The AttributePattern class models an attribute pattern. It handles
 * wildcard matching of lowercased, space-compressed strings. Each
 * element in the parts vector is a PatternPart object. A PatternPart
 * object is a pattern consisting of (maximally) a beginning wildcard and
 * string pattern. A PatternPart may be lacking the
 * any of these, but will always have at least one. 
 *
 * @author James Kempf
 */

class AttributePattern extends AttributeString {

    private static final String WILDCARD = "*";

    private Vector parts = new Vector();

    /**
     * The PatternPart class models a single component of a pattern.
     * It may have a beginning wildcard and string
     * pattern in the middle. Any of the parts may be missing, but it will 
     * always have at least one. 
     *
     * @author James Kempf
     */


    private class PatternPart extends Object {

	boolean wildcard = false;
	String pattern = "";

	PatternPart(boolean wc, String str) {
	    wildcard = wc;
	    pattern = str;

	}
    }

    AttributePattern(String str, Locale locale) {

	super(str, locale);

	// Parse out wildcards into PatternPart objects.

	// If there's no wildcards, simply insert the string in as the pattern.

	if (cstring.indexOf(WILDCARD) == -1) {
	    parts.addElement(new PatternPart(false, cstring));

	} else {

	    // Parse the patterns into parts.

	    StringTokenizer tk = new StringTokenizer(cstring, WILDCARD, true);

	    while (tk.hasMoreTokens()) {
		String middle = "";
		boolean wc = false;

		String tok = tk.nextToken();

		// Beginning wildcard, or, if none, then the middle.

		if (tok.equals(WILDCARD)) {
		    wc = true;

		    // Need to look for middle.

		    if (tk.hasMoreTokens()) {
			middle = tk.nextToken();

		    }

		} else {
		    middle = tok;

		}

		// Note that there may be a terminal pattern part that just
		//  consists of a wildcard.

		parts.addElement(new PatternPart(wc, middle));
	    }
	}
    }

    boolean isWildcarded() {
	return (parts.size() > 1);

    }

    // Match the AttributeString object against this pattern,
    //  returning true if they match.

    public boolean match(AttributeString str) {
	String cstring = str.cstring;
	int offset = 0, len = cstring.length();
	int i = 0, n = parts.size();
	boolean match = true;

	// March through the parts, matching against the string.

	for (; i < n; i++) {
	    PatternPart p = (PatternPart)parts.elementAt(i);

	    // If there's a wildcard, check the remainder of the string for
	    //  the pattern.

	    if (p.wildcard) {

		// Note that if the pattern string is empty (""), then this
		//  will return offset, but on the next iteration, it will
		//  fall out of the loop because an empty pattern string
		//  can only occur at the end (like "foo*").

		if ((offset = cstring.indexOf(p.pattern, offset)) == -1) {

		    // The pattern was not found. Break out of the loop.

		    match = false;
		    break;
		}

		offset += p.pattern.length();

		// We are at the end of the string.

		if (offset >= len) {

		    // If we are not at the end of the pattern, then we may not
		    //  have a match.

		    if (i < (n - 1)) {

			// If there is one more in the pattern, and it is
			// a pure wildcard, then we *do* have a match.

			if (i == (n - 2)) {
			    p = (PatternPart)parts.elementAt(i+1);

			    if (p.wildcard == true &&
			       p.pattern.length() <= 0) {
				break;

			    }
			}

			match = false;

		    }

		    // Break out of the loop, no more string to analyze.

		    break;
		}

	    } else {

		// The pattern string must match the beginning part of the 
		// argument string.

		if (!cstring.regionMatches(offset,
					   p.pattern,
					   0,
					   p.
					   pattern.length())) {
		    match = false;
		    break;

		}

		// Bump up offset by the pattern length, and exit if
		// we're beyond the end of the string.

		offset += p.pattern.length();

		if (offset >= len) {
		    break;

		}
	    }
	}

	return match;
    }
}