Util.java revision 2593:035b01d356ee
1/* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.tools.sjavac; 27 28import java.io.File; 29import java.nio.file.Path; 30import java.util.Arrays; 31import java.util.HashSet; 32import java.util.Set; 33import java.util.StringTokenizer; 34 35/** 36 * Utilities. 37 * 38 * <p><b>This is NOT part of any supported API. 39 * If you write code that depends on this, you do so at your own risk. 40 * This code and its internal interfaces are subject to change or 41 * deletion without notice.</b> 42 */ 43public class Util { 44 45 public static String toFileSystemPath(String pkgId) { 46 if (pkgId == null || pkgId.length()==0) return null; 47 String pn; 48 if (pkgId.charAt(0) == ':') { 49 // When the module is the default empty module. 50 // Do not prepend the module directory, because there is none. 51 // Thus :java.foo.bar translates to java/foo/bar (or \) 52 pn = pkgId.substring(1).replace('.',File.separatorChar); 53 } else { 54 // There is a module. Thus jdk.base:java.foo.bar translates 55 // into jdk.base/java/foo/bar 56 int cp = pkgId.indexOf(':'); 57 String mn = pkgId.substring(0,cp); 58 pn = mn+File.separatorChar+pkgId.substring(cp+1).replace('.',File.separatorChar); 59 } 60 return pn; 61 } 62 63 public static String justPackageName(String pkgName) { 64 int c = pkgName.indexOf(":"); 65 if (c == -1) 66 throw new IllegalArgumentException("Expected ':' in package name (" + pkgName + ")"); 67 return pkgName.substring(c+1); 68 } 69 70 public static String extractStringOption(String opName, String s) { 71 return extractStringOption(opName, s, null); 72 } 73 74 public static String extractStringOption(String opName, String s, String deflt) { 75 int p = s.indexOf(opName+"="); 76 if (p == -1) return deflt; 77 p+=opName.length()+1; 78 int pe = s.indexOf(',', p); 79 if (pe == -1) pe = s.length(); 80 return s.substring(p, pe); 81 } 82 83 public static boolean extractBooleanOption(String opName, String s, boolean deflt) { 84 String str = extractStringOption(opName, s); 85 return "true".equals(str) ? true 86 : "false".equals(str) ? false 87 : deflt; 88 } 89 90 public static int extractIntOption(String opName, String s) { 91 return extractIntOption(opName, s, 0); 92 } 93 94 public static int extractIntOption(String opName, String s, int deflt) { 95 int p = s.indexOf(opName+"="); 96 if (p == -1) return deflt; 97 p+=opName.length()+1; 98 int pe = s.indexOf(',', p); 99 if (pe == -1) pe = s.length(); 100 int v = 0; 101 try { 102 v = Integer.parseInt(s.substring(p, pe)); 103 } catch (Exception e) {} 104 return v; 105 } 106 107 /** 108 * Clean out unwanted sub options supplied inside a primary option. 109 * For example to only had portfile remaining from: 110 * settings="--server:id=foo,portfile=bar" 111 * do settings = cleanOptions("--server:",Util.set("-portfile"),settings); 112 * now settings equals "--server:portfile=bar" 113 * 114 * @param allowsSubOptions A set of the allowed sub options, id portfile etc. 115 * @param s The option settings string. 116 */ 117 public static String cleanSubOptions(Set<String> allowedSubOptions, String s) { 118 StringBuilder sb = new StringBuilder(); 119 StringTokenizer st = new StringTokenizer(s, ","); 120 while (st.hasMoreTokens()) { 121 String o = st.nextToken(); 122 int p = o.indexOf('='); 123 if (p>0) { 124 String key = o.substring(0,p); 125 String val = o.substring(p+1); 126 if (allowedSubOptions.contains(key)) { 127 if (sb.length() > 0) sb.append(','); 128 sb.append(key+"="+val); 129 } 130 } 131 } 132 return sb.toString(); 133 } 134 135 /** 136 * Convenience method to create a set with strings. 137 */ 138 public static Set<String> set(String... ss) { 139 Set<String> set = new HashSet<>(); 140 set.addAll(Arrays.asList(ss)); 141 return set; 142 } 143 144 /** 145 * Normalize windows drive letter paths to upper case to enable string 146 * comparison. 147 * 148 * @param file File name to normalize 149 * @return The normalized string if file has a drive letter at the beginning, 150 * otherwise the original string. 151 */ 152 public static String normalizeDriveLetter(String file) { 153 if (file.length() > 2 && file.charAt(1) == ':') { 154 return Character.toUpperCase(file.charAt(0)) + file.substring(1); 155 } else if (file.length() > 3 && file.charAt(0) == '*' 156 && file.charAt(2) == ':') { 157 // Handle a wildcard * at the beginning of the string. 158 return file.substring(0, 1) + Character.toUpperCase(file.charAt(1)) 159 + file.substring(2); 160 } 161 return file; 162 } 163 164 /** 165 * Locate the setting for the server properties. 166 */ 167 public static String findServerSettings(String[] args) { 168 for (String s : args) { 169 if (s.startsWith("--server:")) { 170 return s; 171 } 172 } 173 return null; 174 } 175 176 // TODO: Remove when refactoring from java.io.File to java.nio.file.Path. 177 public static File pathToFile(Path path) { 178 return path == null ? null : path.toFile(); 179 } 180} 181