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