blob: 12e42769ffdf0973c026939ef83b3643c4958c75 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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
26package 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 */
37class 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 return s.replace('?', '_').replace('*', '%');
117 }
118
119 /*
120 * Tests whether string s is matched by pattern p.
121 * Supports "?", "*", "[", each of which may be escaped with "\";
122 * character classes may use "!" for negation and "-" for range.
123 * Not yet supported: internationalization; "\" inside brackets.<P>
124 * Wildcard matching routine by Karl Heuer. Public Domain.<P>
125 */
126 private static boolean wildmatch(String s, String p) {
127 char c;
128 int si = 0, pi = 0;
129 int slen = s.length();
130 int plen = p.length();
131
132 while (pi < plen) { // While still string
133 c = p.charAt(pi++);
134 if (c == '?') {
135 if (++si > slen)
136 return false;
137 } else if (c == '[') { // Start of choice
138 if (si >= slen)
139 return false;
140 boolean wantit = true;
141 boolean seenit = false;
142 if (p.charAt(pi) == '!') {
143 wantit = false;
144 ++pi;
145 }
146 while ((c = p.charAt(pi)) != ']' && ++pi < plen) {
147 if (p.charAt(pi) == '-' &&
148 pi+1 < plen &&
149 p.charAt(pi+1) != ']') {
150 if (s.charAt(si) >= p.charAt(pi-1) &&
151 s.charAt(si) <= p.charAt(pi+1)) {
152 seenit = true;
153 }
154 ++pi;
155 } else {
156 if (c == s.charAt(si)) {
157 seenit = true;
158 }
159 }
160 }
161 if ((pi >= plen) || (wantit != seenit)) {
162 return false;
163 }
164 ++pi;
165 ++si;
166 } else if (c == '*') { // Wildcard
167 if (pi >= plen)
168 return true;
169 do {
170 if (wildmatch(s.substring(si), p.substring(pi)))
171 return true;
172 } while (++si < slen);
173 return false;
174 } else if (c == '\\') {
175 if (pi >= plen || si >= slen ||
176 p.charAt(pi++) != s.charAt(si++))
177 return false;
178 } else {
179 if (si >= slen || c != s.charAt(si++)) {
180 return false;
181 }
182 }
183 }
184 return (si == slen);
185 }
186 }