001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2004, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.util;
018:
019: // J2SE dependencies
020: import java.io.Serializable;
021: import java.util.Locale;
022: import java.util.MissingResourceException;
023: import java.util.ResourceBundle;
024:
025: // Geotools dependencies
026: import org.geotools.resources.Utilities;
027:
028: /**
029: * An international string backed by a {@linkplain ResourceBundle resource bundle}. A resource
030: * bundle can be a Java class or a {@linkplain java.util.Properties properties} file, one for
031: * each language. The constructor expects the fully qualified class name of the base resource
032: * bundle (the one used when no resource was found in the client's language). The right resource
033: * bundle is loaded at runtime for the client's language by looking for a class or a
034: * {@linkplain java.util.Properties properties} file with the right suffix ("{@code _en}" for
035: * English or "{@code _fr}" for French). This mechanism is explained in J2SE's javadoc for the
036: * {@link ResourceBundle#getBundle(String,Locale,ClassLoader) getBundle} static method.
037: * <p>
038: * <b>Example:</b> If a file named "{@code MyResources.properties}" exists in the package
039: * "{@code org.geotools.mypackage}" and contains a line like "{@code MyKey = some value}",
040: * then an international string for "{@code some value}" can be created using the following
041: * code:
042: *
043: * <blockquote><code>
044: * InternationalString value = new ResourceInternationalString(
045: * "org.geotools.mypackage.MyResources", "MyKey");
046: * </code></blockquote>
047: *
048: * The "{@code some value}" string will be localized if the required properties files exist, for
049: * example "{@code MyResources_fr.properties}" for French, "{@code MyResources_it.properties}"
050: * for Italian, <cite>etc.</cite>
051: *
052: * @since 2.1
053: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/util/ResourceInternationalString.java $
054: * @version $Id: ResourceInternationalString.java 23493 2006-12-17 04:47:25Z desruisseaux $
055: * @author Martin Desruisseaux
056: */
057: public class ResourceInternationalString extends
058: AbstractInternationalString implements Serializable {
059: /**
060: * Serial number for interoperability with different versions.
061: */
062: private static final long serialVersionUID = 6339944890723487336L;
063:
064: /**
065: * The name of the resource bundle from which to fetch the string.
066: */
067: private final String resources;
068:
069: /**
070: * The key for the resource to fetch.
071: */
072: private final String key;
073:
074: /**
075: * Creates a new international string from the specified resource bundle and key.
076: *
077: * @param resources The name of the resource bundle, as a fully qualified class name.
078: * @param key The key for the resource to fetch.
079: */
080: public ResourceInternationalString(final String resources,
081: final String key) {
082: this .resources = resources;
083: this .key = key;
084: ensureNonNull("resources", resources);
085: ensureNonNull("key", key);
086: }
087:
088: /**
089: * Returns a string in the specified locale. If there is no string for the specified
090: * {@code locale}, then this method search for a string in an other locale as
091: * specified in the {@link ResourceBundle} class description.
092: *
093: * @param locale The locale to look for, or {@code null} for an unlocalized version.
094: * @return The string in the specified locale, or in a default locale.
095: * @throws MissingResourceException is the key given to the constructor is invalid.
096: */
097: public String toString(Locale locale)
098: throws MissingResourceException {
099: if (locale == null) {
100: // The English locale (NOT the system default) is often used
101: // as the real identifier in OGC IdentifiedObject naming. If
102: // a user wants a string in the system default locale, he
103: // should invokes the 'toString()' method instead.
104: locale = Locale.ENGLISH;
105: }
106: return ResourceBundle.getBundle(resources, locale).getString(
107: key);
108: }
109:
110: /**
111: * Compares this international string with the specified object for equality.
112: */
113: public boolean equals(final Object object) {
114: if (object != null && object.getClass().equals(getClass())) {
115: final ResourceInternationalString that = (ResourceInternationalString) object;
116: return Utilities.equals(this .key, that.key)
117: && Utilities.equals(this .resources, that.resources);
118: }
119: return false;
120: }
121:
122: /**
123: * Returns a hash code value for this international text.
124: */
125: public int hashCode() {
126: return (int) serialVersionUID ^ key.hashCode()
127: ^ resources.hashCode();
128: }
129: }
|