ohrstrom@1504: /* ohrstrom@1504: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. ohrstrom@1504: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohrstrom@1504: * ohrstrom@1504: * This code is free software; you can redistribute it and/or modify it ohrstrom@1504: * under the terms of the GNU General Public License version 2 only, as ohrstrom@1504: * published by the Free Software Foundation. Oracle designates this ohrstrom@1504: * particular file as subject to the "Classpath" exception as provided ohrstrom@1504: * by Oracle in the LICENSE file that accompanied this code. ohrstrom@1504: * ohrstrom@1504: * This code is distributed in the hope that it will be useful, but WITHOUT ohrstrom@1504: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohrstrom@1504: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohrstrom@1504: * version 2 for more details (a copy is included in the LICENSE file that ohrstrom@1504: * accompanied this code). ohrstrom@1504: * ohrstrom@1504: * You should have received a copy of the GNU General Public License version ohrstrom@1504: * 2 along with this work; if not, write to the Free Software Foundation, ohrstrom@1504: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohrstrom@1504: * ohrstrom@1504: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohrstrom@1504: * or visit www.oracle.com if you need additional information or have any ohrstrom@1504: * questions. ohrstrom@1504: */ ohrstrom@1504: ohrstrom@1504: package com.sun.tools.sjavac; ohrstrom@1504: ohrstrom@1504: import java.io.File; ohrstrom@1504: import java.util.Arrays; ohrstrom@1504: import java.util.HashSet; ohrstrom@1504: import java.util.Set; ohrstrom@1504: import java.util.StringTokenizer; ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Utilities. ohrstrom@1504: * ohrstrom@1504: *

This is NOT part of any supported API. ohrstrom@1504: * If you write code that depends on this, you do so at your own ohrstrom@1504: * risk. This code and its internal interfaces are subject to change ohrstrom@1504: * or deletion without notice.

ohrstrom@1504: */ ohrstrom@1504: public class Util { ohrstrom@1504: ohrstrom@1504: public static String toFileSystemPath(String pkgId) { ohrstrom@1504: if (pkgId == null || pkgId.length()==0) return null; ohrstrom@1504: String pn; ohrstrom@1504: if (pkgId.charAt(0) == ':') { ohrstrom@1504: // When the module is the default empty module. ohrstrom@1504: // Do not prepend the module directory, because there is none. ohrstrom@1504: // Thus :java.foo.bar translates to java/foo/bar (or \) ohrstrom@1504: pn = pkgId.substring(1).replace('.',File.separatorChar); ohrstrom@1504: } else { ohrstrom@1504: // There is a module. Thus jdk.base:java.foo.bar translates ohrstrom@1504: // into jdk.base/java/foo/bar ohrstrom@1504: int cp = pkgId.indexOf(':'); ohrstrom@1504: String mn = pkgId.substring(0,cp); ohrstrom@1504: pn = mn+File.separatorChar+pkgId.substring(cp+1).replace('.',File.separatorChar); ohrstrom@1504: } ohrstrom@1504: return pn; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public static String justPackageName(String pkgName) { ohrstrom@1504: int c = pkgName.indexOf(":"); ohrstrom@1504: assert(c != -1); ohrstrom@1504: return pkgName.substring(c+1); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public static String extractStringOption(String opName, String s) { ohrstrom@1504: int p = s.indexOf(opName+"="); ohrstrom@1504: if (p == -1) return null; ohrstrom@1504: p+=opName.length()+1; ohrstrom@1504: int pe = s.indexOf(',', p); ohrstrom@1504: if (pe == -1) pe = s.length(); ohrstrom@1504: return s.substring(p, pe); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public static int extractIntOption(String opName, String s) { ohrstrom@1504: int p = s.indexOf(opName+"="); ohrstrom@1504: if (p == -1) return 0; ohrstrom@1504: p+=opName.length()+1; ohrstrom@1504: int pe = s.indexOf(',', p); ohrstrom@1504: if (pe == -1) pe = s.length(); ohrstrom@1504: int v = 0; ohrstrom@1504: try { ohrstrom@1504: v = Integer.parseInt(s.substring(p, pe)); ohrstrom@1504: } catch (Exception e) {} ohrstrom@1504: return v; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Clean out unwanted sub options supplied inside a primary option. ohrstrom@1504: * For example to only had portfile remaining from: ohrstrom@1504: * settings="--server:id=foo,portfile=bar" ohrstrom@1504: * do settings = cleanOptions("--server:",Util.set("-portfile"),settings); ohrstrom@1504: * now settings equals "--server:portfile=bar" ohrstrom@1504: * ohrstrom@1504: * @param optionPrefix The option name, including colon, eg --server: ohrstrom@1504: * @param allowsSubOptions A set of the allowed sub options, id portfile etc. ohrstrom@1504: * @param s The option settings string. ohrstrom@1504: */ ohrstrom@1504: public static String cleanSubOptions(String optionPrefix, Set allowedSubOptions, String s) { ohrstrom@1504: StringBuilder sb = new StringBuilder(); ohrstrom@1504: if (!s.startsWith(optionPrefix)) return ""; ohrstrom@1504: StringTokenizer st = new StringTokenizer(s.substring(optionPrefix.length()), ","); ohrstrom@1504: while (st.hasMoreTokens()) { ohrstrom@1504: String o = st.nextToken(); ohrstrom@1504: int p = o.indexOf('='); ohrstrom@1504: if (p>0) { ohrstrom@1504: String key = o.substring(0,p); ohrstrom@1504: String val = o.substring(p+1); ohrstrom@1504: if (allowedSubOptions.contains(key)) { ohrstrom@1504: if (sb.length() > 0) sb.append(','); ohrstrom@1504: sb.append(key+"="+val); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: return sb.toString(); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Convenience method to create a set with strings. ohrstrom@1504: */ ohrstrom@1504: public static Set set(String... ss) { ohrstrom@1504: Set set = new HashSet(); ohrstrom@1504: set.addAll(Arrays.asList(ss)); ohrstrom@1504: return set; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Normalize windows drive letter paths to upper case to enable string ohrstrom@1504: * comparison. ohrstrom@1504: * ohrstrom@1504: * @param file File name to normalize ohrstrom@1504: * @return The normalized string if file has a drive letter at the beginning, ohrstrom@1504: * otherwise the original string. ohrstrom@1504: */ ohrstrom@1504: public static String normalizeDriveLetter(String file) { ohrstrom@1504: if (file.length() > 2 && file.charAt(1) == ':') { ohrstrom@1504: return Character.toUpperCase(file.charAt(0)) + file.substring(1); ohrstrom@1504: } else if (file.length() > 3 && file.charAt(0) == '*' ohrstrom@1504: && file.charAt(2) == ':') { ohrstrom@1504: // Handle a wildcard * at the beginning of the string. ohrstrom@1504: return file.substring(0, 1) + Character.toUpperCase(file.charAt(1)) ohrstrom@1504: + file.substring(2); ohrstrom@1504: } ohrstrom@1504: return file; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * Locate the setting for the server properties. ohrstrom@1504: */ ohrstrom@1504: public static String findServerSettings(String[] args) { ohrstrom@1504: for (String s : args) { ohrstrom@1504: if (s.startsWith("--server:")) { ohrstrom@1504: return s; ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: return null; ohrstrom@1504: } ohrstrom@1504: }