KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > MessageFormatter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.admin.util;
24
25 /**
26     Class for replacing numbered tokens in Strings with other Strings. The
27     static methods can be used for working with passed-in String arguments.
28     Use the non-static methods for working with <i>indirect</i> Strings.
29     I.e. String keys are passed in which in turn are used to locate the actual
30     String to substitute. The class in that case is instantiated by passing in
31     a IStringSource object which is used for fetching Strings.
32     <p>The token format is <b>{#}</b>. Where <b>#</b> is 1-9. <b>Note that the
33     numbering is one-based, not zero-based.</b> E.g.
34     <p><code>MessageFormatter.format("Hello {1}, how are {2}?", "Carbon-based Lifeform", "you");</code>
35     
36  */

37
38
39 public class MessageFormatter implements IStringSource
40 {
41     /** Create an instance using the supplied IStringSource to find Strings
42      * @param lookup The string library
43      */

44     public MessageFormatter(IStringSource source )
45     {
46         setSource(source);
47     }
48     
49     
50     /** Set the IStringSource object
51      * @param lookup The IStringSource object
52      */

53     public void setSource(IStringSource source)
54     {
55         //ArgChecker.check(source, "source");
56
//ArgChecker.check(source != this, "Can't setSource to self!");
57
mSource = source;
58     }
59     
60     
61     /** Get the IStringSource object
62      * @return the IStringSource object
63      */

64     public IStringSource getSource()
65     {
66         Assert.assertit((mSource!=null), "mSource");
67         return mSource;
68     }
69     
70     
71     /** IStringSource signature method. In this case it asks the IStringSource
72      * member variable to get the String.
73      * @param lookupKey key to locate String with
74      * @return The String value for the key. Null if not found.
75      */

76     public String JavaDoc getString(String JavaDoc lookupKey)
77     {
78         //ArgChecker.check(lookupKey, "lookupKey");
79
return(mSource.getString(lookupKey));
80     }
81
82     
83     /** This is the workhorse method of all of these overloaded methods.
84      * The array of Objects' toString() methods are called one at a time. The
85      * resulting Strings are used to replace numbered tokens. I.e. replacement
86      * String #0 replaces this token: <b>{1}</b> and replacement String #1
87      * replaces this token: <b>{2}</b>.
88      * Avoid messy calling code by dedicating methods to the common cases of
89      * 1-3 inserts.
90      * @param base String with embedded tokens
91      * @param toInsert An ordered array of Objects to replace the tokens with
92      * @return The original String with token substitution
93      */

94     public static String JavaDoc format(String JavaDoc base, Object JavaDoc[] toInsert)
95     {
96         // the only "format" method that actually does much work!
97
// let's be gentle with nulls & empty Strings...
98

99         //ArgChecker.checkValid(base, "base");
100

101         if(toInsert == null || toInsert.length <= 0)
102             return null;
103         
104         String JavaDoc ret = base;
105         
106         for(int i = 0; i < toInsert.length; i++)
107         {
108             String JavaDoc token = makeToken(i + 1);
109             String JavaDoc replace = toInsert[i].toString();
110             ret = replaceToken(ret, token, replace);
111             
112             if(ret == null || ret.length() <= 0)
113                 return null;
114         }
115         return ret;
116     }
117     
118     
119     /** Version of format with one token to replace
120      * @param base String with embedded tokens
121      * @param o1 An Object whose toString() results replace the token
122      * @return The original String with token substitution
123      */

124     public static String JavaDoc format(String JavaDoc base, Object JavaDoc o1)
125     {
126         return format(base, new Object JavaDoc[] {o1});
127     }
128     
129     
130     /** Version of format with two tokens to replace
131      * @param base String with embedded tokens
132      * @param o1 An Object whose toString() results replace the token
133      * @param o2 An Object whose toString() results replace the token
134      * @return The original String with token substitution
135      */

136     public static String JavaDoc format(String JavaDoc base, Object JavaDoc o1, Object JavaDoc o2)
137     {
138         return format(base, new Object JavaDoc[] {o1, o2});
139     }
140
141     
142     /** Version of format with three tokens to replace
143      * @param base String with embedded tokens
144      * @param o1 An Object whose toString() results replace the token
145      * @param o2 An Object whose toString() results replace the token
146      * @param o3 An Object whose toString() results replace the token
147      * @return The original String with token substitution
148      */

149     public static String JavaDoc format(String JavaDoc base, Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3)
150     {
151         return format(base, new Object JavaDoc[] {o1, o2, o3});
152     }
153
154     
155     /** Get the String from the member IStringSource object -- then call
156      * <b>format</b> using that String.
157      *
158      * @param key String key to locate String value with
159      * @param o1 An Object whose toString() results replace the token
160      * @return The token-replaced String
161      */

162     public String JavaDoc getAndFormat(String JavaDoc key, Object JavaDoc o1)
163     {
164         return getAndFormat(key, new Object JavaDoc[] { o1} );
165     }
166         
167     
168     /** Get the String from the member IStringSource object -- then call
169      * <b>format</b> using that String.
170      *
171      * @param key String key to locate String value with
172      * @param o1 An Object whose toString() results replace the token
173      * @param o2 An Object whose toString() results replace the token
174      * @return The token-replaced String
175      */

176     public String JavaDoc getAndFormat(String JavaDoc key, Object JavaDoc o1, Object JavaDoc o2)
177     {
178         return getAndFormat(key, new Object JavaDoc[] { o1, o2} );
179     }
180         
181     
182     /** Get the String from the member IStringSource object -- then call
183      * <b>format</b> using that String.
184      *
185      * @param key String key to locate String value with
186      * @param o1 An Object whose toString() results replace the token
187      * @param o2 An Object whose toString() results replace the token
188      * @param o3 An Object whose toString() results replace the token
189      * @return The token-replaced String
190      */

191     public String JavaDoc getAndFormat(String JavaDoc key, Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3)
192     {
193         return getAndFormat(key, new Object JavaDoc[] { o1, o2, o3} );
194     }
195         
196     
197     /** Get the String from the member IStringSource object -- then call
198      * <b>format</b> using that String.
199      *
200      * @param key key String to locate value String with
201      * @param toInsert An ordered array of Objects to replace the tokens with
202      * @return The located String with token substitution
203      */

204     public String JavaDoc getAndFormat(String JavaDoc key, Object JavaDoc[] toInsert)
205     {
206         return format(getString(key), toInsert);
207     }
208         
209
210     private static String JavaDoc makeToken(int num)
211     {
212             /* this is the one and only place where the specifics of how tokens
213              * are represented are kept.
214              * It would have been easy (and nice!) to change this token
215              * programmatically. But unfortunately, impossible, because we
216              * have static methods
217              */

218             return "{" + num + "}";
219     }
220     
221     
222     private static String JavaDoc replaceToken(String JavaDoc s, String JavaDoc token, String JavaDoc replace)
223     {
224             /* look for the token, 'token', inside the String, 's', and replace
225              * with the String, 'replace'
226              */

227             
228             if(s == null || s.length() <= 0 || token == null || token.length() <= 0)
229             return s;
230         
231         int index = s.indexOf(token);
232
233         if(index < 0)
234             return s;
235
236         int tokenLength = token.length();
237         String JavaDoc ret = s.substring(0, index);
238         ret += replace;
239         ret += s.substring(index + tokenLength);
240
241         return ret;
242     }
243     
244     
245     private IStringSource mSource = null;
246
247     /**
248          * TEMPORARY -- until unit testing code is created...
249      * @param notUsed */

250     public static void main(String JavaDoc[] notUsed)
251     {
252         String JavaDoc test2A = "hello {1}, How are {2}?";
253         String JavaDoc test2AResult = format(test2A, "Carbon-based lifeform", "you");
254         Debug.println("INPUT: " + test2A + "\nOUTPUT: " + test2AResult);
255     }
256 }
257
258
259
Popular Tags