KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > misc > Formatter


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  
24 /*
25  * $Header: /cvs/glassfish/admin-core/mbeanapi/src/java/com/sun/appserv/management/util/misc/Formatter.java,v 1.3 2005/12/25 03:51:46 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:51:46 $
28  */

29  
30 package com.sun.appserv.management.util.misc;
31
32
33 import java.text.MessageFormat JavaDoc;
34
35 import com.sun.appserv.management.util.misc.ClassUtil;
36
37 import com.sun.appserv.management.util.stringifier.Stringifier;
38 import com.sun.appserv.management.util.stringifier.SmartStringifier;
39 import com.sun.appserv.management.util.stringifier.StringifierRegistry;
40 import com.sun.appserv.management.util.stringifier.StringifierRegistryImpl;
41
42 /**
43     Escapes/unescapes strings
44  */

45 public final class Formatter implements StringSource
46 {
47     final StringSource mStringSource;
48     final StringifierRegistry mRegistry;
49     
50         public
51     Formatter( StringSource source )
52     {
53         mStringSource = source;
54         mRegistry = StringifierRegistryImpl.DEFAULT;
55     }
56     
57         public Object JavaDoc
58     prepare( Object JavaDoc o )
59     {
60         Object JavaDoc result = o;
61         
62         if ( mRegistry.lookup( o.getClass() ) != null ||
63             ClassUtil.objectIsArray( o ) )
64         {
65             result = SmartStringifier.toString( o );
66         }
67         
68         return( result );
69     }
70     
71     /**
72         Prepare objects for formatting. Certain objects are not properly handled by MesageFormat
73         (such as Sets and arrays). We'll intercept these types, but leave dates and numbers
74         to MessageFormat.
75      */

76         private Object JavaDoc[]
77     prepare( Object JavaDoc[] objects )
78     {
79         final Object JavaDoc[] results = new Object JavaDoc[ objects.length ];
80         
81         for( int i = 0; i < objects.length; ++i )
82         {
83             results[ i ] = prepare( objects[ i ] );
84         }
85         
86         return( results );
87     }
88     
89     /**
90         Format the objects into a String using the pattern specified by 'key'.
91         
92         @param key key used to look up the pattern
93         @param objects array of objects to insert into the pattern
94      */

95         public String JavaDoc
96     format( String JavaDoc key, Object JavaDoc[] objects )
97     {
98         return( MessageFormat.format( getString( key ), prepare( objects ) ) );
99     }
100     
101         public String JavaDoc
102     format( String JavaDoc key, Object JavaDoc o1 )
103     {
104         return( format( key, new Object JavaDoc[] { o1 } ) );
105     }
106     
107         public String JavaDoc
108     format( String JavaDoc key, Object JavaDoc o1, Object JavaDoc o2)
109     {
110         return( format( key, new Object JavaDoc[] { o1, o2 } ) );
111     }
112     
113         public String JavaDoc
114     format( String JavaDoc key, Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3)
115     {
116         return( format( key, new Object JavaDoc[] { o1, o2, o3} ) );
117     }
118     
119         public String JavaDoc
120     format( String JavaDoc key, Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3, Object JavaDoc o4)
121     {
122         return( format( key, new Object JavaDoc[] { o1, o2, o3, o4 } ) );
123     }
124     
125     
126         public String JavaDoc
127     getString( String JavaDoc id )
128     {
129         return( mStringSource.getString( id ) );
130     }
131     
132         public String JavaDoc
133     getString( String JavaDoc id, String JavaDoc defaultValue)
134     {
135         return( mStringSource.getString( id, defaultValue ) );
136     }
137 }
138
139
140
Popular Tags