Showing the changes to java/pkg/javax/management/MatchQueryExp.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,182 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1999, 2008, Oracle and/or its affiliates. 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. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * 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(pattern); | ||
| 113 | } | ||
| 114 | |||
| 115 | /* | ||
| 116 | * Tests whether string s is matched by pattern p. | ||
| 117 | * Supports "?", "*", "[", each of which may be escaped with "\"; | ||
| 118 | * character classes may use "!" for negation and "-" for range. | ||
| 119 | * Not yet supported: internationalization; "\" inside brackets.<P> | ||
| 120 | * Wildcard matching routine by Karl Heuer. Public Domain.<P> | ||
| 121 | */ | ||
| 122 | private static boolean wildmatch(String s, String p) { | ||
| 123 | char c; | ||
| 124 | int si = 0, pi = 0; | ||
| 125 | int slen = s.length(); | ||
| 126 | int plen = p.length(); | ||
| 127 | |||
| 128 | while (pi < plen) { // While still string | ||
| 129 | c = p.charAt(pi++); | ||
| 130 | if (c == '?') { | ||
| 131 | if (++si > slen) | ||
| 132 | return false; | ||
| 133 | } else if (c == '[') { // Start of choice | ||
| 134 | if (si >= slen) | ||
| 135 | return false; | ||
| 136 | boolean wantit = true; | ||
| 137 | boolean seenit = false; | ||
| 138 | if (p.charAt(pi) == '!') { | ||
| 139 | wantit = false; | ||
| 140 | ++pi; | ||
| 141 | } | ||
| 142 | while ((c = p.charAt(pi)) != ']' && ++pi < plen) { | ||
| 143 | if (p.charAt(pi) == '-' && | ||
| 144 | pi+1 < plen && | ||
| 145 | p.charAt(pi+1) != ']') { | ||
| 146 | if (s.charAt(si) >= p.charAt(pi-1) && | ||
| 147 | s.charAt(si) <= p.charAt(pi+1)) { | ||
| 148 | seenit = true; | ||
| 149 | } | ||
| 150 | ++pi; | ||
| 151 | } else { | ||
| 152 | if (c == s.charAt(si)) { | ||
| 153 | seenit = true; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | if ((pi >= plen) || (wantit != seenit)) { | ||
| 158 | return false; | ||
| 159 | } | ||
| 160 | ++pi; | ||
| 161 | ++si; | ||
| 162 | } else if (c == '*') { // Wildcard | ||
| 163 | if (pi >= plen) | ||
| 164 | return true; | ||
| 165 | do { | ||
| 166 | if (wildmatch(s.substring(si), p.substring(pi))) | ||
| 167 | return true; | ||
| 168 | } while (++si < slen); | ||
| 169 | return false; | ||
| 170 | } else if (c == '\\') { | ||
| 171 | if (pi >= plen || si >= slen || | ||
| 172 | p.charAt(pi++) != s.charAt(si++)) | ||
| 173 | return false; | ||
| 174 | } else { | ||
| 175 | if (si >= slen || c != s.charAt(si++)) { | ||
| 176 | return false; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | } | ||
| 180 | return (si == slen); | ||
| 181 | } | ||
| 182 | } | ||