Index: lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleEntry.java diff -u lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleEntry.java:1.2 lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleEntry.java:1.3 --- lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleEntry.java:1.2 Sun Mar 23 06:57:07 2008 +++ lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleEntry.java Sun Mar 23 16:42:11 2008 @@ -21,46 +21,82 @@ import org.codelutin.i18n.Language; import java.io.IOException; +import java.io.InputStream; import java.net.URL; -import java.util.Comparator; import java.util.Locale; import java.util.Properties; /** - * Une classe pour representer une entree dans un bundle, i.e une implementation - * de bundle, soit un fichier de 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 : + *

+ * This object defines a equals order base on property {@link #path}. + *

+ * This object is {@link Comparable}, the order relation is defined like this : + *

* * @author chemit + * @see I18nBundleScope */ -public class I18nBundleEntry { +public class I18nBundleEntry implements Comparable { - /** le niveau i18n du bundle encapsulé */ - protected I18nBundleScope scope; + /** path to resource file */ + protected URL path; - /** la locale du bundle (null si le scope est générale ???) */ + /** local of the entry, can be null if general scope */ protected Locale locale; - /** le chemin d'accès au bundle */ - protected URL path; + /** scope of the entry */ + protected I18nBundleScope scope; - public I18nBundleEntry(Locale locale, I18nBundleScope scope, URL path) { + /** + * 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; - this.path = path; } - public I18nBundleScope getScope() { - return scope; + public URL getPath() { + return path; } public Locale getLocale() { return locale; } - public URL getPath() { - return path; + 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 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! @@ -70,35 +106,48 @@ // can not match a specialized entry with a general scope return false; } - // this.locale!=null && locale!=null return this.locale.equals(locale) || (this.scope.ordinal() < scope.ordinal() && locale.getLanguage().equals(this.locale.getLanguage())); - } - public void load(Language language, Properties resource) { + /** + * For a given language, load the resource file of this entry into the resource + * properties object. + * + * @param language the given language + * @param resource the save of resources already loaded + * @throws IOException if any pb while reading resource file + */ + public void load(Language language, Properties resource) throws IOException { + InputStream inputStream = null; try { - loadResource(language.getEncoding(), resource, getPath()); + I18nFileReader fileReader = new I18nFileReader(); + inputStream = getPath().openStream(); + fileReader.load2(inputStream, language.getEncoding()); + resource.putAll(fileReader); + fileReader.clear(); if (I18nBundle.log.isDebugEnabled()) { I18nBundle.log.debug(getPath()); } - } catch (IOException e) { - throw new RuntimeException(e); + } finally { + if (inputStream != null) { + inputStream.close(); + } } } - protected void loadResource(String encoding, Properties resource, URL url) throws IOException { - I18nFileReader fileReader; - fileReader = new I18nFileReader(); - fileReader.load2(url.openStream(), encoding); - resource.putAll(fileReader); - fileReader.clear(); + public int compareTo(I18nBundleEntry o) { + int i = getScope().compareTo(o.getScope()); + if (i == 0) { + // same scope, sort on locale + i = getLocale().toString().compareTo(o.getLocale().toString()); + } + return i; } @Override public boolean equals(Object o) { return this == o || o instanceof I18nBundleEntry && path.equals(((I18nBundleEntry) o).path); - } @Override @@ -112,9 +161,4 @@ return "<" + s.substring(s.lastIndexOf(".") + 1) + ", locale:" + locale + ", scope " + scope + ", path:" + path + ">"; } - public static class I18nBundleEntryByLevelComparator implements Comparator { - public int compare(I18nBundleEntry o1, I18nBundleEntry o2) { - return o1.getScope().compareTo(o2.getScope()); - } - } }