001: /*
002: * Copyright 2002-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javah;
027:
028: import java.io.File;
029: import java.io.FileInputStream;
030: import java.io.IOException;
031: import java.util.ResourceBundle;
032: import java.text.MessageFormat;
033: import java.util.MissingResourceException;
034:
035: /**
036: * Messages, verbose and error handling support.
037: *
038: * For errors, the failure modes are:
039: * error -- User did something wrong
040: * bug -- Bug has occurred in javah
041: * fatal -- We can't even find resources, so bail fast, don't localize
042: *
043: */
044: public class Util {
045:
046: /*
047: * Help for verbosity.
048: */
049: public static boolean verbose = false;
050:
051: public static void log(String s) {
052: System.out.println(s);
053: }
054:
055: /*
056: * Help for loading localized messages.
057: */
058: private static ResourceBundle m;
059:
060: private static void initMessages() {
061: try {
062: m = ResourceBundle
063: .getBundle("com.sun.tools.javah.resources.l10n");
064: } catch (MissingResourceException mre) {
065: fatal(
066: "Error loading resources. Please file a bug report.",
067: mre);
068: }
069: }
070:
071: public static String getText(String key) {
072: return getText(key, null, null);
073: }
074:
075: private static String getText(String key, String a1, String a2) {
076: if (m == null)
077: initMessages();
078: try {
079: return MessageFormat.format(m.getString(key), new Object[] {
080: a1, a2 });
081: } catch (MissingResourceException e) {
082: fatal("Key " + key + " not found in resources.", e);
083: }
084: return null; /* dead code */
085: }
086:
087: /*
088: * Usage message.
089: */
090: public static void usage(int exitValue) {
091: if (exitValue == 0) {
092: System.out.println(getText("usage"));
093: } else {
094: System.err.println(getText("usage"));
095: }
096: System.exit(exitValue);
097: }
098:
099: public static void version() {
100: System.out.println(getText("javah.version", System
101: .getProperty("java.version"), null));
102: System.exit(0);
103: }
104:
105: /*
106: * Failure modes.
107: */
108: public static void bug(String key) {
109: bug(key, null);
110: }
111:
112: public static void bug(String key, Exception e) {
113: if (e != null)
114: e.printStackTrace();
115: System.err.println(getText(key));
116: System.err.println(getText("bug.report"));
117: System.exit(11);
118: }
119:
120: public static void error(String key) {
121: error(key, null);
122: }
123:
124: public static void error(String key, String a1) {
125: error(key, a1, null);
126: }
127:
128: public static void error(String key, String a1, String a2) {
129: error(key, a1, a2, false);
130: }
131:
132: public static void error(String key, String a1, String a2,
133: boolean showUsage) {
134: System.err.println("Error: " + getText(key, a1, a2));
135: if (showUsage)
136: usage(15);
137: System.exit(15);
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 + "_"
186: + arch;
187: try {
188: platform = ResourceBundle.getBundle(resname);
189: } catch (MissingResourceException mre) {
190: // fatal("Error loading resources. Please file a bug report.", mre);
191: }
192: }
193: }
|