src/share/classes/com/sun/tools/sjavac/Util.java

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

mercurial