| 1 | /* |
| 2 | * Copyright 2005-2006 The RbUtils Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | // $Id: PropertiesRB.java,v 1.2 2006/09/16 14:31:40 redfish Exp $ |
| 19 | |
| 20 | package org.ktc.rbutils.api.i18n; |
| 21 | |
| 22 | import java.io.BufferedInputStream; |
| 23 | import java.io.File; |
| 24 | import java.io.FileInputStream; |
| 25 | import java.io.IOException; |
| 26 | import java.io.InputStream; |
| 27 | import java.util.Locale; |
| 28 | import java.util.Properties; |
| 29 | import org.apache.commons.io.IOUtils; |
| 30 | import org.ktc.rbutils.api.file.FileTools; |
| 31 | import org.ktc.rbutils.api.file.ValidateFile; |
| 32 | |
| 33 | /** |
| 34 | * Adds locale feature to the {@link Properties} class. |
| 35 | * @since RbUtils 0.1 |
| 36 | * @version $Revision: 1.2 $ |
| 37 | * @author moishi |
| 38 | */ |
| 39 | public class PropertiesRB extends Properties { |
| 40 | |
| 41 | /** Locale used to load this Properties. */ |
| 42 | private Locale locale; |
| 43 | /** Automaticly generated for backward compatibility. */ |
| 44 | private static final long serialVersionUID = 3256726173668357681L; |
| 45 | |
| 46 | /** |
| 47 | * Calls the super constructor and sets the locale of the current object with the |
| 48 | * {@link LocaleUtils#getEmptyLocale() empty locale}. |
| 49 | * @see Properties#Properties() |
| 50 | */ |
| 51 | public PropertiesRB() { |
| 52 | super(); |
| 53 | setDefaultLocale(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Calls the super constructor and sets the locale of the current object with the |
| 58 | * {@link LocaleUtils#getEmptyLocale() empty locale}. |
| 59 | * @param defaults the defaults. |
| 60 | * @see Properties#Properties(java.util.Properties) |
| 61 | */ |
| 62 | public PropertiesRB(final Properties defaults) { |
| 63 | super(defaults); |
| 64 | setDefaultLocale(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Reads a property list (key and element pairs) from a file. The locale of the current object |
| 69 | * is set according to the name of the file. |
| 70 | * @param propertiesFile the file to load as properties file. |
| 71 | * @see Properties#load(java.io.InputStream) |
| 72 | * @see FileTools#getLocaleFrom(File) |
| 73 | * @throws org.apache.commons.lang.NullArgumentException if <code>propertiesFile</code> is |
| 74 | * <code>null</code>. |
| 75 | * @throws java.io.FileNotFoundException if <code>propertiesFile</code> does not exist. |
| 76 | * @throws org.ktc.rbutils.api.file.NotFileException if <code>propertiesFile</code> is not a |
| 77 | * file. |
| 78 | * @throws IOException if an error occurred when reading from the input file. |
| 79 | */ |
| 80 | public void load(final File propertiesFile) throws IOException { |
| 81 | // Validate argument |
| 82 | ValidateFile.isFile(propertiesFile); |
| 83 | |
| 84 | // Load RB file to Properties |
| 85 | final InputStream is = new BufferedInputStream(new FileInputStream(propertiesFile)); |
| 86 | super.load(is); |
| 87 | IOUtils.closeQuietly(is); |
| 88 | |
| 89 | // Set locale from RB file |
| 90 | locale = FileTools.getLocaleFrom(propertiesFile); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns the locale of the current object. |
| 95 | * @return the locale of the current object. |
| 96 | */ |
| 97 | public Locale getLocale() { |
| 98 | return locale; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Sets the locale of the current object with the |
| 103 | * {@link LocaleUtils#getEmptyLocale() empty locale}. |
| 104 | */ |
| 105 | private void setDefaultLocale() { |
| 106 | locale = LocaleUtils.getEmptyLocale(); |
| 107 | } |
| 108 | |
| 109 | } |