1 /*
2 * Copyright 1999-2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package javax.management;
27
28
29
30 /**
31 * This class is used by the query-building mechanism to represent binary
32 * relations.
33 * @serial include
34 *
35 * @since 1.5
36 */
37 class MatchQueryExp extends QueryEval implements QueryExp {
38
39 /* Serial version */
40 private static final long serialVersionUID = -7156603696948215014L;
41
42 /**
43 * @serial The attribute value to be matched
44 */
45 private AttributeValueExp exp;
46
47 /**
48 * @serial The pattern to be matched
49 */
50 private String pattern;
51
52
53 /**
54 * Basic Constructor.
55 */
56 public MatchQueryExp() {
57 }
58
59 /**
60 * Creates a new MatchQueryExp where the specified AttributeValueExp matches
61 * the specified pattern StringValueExp.
62 */
63 public MatchQueryExp(AttributeValueExp a, StringValueExp s) {
64 exp = a;
65 pattern = s.getValue();
66 }
67
68
69 /**
70 * Returns the attribute of the query.
71 */
72 public AttributeValueExp getAttribute() {
73 return exp;
74 }
75
76 /**
77 * Returns the pattern of the query.
78 */
79 public String getPattern() {
80 return pattern;
81 }
82
83 /**
84 * Applies the MatchQueryExp on a MBean.
85 *
86 * @param name The name of the MBean on which the MatchQueryExp will be applied.
87 *
88 * @return True if the query was successfully applied to the MBean, false otherwise.
89 *
90 * @exception BadStringOperationException
91 * @exception BadBinaryOpValueExpException
92 * @exception BadAttributeValueExpException
93 * @exception InvalidApplicationException
94 */
95 public boolean apply(ObjectName name) throws
96 BadStringOperationException,
97 BadBinaryOpValueExpException,
98 BadAttributeValueExpException,
99 InvalidApplicationException {
100
101 ValueExp val = exp.apply(name);
102 if (!(val instanceof StringValueExp)) {
103 return false;
104 }
105 return wildmatch(((StringValueExp)val).getValue(), pattern);
106 }
107
108 /**
109 * Returns the string representing the object
110 */
111 public String toString() {
112 return exp + " like " + new StringValueExp(likeTranslate(pattern));
113 }
114
115 private static String likeTranslate(String s) {
116 StringBuilder sb = new StringBuilder();
117 int c;
118 for (int i = 0; i < s.length(); i += Character.charCount(c)) {
119 c = s.codePointAt(i);
120 switch (c) {
121 case '\\':
122 i += Character.charCount(c);
123 sb.append('\\');
124 if (i < s.length()) {
125 c = s.codePointAt(i);
126 sb.appendCodePoint(c);
127 }
128 break;
129 case '*':
130 sb.append('%'); break;
131 case '?':
132 sb.append('_'); break;
133 case '%':
134 sb.append("\\%"); break;
135 case '_':
136 sb.append("\\_"); break;
137 default:
138 sb.appendCodePoint(c); break;
139 }
140 }
141 return sb.toString();
142 }
143
144 /*
145 * Tests whether string s is matched by pattern p.
146 * Supports "?", "*", "[", each of which may be escaped with "\";
147 * character classes may use "!" for negation and "-" for range.
148 * Not yet supported: internationalization; "\" inside brackets.<P>
149 * Wildcard matching routine by Karl Heuer. Public Domain.<P>
150 */
151 private static boolean wildmatch(String s, String p) {
152 char c;
153 int si = 0, pi = 0;
154 int slen = s.length();
155 int plen = p.length();
156
157 while (pi < plen) { // While still string
158 c = p.charAt(pi++);
159 if (c == '?') {
160 if (++si > slen)
161 return false;
162 } else if (c == '[') { // Start of choice
163 if (si >= slen)
164 return false;
165 boolean wantit = true;
166 boolean seenit = false;
167 if (p.charAt(pi) == '!') {
168 wantit = false;
169 ++pi;
170 }
171 while ((c = p.charAt(pi)) != ']' && ++pi < plen) {
172 if (p.charAt(pi) == '-' &&
173 pi+1 < plen &&
174 p.charAt(pi+1) != ']') {
175 if (s.charAt(si) >= p.charAt(pi-1) &&
176 s.charAt(si) <= p.charAt(pi+1)) {
177 seenit = true;
178 }
179 ++pi;
180 } else {
181 if (c == s.charAt(si)) {
182 seenit = true;
183 }
184 }
185 }
186 if ((pi >= plen) || (wantit != seenit)) {
187 return false;
188 }
189 ++pi;
190 ++si;
191 } else if (c == '*') { // Wildcard
192 if (pi >= plen)
193 return true;
194 do {
195 if (wildmatch(s.substring(si), p.substring(pi)))
196 return true;
197 } while (++si < slen);
198 return false;
199 } else if (c == '\\') {
200 if (pi >= plen || si >= slen ||
201 p.charAt(pi++) != s.charAt(si++))
202 return false;
203 } else {
204 if (si >= slen || c != s.charAt(si++)) {
205 return false;
206 }
207 }
208 }
209 return (si == slen);
210 }
211 }