001: package U2.T2.Msg;
002:
003: import U2.P2.*;
004: import java.io.*;
005:
006: /**
007: * Utility for producing standards messages in the T2 tool.
008: */
009:
010: public class Message {
011:
012: // -----------------------------------------------------------------
013: // PROMPT like
014: // -----------------------------------------------------------------
015: /**
016: * Just a T2 greeting message.
017: */
018: static public final String GREET = "----------------------------------------------------\n"
019: + "** T2 Testing Tool for Java. Version 0.4, 2007.\n"
020: + "----------------------------------------------------";
021: static public final String BEGIN = "----";
022: static public final String END = "----";
023:
024: // -----------------------------------------------------------------
025: // MESSAGES
026: // -----------------------------------------------------------------
027:
028: static public final String PROBLEM = "PROBLEM";
029: static public final String ILLEGAL_STATE = "T2 is in an illegal state!.";
030:
031: // -----------------------------------------------------------------
032: // Utilities
033: // -----------------------------------------------------------------
034:
035: /**
036: * For producing a string of message, encapsulated with some T2
037: * standard tagging.
038: *
039: * @param msg A string describing something (like a problem) in u.
040: */
041: static public String mk(String msgCode, String msg, Object u) {
042:
043: PP title = PP.text("##" + msgCode);
044:
045: if (u != null)
046: title.aside_(PP.text("in " + u.getClass().getName() + "."));
047:
048: PP msgPP = PP.text(BEGIN);
049: msgPP.ontop(title);
050: msgPP.ontop(PP.text(msg));
051: msgPP.ontop(PP.text(END));
052:
053: return "\n" + msgPP.render(3);
054: }
055:
056: /**
057: * As the other mk, except that we have no u parameter.
058: */
059: static public String mk(String msgCode, String msg) {
060: return mk(msgCode, msg, null);
061: }
062:
063: /**
064: * As but send the message directly to an output stream.
065: *
066: * @see Message#mk
067: */
068: static public void print(String msgCode, String msg, Object u,
069: PrintStream out) {
070:
071: out.println(mk(msgCode, msg, u));
072:
073: }
074:
075: /**
076: * As the other print.
077: */
078: static public void print(String msgCode, String msg, PrintStream out) {
079:
080: out.println(mk(msgCode, msg));
081:
082: }
083:
084: /**
085: * As print, but send the message to the console.
086: *
087: * @see Message#print
088: */
089: static public void consolePrint(String msgCode, String msg, Object u) {
090:
091: print(msgCode, msg, u, System.out);
092:
093: }
094:
095: /**
096: * As the other console.
097: */
098: static public void consolePrint(String msgCode, String msg) {
099:
100: print(msgCode, msg, System.out);
101:
102: }
103:
104: /**
105: * Throwing E2error, and printing its message before the throw.
106: */
107: static public void throwT2Error(String msgCode, String msg,
108: PrintStream out) {
109: String m = mk(msgCode, msg);
110: out.println(m);
111: throw new T2Error(m);
112: }
113:
114: static public void throwT2Error(String msgCode, String msg,
115: Throwable cause) {
116: String m = mk(msgCode, msg);
117: throw new T2Error(m, cause);
118: }
119:
120: static public void throwT2Error(String msgCode, String msg) {
121:
122: throwT2Error(msgCode, msg, System.out);
123: }
124:
125: /**
126: * test
127: */
128: static public void main(String[] args) {
129:
130: System.out.println(GREET);
131:
132: Message m = new Message();
133:
134: consolePrint(T2Error.DEVEL_WARNING, "just a try.", m);
135: consolePrint(T2Error.DEVEL_WARNING, "just a try.", m);
136:
137: }
138:
139: }
|