KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > dottedname > DottedNameStrings


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  * LocalStringsManager.java
26  *
27  * Created on July 29, 2003, 4:26 PM
28  */

29
30 package com.sun.enterprise.admin.dottedname;
31
32 import java.util.ResourceBundle JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.MissingResourceException JavaDoc;
35 import java.text.MessageFormat JavaDoc;
36
37
38 public class DottedNameStrings
39 {
40     private DottedNameStrings() {}
41     private static final ResourceStringSource mImpl = initImpl();
42     
43     private final static String JavaDoc PACKAGE_NAME = "com.sun.enterprise.admin.dottedname";
44     private final static String JavaDoc STRINGS_FILENAME = "DottedNameStrings"; // .properties
45

46         private static ResourceStringSource
47     initImpl()
48     {
49         ResourceStringSource src = null;
50         
51         try
52         {
53             src = new ResourceStringSource( PACKAGE_NAME, STRINGS_FILENAME );
54         }
55         catch( MissingResourceException JavaDoc e)
56         {
57             DottedNameLogger.logException( e );
58         }
59         
60         return( src );
61     }
62     
63         public static String JavaDoc
64     getString( String JavaDoc key )
65     {
66         if ( mImpl == null )
67         {
68             return( key );
69         }
70
71         return( mImpl.getString( key ) );
72     }
73     
74         public static String JavaDoc
75     getString( String JavaDoc key, final Object JavaDoc toInsert)
76     {
77         return( getString( key, new Object JavaDoc [] { toInsert } ) );
78     }
79     
80         public static String JavaDoc
81     getString( String JavaDoc key, final Object JavaDoc toInsert1, final Object JavaDoc toInsert2)
82     {
83         return( getString( key, new Object JavaDoc [] { toInsert1, toInsert2 } ) );
84     }
85     
86         public static String JavaDoc
87     getString( String JavaDoc key, final Object JavaDoc[] toInsert)
88     {
89         if ( mImpl == null )
90         {
91             return( key );
92         }
93         
94         return( mImpl.getString( key, toInsert) );
95     }
96     
97     
98     public final static String JavaDoc OBJECT_INSTANCE_NOT_FOUND_KEY = "ObjectInstanceNotFound";
99     public final static String JavaDoc MALFORMED_DOTTED_NAME_KEY = "MalformedDottedName";
100     public final static String JavaDoc WILDCARD_DISALLOWED_FOR_SET_KEY = "WildcardDisallowedForSet";
101     public final static String JavaDoc ATTRIBUTE_NOT_FOUND_KEY = "AttributeNotFound";
102     public final static String JavaDoc ILLEGAL_TO_SET_NULL_KEY = "IllegalToSetNull";
103     public final static String JavaDoc ILLEGAL_CHARACTER_KEY = "IllegalCharacter";
104     public final static String JavaDoc MISSING_EXPECTED_NAME_PART_KEY = "MissingExpectedNamePart";
105     public final static String JavaDoc DOTTED_NAME_MUST_HAVE_ONE_PART_KEY = "DottedNameMustHaveAtLeastOnePart";
106     public final static String JavaDoc NO_VALUE_NAME_SPECIFIED_KEY = "NoValueNameSpecified";
107     
108     
109 }
110
111
112 class ResourceStringSource
113 {
114     private final String JavaDoc mPackageName;
115     private final String JavaDoc mPropertyFile;
116     private final ResourceBundle JavaDoc mResourceBundle;
117     
118     public static String JavaDoc DEFAULT_STRING_VALUE = "Key not found";
119
120     /** Creates a new instance of LocalStringsManager */
121         public
122     ResourceStringSource(String JavaDoc packageName, String JavaDoc propertyFile)
123     {
124         mPackageName = packageName;
125         mPropertyFile = propertyFile;
126         mResourceBundle = ResourceBundle.getBundle( packageName + "." + propertyFile);
127     }
128
129     public String JavaDoc getPropertiesFile()
130     {
131         return mPropertyFile;
132     }
133
134     public String JavaDoc getPackageName()
135     {
136         return mPackageName;
137     }
138
139     public String JavaDoc getString(String JavaDoc key)
140     {
141         return( getString( key, DEFAULT_STRING_VALUE + " (" + key + ")" ) );
142     }
143     
144     public String JavaDoc getString(String JavaDoc key, String JavaDoc defaultValue)
145     {
146         String JavaDoc value = defaultValue;
147         
148         try
149         {
150             value = mResourceBundle.getString(key);
151         }
152         catch (MissingResourceException JavaDoc mre)
153         {
154             // return the default string
155
}
156         return value;
157     }
158
159     /*
160      * Return the Localized string with the inserted values
161      */

162     public String JavaDoc getString(String JavaDoc key, Object JavaDoc[] toInsert)
163     {
164         String JavaDoc template = getString(key);
165         String JavaDoc result = template;
166         
167         final MessageFormat JavaDoc msgFormat = new MessageFormat JavaDoc( template );
168         result = msgFormat.format(toInsert);
169             
170         return result;
171     }
172 }
173
Popular Tags