KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > LocalStringsManager


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.cli.framework;
31
32 //imports
33
import java.util.ResourceBundle JavaDoc;
34 import java.util.Locale JavaDoc;
35 import java.util.Vector JavaDoc;
36 import java.util.Properties JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.MissingResourceException JavaDoc;
39 import java.text.MessageFormat JavaDoc;
40
41 /**
42  *
43  * @author pa100654
44  */

45 public class LocalStringsManager {
46     
47     private String JavaDoc packageName;
48     private String JavaDoc propertyFile;
49     private Vector JavaDoc resourceBundles = new Vector JavaDoc();
50     public static String JavaDoc DEFAULT_STRING_VALUE = "Key not found";
51
52     /** Creates a new instance of LocalStringsManager */
53     public LocalStringsManager(String JavaDoc packageName, String JavaDoc propertyFile)
54     {
55         this.packageName = packageName;
56         this.propertyFile = propertyFile;
57         ResourceBundle JavaDoc resourceBundle =
58                 ResourceBundle.getBundle(packageName + "." + propertyFile);
59         resourceBundles.add(resourceBundle);
60     }
61     
62     /** Creates a new instance of LocalStringsManager from the
63         properties Vector
64      */

65     public LocalStringsManager(Vector JavaDoc localizePropertiesList)
66     {
67         for (int i = 0; i < localizePropertiesList.size(); i++)
68         {
69             Properties JavaDoc properties = (Properties JavaDoc) localizePropertiesList.get(i);
70             String JavaDoc packageNameStr = (String JavaDoc) properties.get("base-package");
71             String JavaDoc propertyFileStr = (String JavaDoc) properties.get("property-file-name");
72             ResourceBundle JavaDoc resourceBundle =
73                 ResourceBundle.getBundle(packageNameStr + "." + propertyFileStr);
74             resourceBundles.add(resourceBundle);
75         }
76     }
77
78         
79     /** Creates a new instance of LocalStringsManager from the
80         properties Vector of Strings and locale
81      */

82     public LocalStringsManager(Vector JavaDoc localizeStringList, Locale JavaDoc locale)
83     {
84         for (int i = 0; i < localizeStringList.size(); i++)
85         {
86             ResourceBundle JavaDoc resourceBundle =
87                 ResourceBundle.getBundle((String JavaDoc)localizeStringList.get(i),
88                                          locale);
89             resourceBundles.add(resourceBundle);
90         }
91     }
92
93     
94     /*
95      *returns the property file name
96      */

97     public String JavaDoc getPropertiesFile()
98     {
99         return propertyFile;
100     }
101     
102     /*
103      * returns the base package name
104      */

105     public String JavaDoc getPackageName()
106     {
107         return packageName;
108     }
109
110     
111     /*
112      *returns resourceBundles
113      */

114     public Vector JavaDoc getResourceBundles()
115     {
116         return resourceBundles;
117     }
118     
119     
120     /*
121      * Returns the Localized string properties file
122      */

123     public String JavaDoc getString(String JavaDoc key)
124     {
125         Iterator JavaDoc resourcesIter = resourceBundles.iterator();
126         String JavaDoc value = DEFAULT_STRING_VALUE + " (" + key + ")";
127         while (resourcesIter.hasNext())
128         {
129             ResourceBundle JavaDoc resourceBundle = (ResourceBundle JavaDoc)resourcesIter.next();
130             try
131             {
132                 value = resourceBundle.getString(key);
133                 break;
134             }
135             catch (MissingResourceException JavaDoc mre)
136             {
137                 // if not found, try next resource bundle in the iterator
138
}
139         }
140         return value;
141     }
142
143     /*
144      * Return the Localized string with the inserted values
145      */

146     public String JavaDoc getString(String JavaDoc key, Object JavaDoc[] toInsert)
147                     throws CommandValidationException
148     {
149         String JavaDoc fmtStr = null;
150         try
151         {
152             MessageFormat JavaDoc msgFormat =
153                             new MessageFormat JavaDoc(getString(key));
154             fmtStr = msgFormat.format(toInsert);
155         }
156         catch(Exception JavaDoc e)
157         {
158             throw new CommandValidationException(e);
159         }
160         return fmtStr;
161     }
162 }
163
Popular Tags