Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/LDAPResourceBundle.java

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38 package org.ietf.ldap;
39
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.util.Locale;
43 import java.util.PropertyResourceBundle;
44
45 /**
46 * This class represents a locale-specific resource for a property file.
47 * It retrieves the property file for the given base name including the
48 * absolute path name and locale. The property file has to be located in the
49 * CLASSPATH and the property file's suffix is .props.
50 * <p>
51 * If the specified locale is en and us and the base name of the file is
52 * netscape/ldap/errors/ErrorCodes, then the class loader will search for
53 * the file in the following order:
54 * <pre>
55 *
56 * ErrorCodes_en_us.properties
57 * ErrorCodes_en.properties
58 * ErrorCodes.properties
59 *
60 * </pre>
61 * @see java.util.Locale
62 */
63 class LDAPResourceBundle implements java.io.Serializable {
64
65 static final long serialVersionUID = -5903986665461157980L;
66 private static final boolean _debug = false;
67 private static final String _suffix = ".properties";
68 private static final String _locale_separator = "_";
69
70 /**
71 * Return the property resource bundle according to the base name of the
72 * property file and the locale. The class loader will find the closest match
73 * with the given locale.
74 * @return the property resource bundle.
75 * @exception IOException Gets thrown when failed to open the resource
76 * bundle file.
77 */
78 static PropertyResourceBundle getBundle( String baseName )
79 throws IOException {
80
81 return getBundle( baseName, Locale.getDefault() );
82 }
83
84 /**
85 * Return the property resource bundle according to the base name of the
86 * property file and the locale. The class loader will find the closest match
87 * with the given locale.
88 * @param baseName the base name of the property file. The base name contains
89 * no locale context and no . suffix.
90 * @param l the locale
91 * @return the property resource bundle.
92 * @exception IOException Gets thrown when failed to create a property
93 * resource
94 */
95 static PropertyResourceBundle getBundle( String baseName, Locale l )
96 throws IOException {
97 String localeStr = _locale_separator + l.toString();
98
99 InputStream fin = null;
100
101 while ( true ) {
102 if ( (fin = getStream(baseName, localeStr)) != null ) {
103 PropertyResourceBundle p = new PropertyResourceBundle( fin );
104 return p;
105 } else {
106 int index = localeStr.lastIndexOf( _locale_separator );
107 if ( index == -1 ) {
108 printDebug( "File " + baseName + localeStr + _suffix +
109 " not found" );
110 return null;
111 } else {
112 localeStr = localeStr.substring( 0, index );
113 }
114 }
115 }
116 }
117
118 /**
119 * Constructs the whole absolute path name of a property file and retrieves
120 * an input stream on the file.
121 * @param baseName the base name of the property file. The base name contains
122 * no locale context and no . suffix.
123 * @param the locale string to insert into the file name
124 * @return the input stream of the property file.
125 */
126 private static InputStream getStream( String baseName, String locale ) {
127 String fStr = baseName + locale + _suffix;
128 return Thread.currentThread().getContextClassLoader().getResourceAsStream(fStr);
129 }
130
131 /**
132 * Prints debug messages if the debug mode is on.
133 * @param str the message that is printed
134 */
135 private static void printDebug(String str) {
136 if ( _debug ) {
137 System.out.println( str );
138 }
139 }
140 }
141