Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/media/mn8/MessageFormatter.java


1   /* 
2    * $COPYRIGHT$
3    * $Id: MessageFormatter.java,v 1.5 2002/07/24 23:25:27 neuro Exp $
4    *
5    * Date        Author            Changes 
6    * May 08 2001 Remus Pereni      Created
7    */
8   package org.media.mn8;
9   
10  
11  import java.util.Properties;
12  import org.media.mn8.event.*;
13  /**
14   * Class responsible for formatting logging messages. Loading them from the resource 
15   * bundles and properly formatting them to be displayed.
16   * @author <a href="mailto:remus@nolimits.ro">Remus Pereni</a>
17   * @version $Revision: 1.5 $ $Date: 2002/07/24 23:25:27 $
18   */
19  public class MessageFormatter {
20  
21      private static final String _MESSAGES_RESOURCE_FILE = "conf/res/mn8messages.properties";
22  
23      private static Properties _prop;
24  
25      private static String _head = ""; //"mn8 : ";
26      private static String _warning = "[WARNING] - ";
27      private static String _error = "[ERROR] - ";
28      private static String _info = "[INFO] - ";
29  
30  
31      private static MessageFormatter _formatter = new MessageFormatter();
32          
33      /**
34       * Dafault constructor
35       */
36      private MessageFormatter () {        
37          try {
38              _prop = new Properties();
39              Class c = Class.forName("org.media.mn8.LogManager");
40  
41              if ( c.getResourceAsStream( _MESSAGES_RESOURCE_FILE ) == null)
42                  throw new RuntimeException ("Unable to load resource file : " 
43                                              + _MESSAGES_RESOURCE_FILE);
44  
45              _prop.load(c.getResourceAsStream( _MESSAGES_RESOURCE_FILE ));           
46  
47              if ( _prop.getProperty ("mn8.messages.head") != null) 
48                  _head = _prop.getProperty ("mn8.messages.head") ;
49  
50              if ( _prop.getProperty ("mn8.messages.type.warning") != null) 
51                  _warning = _prop.getProperty ("mn8.messages.type.warning") ;
52  
53              if ( _prop.getProperty ("mn8.messages.type.error") != null) 
54                  _error = _prop.getProperty ("mn8.messages.type.error") ;
55  
56          } catch (Exception ex) {
57              sysError ( _warning, ex.getMessage());
58              _prop =  null;
59          }
60      }
61  
62  
63      /**
64       * Method for getting the MessageFormatter instance. Don't forger that it
65       * implements a Singleton pattern.
66       * @return The default instance of MessageFormatter
67       */
68      public static MessageFormatter getReference () {
69          return _formatter;
70      }
71  
72  
73      /**
74       * The main method for displaying system messages for mn8.
75       * @param type_id The type of the messages (error, warning, ...). This is
76       * actually an key in the resource bundle where the actual value which will be
77       * displayed is hold. mn8.messages.type. has to be inserted before the key in the resource 
78       * bundle to store such messages types.
79       * <pre>
80       *   mn8.messages.type.warning=[WARNING]
81       * </pre>
82       * @param message_id The id of the messages. This is
83       * actually an key in the resource bundle where the actual value which will be
84       * displayed is hold. mn8.messages. has to be inserted before the key in the resource 
85       * bundle to store such messages.
86       * <pre>
87       *   mn8.messages.file_missing=Sorry the file: xxx.jar \n is mising.
88       * </pre>
89       */
90      public String getMessage(String type_id, String message_id ) {
91          return getMessage(type_id, message_id, "", "");
92      }
93  
94  
95      /**
96       * The main method for displaying system messages for mn8.
97       * @param type_id The type of the messages (error, warning, ...). This is
98       * actually an key in the resource bundle where the actual value which will be
99       * displayed is hold. mn8.messages.type. has to be inserted before the key in the resource 
100      * bundle to store such messages types.
101      * <pre>
102      *   mn8.messages.type.warning=[WARNING]
103      * </pre>
104      * @param message_id The id of the messages. This is
105      * actually an key in the resource bundle where the actual value which will be
106      * displayed is hold. mn8.messages. has to be inserted before the key in the resource 
107      * bundle to store such messages.
108      * <pre>
109      *   mn8.messages.file_missing=Sorry the file: xxx.jar \n is mising.
110      * </pre>
111      * @param optional Another text to be added to the message. Usualy displayed on the same line.
112      * @return The formatted message.
113      */
114     public String getMessage(String type_id, String message_id, String optional) {
115         return getMessage( type_id, message_id, optional, "");
116     }
117 
118 
119     /**
120      * The main method for displaying system interpretation related messages for mn8.
121      * @param type_id The type of the messages (error, warning, ...). This is
122      * actually an key in the resource bundle where the actual value which will be
123      * displayed is hold. mn8.messages.type. has to be inserted before the key in the resource 
124      * bundle to store such messages types.
125      * <pre>
126      *   mn8.messages.type.warning=[WARNING]
127      * </pre>
128      * @param message_id The id of the messages. This is
129      * actually an key in the resource bundle where the actual value which will be
130      * displayed is hold. mn8.messages. has to be inserted before the key in the resource 
131      * bundle to store such messages.
132      * <pre>
133      *   mn8.messages.file_missing=Sorry the file: xxx.jar \n is mising.
134      * </pre>
135      * @param optional Another text to be added to the message. Usualy displayed on the same line.
136      * @param line The line number of the erroneous message.
137      * @return The formatted message.
138      */
139     public String getInterpMessage(String type_id, String message_id, String optional, int line) {
140         if( optional == null ) optional = "";
141 
142         return _head 
143             + getTypeInfo( type_id ) 
144             + "Line: " + line + " "
145             + getMessageInfo( message_id ) 
146             + optional;
147     }
148 
149 
150     /**
151      * The main method for displaying system messages for mn8.
152      * @param type_id The type of the messages (error, warning, ...). This is
153      * actually an key in the resource bundle where the actual value which will be
154      * displayed is hold. mn8.messages.type. has to be inserted before the key in the resource 
155      * bundle to store such messages types.
156      * <pre>
157      *   mn8.messages.type.warning=[WARNING]
158      * </pre>
159      * @param message_id The id of the messages. This is
160      * actually an key in the resource bundle where the actual value which will be
161      * displayed is hold. mn8.messages. has to be inserted before the key in the resource 
162      * bundle to store such messages.
163      * <pre>
164      *   mn8.messages.file_missing=Sorry the file: xxx.jar \n is mising.
165      * </pre>
166      * @param optional Another text to be added to the message. Usualy displayed on the same line.
167      * @param suplement Another text to be added to the message. Usualy displayed on another line.
168      * @return The formatted message.
169      */
170     public String getMessage(String type_id, String message_id, String optional, String suplementary ) {
171         if( optional == null ) optional = "";
172         if( suplementary == null ) suplementary = "";
173 
174         return _head 
175             // + getTypeInfo( type_id ) 
176             + getMessageInfo( message_id ) 
177             + optional + "\n" + suplementary;
178     }
179 
180 
181     public String getMessageInfo( String message_id ) {
182         if ( message_id == null || message_id.equals("") ) return "";
183 
184         if ( _prop.getProperty("mn8.messages." + message_id) == null ){
185             sysError (_warning, "Unable to find mn8.messages." + message_id 
186                       + " in resource bundle: " + _MESSAGES_RESOURCE_FILE);
187 
188             return "";
189         } else return _prop.getProperty("mn8.messages." + message_id);
190         
191     }
192 
193 
194     public String getTypeInfo( String type_id ) {
195         if( type_id == null || type_id.equals("") ) return "";
196 
197         if ( _prop.getProperty("mn8.messages.type." + type_id) == null ){
198             sysError (_warning, "Unable to find mn8.messages.type." + type_id
199                       + " in resource bundle: " + _MESSAGES_RESOURCE_FILE);
200             return "";
201         } else return _prop.getProperty("mn8.messages.type." + type_id);
202         
203     }
204 
205     
206     private void sysError (String type, String message ) {
207         throw new mn8SystemException( _head + type + message );
208     }
209 
210 
211     private void sysError (String type, String message, String optional ) {
212         throw new mn8SystemException( _head + type + message + optional );
213     }
214 
215 
216 }