KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > tty > MessageOutput


1 /*
2  * @(#)MessageOutput.java 1.8 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.tools.example.debug.tty;
8
9 import java.util.*;
10 import java.text.MessageFormat JavaDoc;
11 /**
12  * Internationalization (i18n) convenience methods for jdb.
13  *
14  * All program output should flow through these methods, and this is
15  * the only class that should be printing directly or otherwise
16  * accessing System.[out,err].
17  *
18  * @version @(#) MessageOutput.java 1.8 05/11/17 00:25:20
19  * @bug 4348376
20  * @author Tim Bell
21  */

22 public class MessageOutput {
23     /**
24      * The resource bundle containing localizable message content.
25      * This is loaded by TTY.main() at start-up
26      */

27     static ResourceBundle textResources;
28
29     /** Our message formatter. Allocated once, used many times */
30     private static MessageFormat JavaDoc messageFormat;
31
32     /**
33      * Fatal shutdown notification. This is sent to System.err
34      * instead of System.out
35      */

36     static void fatalError(String JavaDoc messageKey) {
37         System.err.println();
38         System.err.println(format("Fatal error"));
39         System.err.println(format(messageKey));
40         Env.shutdown();
41     }
42
43     /**
44      * "Format" a string by doing a simple key lookup.
45      */

46     static String JavaDoc format(String JavaDoc key) {
47         return (textResources.getString(key));
48     }
49
50     /**
51      * Fetch and format a message with one string argument.
52      * This is the most common usage.
53      */

54     static String JavaDoc format(String JavaDoc key, String JavaDoc argument) {
55         return format(key, new Object JavaDoc [] {argument});
56     }
57
58     /**
59      * Fetch a string by key lookup and format in the arguments.
60      */

61     static synchronized String JavaDoc format(String JavaDoc key, Object JavaDoc [] arguments) {
62         if (messageFormat == null) {
63             messageFormat = new MessageFormat JavaDoc (textResources.getString(key));
64         } else {
65             messageFormat.applyPattern (textResources.getString(key));
66         }
67         return (messageFormat.format (arguments));
68     }
69
70     /**
71      * Print directly to System.out.
72      * Every rule has a few exceptions.
73      * The exceptions to "must use the MessageOutput formatters" are:
74      * VMConnection.dumpStream()
75      * TTY.monitorCommand()
76      * TTY.TTY() (for the '!!' command only)
77      * Commands.java (multiple locations)
78      * These are the only sites that should be calling this
79      * method.
80      */

81     static void printDirectln(String JavaDoc line) {
82         System.out.println(line);
83     }
84     static void printDirect(String JavaDoc line) {
85         System.out.print(line);
86     }
87     static void printDirect(char c) {
88         System.out.print(c);
89     }
90
91     /**
92      * Print a newline.
93      * Use this instead of '\n'
94      */

95     static void println() {
96         System.out.println();
97     }
98
99     /**
100      * Format and print a simple string.
101      */

102     static void print(String JavaDoc key) {
103         System.out.print(format(key));
104     }
105     /**
106      * Format and print a simple string.
107      */

108     static void println(String JavaDoc key) {
109         System.out.println(format(key));
110     }
111
112
113     /**
114      * Fetch, format and print a message with one string argument.
115      * This is the most common usage.
116      */

117     static void print(String JavaDoc key, String JavaDoc argument) {
118         System.out.print(format(key, argument));
119     }
120     static void println(String JavaDoc key, String JavaDoc argument) {
121         System.out.println(format(key, argument));
122     }
123
124     /**
125      * Fetch, format and print a message with an arbitrary
126      * number of message arguments.
127      */

128     static void println(String JavaDoc key, Object JavaDoc [] arguments) {
129         System.out.println(format(key, arguments));
130     }
131
132     /**
133      * Print a newline, followed by the string.
134      */

135     static void lnprint(String JavaDoc key) {
136         System.out.println();
137         System.out.print(textResources.getString(key));
138     }
139
140     static void lnprint(String JavaDoc key, String JavaDoc argument) {
141         System.out.println();
142         System.out.print(format(key, argument));
143     }
144
145     static void lnprint(String JavaDoc key, Object JavaDoc [] arguments) {
146         System.out.println();
147         System.out.print(format(key, arguments));
148     }
149
150     /**
151      * Print an exception message with a stack trace.
152      */

153     static void printException(String JavaDoc key, Exception JavaDoc e) {
154         if (key != null) {
155             try {
156                 println(key);
157             } catch (MissingResourceException mex) {
158                 printDirectln(key);
159             }
160         }
161         System.out.flush();
162         e.printStackTrace();
163     }
164
165     static void printPrompt() {
166         ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
167         if (threadInfo == null) {
168             System.out.print
169                 (MessageOutput.format("jdb prompt with no current thread"));
170         } else {
171             System.out.print
172                 (MessageOutput.format("jdb prompt thread name and current stack frame",
173                                       new Object JavaDoc [] {
174                                           threadInfo.getThread().name(),
175                                           new Integer JavaDoc (threadInfo.getCurrentFrameIndex() + 1)}));
176         }
177         System.out.flush();
178     }
179 }
180
Popular Tags