FileDocCategorySizeDatePackage
MatchQueryExp.javaAPI DocJava SE 6 API4770Tue Jun 10 00:26:14 BST 2008javax.management

MatchQueryExp

public class MatchQueryExp extends QueryEval implements QueryExp
This class is used by the query-building mechanism to represent binary relations.
serial
include
since
1.5

Fields Summary
private static final long
serialVersionUID
private AttributeValueExp
exp
private String
pattern
Constructors Summary
public MatchQueryExp()
Basic Constructor.



           
       
    
public MatchQueryExp(AttributeValueExp a, StringValueExp s)
Creates a new MatchQueryExp where the specified AttributeValueExp matches the specified pattern StringValueExp.

 
	exp     = a;
	pattern = s.getValue();
    
Methods Summary
public booleanapply(javax.management.ObjectName name)
Applies the MatchQueryExp on a MBean.

param
name The name of the MBean on which the MatchQueryExp will be applied.
return
True if the query was successfully applied to the MBean, false otherwise.
exception
BadStringOperationException
exception
BadBinaryOpValueExpException
exception
BadAttributeValueExpException
exception
InvalidApplicationException


	ValueExp val = exp.apply(name);	
	if (!(val instanceof StringValueExp)) {
	    return false;
	}	
	return wildmatch(((StringValueExp)val).getValue(), pattern);
    
public javax.management.AttributeValueExpgetAttribute()
Returns the attribute of the query.

 
	return exp;
    
public java.lang.StringgetPattern()
Returns the pattern of the query.

 
	return pattern;
    
private static java.lang.StringlikeTranslate(java.lang.String s)

	return s.replace('?", '_").replace('*", '%");
    
public java.lang.StringtoString()
Returns the string representing the object

 
	return exp + " like " + new StringValueExp(likeTranslate(pattern));
    
private static booleanwildmatch(java.lang.String s, java.lang.String p)

        char c;
        int si = 0, pi = 0;
        int slen = s.length();
        int plen = p.length();

        while (pi < plen) { // While still string
            c = p.charAt(pi++);
            if (c == '?") {
                if (++si > slen)
                    return false;
            } else if (c == '[") { // Start of choice
                if (si >= slen)
                    return false;
                boolean wantit = true;
                boolean seenit = false;
                if (p.charAt(pi) == '!") {
                    wantit = false;
                    ++pi;
                }
                while ((c = p.charAt(pi)) != ']" && ++pi < plen) {
                    if (p.charAt(pi) == '-" &&
                        pi+1 < plen &&
                        p.charAt(pi+1) != ']") {
                        if (s.charAt(si) >= p.charAt(pi-1) &&
                            s.charAt(si) <= p.charAt(pi+1)) {
                            seenit = true;
			}
			++pi;
                    } else {
                        if (c == s.charAt(si)) {
                            seenit = true;
			}
                    }
                }
                if ((pi >= plen) || (wantit != seenit)) {
                    return false;
                }
                ++pi;
                ++si;
            } else if (c == '*") { // Wildcard
                if (pi >= plen)
                    return true;
                do {
                    if (wildmatch(s.substring(si), p.substring(pi)))
                        return true;
                } while (++si < slen);
                return false;
            } else if (c == '\\") {
                if (pi >= plen || si >= slen ||
                    p.charAt(pi++) != s.charAt(si++))
                    return false;
            } else {
                if (si >= slen || c != s.charAt(si++)) {
                    return false;
                }
            }
        }
        return (si == slen);