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

changeset 1570
f91144b7da75
parent 1504
22e417cdddee
child 2227
998b10c43157
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/sjavac/Util.java	Mon Feb 04 18:08:53 2013 -0500
     1.3 @@ -0,0 +1,160 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.sjavac;
    1.30 +
    1.31 +import java.io.File;
    1.32 +import java.util.Arrays;
    1.33 +import java.util.HashSet;
    1.34 +import java.util.Set;
    1.35 +import java.util.StringTokenizer;
    1.36 +
    1.37 +/**
    1.38 + * Utilities.
    1.39 + *
    1.40 + * <p><b>This is NOT part of any supported API.
    1.41 + * If you write code that depends on this, you do so at your own
    1.42 + * risk.  This code and its internal interfaces are subject to change
    1.43 + * or deletion without notice.</b></p>
    1.44 + */
    1.45 +public class Util {
    1.46 +
    1.47 +    public static String toFileSystemPath(String pkgId) {
    1.48 +        if (pkgId == null || pkgId.length()==0) return null;
    1.49 +        String pn;
    1.50 +        if (pkgId.charAt(0) == ':') {
    1.51 +            // When the module is the default empty module.
    1.52 +            // Do not prepend the module directory, because there is none.
    1.53 +            // Thus :java.foo.bar translates to java/foo/bar (or \)
    1.54 +            pn = pkgId.substring(1).replace('.',File.separatorChar);
    1.55 +        } else {
    1.56 +            // There is a module. Thus jdk.base:java.foo.bar translates
    1.57 +            // into jdk.base/java/foo/bar
    1.58 +            int cp = pkgId.indexOf(':');
    1.59 +            String mn = pkgId.substring(0,cp);
    1.60 +            pn = mn+File.separatorChar+pkgId.substring(cp+1).replace('.',File.separatorChar);
    1.61 +        }
    1.62 +        return pn;
    1.63 +    }
    1.64 +
    1.65 +    public static String justPackageName(String pkgName) {
    1.66 +        int c = pkgName.indexOf(":");
    1.67 +        assert(c != -1);
    1.68 +        return pkgName.substring(c+1);
    1.69 +    }
    1.70 +
    1.71 +    public static String extractStringOption(String opName, String s) {
    1.72 +        int p = s.indexOf(opName+"=");
    1.73 +        if (p == -1) return null;
    1.74 +        p+=opName.length()+1;
    1.75 +        int pe = s.indexOf(',', p);
    1.76 +        if (pe == -1) pe = s.length();
    1.77 +        return s.substring(p, pe);
    1.78 +    }
    1.79 +
    1.80 +    public static int extractIntOption(String opName, String s) {
    1.81 +        int p = s.indexOf(opName+"=");
    1.82 +        if (p == -1) return 0;
    1.83 +        p+=opName.length()+1;
    1.84 +        int pe = s.indexOf(',', p);
    1.85 +        if (pe == -1) pe = s.length();
    1.86 +        int v = 0;
    1.87 +        try {
    1.88 +            v = Integer.parseInt(s.substring(p, pe));
    1.89 +        } catch (Exception e) {}
    1.90 +        return v;
    1.91 +    }
    1.92 +
    1.93 +    /**
    1.94 +     * Clean out unwanted sub options supplied inside a primary option.
    1.95 +     * For example to only had portfile remaining from:
    1.96 +     *    settings="--server:id=foo,portfile=bar"
    1.97 +     * do settings = cleanOptions("--server:",Util.set("-portfile"),settings);
    1.98 +     *    now settings equals "--server:portfile=bar"
    1.99 +     *
   1.100 +     * @param optionPrefix The option name, including colon, eg --server:
   1.101 +     * @param allowsSubOptions A set of the allowed sub options, id portfile etc.
   1.102 +     * @param s The option settings string.
   1.103 +     */
   1.104 +    public static String cleanSubOptions(String optionPrefix, Set<String> allowedSubOptions, String s) {
   1.105 +        StringBuilder sb = new StringBuilder();
   1.106 +        if (!s.startsWith(optionPrefix)) return "";
   1.107 +        StringTokenizer st = new StringTokenizer(s.substring(optionPrefix.length()), ",");
   1.108 +        while (st.hasMoreTokens()) {
   1.109 +            String o = st.nextToken();
   1.110 +            int p = o.indexOf('=');
   1.111 +            if (p>0) {
   1.112 +                String key = o.substring(0,p);
   1.113 +                String val = o.substring(p+1);
   1.114 +                if (allowedSubOptions.contains(key)) {
   1.115 +                    if (sb.length() > 0) sb.append(',');
   1.116 +                    sb.append(key+"="+val);
   1.117 +                }
   1.118 +            }
   1.119 +        }
   1.120 +        return sb.toString();
   1.121 +    }
   1.122 +
   1.123 +    /**
   1.124 +     * Convenience method to create a set with strings.
   1.125 +     */
   1.126 +    public static Set<String> set(String... ss) {
   1.127 +        Set<String> set = new HashSet<String>();
   1.128 +        set.addAll(Arrays.asList(ss));
   1.129 +        return set;
   1.130 +    }
   1.131 +
   1.132 +    /**
   1.133 +     * Normalize windows drive letter paths to upper case to enable string
   1.134 +     * comparison.
   1.135 +     *
   1.136 +     * @param file File name to normalize
   1.137 +     * @return The normalized string if file has a drive letter at the beginning,
   1.138 +     *         otherwise the original string.
   1.139 +     */
   1.140 +    public static String normalizeDriveLetter(String file) {
   1.141 +        if (file.length() > 2 && file.charAt(1) == ':') {
   1.142 +            return Character.toUpperCase(file.charAt(0)) + file.substring(1);
   1.143 +        } else if (file.length() > 3 && file.charAt(0) == '*'
   1.144 +                   && file.charAt(2) == ':') {
   1.145 +            // Handle a wildcard * at the beginning of the string.
   1.146 +            return file.substring(0, 1) + Character.toUpperCase(file.charAt(1))
   1.147 +                   + file.substring(2);
   1.148 +        }
   1.149 +        return file;
   1.150 +    }
   1.151 +
   1.152 +    /**
   1.153 +     * Locate the setting for the server properties.
   1.154 +     */
   1.155 +    public static String findServerSettings(String[] args) {
   1.156 +        for (String s : args) {
   1.157 +            if (s.startsWith("--server:")) {
   1.158 +                return s;
   1.159 +            }
   1.160 +        }
   1.161 +        return null;
   1.162 +    }
   1.163 +}

mercurial