--- /dev/null
+/*\r
+ * Copyright 2002-2005 the original author or authors.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.badlogic.gdx.jnigen;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.StringTokenizer;\r
+\r
+/**\r
+ * PathMatcher implementation for Ant-style path patterns. Examples are provided\r
+ * below.\r
+ * \r
+ * <p>\r
+ * Part of this mapping code has been kindly borrowed from <a\r
+ * href="http://ant.apache.org">Apache Ant</a>.\r
+ * \r
+ * <p>\r
+ * The mapping matches URLs using the following rules:<br>\r
+ * <ul>\r
+ * <li>? matches one character</li>\r
+ * <li>* matches zero or more characters</li>\r
+ * <li>** matches zero or more 'directories' in a path</li>\r
+ * </ul>\r
+ * \r
+ * <p>\r
+ * Some examples:<br>\r
+ * <ul>\r
+ * <li>com/t?st.jsp - matches test.jsp but also tast.jsp or txst.jsp</li>\r
+ * <li>com/*.jsp - matches all .jsp files in the com directory</li>\r
+ * <li>com/**/test.jsp - matches all test.jsp path underneath the com\r
+ * path</li>\r
+ * <li>org/springframework/**/*.jsp - matches all .jsp files underneath\r
+ * the org/springframework path</li>\r
+ * <li>org/**/servlet/bla.jsp - matches\r
+ * org/springframework/servlet/bla.jsp but also\r
+ * org/springframework/testing/servlet/bla.jsp and com/servlet/bla.jsp</li>\r
+ * </ul>\r
+ * \r
+ * @author Alef Arendsen\r
+ * @author Juergen Hoeller\r
+ * @since 16.07.2003\r
+ */\r
+public class AntPathMatcher {\r
+\r
+ public boolean isPattern(String str) {\r
+ return (str.indexOf('*') != -1 || str.indexOf('?') != -1);\r
+ }\r
+\r
+ public static String[] tokenizeToStringArray(String str, String delimiters,\r
+ boolean trimTokens, boolean ignoreEmptyTokens) {\r
+ if (str == null) {\r
+ return null;\r
+ }\r
+ StringTokenizer st = new StringTokenizer(str, delimiters);\r
+ List<String> tokens = new ArrayList<String>();\r
+ while (st.hasMoreTokens()) {\r
+ String token = st.nextToken();\r
+ if (trimTokens) {\r
+ token = token.trim();\r
+ }\r
+ if (!ignoreEmptyTokens || token.length() > 0) {\r
+ tokens.add(token);\r
+ }\r
+ }\r
+ return tokens.toArray(new String[tokens.size()]);\r
+ }\r
+\r
+ public boolean match(String pattern, String str) {\r
+ if (str.startsWith("/") != pattern.startsWith("/")) {\r
+ return false;\r
+ }\r
+\r
+ String[] patDirs = tokenizeToStringArray(pattern, "/", true, true);\r
+ String[] strDirs = tokenizeToStringArray(str, "/", true, true);\r
+\r
+ int patIdxStart = 0;\r
+ int patIdxEnd = patDirs.length - 1;\r
+ int strIdxStart = 0;\r
+ int strIdxEnd = strDirs.length - 1;\r
+\r
+ // Match all elements up to the first **\r
+ while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) {\r
+ String patDir = (String) patDirs[patIdxStart];\r
+ if (patDir.equals("**")) {\r
+ break;\r
+ }\r
+ if (!matchStrings(patDir, (String) strDirs[strIdxStart])) {\r
+ return false;\r
+ }\r
+ patIdxStart++;\r
+ strIdxStart++;\r
+ }\r
+\r
+ if (strIdxStart > strIdxEnd) {\r
+ // String is exhausted, only match if rest of pattern is **'s\r
+ for (int i = patIdxStart; i <= patIdxEnd; i++) {\r
+ if (!patDirs[i].equals("**")) {\r
+ return false;\r
+ }\r
+ }\r
+ return true;\r
+ } else {\r
+ if (patIdxStart > patIdxEnd) {\r
+ // String not exhausted, but pattern is. Failure.\r
+ return false;\r
+ }\r
+ }\r
+\r
+ // up to last '**'\r
+ while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) {\r
+ String patDir = (String) patDirs[patIdxEnd];\r
+ if (patDir.equals("**")) {\r
+ break;\r
+ }\r
+ if (!matchStrings(patDir, (String) strDirs[strIdxEnd])) {\r
+ return false;\r
+ }\r
+ patIdxEnd--;\r
+ strIdxEnd--;\r
+ }\r
+ if (strIdxStart > strIdxEnd) {\r
+ // String is exhausted\r
+ for (int i = patIdxStart; i <= patIdxEnd; i++) {\r
+ if (!patDirs[i].equals("**")) {\r
+ return false;\r
+ }\r
+ }\r
+ return true;\r
+ }\r
+\r
+ while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) {\r
+ int patIdxTmp = -1;\r
+ for (int i = patIdxStart + 1; i <= patIdxEnd; i++) {\r
+ if (patDirs[i].equals("**")) {\r
+ patIdxTmp = i;\r
+ break;\r
+ }\r
+ }\r
+ if (patIdxTmp == patIdxStart + 1) {\r
+ // '**/**' situation, so skip one\r
+ patIdxStart++;\r
+ continue;\r
+ }\r
+ // Find the pattern between padIdxStart & padIdxTmp in str between\r
+ // strIdxStart & strIdxEnd\r
+ int patLength = (patIdxTmp - patIdxStart - 1);\r
+ int strLength = (strIdxEnd - strIdxStart + 1);\r
+ int foundIdx = -1;\r
+ strLoop: for (int i = 0; i <= strLength - patLength; i++) {\r
+ for (int j = 0; j < patLength; j++) {\r
+ String subPat = (String) patDirs[patIdxStart + j + 1];\r
+ String subStr = (String) strDirs[strIdxStart + i + j];\r
+ if (!matchStrings(subPat, subStr)) {\r
+ continue strLoop;\r
+ }\r
+ }\r
+\r
+ foundIdx = strIdxStart + i;\r
+ break;\r
+ }\r
+\r
+ if (foundIdx == -1) {\r
+ return false;\r
+ }\r
+\r
+ patIdxStart = patIdxTmp;\r
+ strIdxStart = foundIdx + patLength;\r
+ }\r
+\r
+ for (int i = patIdxStart; i <= patIdxEnd; i++) {\r
+ if (!patDirs[i].equals("**")) {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * Tests whether or not a string matches against a pattern. The pattern may\r
+ * contain two special characters:<br>\r
+ * '*' means zero or more characters<br>\r
+ * '?' means one and only one character\r
+ * \r
+ * @param pattern\r
+ * pattern to match against. Must not be <code>null</code>.\r
+ * @param str\r
+ * string which must be matched against the pattern. Must not be\r
+ * <code>null</code>.\r
+ * @return <code>true</code> if the string matches against the pattern, or\r
+ * <code>false</code> otherwise.\r
+ */\r
+ private boolean matchStrings(String pattern, String str) {\r
+ char[] patArr = pattern.toCharArray();\r
+ char[] strArr = str.toCharArray();\r
+ int patIdxStart = 0;\r
+ int patIdxEnd = patArr.length - 1;\r
+ int strIdxStart = 0;\r
+ int strIdxEnd = strArr.length - 1;\r
+ char ch;\r
+\r
+ boolean containsStar = false;\r
+ for (int i = 0; i < patArr.length; i++) {\r
+ if (patArr[i] == '*') {\r
+ containsStar = true;\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (!containsStar) {\r
+ // No '*'s, so we make a shortcut\r
+ if (patIdxEnd != strIdxEnd) {\r
+ return false; // Pattern and string do not have the same size\r
+ }\r
+ for (int i = 0; i <= patIdxEnd; i++) {\r
+ ch = patArr[i];\r
+ if (ch != '?') {\r
+ if (ch != strArr[i]) {\r
+ return false;// Character mismatch\r
+ }\r
+ }\r
+ }\r
+ return true; // String matches against pattern\r
+ }\r
+\r
+ if (patIdxEnd == 0) {\r
+ return true; // Pattern contains only '*', which matches anything\r
+ }\r
+\r
+ // Process characters before first star\r
+ while ((ch = patArr[patIdxStart]) != '*' && strIdxStart <= strIdxEnd) {\r
+ if (ch != '?') {\r
+ if (ch != strArr[strIdxStart]) {\r
+ return false;// Character mismatch\r
+ }\r
+ }\r
+ patIdxStart++;\r
+ strIdxStart++;\r
+ }\r
+ if (strIdxStart > strIdxEnd) {\r
+ // All characters in the string are used. Check if only '*'s are\r
+ // left in the pattern. If so, we succeeded. Otherwise failure.\r
+ for (int i = patIdxStart; i <= patIdxEnd; i++) {\r
+ if (patArr[i] != '*') {\r
+ return false;\r
+ }\r
+ }\r
+ return true;\r
+ }\r
+\r
+ // Process characters after last star\r
+ while ((ch = patArr[patIdxEnd]) != '*' && strIdxStart <= strIdxEnd) {\r
+ if (ch != '?') {\r
+ if (ch != strArr[strIdxEnd]) {\r
+ return false;// Character mismatch\r
+ }\r
+ }\r
+ patIdxEnd--;\r
+ strIdxEnd--;\r
+ }\r
+ if (strIdxStart > strIdxEnd) {\r
+ // All characters in the string are used. Check if only '*'s are\r
+ // left in the pattern. If so, we succeeded. Otherwise failure.\r
+ for (int i = patIdxStart; i <= patIdxEnd; i++) {\r
+ if (patArr[i] != '*') {\r
+ return false;\r
+ }\r
+ }\r
+ return true;\r
+ }\r
+\r
+ // process pattern between stars. padIdxStart and patIdxEnd point\r
+ // always to a '*'.\r
+ while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) {\r
+ int patIdxTmp = -1;\r
+ for (int i = patIdxStart + 1; i <= patIdxEnd; i++) {\r
+ if (patArr[i] == '*') {\r
+ patIdxTmp = i;\r
+ break;\r
+ }\r
+ }\r
+ if (patIdxTmp == patIdxStart + 1) {\r
+ // Two stars next to each other, skip the first one.\r
+ patIdxStart++;\r
+ continue;\r
+ }\r
+ // Find the pattern between padIdxStart & padIdxTmp in str between\r
+ // strIdxStart & strIdxEnd\r
+ int patLength = (patIdxTmp - patIdxStart - 1);\r
+ int strLength = (strIdxEnd - strIdxStart + 1);\r
+ int foundIdx = -1;\r
+ strLoop: for (int i = 0; i <= strLength - patLength; i++) {\r
+ for (int j = 0; j < patLength; j++) {\r
+ ch = patArr[patIdxStart + j + 1];\r
+ if (ch != '?') {\r
+ if (ch != strArr[strIdxStart + i + j]) {\r
+ continue strLoop;\r
+ }\r
+ }\r
+ }\r
+\r
+ foundIdx = strIdxStart + i;\r
+ break;\r
+ }\r
+\r
+ if (foundIdx == -1) {\r
+ return false;\r
+ }\r
+\r
+ patIdxStart = patIdxTmp;\r
+ strIdxStart = foundIdx + patLength;\r
+ }\r
+\r
+ // All characters in the string are used. Check if only '*'s are left\r
+ // in the pattern. If so, we succeeded. Otherwise failure.\r
+ for (int i = patIdxStart; i <= patIdxEnd; i++) {\r
+ if (patArr[i] != '*') {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ return true;\r
+ }\r
+\r
+}
\ No newline at end of file