(file) Return to MessageLoader.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 chuck 1.1 //%/////////////////////////////////////////////////////////////////////////////
  2           //
  3           // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
  4           // The Open Group, Tivoli Systems
  5           //
  6           // Permission is hereby granted, free of charge, to any person obtaining a copy
  7           // of this software and associated documentation files (the "Software"), to
  8           // deal in the Software without restriction, including without limitation the
  9           // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 10           // sell copies of the Software, and to permit persons to whom the Software is
 11           // furnished to do so, subject to the following conditions:
 12           // 
 13           // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 14           // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 15           // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 16           // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 17           // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 18           // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 19           // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 20           // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 21           //
 22 chuck 1.1 //==============================================================================
 23           //
 24           // Author: Humberto Rivero (hurivero@us.ibm.com)
 25           //
 26           // Modified By:
 27           //
 28           //%/////////////////////////////////////////////////////////////////////////////
 29           
 30           #ifndef Pegasus_MessageLoader_h
 31           #define Pegasus_MessageLoader_h
 32           
 33           #include <cstdlib>
 34           #include <cctype>
 35           #include <Pegasus/Common/Linkage.h>
 36           #include <Pegasus/Common/Config.h>
 37           #include <Pegasus/Common/String.h>
 38           #include <Pegasus/Common/Formatter.h>
 39           #include <Pegasus/Common/AcceptLanguages.h>
 40           #include <Pegasus/Common/ContentLanguages.h>
 41           
 42           //ICU specific 
 43 chuck 1.1 #ifdef PEGASUS_HAS_ICU
 44           #include <unicode/uloc.h> 
 45           #include <unicode/ures.h>
 46           #include <unicode/umsg.h>
 47           #include <unicode/ucnv.h>
 48           #include <unicode/fmtable.h>
 49           #include <unicode/msgfmt.h>
 50           #endif
 51           
 52           PEGASUS_NAMESPACE_BEGIN
 53           
 54           /**
 55            * MessageLoaderParms class is basically a stuct class containing public variables that control
 56            * the way MessageLoader behaves. MessageLoader uses the fields in this class to decide where and
 57            * how to load messages from the message resources.
 58            */
 59           
 60           class PEGASUS_COMMON_LINKAGE MessageLoaderParms{
 61           	
 62           public:
 63           	
 64 chuck 1.1 	/*
 65           	 * String msg_id: unique message identifier for a particular message in a message resource
 66           	 */
 67           	String msg_id;	
 68           	
 69           	/*
 70           	 * String default_msg: the default message to use if a message cannot be loaded from a message resource
 71           	 */		
 72           	String default_msg;     
 73           	
 74           	/*
 75           	 * String msg_src_path: this path tells MessageLoader where to find message resources
 76           	 * it can be empty, fully qualified or relative to $PEGASUS_HOME
 77           	 */
 78           	String msg_src_path;
 79           	
 80           	/*
 81           	 * AcceptLanguages acceptlanguages: This contains the languages that are acceptable by the caller
 82           	 * of MessageLoader::getMessage(). That is, MessageLoader will do its best to return a message in 
 83           	 * a language that was specified in this container.  This container is naturally ordered using the quality 
 84           	 * values attached to the languages and MessageLoader iterates through this container in its natural 
 85 chuck 1.1 	 * ordering.  This container is used by MessageLoader to load messages if it is not empty.
 86           	 */
 87           	AcceptLanguages acceptlanguages;
 88           	
 89           	/*
 90           	 * ContentLanguages contentlanguages: This is set by MessageLoader::getMessage() after a message has
 91           	 * been loaded from either a message resource or the default message.  After the call to MessageLoader::getMessage()
 92           	 * the caller can check the MessageLoaderParms.contentlanguages object to see what MessageLoader set it to.
 93           	 * In all cases where a message is returned from MessageLoader::getMessage(), this field will be set to match the
 94           	 * language that the message was found in.
 95           	 */
 96           	ContentLanguages contentlanguages;
 97           	
 98           	/*
 99           	 * Boolean useProcessLocale: Default is false, if true, MessageLoader uses the system default language
100           	 * to loads messages from.
101           	 */
102           	Boolean useProcessLocale;
103           	
104           	/*
105           	 * Boolean useThreadLocale: Default is true, this tells MessageLoader to use the AcceptLanguages container
106 chuck 1.1 	 * from the current Pegasus thread.
107           	 */
108           	Boolean useThreadLocale;
109           	
110           	/*
111           	 * Boolean useICUfallback: Default is false.  Only relevant if PEGASUS_HAS_ICU is defined.
112           	 * MessageLoader::getMessage() default behaviour is to extract messages for the langauge exactly
113           	 * matching an available message resource.  If this is set to true, the MessageLoader is free to extract
114           	 * a message from a less specific message resource according to its search algorithm.
115           	 */
116           	#ifdef PEGASUS_HAS_ICU
117           	Boolean useICUfallback;
118           	#endif
119           	
120           	/*
121           	 * Formatter::Arg0-9: These are assigned the various substitutions necessary to properly format
122           	 * the message being extracted.  MessageLoader substitutes these in the correct places in the message
123           	 * being returned from MessageLoader::getMessage()
124           	 */
125           	Formatter::Arg arg0; 
126           	Formatter::Arg arg1;
127 chuck 1.1 	Formatter::Arg arg2;
128           	Formatter::Arg arg3;
129           	Formatter::Arg arg4;
130           	Formatter::Arg arg5;
131           	Formatter::Arg arg6;
132           	Formatter::Arg arg7;
133           	Formatter::Arg arg8;
134           	Formatter::Arg arg9;
135           	
136           	/**
137           	 * Constructor:
138           	 */
139 mday  1.1.2.2 	MessageLoaderParms(){
140               		useProcessLocale = false;
141               		useThreadLocale = true;
142               		
143               		#ifdef PEGASUS_HAS_ICU
144               		useICUfallback = false;
145               		#endif
146               		
147               		acceptlanguages = AcceptLanguages();
148               		contentlanguages = ContentLanguages();
149               		msg_src_path = String();
150               	}
151 chuck 1.1     	
152               	/*
153               	 * Constructor: 
154               	 * @param id String - message identifier used to look up a message in a message resource
155               	 * @param msg String - default message to use if a message cannot be found in the message resources.
156               	 * @param arg0 Formatter::Arg - optional substitution parameter
157               	 * @param arg1 Formatter::Arg - optional substitution parameter
158               	 * @param arg2 Formatter::Arg - optional substitution parameter
159               	 * @param arg3 Formatter::Arg - optional substitution parameter
160               	 * @param arg4 Formatter::Arg - optional substitution parameter
161               	 * @param arg5 Formatter::Arg - optional substitution parameter
162               	 * @param arg6 Formatter::Arg - optional substitution parameter
163               	 * @param arg7 Formatter::Arg - optional substitution parameter
164               	 * @param arg8 Formatter::Arg - optional substitution parameter
165               	 * @param arg9 Formatter::Arg - optional substitution parameter
166               	 */
167               	MessageLoaderParms( String id, String msg, 
168 mday  1.1.2.3 						Formatter::Arg anArg0 = Formatter::Arg(), 
169               						Formatter::Arg anArg1 = Formatter::Arg(),
170               						Formatter::Arg anArg2 = Formatter::Arg(),
171               						Formatter::Arg anArg3 = Formatter::Arg(),
172               						Formatter::Arg anArg4 = Formatter::Arg(),
173               						Formatter::Arg anArg5 = Formatter::Arg(),
174               						Formatter::Arg anArg6 = Formatter::Arg(),
175               						Formatter::Arg anArg7 = Formatter::Arg(),
176               						Formatter::Arg anArg8 = Formatter::Arg(),
177               						Formatter::Arg anArg9 = Formatter::Arg() ){
178 chuck 1.1     							
179               		msg_id = id;
180               		default_msg = msg;
181               		useProcessLocale = false;
182               		useThreadLocale = true;
183               		
184               		#ifdef PEGASUS_HAS_ICU
185               		useICUfallback = false;
186               		#endif
187               		
188               		acceptlanguages = AcceptLanguages::EMPTY;
189               		contentlanguages = ContentLanguages::EMPTY;
190               		msg_src_path = String::EMPTY;
191 mday  1.1.2.3 		this->arg0 = anArg0;
192               		this->arg1 = anArg1;
193               		this->arg2 = anArg2;
194               		this->arg3 = anArg3;
195               		this->arg4 = anArg4;
196               		this->arg5 = anArg5;
197               		this->arg6 = anArg6;
198               		this->arg7 = anArg7;
199               		this->arg8 = anArg8;
200               		this->arg9 = anArg9;
201 chuck 1.1     	}	
202               	
203               	String toString(){
204               		
205               		String s;
206               		String processLoc,threadLoc,ICUfallback;
207               		processLoc = (useProcessLocale) ? "true" : "false";
208               		threadLoc = (useThreadLocale) ? "true" : "false";
209               		#ifdef PEGASUS_HAS_ICU
210               		ICUfallback = (useICUfallback) ? "true" : "false";
211               		#endif
212               		
213               		s.append("msg_id = " + msg_id + "\n");
214               		s.append("default_msg = " + default_msg + "\n");
215               		s.append("msg_src_path = " + msg_src_path + "\n");
216               		s.append("acceptlanguages = " + acceptlanguages.toString() + "\n");
217               		s.append("contentlanguages = " + contentlanguages.toString() + "\n");
218               		
219               		s.append("useProcessLocale = " + processLoc + "\n");
220               		s.append("useThreadLocale = " + threadLoc + "\n");
221               		#ifdef PEGASUS_HAS_ICU
222 chuck 1.1     		s.append("useICUfallback = " + ICUfallback + "\n");
223               		#endif
224               		s.append("arg0 = " + arg0.toString() + "\n" + "arg1 = " + arg1.toString() + "\n" + "arg2 = " + arg2.toString() + "\n" + "arg3 = " + arg3.toString() + "\n" + 
225               		          "arg4 = " + arg4.toString() + "\n" + "arg5 = " + arg5.toString() + "\n" + "arg6 = " + arg6.toString() + "\n" + "arg7 = " + arg7.toString() + "\n" + 
226               		          "arg8 = " + arg8.toString() + "\n" + "arg9 = " + arg9.toString() + "\n\n");
227               		          
228               		return s;
229               	}	
230               		
231               }; // end MessageLoaderParms
232               
233               
234               /*
235                * MessageLoader is a static class resposible for looking up messages in message resources. 
236                * For specific behaviour details of this class see the Globalization HOWTO.
237                */
238               
239               class PEGASUS_COMMON_LINKAGE MessageLoader{
240               	
241               public:
242               	
243 chuck 1.1     	/*
244               	 * Retrieves a message from a message resource
245               	 * @param parms MessageLoaderParms - controls the behaviour of how a message is retrieved
246               	 * @return String - the formatted message
247               	 */
248               	static String getMessage(MessageLoaderParms &parms);
249               	
250               	static void setPegasusMsgHome(String home);
251               	
252               	static Boolean _useProcessLocale;
253               	
254               	static Boolean _useDefaultMsg;
255                	
256               	static AcceptLanguages _acceptlanguages;
257               	
258               private:
259               
260               	static String formatDefaultMessage(MessageLoaderParms &parms);
261               	
262               	static String getQualifiedMsgPath(String path);
263               
264 chuck 1.1     	static void initPegasusMsgHome();
265               	
266               	static String pegasus_MSG_HOME;
267               	
268               	#ifdef PEGASUS_HAS_ICU
269               		static String loadICUMessage(MessageLoaderParms &parms);
270               		
271               		static String extractICUMessage(UResourceBundle * resbundl, MessageLoaderParms &parms);
272               		
273               		static String formatICUMessage(UResourceBundle * resbundl, const UChar *msg, int msg_len, MessageLoaderParms &parms);
274               		
275               		static String uChar2String(UChar * uchar_str);
276               		
277               		static String uChar2String(UChar * uchar_str, int len);
278               		
279               		static UChar* string2UChar(String s);
280               		
281               		static void xferFormattables(MessageLoaderParms &parms, Formattable *formattables);
282               	#endif
283               	
284               }; // end MessageLoader
285 chuck 1.1     
286               
287               PEGASUS_NAMESPACE_END
288               
289               #endif

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2