001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.satsa.jcrmic.utils;
028:
029: import java.util.*;
030: import java.io.*;
031:
032: /**
033: * This class is responsible for reporting any messages (error, warning,
034: * progress, banner and status messages) during the compilation.
035: */
036: public class Notifier {
037:
038: /**
039: * The stream where error messages are printed.
040: */
041: OutputStream out;
042:
043: /**
044: * Constructor.
045: * @param out output stream
046: */
047: public Notifier(OutputStream out) {
048:
049: this .out = out;
050: }
051:
052: /**
053: * Output a message.
054: * @param msg the message
055: */
056: public void output(String msg) {
057:
058: if (this .out instanceof PrintStream) {
059: ((PrintStream) out).println(msg);
060: } else {
061: OutputStreamWriter osw = new OutputStreamWriter(this .out);
062: PrintWriter pw = new PrintWriter(osw, true);
063: pw.println(msg);
064: try {
065: osw.flush();
066: } catch (IOException phooey) {
067: }
068: }
069: }
070:
071: /**
072: * Prints error message.
073: * @param msg the message
074: */
075: public void error(String msg) {
076: output(getText(msg));
077: }
078:
079: /**
080: * Prints error message.
081: * @param msg the message
082: * @param arg1 argument
083: */
084: public void error(String msg, String arg1) {
085: output(getText(msg, arg1));
086: }
087:
088: /**
089: * Prints error message.
090: * @param msg the message
091: * @param arg1 argument
092: * @param arg2 argument
093: */
094: public void error(String msg, String arg1, String arg2) {
095: output(getText(msg, arg1, arg2));
096: }
097:
098: /**
099: * Prints error message.
100: * @param msg the message
101: * @param arg1 argument
102: * @param arg2 argument
103: * @param arg3 argument
104: */
105: public void error(String msg, String arg1, String arg2, String arg3) {
106: output(getText(msg, arg1, arg2, arg3));
107: }
108:
109: /**
110: * Return the string value of a named resource in the rmic.properties
111: * resource bundle. If the resource is not found, null is returned.
112: * @param key resource name
113: * @return the value
114: */
115: public static String getString(String key) {
116:
117: if (!resourcesInitialized) {
118: initResources();
119: }
120:
121: try {
122: return resources.getString(key);
123: } catch (MissingResourceException ignore) {
124: }
125: return null;
126: }
127:
128: /**
129: * Flag that indicates that resoyrces were initialized.
130: */
131: private static boolean resourcesInitialized = false;
132:
133: /**
134: * Resource bundle.
135: */
136: private static ResourceBundle resources;
137:
138: /**
139: * Initializes resource bundle.
140: */
141: private static void initResources() {
142:
143: try {
144: resources = ResourceBundle
145: .getBundle("com/sun/satsa/jcrmic/jcrmic");
146: resourcesInitialized = true;
147: } catch (MissingResourceException e) {
148: throw new Error("fatal: missing resource bundle: "
149: + e.getClassName());
150: }
151: }
152:
153: /**
154: * Returns the string for given key.
155: * @param key the key
156: * @return the string
157: */
158: public static String getText(String key) {
159:
160: String message = getString(key);
161:
162: if (message == null) {
163: message = "no text found: \"" + key + "\"";
164: }
165:
166: return message;
167: }
168:
169: /**
170: * Returns the message.
171: * @param key the key
172: * @param num parameter
173: * @return the message
174: */
175: public static String getText(String key, int num) {
176: return getText(key, Integer.toString(num), null, null);
177: }
178:
179: /**
180: * Returns the message.
181: * @param key the key
182: * @param arg0 parameter
183: * @return the message
184: */
185: public static String getText(String key, String arg0) {
186: return getText(key, arg0, null, null);
187: }
188:
189: /**
190: * Returns the message.
191: * @param key the key
192: * @param arg0 parameter
193: * @param arg1 parameter
194: * @return the message
195: */
196: public static String getText(String key, String arg0, String arg1) {
197: return getText(key, arg0, arg1, null);
198: }
199:
200: /**
201: * Returns the message.
202: * @param key the key
203: * @param arg0 parameter
204: * @param arg1 parameter
205: * @param arg2 parameter
206: * @return the message
207: */
208: public static String getText(String key, String arg0, String arg1,
209: String arg2) {
210:
211: String format = getString(key);
212: if (format == null) {
213: format = "no text found: "
214: + "key = \"{0}\", arguments = \"{1}\", \"{2}\", \"{3}\"";
215: }
216:
217: String[] args = new String[3];
218: args[0] = (arg0 != null ? arg0.toString() : "null");
219: args[1] = (arg1 != null ? arg1.toString() : "null");
220: args[2] = (arg2 != null ? arg2.toString() : "null");
221:
222: return java.text.MessageFormat.format(format, args);
223: }
224: }
|