Util.java revision 2656:a0125e2a10e8
150477Speter/*
233548Sjkh * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
32893Sdfr * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
42893Sdfr *
533548Sjkh * This code is free software; you can redistribute it and/or modify it
633548Sjkh * under the terms of the GNU General Public License version 2 only, as
72893Sdfr * published by the Free Software Foundation.  Oracle designates this
82893Sdfr * particular file as subject to the "Classpath" exception as provided
92893Sdfr * by Oracle in the LICENSE file that accompanied this code.
102893Sdfr *
112893Sdfr * This code is distributed in the hope that it will be useful, but WITHOUT
122893Sdfr * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
132893Sdfr * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
142893Sdfr * version 2 for more details (a copy is included in the LICENSE file that
152893Sdfr * accompanied this code).
162893Sdfr *
172893Sdfr * You should have received a copy of the GNU General Public License version
182893Sdfr * 2 along with this work; if not, write to the Free Software Foundation,
192893Sdfr * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
202893Sdfr *
212893Sdfr * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
222893Sdfr * or visit www.oracle.com if you need additional information or have any
232893Sdfr * questions.
242893Sdfr */
252893Sdfr
262893Sdfrpackage com.sun.tools.sjavac;
272893Sdfr
282893Sdfrimport java.io.File;
292893Sdfrimport java.io.PrintWriter;
302893Sdfrimport java.io.StringWriter;
312893Sdfrimport java.nio.file.Path;
322893Sdfrimport java.util.Arrays;
332893Sdfrimport java.util.HashSet;
342893Sdfrimport java.util.Set;
352893Sdfrimport java.util.StringTokenizer;
362893Sdfr
378876Srgrimes/**
382893Sdfr * Utilities.
392893Sdfr *
408876Srgrimes *  <p><b>This is NOT part of any supported API.
412893Sdfr *  If you write code that depends on this, you do so at your own risk.
428876Srgrimes *  This code and its internal interfaces are subject to change or
432893Sdfr *  deletion without notice.</b>
442893Sdfr */
452893Sdfrpublic class Util {
462893Sdfr
478876Srgrimes    public static String toFileSystemPath(String pkgId) {
482893Sdfr        if (pkgId == null || pkgId.length()==0) return null;
492893Sdfr        String pn;
502893Sdfr        if (pkgId.charAt(0) == ':') {
512893Sdfr            // When the module is the default empty module.
522893Sdfr            // Do not prepend the module directory, because there is none.
532893Sdfr            // Thus :java.foo.bar translates to java/foo/bar (or \)
542893Sdfr            pn = pkgId.substring(1).replace('.',File.separatorChar);
5533548Sjkh        } else {
5633548Sjkh            // There is a module. Thus jdk.base:java.foo.bar translates
5733548Sjkh            // into jdk.base/java/foo/bar
5833548Sjkh            int cp = pkgId.indexOf(':');
5933548Sjkh            String mn = pkgId.substring(0,cp);
6033548Sjkh            pn = mn+File.separatorChar+pkgId.substring(cp+1).replace('.',File.separatorChar);
6133548Sjkh        }
6233548Sjkh        return pn;
6333548Sjkh    }
6433548Sjkh
6533548Sjkh    public static String justPackageName(String pkgName) {
6633548Sjkh        int c = pkgName.indexOf(":");
6733548Sjkh        if (c == -1)
6841275Sdt            throw new IllegalArgumentException("Expected ':' in package name (" + pkgName + ")");
6941275Sdt        return pkgName.substring(c+1);
7041275Sdt    }
7133548Sjkh
7233548Sjkh    public static String extractStringOption(String opName, String s) {
7333548Sjkh        return extractStringOption(opName, s, null);
7433548Sjkh    }
7533548Sjkh
7633548Sjkh    public static String extractStringOption(String opName, String s, String deflt) {
7733548Sjkh        int p = s.indexOf(opName+"=");
7833548Sjkh        if (p == -1) return deflt;
7933548Sjkh        p+=opName.length()+1;
802893Sdfr        int pe = s.indexOf(',', p);
812893Sdfr        if (pe == -1) pe = s.length();
822893Sdfr        return s.substring(p, pe);
8333548Sjkh    }
8433548Sjkh
8533548Sjkh    public static boolean extractBooleanOption(String opName, String s, boolean deflt) {
8633548Sjkh       String str = extractStringOption(opName, s);
8733548Sjkh        return "true".equals(str) ? true
8833548Sjkh             : "false".equals(str) ? false
8933548Sjkh             : deflt;
9033548Sjkh    }
9133548Sjkh
9233548Sjkh    public static int extractIntOption(String opName, String s) {
9333548Sjkh        return extractIntOption(opName, s, 0);
9433548Sjkh    }
9533548Sjkh
9633548Sjkh    public static int extractIntOption(String opName, String s, int deflt) {
9733548Sjkh        int p = s.indexOf(opName+"=");
9833548Sjkh        if (p == -1) return deflt;
9933548Sjkh        p+=opName.length()+1;
10033548Sjkh        int pe = s.indexOf(',', p);
101120498Sbde        if (pe == -1) pe = s.length();
102120492Sfjoe        int v = 0;
103120498Sbde        try {
104120492Sfjoe            v = Integer.parseInt(s.substring(p, pe));
105120492Sfjoe        } catch (Exception e) {}
10633548Sjkh        return v;
10733548Sjkh    }
10833548Sjkh
10933548Sjkh    /**
11033548Sjkh     * Clean out unwanted sub options supplied inside a primary option.
11133548Sjkh     * For example to only had portfile remaining from:
1122893Sdfr     *    settings="--server:id=foo,portfile=bar"
1132893Sdfr     * do settings = cleanOptions("--server:",Util.set("-portfile"),settings);
1142893Sdfr     *    now settings equals "--server:portfile=bar"
1152893Sdfr     *
1162893Sdfr     * @param allowsSubOptions A set of the allowed sub options, id portfile etc.
1172893Sdfr     * @param s The option settings string.
1182893Sdfr     */
1192893Sdfr    public static String cleanSubOptions(Set<String> allowedSubOptions, String s) {
1202893Sdfr        StringBuilder sb = new StringBuilder();
1212893Sdfr        StringTokenizer st = new StringTokenizer(s, ",");
1222893Sdfr        while (st.hasMoreTokens()) {
1232893Sdfr            String o = st.nextToken();
1242893Sdfr            int p = o.indexOf('=');
1252893Sdfr            if (p>0) {
1262893Sdfr                String key = o.substring(0,p);
1272893Sdfr                String val = o.substring(p+1);
1282893Sdfr                if (allowedSubOptions.contains(key)) {
1292893Sdfr                    if (sb.length() > 0) sb.append(',');
1302893Sdfr                    sb.append(key+"="+val);
1312893Sdfr                }
1322893Sdfr            }
1332893Sdfr        }
1342893Sdfr        return sb.toString();
13555206Speter    }
13633548Sjkh
137120498Sbde    /**
138120498Sbde     * Convenience method to create a set with strings.
139120498Sbde     */
140120498Sbde    public static Set<String> set(String... ss) {
141120498Sbde        Set<String> set = new HashSet<>();
142120498Sbde        set.addAll(Arrays.asList(ss));
143120498Sbde        return set;
144120498Sbde    }
145120498Sbde
146120498Sbde    /**
147120498Sbde     * Normalize windows drive letter paths to upper case to enable string
148120498Sbde     * comparison.
149120498Sbde     *
150120498Sbde     * @param file File name to normalize
151120498Sbde     * @return The normalized string if file has a drive letter at the beginning,
152120498Sbde     *         otherwise the original string.
153120498Sbde     */
15492727Salfred    public static String normalizeDriveLetter(String file) {
155120498Sbde        if (file.length() > 2 && file.charAt(1) == ':') {
156120498Sbde            return Character.toUpperCase(file.charAt(0)) + file.substring(1);
15755206Speter        } else if (file.length() > 3 && file.charAt(0) == '*'
158                   && file.charAt(2) == ':') {
159            // Handle a wildcard * at the beginning of the string.
160            return file.substring(0, 1) + Character.toUpperCase(file.charAt(1))
161                   + file.substring(2);
162        }
163        return file;
164    }
165
166    /**
167     * Locate the setting for the server properties.
168     */
169    public static String findServerSettings(String[] args) {
170        for (String s : args) {
171            if (s.startsWith("--server:")) {
172                return s;
173            }
174        }
175        return null;
176    }
177
178    public static <E> Set<E> union(Set<? extends E> s1,
179                                   Set<? extends E> s2) {
180        Set<E> union = new HashSet<>();
181        union.addAll(s1);
182        union.addAll(s2);
183        return union;
184    }
185
186    public static String getStackTrace(Throwable t) {
187        StringWriter sw = new StringWriter();
188        t.printStackTrace(new PrintWriter(sw));
189        return sw.toString();
190    }
191
192    // TODO: Remove when refactoring from java.io.File to java.nio.file.Path.
193    public static File pathToFile(Path path) {
194        return path == null ? null : path.toFile();
195    }
196}
197