Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/sjavac/Util.java
38899 views
/*1* Copyright (c) 2012, 2013, 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 com.sun.tools.sjavac;2627import java.io.File;28import java.util.Arrays;29import java.util.HashSet;30import java.util.Set;31import java.util.StringTokenizer;3233/**34* Utilities.35*36* <p><b>This is NOT part of any supported API.37* If you write code that depends on this, you do so at your own38* risk. This code and its internal interfaces are subject to change39* or deletion without notice.</b></p>40*/41public class Util {4243public static String toFileSystemPath(String pkgId) {44if (pkgId == null || pkgId.length()==0) return null;45String pn;46if (pkgId.charAt(0) == ':') {47// When the module is the default empty module.48// Do not prepend the module directory, because there is none.49// Thus :java.foo.bar translates to java/foo/bar (or \)50pn = pkgId.substring(1).replace('.',File.separatorChar);51} else {52// There is a module. Thus jdk.base:java.foo.bar translates53// into jdk.base/java/foo/bar54int cp = pkgId.indexOf(':');55String mn = pkgId.substring(0,cp);56pn = mn+File.separatorChar+pkgId.substring(cp+1).replace('.',File.separatorChar);57}58return pn;59}6061public static String justPackageName(String pkgName) {62int c = pkgName.indexOf(":");63assert(c != -1);64return pkgName.substring(c+1);65}6667public static String extractStringOption(String opName, String s) {68int p = s.indexOf(opName+"=");69if (p == -1) return null;70p+=opName.length()+1;71int pe = s.indexOf(',', p);72if (pe == -1) pe = s.length();73return s.substring(p, pe);74}7576public static int extractIntOption(String opName, String s) {77int p = s.indexOf(opName+"=");78if (p == -1) return 0;79p+=opName.length()+1;80int pe = s.indexOf(',', p);81if (pe == -1) pe = s.length();82int v = 0;83try {84v = Integer.parseInt(s.substring(p, pe));85} catch (Exception e) {}86return v;87}8889/**90* Clean out unwanted sub options supplied inside a primary option.91* For example to only had portfile remaining from:92* settings="--server:id=foo,portfile=bar"93* do settings = cleanOptions("--server:",Util.set("-portfile"),settings);94* now settings equals "--server:portfile=bar"95*96* @param optionPrefix The option name, including colon, eg --server:97* @param allowsSubOptions A set of the allowed sub options, id portfile etc.98* @param s The option settings string.99*/100public static String cleanSubOptions(String optionPrefix, Set<String> allowedSubOptions, String s) {101StringBuilder sb = new StringBuilder();102if (!s.startsWith(optionPrefix)) return "";103StringTokenizer st = new StringTokenizer(s.substring(optionPrefix.length()), ",");104while (st.hasMoreTokens()) {105String o = st.nextToken();106int p = o.indexOf('=');107if (p>0) {108String key = o.substring(0,p);109String val = o.substring(p+1);110if (allowedSubOptions.contains(key)) {111if (sb.length() > 0) sb.append(',');112sb.append(key+"="+val);113}114}115}116return sb.toString();117}118119/**120* Convenience method to create a set with strings.121*/122public static Set<String> set(String... ss) {123Set<String> set = new HashSet<String>();124set.addAll(Arrays.asList(ss));125return set;126}127128/**129* Normalize windows drive letter paths to upper case to enable string130* comparison.131*132* @param file File name to normalize133* @return The normalized string if file has a drive letter at the beginning,134* otherwise the original string.135*/136public static String normalizeDriveLetter(String file) {137if (file.length() > 2 && file.charAt(1) == ':') {138return Character.toUpperCase(file.charAt(0)) + file.substring(1);139} else if (file.length() > 3 && file.charAt(0) == '*'140&& file.charAt(2) == ':') {141// Handle a wildcard * at the beginning of the string.142return file.substring(0, 1) + Character.toUpperCase(file.charAt(1))143+ file.substring(2);144}145return file;146}147148/**149* Locate the setting for the server properties.150*/151public static String findServerSettings(String[] args) {152for (String s : args) {153if (s.startsWith("--server:")) {154return s;155}156}157return null;158}159}160161162