1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (c) 1995, 2000, Oracle and/or its affiliates. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 
27 package sun.misc;
28 
29 /**
30  * MessageUtils: miscellaneous utilities for handling error and status
31  * properties and messages.
32  *
33  * @author Herb Jellinek
34  */
35 
36 public class MessageUtils {
37     // can instantiate it for to allow less verbose use - via instance
38     // instead of classname
39 
MessageUtils()40     public MessageUtils() { }
41 
subst(String patt, String arg)42     public static String subst(String patt, String arg) {
43         String args[] = { arg };
44         return subst(patt, args);
45     }
46 
subst(String patt, String arg1, String arg2)47     public static String subst(String patt, String arg1, String arg2) {
48         String args[] = { arg1, arg2 };
49         return subst(patt, args);
50     }
51 
subst(String patt, String arg1, String arg2, String arg3)52     public static String subst(String patt, String arg1, String arg2,
53                                String arg3) {
54         String args[] = { arg1, arg2, arg3 };
55         return subst(patt, args);
56     }
57 
subst(String patt, String args[])58     public static String subst(String patt, String args[]) {
59         StringBuffer result = new StringBuffer();
60         int len = patt.length();
61         for (int i = 0; i >= 0 && i < len; i++) {
62             char ch = patt.charAt(i);
63             if (ch == '%') {
64                 if (i != len) {
65                     int index = Character.digit(patt.charAt(i + 1), 10);
66                     if (index == -1) {
67                         result.append(patt.charAt(i + 1));
68                         i++;
69                     } else if (index < args.length) {
70                         result.append(args[index]);
71                         i++;
72                     }
73                 }
74             } else {
75                 result.append(ch);
76             }
77         }
78         return result.toString();
79     }
80 
substProp(String propName, String arg)81     public static String substProp(String propName, String arg) {
82         return subst(System.getProperty(propName), arg);
83     }
84 
substProp(String propName, String arg1, String arg2)85     public static String substProp(String propName, String arg1, String arg2) {
86         return subst(System.getProperty(propName), arg1, arg2);
87     }
88 
substProp(String propName, String arg1, String arg2, String arg3)89     public static String substProp(String propName, String arg1, String arg2,
90                                    String arg3) {
91         return subst(System.getProperty(propName), arg1, arg2, arg3);
92     }
93 
94     /**
95      *  Print a message directly to stderr, bypassing all the
96      *  character conversion methods.
97      *  @param msg   message to print
98      */
99     // Android-removed: Remove support for bypassing character conversion
100     // in printing debug message.
101     //
102     // public static native void toStderr(String msg);
103 
104     /**
105      *  Print a message directly to stdout, bypassing all the
106      *  character conversion methods.
107      *  @param msg   message to print
108      */
109     // Android-removed: Remove support for bypassing character conversion
110     // in printing debug message.
111     //
112 
113     // public static native void toStdout(String msg);
114 
115 
116     // Short forms of the above
117 
err(String s)118     public static void err(String s) {
119         // Android-changed.
120         // toStderr(s + "\n");
121         System.err.println(s);
122     }
123 
out(String s)124     public static void out(String s) {
125         // Android-changed.
126         // toStdout(s + "\n");
127         System.out.println(s);
128     }
129 
130     // Print a stack trace to stderr
131     //
where()132     public static void where() {
133         Throwable t = new Throwable();
134         StackTraceElement[] es = t.getStackTrace();
135         for (int i = 1; i < es.length; i++) {
136             // Android-changed.
137             // toStderr("\t" + es[i].toString() + "\n");
138             System.err.println("\t" + es[i].toString());
139         }
140     }
141 
142 }
143