org.nuiton.i18n.bundle.I18nBundleEntry Maven / Gradle / Ivy
/*
* #%L
* I18n :: Api
*
* $Id$
* $HeadURL$
* %%
* Copyright (C) 2004 - 2010 CodeLutin
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package org.nuiton.i18n.bundle;
import org.nuiton.i18n.I18nUtil;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Properties;
/**
* A class to represent an entry in a bundle.
*
* The object matches exactly one resource file in a given scope.
*
* The object has three properties : - {@link #path} : the path to
* resource file where to find transaltion for the entry.
- {@link #locale} :
* the locale of the entry
- {link #scope} ; the scope of the entry
* This object defines a equals order base on property {@link #path}.
*
* This object is {@link Comparable}, the order relation is defined like this :
* - sort first on {@link #scope}, in the scope order (see {@link
* I18nBundleScope}),
- if scopes are equals, sort on {@link #locale} string
* representation.
*
* @author Tony Chemit - [email protected]
* @see I18nBundleScope
*/
public class I18nBundleEntry implements Comparable {
/** path to resource file */
protected URL path;
/** local of the entry, can be null if general scope */
protected Locale locale;
/** scope of the entry */
protected I18nBundleScope scope;
/**
* Constructor if an bundle entry.
*
* It is defined by a path
of the resource file, a scope and a
* locale.
*
* @param path the path of the resource file fo the bundle entry
* @param locale the given locale of the bundle entry
* @param scope the scope of the given entry
*/
public I18nBundleEntry(URL path, Locale locale, I18nBundleScope scope) {
this.path = path;
this.locale = locale;
this.scope = scope;
}
public URL getPath() {
return path;
}
public Locale getLocale() {
return locale;
}
public I18nBundleScope getScope() {
return scope;
}
/**
* Method to match or not a bundle entry for a given scope and locale.
*
* We use the inclusive property of scope, means that we accept all entries
* on the path to the generalest entry for a givne locale.
*
* @param locale the locale to match
* @param scope the highest scope to match
* @return {@code true} if the entry match the scope and locale *
*/
public boolean matchLocale(Locale locale, I18nBundleScope scope) {
if (this.locale == null) {
// a general bundle entry is always matched!
return true;
}
if (locale == null) {
// can not match a specialized entry with a general scope
return false;
}
// match full locale, or at least a language
return this.locale.equals(locale) ||
this.scope.ordinal() < scope.ordinal() &&
locale.getLanguage().equals(this.locale.getLanguage());
}
/**
* For a given language, load the resource file of this entry into the
* resource
properties object.
*
* @param resource the save of resources already loaded
* @throws IOException if any pb while reading resource file
* @deprecated since 2.4 use {@link #load(Properties, Charset)} instead,
* charset must be provided to avoid encoding problems
*/
@Deprecated
public void load(Properties resource) throws IOException {
load(resource, I18nUtil.DEFAULT_CHARSET);
}
/**
* For a given language, load the resource file of this entry into the
* resource
properties object. Use {@code charset} to load
* properties. It could be different from resulting properties store.
*
* @param resource the save of resources already loaded
* @param encoding Charset used to store the properties
* @throws IOException if any pb while reading resource file
* @since 2.4
*/
public void load(Properties resource, Charset encoding) throws IOException {
InputStream inputStream = null;
StringBuilder sb = new StringBuilder();
try {
inputStream = getPath().openStream();
if (I18nBundle.log.isDebugEnabled()) {
sb.append(getPath()).append("\n");
}
// Prepare new Properties using charset to load entries
Properties fileReader = new Properties();
Reader reader = new InputStreamReader(inputStream, encoding);
try {
fileReader.load(reader);
} finally {
reader.close();
}
if (I18nBundle.log.isDebugEnabled()) {
for (Entry