Path: blob/master/src/java.base/share/classes/jdk/internal/jrtfs/JrtUtils.java
41159 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.jrtfs;2627import java.util.regex.PatternSyntaxException;2829/**30* @implNote This class needs to maintain JDK 8 source compatibility.31*32* It is used internally in the JDK to implement jimage/jrtfs access,33* but also compiled and delivered as part of the jrtfs.jar to support access34* to the jimage file provided by the shipped JDK by tools running on JDK 8.35*/36final class JrtUtils {37private JrtUtils() {}3839private static final String regexMetaChars = ".^$+{[]|()";40private static final String globMetaChars = "\\*?[{";41private static boolean isRegexMeta(char c) {42return regexMetaChars.indexOf(c) != -1;43}44private static boolean isGlobMeta(char c) {45return globMetaChars.indexOf(c) != -1;46}47private static final char EOL = 0;48private static char next(String glob, int i) {49if (i < glob.length()) {50return glob.charAt(i);51}52return EOL;53}5455/*56* Creates a regex pattern from the given glob expression.57*58* @throws PatternSyntaxException59*/60public static String toRegexPattern(String globPattern) {61boolean inGroup = false;62StringBuilder regex = new StringBuilder("^");6364int i = 0;65while (i < globPattern.length()) {66char c = globPattern.charAt(i++);67switch (c) {68case '\\':69// escape special characters70if (i == globPattern.length()) {71throw new PatternSyntaxException("No character to escape",72globPattern, i - 1);73}74char next = globPattern.charAt(i++);75if (isGlobMeta(next) || isRegexMeta(next)) {76regex.append('\\');77}78regex.append(next);79break;80case '/':81regex.append(c);82break;83case '[':84// don't match name separator in class85regex.append("[[^/]&&[");86if (next(globPattern, i) == '^') {87// escape the regex negation char if it appears88regex.append("\\^");89i++;90} else {91// negation92if (next(globPattern, i) == '!') {93regex.append('^');94i++;95}96// hyphen allowed at start97if (next(globPattern, i) == '-') {98regex.append('-');99i++;100}101}102boolean hasRangeStart = false;103char last = 0;104while (i < globPattern.length()) {105c = globPattern.charAt(i++);106if (c == ']') {107break;108}109if (c == '/') {110throw new PatternSyntaxException("Explicit 'name separator' in class",111globPattern, i - 1);112}113// TBD: how to specify ']' in a class?114if (c == '\\' || c == '[' ||115c == '&' && next(globPattern, i) == '&') {116// escape '\', '[' or "&&" for regex class117regex.append('\\');118}119regex.append(c);120121if (c == '-') {122if (!hasRangeStart) {123throw new PatternSyntaxException("Invalid range",124globPattern, i - 1);125}126if ((c = next(globPattern, i++)) == EOL || c == ']') {127break;128}129if (c < last) {130throw new PatternSyntaxException("Invalid range",131globPattern, i - 3);132}133regex.append(c);134hasRangeStart = false;135} else {136hasRangeStart = true;137last = c;138}139}140if (c != ']') {141throw new PatternSyntaxException("Missing ']", globPattern, i - 1);142}143regex.append("]]");144break;145case '{':146if (inGroup) {147throw new PatternSyntaxException("Cannot nest groups",148globPattern, i - 1);149}150regex.append("(?:(?:");151inGroup = true;152break;153case '}':154if (inGroup) {155regex.append("))");156inGroup = false;157} else {158regex.append('}');159}160break;161case ',':162if (inGroup) {163regex.append(")|(?:");164} else {165regex.append(',');166}167break;168case '*':169if (next(globPattern, i) == '*') {170// crosses directory boundaries171regex.append(".*");172i++;173} else {174// within directory boundary175regex.append("[^/]*");176}177break;178case '?':179regex.append("[^/]");180break;181default:182if (isRegexMeta(c)) {183regex.append('\\');184}185regex.append(c);186}187}188if (inGroup) {189throw new PatternSyntaxException("Missing '}", globPattern, i - 1);190}191return regex.append('$').toString();192}193}194195196