Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

bugfix: ClassNameWildcard/PathNameWildcard,当以?开头或结尾时,不能正确匹配的问题。

  • Loading branch information...
commit f696d311fd2dd381a69ee791732f18a829be79e8 1 parent b30c736
Michael Zhou authored
31 common/util/src/main/java/com/alibaba/citrus/util/regex/ClassNameWildcardCompiler.java
@@ -61,6 +61,7 @@
61 61 private static final String REGEX_CLASS_NAME = "(" + REGEX_CLASS_NAME_CHAR + "*)";
62 62 private static final String REGEX_CLASS_NAME_FULL = "(" + REGEX_CLASS_NAME_CHAR + "+(?:" + REGEX_DOT_NO_DUP
63 63 + REGEX_CLASS_NAME_CHAR + "*)*(?=" + REGEX_DOT + "|$)|)" + REGEX_DOT + "?";
  64 + private static final String REGEX_END_OF_NAME = "(?=" + REGEX_DOT + "|$)";
64 65
65 66 // 上一个token的状态
66 67 private static final int LAST_TOKEN_START = 0;
@@ -119,12 +120,12 @@ public static String classNameToRegex(String pattern, int options) throws Patter
119 120 pattern = assertNotNull(normalizeClassName(pattern), "pattern");
120 121
121 122 int lastToken = LAST_TOKEN_START;
122   - StringBuilder buffer = new StringBuilder(pattern.length() * 2);
  123 + StringBuilder buf = new StringBuilder(pattern.length() * 2);
123 124
124 125 boolean matchPrefix = (options & MATCH_PREFIX) != 0;
125 126
126 127 if (matchPrefix) {
127   - buffer.append(REGEX_MATCH_PREFIX);
  128 + buf.append(REGEX_MATCH_PREFIX);
128 129 }
129 130
130 131 for (int i = 0; i < pattern.length(); i++) {
@@ -139,7 +140,7 @@ public static String classNameToRegex(String pattern, int options) throws Patter
139 140
140 141 // 因为**已经包括了dot, 所以不需要额外地匹配dot
141 142 if (lastToken != LAST_TOKEN_DOUBLE_STAR) {
142   - buffer.append(REGEX_DOT_NO_DUP);
  143 + buf.append(REGEX_DOT_NO_DUP);
143 144 }
144 145
145 146 lastToken = LAST_TOKEN_DOT;
@@ -157,7 +158,7 @@ public static String classNameToRegex(String pattern, int options) throws Patter
157 158 }
158 159
159 160 lastToken = LAST_TOKEN_DOUBLE_STAR;
160   - buffer.append(REGEX_CLASS_NAME_FULL);
  161 + buf.append(REGEX_CLASS_NAME_FULL);
161 162 } else {
162 163 // *前面不能是*或**
163 164 if (lastToken == LAST_TOKEN_STAR || lastToken == LAST_TOKEN_DOUBLE_STAR) {
@@ -165,14 +166,22 @@ public static String classNameToRegex(String pattern, int options) throws Patter
165 166 }
166 167
167 168 lastToken = LAST_TOKEN_STAR;
168   - buffer.append(REGEX_CLASS_NAME);
  169 + buf.append(REGEX_CLASS_NAME);
169 170 }
170 171
171 172 break;
172 173
173 174 case QUESTION:
  175 + if (lastToken == LAST_TOKEN_START) {
  176 + buf.append(REGEX_WORD_BOUNDARY).append(REGEX_CLASS_NAME_SINGLE_CHAR); // 前边界
  177 + } else if (i + 1 == pattern.length()) {
  178 + buf.append(REGEX_CLASS_NAME_SINGLE_CHAR).append(REGEX_END_OF_NAME); // 后边界
  179 + } else {
  180 + buf.append(REGEX_CLASS_NAME_SINGLE_CHAR);
  181 + }
  182 +
174 183 lastToken = LAST_TOKEN_QUESTION;
175   - buffer.append(REGEX_CLASS_NAME_SINGLE_CHAR);
  184 +
176 185 break;
177 186
178 187 default:
@@ -184,14 +193,14 @@ public static String classNameToRegex(String pattern, int options) throws Patter
184 193 if (Character.isLetterOrDigit(ch) || ch == UNDERSCORE) {
185 194 // 加上word边界, 进行整字匹配
186 195 if (lastToken == LAST_TOKEN_START) {
187   - buffer.append(REGEX_WORD_BOUNDARY).append(ch); // 前边界
  196 + buf.append(REGEX_WORD_BOUNDARY).append(ch); // 前边界
188 197 } else if (i + 1 == pattern.length()) {
189   - buffer.append(ch).append(REGEX_WORD_BOUNDARY); // 后边界
  198 + buf.append(ch).append(REGEX_WORD_BOUNDARY); // 后边界
190 199 } else {
191   - buffer.append(ch);
  200 + buf.append(ch);
192 201 }
193 202 } else if (ch == DOLLAR) {
194   - buffer.append(ESCAPE_CHAR).append(DOLLAR);
  203 + buf.append(ESCAPE_CHAR).append(DOLLAR);
195 204 } else {
196 205 throw new PatternSyntaxException("Syntax Error", pattern, i);
197 206 }
@@ -200,7 +209,7 @@ public static String classNameToRegex(String pattern, int options) throws Patter
200 209 }
201 210 }
202 211
203   - return buffer.toString();
  212 + return buf.toString();
204 213 }
205 214
206 215 /**
11 common/util/src/main/java/com/alibaba/citrus/util/regex/PathNameWildcardCompiler.java
@@ -67,6 +67,7 @@
67 67 private static final String REGEX_FILE_NAME = "(" + REGEX_FILE_NAME_CHAR + "*)";
68 68 private static final String REGEX_FILE_PATH = "(" + REGEX_FILE_NAME_CHAR + "+(?:" + REGEX_SLASH_NO_DUP
69 69 + REGEX_FILE_NAME_CHAR + "*)*(?=" + REGEX_SLASH + "|$)|)" + REGEX_SLASH + "?";
  70 + private static final String REGEX_END_OF_PATH = "(?=" + REGEX_SLASH + "|$)" + REGEX_SLASH + "?";
70 71
71 72 // 上一个token的状态
72 73 private static final int LAST_TOKEN_START = 0;
@@ -191,8 +192,16 @@ public static String pathNameToRegex(String pattern, int options) throws Pattern
191 192 break;
192 193
193 194 case QUESTION:
  195 + if (lastToken == LAST_TOKEN_START) {
  196 + buf.append(REGEX_WORD_BOUNDARY).append(REGEX_FILE_NAME_SINGLE_CHAR); // 前边界
  197 + } else if (i + 1 == pattern.length()) {
  198 + buf.append(REGEX_FILE_NAME_SINGLE_CHAR).append(REGEX_END_OF_PATH); // 后边界
  199 + } else {
  200 + buf.append(REGEX_FILE_NAME_SINGLE_CHAR);
  201 + }
  202 +
194 203 lastToken = LAST_TOKEN_QUESTION;
195   - buf.append(REGEX_FILE_NAME_SINGLE_CHAR);
  204 +
196 205 break;
197 206
198 207 default:
25 common/util/src/test/java/com/alibaba/citrus/util/regex/WildcardCompilerTests.java
@@ -66,6 +66,19 @@ public void pathNameWildcard() throws Exception {
66 66 assertTrue(contains("/xxx/yyy", pattern, ""));
67 67 assertTrue(contains("/xxx/yyyzzz", pattern, "zzz"));
68 68
  69 + // 以?结束
  70 + pattern = PathNameWildcardCompiler.compilePathName("/xxx/yyy?");
  71 +
  72 + assertTrue(contains("/xxx/yyyz", pattern, "z"));
  73 + assertTrue(contains("/xxx/yyyz/", pattern, "z"));
  74 + assertFalse(contains("/xxx/yyyzz", pattern));
  75 +
  76 + // 以?开始
  77 + pattern = PathNameWildcardCompiler.compilePathName("?xxx/yyy");
  78 +
  79 + assertTrue(contains("/zxxx/yyy", pattern, "z"));
  80 + assertFalse(contains("/zzxxx/yyy", pattern));
  81 +
69 82 // 特殊处理
70 83 pattern = PathNameWildcardCompiler.compilePathName("/");
71 84
@@ -126,6 +139,18 @@ public void classNameWildcard() throws Exception {
126 139 assertTrue(contains("xxx.yyy", pattern, ""));
127 140 assertTrue(contains("xxx.yyyzzz", pattern, "zzz"));
128 141
  142 + // 以?结尾
  143 + pattern = ClassNameWildcardCompiler.compileClassName("xxx.yyy?");
  144 +
  145 + assertTrue(contains("xxx.yyyz", pattern, "z"));
  146 + assertFalse(contains("xxx.yyyzz", pattern));
  147 +
  148 + // 以?开始
  149 + pattern = ClassNameWildcardCompiler.compileClassName("?xxx.yyy");
  150 +
  151 + assertTrue(contains("zxxx.yyy", pattern, "z"));
  152 + assertFalse(contains("zzxxx.yyy", pattern));
  153 +
129 154 pattern = ClassNameWildcardCompiler.compileClassName("");
130 155
131 156 assertTrue(contains("", pattern));

0 comments on commit f696d31

Please sign in to comment.
Something went wrong with that request. Please try again.