jdk/src/java.base/share/classes/sun/misc/MessageUtils.java
changeset 35233 357bf863e7c1
parent 35232 76aed99c0ddd
parent 34918 80f67512daa1
child 35234 854fc566323e
equal deleted inserted replaced
35232:76aed99c0ddd 35233:357bf863e7c1
     1 /*
       
     2  * Copyright (c) 1995, 2000, 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 sun.misc;
       
    27 
       
    28 /**
       
    29  * MessageUtils: miscellaneous utilities for handling error and status
       
    30  * properties and messages.
       
    31  *
       
    32  * @author Herb Jellinek
       
    33  */
       
    34 
       
    35 public class MessageUtils {
       
    36     // can instantiate it for to allow less verbose use - via instance
       
    37     // instead of classname
       
    38 
       
    39     public MessageUtils() { }
       
    40 
       
    41     public static String subst(String patt, String arg) {
       
    42         String args[] = { arg };
       
    43         return subst(patt, args);
       
    44     }
       
    45 
       
    46     public static String subst(String patt, String arg1, String arg2) {
       
    47         String args[] = { arg1, arg2 };
       
    48         return subst(patt, args);
       
    49     }
       
    50 
       
    51     public static String subst(String patt, String arg1, String arg2,
       
    52                                String arg3) {
       
    53         String args[] = { arg1, arg2, arg3 };
       
    54         return subst(patt, args);
       
    55     }
       
    56 
       
    57     public static String subst(String patt, String args[]) {
       
    58         StringBuilder result = new StringBuilder();
       
    59         int len = patt.length();
       
    60         for (int i = 0; i >= 0 && i < len; i++) {
       
    61             char ch = patt.charAt(i);
       
    62             if (ch == '%') {
       
    63                 if (i != len) {
       
    64                     int index = Character.digit(patt.charAt(i + 1), 10);
       
    65                     if (index == -1) {
       
    66                         result.append(patt.charAt(i + 1));
       
    67                         i++;
       
    68                     } else if (index < args.length) {
       
    69                         result.append(args[index]);
       
    70                         i++;
       
    71                     }
       
    72                 }
       
    73             } else {
       
    74                 result.append(ch);
       
    75             }
       
    76         }
       
    77         return result.toString();
       
    78     }
       
    79 
       
    80     public static String substProp(String propName, String arg) {
       
    81         return subst(System.getProperty(propName), arg);
       
    82     }
       
    83 
       
    84     public static String substProp(String propName, String arg1, String arg2) {
       
    85         return subst(System.getProperty(propName), arg1, arg2);
       
    86     }
       
    87 
       
    88     public static String substProp(String propName, String arg1, String arg2,
       
    89                                    String arg3) {
       
    90         return subst(System.getProperty(propName), arg1, arg2, arg3);
       
    91     }
       
    92 
       
    93     /**
       
    94      *  Print a message directly to stderr, bypassing all the
       
    95      *  character conversion methods.
       
    96      *  @param msg   message to print
       
    97      */
       
    98     public static native void toStderr(String msg);
       
    99 
       
   100     /**
       
   101      *  Print a message directly to stdout, bypassing all the
       
   102      *  character conversion methods.
       
   103      *  @param msg   message to print
       
   104      */
       
   105     public static native void toStdout(String msg);
       
   106 
       
   107 
       
   108     // Short forms of the above
       
   109 
       
   110     public static void err(String s) {
       
   111         toStderr(s + "\n");
       
   112     }
       
   113 
       
   114     public static void out(String s) {
       
   115         toStdout(s + "\n");
       
   116     }
       
   117 
       
   118     // Print a stack trace to stderr
       
   119     //
       
   120     public static void where() {
       
   121         Throwable t = new Throwable();
       
   122         StackTraceElement[] es = t.getStackTrace();
       
   123         for (int i = 1; i < es.length; i++)
       
   124             toStderr("\t" + es[i].toString() + "\n");
       
   125     }
       
   126 
       
   127 }