KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > MatchQueryExp


1 /*
2  * @(#)MatchQueryExp.java 1.25 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.management;
9
10
11
12 /**
13  * This class is used by the query-building mechanism to represent binary
14  * relations.
15  * @serial include
16  *
17  * @since 1.5
18  */

19 class MatchQueryExp extends QueryEval JavaDoc implements QueryExp JavaDoc {
20     
21     /* Serial version */
22     private static final long serialVersionUID = -7156603696948215014L;
23
24     /**
25      * @serial The attribute value to be matched
26      */

27     private AttributeValueExp JavaDoc exp;
28
29     /**
30      * @serial The pattern to be matched
31      */

32     private String JavaDoc pattern;
33
34
35     /**
36      * Basic Constructor.
37      */

38     public MatchQueryExp() {
39     }
40
41     /**
42      * Creates a new MatchQueryExp where the specified AttributeValueExp matches
43      * the specified pattern StringValueExp.
44      */

45     public MatchQueryExp(AttributeValueExp JavaDoc a, StringValueExp JavaDoc s) {
46     exp = a;
47     pattern = s.getValue();
48     }
49     
50
51     /**
52      * Returns the attribute of the query.
53      */

54     public AttributeValueExp JavaDoc getAttribute() {
55     return exp;
56     }
57
58     /**
59      * Returns the pattern of the query.
60      */

61     public String JavaDoc getPattern() {
62     return pattern;
63     }
64
65     /**
66      * Applies the MatchQueryExp on a MBean.
67      *
68      * @param name The name of the MBean on which the MatchQueryExp will be applied.
69      *
70      * @return True if the query was successfully applied to the MBean, false otherwise.
71      *
72      * @exception BadStringOperationException
73      * @exception BadBinaryOpValueExpException
74      * @exception BadAttributeValueExpException
75      * @exception InvalidApplicationException
76      */

77     public boolean apply(ObjectName JavaDoc name) throws BadStringOperationException JavaDoc, BadBinaryOpValueExpException JavaDoc,
78     BadAttributeValueExpException JavaDoc, InvalidApplicationException JavaDoc {
79
80     ValueExp JavaDoc val = exp.apply(name);
81     if (!(val instanceof StringValueExp JavaDoc)) {
82         return false;
83     }
84     return wildmatch(((StringValueExp JavaDoc)val).getValue(), pattern);
85     }
86
87     /**
88      * Returns the string representing the object
89      */

90     public String JavaDoc toString() {
91     return exp + " like " + new StringValueExp JavaDoc(likeTranslate(pattern));
92     }
93
94     private static String JavaDoc likeTranslate(String JavaDoc s) {
95     return s.replace('?', '_').replace('*', '%');
96     }
97     
98     /*
99      * Tests whether string s is matched by pattern p.
100      * Supports "?", "*", "[", each of which may be escaped with "\";
101      * Character classes may use "!" for negation and "-" for range.
102      * Not yet supported: internationalization; "\" inside brackets.<P>
103      * Wildcard matching routine by Karl Heuer. Public Domain.<P>
104      */

105     private static boolean wildmatch(String JavaDoc s, String JavaDoc p) {
106     char c;
107         int si = 0, pi = 0;
108         int slen = s.length();
109         int plen = p.length();
110
111         while (pi < plen) { // While still string
112
c = p.charAt(pi++);
113             if (c == '?') {
114                 if (++si > slen)
115                     return false;
116             } else if (c == '[') { // Start of choice
117
boolean wantit = true;
118                 boolean seenit = false;
119                 if (p.charAt(pi) == '!') {
120                     wantit = false;
121                     ++pi;
122                 }
123                 while (++pi < plen && (c = p.charAt(pi)) != ']') {
124                     if (p.charAt(pi) == '-' && pi+1 < plen) {
125                         if (s.charAt(si) >= c && s.charAt(si) <= p.charAt(pi+1)) {
126                             seenit = true;
127             }
128             pi += 1;
129                     } else {
130                         if (c == s.charAt(si)) {
131                             seenit = true;
132             }
133                     }
134                 }
135                 if ((pi >= plen) || (wantit != seenit)) {
136                     return false;
137         }
138         ++pi;
139                 ++si;
140             } else if (c == '*') { // Wildcard
141
if (pi >= plen)
142                     return true;
143                 do {
144                     if (wildmatch(s.substring(si), p.substring(pi)))
145                         return true;
146                 } while (++si < slen);
147                 return false;
148             } else if (c == '\\') {
149                 if (pi >= plen || p.charAt(pi++) != s.charAt(si++))
150                     return false;
151             } else {
152                 if (si >= slen || c != s.charAt(si++)) {
153                     return false;
154         }
155             }
156         }
157         return (si == slen);
158     }
159
160  }
161
Popular Tags