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

changeset 1
9a66ca7c79fa
child 416
c287d51c57da
equal deleted inserted replaced
-1:000000000000 1:9a66ca7c79fa
1 /*
2 * Copyright 2002-2004 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26
27 package com.sun.tools.javah;
28
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.util.ResourceBundle;
33 import java.text.MessageFormat;
34 import java.util.MissingResourceException;
35
36 /**
37 * Messages, verbose and error handling support.
38 *
39 * For errors, the failure modes are:
40 * error -- User did something wrong
41 * bug -- Bug has occurred in javah
42 * fatal -- We can't even find resources, so bail fast, don't localize
43 *
44 */
45 public class Util {
46
47 /*
48 * Help for verbosity.
49 */
50 public static boolean verbose = false;
51
52 public static void log(String s) {
53 System.out.println(s);
54 }
55
56
57 /*
58 * Help for loading localized messages.
59 */
60 private static ResourceBundle m;
61
62 private static void initMessages() {
63 try {
64 m=ResourceBundle.getBundle("com.sun.tools.javah.resources.l10n");
65 } catch (MissingResourceException mre) {
66 fatal("Error loading resources. Please file a bug report.", mre);
67 }
68 }
69
70 public static String getText(String key) {
71 return getText(key, null, null);
72 }
73
74 private static String getText(String key, String a1, String a2){
75 if (m == null)
76 initMessages();
77 try {
78 return MessageFormat.format(m.getString(key),
79 new Object[] { a1, a2 });
80 } catch (MissingResourceException e) {
81 fatal("Key " + key + " not found in resources.", e);
82 }
83 return null; /* dead code */
84 }
85
86 /*
87 * Usage message.
88 */
89 public static void usage(int exitValue) {
90 if (exitValue == 0) {
91 System.out.println(getText("usage"));
92 } else {
93 System.err.println(getText("usage"));
94 }
95 System.exit(exitValue);
96 }
97
98 public static void version() {
99 System.out.println(getText("javah.version",
100 System.getProperty("java.version"), null));
101 System.exit(0);
102 }
103
104 /*
105 * Failure modes.
106 */
107 public static void bug(String key) {
108 bug(key, null);
109 }
110
111 public static void bug(String key, Exception e) {
112 if (e != null)
113 e.printStackTrace();
114 System.err.println(getText(key));
115 System.err.println(getText("bug.report"));
116 System.exit(11);
117 }
118
119 public static void error(String key) {
120 error(key, null);
121 }
122
123 public static void error(String key, String a1) {
124 error(key, a1, null);
125 }
126
127 public static void error(String key, String a1, String a2) {
128 error(key, a1, a2, false);
129 }
130
131 public static void error(String key, String a1, String a2,
132 boolean showUsage) {
133 System.err.println("Error: " + getText(key, a1, a2));
134 if (showUsage)
135 usage(15);
136 System.exit(15);
137 }
138
139
140 private static void fatal(String msg) {
141 fatal(msg, null);
142 }
143
144 private static void fatal(String msg, Exception e) {
145 if (e != null) {
146 e.printStackTrace();
147 }
148 System.err.println(msg);
149 System.exit(10);
150 }
151
152 /*
153 * Support for platform specific things in javah, such as pragma
154 * directives, exported symbols etc.
155 */
156 static private ResourceBundle platform = null;
157
158 /*
159 * Set when platform has been initialized.
160 */
161 static private boolean platformInit = false;
162
163 static String getPlatformString(String key) {
164 if (!platformInit) {
165 initPlatform();
166 platformInit = true;
167 }
168 if (platform == null)
169 return null;
170 try {
171 return platform.getString(key);
172 } catch (MissingResourceException mre) {
173 return null;
174 }
175 }
176
177 private static void initPlatform() {
178 String os = System.getProperty("os.name");
179 if (os.startsWith("Windows")) {
180 os = "win32";
181 } else if (os.indexOf("Linux") >= 0) {
182 os = "Linux";
183 }
184 String arch = System.getProperty("os.arch");
185 String resname = "com.sun.tools.javah.resources." + os + "_" + arch;
186 try {
187 platform=ResourceBundle.getBundle(resname);
188 } catch (MissingResourceException mre) {
189 // fatal("Error loading resources. Please file a bug report.", mre);
190 }
191 }
192 }

mercurial