@NotNull private static String stripPath(@NotNull String path) { String[] endingsToStrip = {"/", "!", ".jar"}; StringBuilder buffer = new StringBuilder(path); for (String ending : endingsToStrip) { if (buffer.lastIndexOf(ending) == buffer.length() - ending.length()) { buffer.setLength(buffer.length() - ending.length()); } } return buffer.toString(); }
/** * @param antPattern ant-style path pattern * @return java regexp pattern. Note that no matter whether forward or backward slashes were used * in the antPattern the returned regexp pattern will use forward slashes ('/') as file * separators. Paths containing windows-style backslashes must be converted before matching * against the resulting regexp * @see com.intellij.openapi.util.io.FileUtil#toSystemIndependentName */ @RegExp @NotNull public static String convertAntToRegexp(@NotNull String antPattern, boolean ignoreStartingSlash) { final StringBuilder builder = new StringBuilder(); int asteriskCount = 0; boolean recursive = true; final int start = ignoreStartingSlash && (StringUtil.startsWithChar(antPattern, '/') || StringUtil.startsWithChar(antPattern, '\\')) ? 1 : 0; for (int idx = start; idx < antPattern.length(); idx++) { final char ch = antPattern.charAt(idx); if (ch == '*') { asteriskCount++; continue; } final boolean foundRecursivePattern = recursive && asteriskCount == 2 && (ch == '/' || ch == '\\'); final boolean asterisksFound = asteriskCount > 0; asteriskCount = 0; recursive = ch == '/' || ch == '\\'; if (foundRecursivePattern) { builder.append("(?:[^/]+/)*?"); continue; } if (asterisksFound) { builder.append("[^/]*?"); } if (ch == '(' || ch == ')' || ch == '[' || ch == ']' || ch == '^' || ch == '$' || ch == '.' || ch == '{' || ch == '}' || ch == '+' || ch == '|') { // quote regexp-specific symbols builder.append('\\').append(ch); continue; } if (ch == '?') { builder.append("[^/]{1}"); continue; } if (ch == '\\') { builder.append('/'); continue; } builder.append(ch); } // handle ant shorthand: mypackage/test/ is interpreted as if it were mypackage/test/** final boolean isTrailingSlash = builder.length() > 0 && builder.charAt(builder.length() - 1) == '/'; if (asteriskCount == 0 && isTrailingSlash || recursive && asteriskCount == 2) { if (isTrailingSlash) { builder.setLength(builder.length() - 1); } if (builder.length() == 0) { builder.append(".*"); } else { builder.append("(?:$|/.+)"); } } else if (asteriskCount > 0) { builder.append("[^/]*?"); } return builder.toString(); }