1 7 8 package javax.management; 9 10 11 12 19 class MatchQueryExp extends QueryEval implements QueryExp { 20 21 22 private static final long serialVersionUID = -7156603696948215014L; 23 24 27 private AttributeValueExp exp; 28 29 32 private String pattern; 33 34 35 38 public MatchQueryExp() { 39 } 40 41 45 public MatchQueryExp(AttributeValueExp a, StringValueExp s) { 46 exp = a; 47 pattern = s.getValue(); 48 } 49 50 51 54 public AttributeValueExp getAttribute() { 55 return exp; 56 } 57 58 61 public String getPattern() { 62 return pattern; 63 } 64 65 77 public boolean apply(ObjectName name) throws BadStringOperationException , BadBinaryOpValueExpException , 78 BadAttributeValueExpException , InvalidApplicationException { 79 80 ValueExp val = exp.apply(name); 81 if (!(val instanceof StringValueExp )) { 82 return false; 83 } 84 return wildmatch(((StringValueExp )val).getValue(), pattern); 85 } 86 87 90 public String toString() { 91 return exp + " like " + new StringValueExp (likeTranslate(pattern)); 92 } 93 94 private static String likeTranslate(String s) { 95 return s.replace('?', '_').replace('*', '%'); 96 } 97 98 105 private static boolean wildmatch(String s, String 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) { c = p.charAt(pi++); 113 if (c == '?') { 114 if (++si > slen) 115 return false; 116 } else if (c == '[') { 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 == '*') { 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 |