resourceNotFoundMessagesShown = FastSet.newInstance();
@@ -522,7 +523,7 @@ public class UtilProperties implements java.io.Serializable {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
ResourceBundle bundle = null;
try {
- bundle = XmlResourceBundle.getBundle(resource, locale, loader);
+ bundle = UtilResourceBundle.getBundle(resource, locale, loader);
} catch (MissingResourceException e) {
String resourceFullName = resource + "_" + locale.toString();
if (!resourceNotFoundMessagesShown.contains(resourceFullName)) {
@@ -543,75 +544,220 @@ public class UtilProperties implements java.io.Serializable {
return bundle;
}
- /** Returns the specified resource/properties file
- *
- * NOTE: This is NOT fully implemented yet to fulfill all of the requirements
- * for i18n messages. Do NOT use.
- *
- * To be used in an i18n context this still needs to be extended quite
- * a bit. The behavior needed is that for each getMessage the most specific
- * locale (with fname_en_US for instance) is searched first, then the next
- * less specific (fname_en for instance), then without the locale if it is
- * still not found (plain fname for example, not that these examples would
- * have .properties appended to them).
- * This would be accomplished by returning the following structure:
- * 1. Get "fname" FlexibleProperties object
- * 2. Get the "fname_en" FlexibleProperties object and if the "fname" one
- * is not null, set it as the default/parent of the "fname_en" object
- * 3. Get the "fname_en_US" FlexibleProperties object and if the
- * "fname_en" one is not null, set it as the default/parent of the
- * "fname_en_US" object; if the "fname_en" one is null, but the "fname"
- * one is not, set the "fname" object as the default/parent of the
- * "fname_en_US" object
- * Then return the fname_en_US object if not null, else the fname_en, else the fname.
- *
- * To make this all more fun, the default locale should be the parent of
- * the "fname" object in this example so that there is an even higher
- * chance of finding something for each request.
- *
- * For efficiency all of these should be cached indendependently so the same
- * instance can be shared, speeding up loading time/efficiency.
- *
- * All of this should work with the setDefaultProperties method of the
- * FlexibleProperties class, but it should be tested and updated as
- * necessary. It's a bit tricky, so chances are it won't work as desired...
- *
+ /** Returns the specified resource/properties file.Note that this method
+ * will return a Properties instance for the specified locale only -
+ * if you need I18n properties, then use
+ *
+ * getResourceBundle(String resource, Locale locale). This method is
+ * intended to be used primarily by the UtilProperties class.
* @param resource The name of the resource - can be a file, class, or URL
- * @param locale The locale that the given resource will correspond to
- * @return The Properties class
+ * @param locale The desired locale
+ * @return The Properties instance, or null if no matching properties are found
*/
public static Properties getProperties(String resource, Locale locale) {
- if (resource == null || resource.length() <= 0) return null;
- if (locale == null) locale = Locale.getDefault();
-
- String localeString = locale.toString();
- String resourceLocale = resource + "_" + localeString;
- FlexibleProperties properties = resourceCache.get(resourceLocale);
-
- if (properties == null) {
+ if (UtilValidate.isEmpty(resource)) {
+ throw new IllegalArgumentException("resource cannot be null or empty");
+ }
+ if (locale == null) {
+ throw new IllegalArgumentException("locale cannot be null");
+ }
+ Properties properties = null;
+ URL url = resolvePropertiesUrl(resource, locale);
+ if (url != null) {
try {
- URL url = UtilURL.fromResource(resourceLocale);
- if (url == null) {
- properties = (FlexibleProperties) getProperties(resource);
- } else {
- properties = FlexibleProperties.makeFlexibleProperties(url);
+ properties = new ExtendedProperties(url, locale);
+ } catch (Exception e) {
+ if (UtilValidate.isNotEmpty(e.getMessage())) {
+ Debug.log(e.getMessage(), module);
}
- } catch (MissingResourceException e) {
- Debug.log(e.getMessage(), module);
+ properties = null;
}
- resourceCache.put(resourceLocale, properties);
+ }
+ if (UtilValidate.isNotEmpty(properties)) {
+ Debug.logInfo("Loaded " + properties.size() + " properties for: " + resource + " (" + locale + ")", module);
}
-
- if (properties == null)
- Debug.logInfo("[UtilProperties.getProperties] could not find resource: " + resource + ", locale: " + locale, module);
-
return properties;
}
-
- /**
- * Custom ResourceBundle class. This class reads XML properties files and supports
- * the OFBiz component:// location protocol.The format of the XML
- * properties file is:
+
+ // ========= Classes and Methods for expanded Properties file support ========== //
+
+ public static final Locale LOCALE_ROOT = new Locale("", "", "");
+
+ protected static Locale fallbackLocale = null;
+ /** Returns the configured fallback locale. UtilProperties uses this locale
+ * to resolve locale-specific XML properties.The fallback locale can be
+ * configured using the locale.properties.fallback property in
+ * general.properties.
+ * @return The configured fallback locale
+ */
+ public static Locale getFallbackLocale() {
+ if (fallbackLocale == null) {
+ synchronized (UtilResourceBundle.class) {
+ if (fallbackLocale == null) {
+ String locale = getPropertyValue("general", "locale.properties.fallback");
+ if (UtilValidate.isNotEmpty(locale)) {
+ fallbackLocale = UtilMisc.parseLocale(locale);
+ }
+ if (fallbackLocale == null) {
+ fallbackLocale = UtilMisc.parseLocale("en");
+ }
+ }
+ }
+ }
+ return fallbackLocale;
+ }
+
+ /** Converts a Locale instance to a candidate Locale list. The list
+ * is ordered most-specific to least-specific. Example:
+ * localeToCandidateList(Locale.US) would return
+ * a list containing en_US and en.
+ * @return A list of default candidate locales.
+ */
+ public static List localeToCandidateList(Locale locale) {
+ List localeList = FastList.newInstance();
+ localeList.add(locale);
+ String localeString = locale.toString();
+ int pos = localeString.lastIndexOf("_", localeString.length());
+ while (pos != -1) {
+ localeString = localeString.substring(0, pos);
+ localeList.add(new Locale(localeString));
+ pos = localeString.lastIndexOf("_", localeString.length());
+ }
+ return localeList;
+ }
+
+ protected static Set defaultCandidateLocales = null;
+ /** Returns the default candidate Locale list. The list is populated
+ * with the JVM's default locale, the OFBiz fallback locale, and
+ * the LOCALE_ROOT (empty) locale - in that order.
+ * @return A list of default candidate locales.
+ */
+ public static Set getDefaultCandidateLocales() {
+ if (defaultCandidateLocales == null) {
+ synchronized (UtilProperties.class) {
+ if (defaultCandidateLocales == null) {
+ defaultCandidateLocales = FastSet.newInstance();
+ defaultCandidateLocales.addAll(localeToCandidateList(Locale.getDefault()));
+ // Change to Locale.ROOT in Java 6
+ defaultCandidateLocales.add(LOCALE_ROOT);
+ defaultCandidateLocales.addAll(localeToCandidateList(getFallbackLocale()));
+ }
+ }
+ }
+ return defaultCandidateLocales;
+ }
+
+ /** Returns a list of candidate locales based on a supplied locale.
+ * The returned list consists of the supplied locale and the
+ * default candidate locales
+ * - in that order.
+ * @param locale The desired locale
+ * @return A list of candidate locales
+ */
+ public static List getCandidateLocales(Locale locale) {
+ // Java 6 conformance
+ if (LOCALE_ROOT.equals(locale)) {
+ return UtilMisc.toList(locale);
+ }
+ Set localeSet = FastSet.newInstance();
+ localeSet.addAll(localeToCandidateList(locale));
+ localeSet.addAll(getDefaultCandidateLocales());
+ List localeList = FastList.newInstance();
+ localeList.addAll(localeSet);
+ return localeList;
+ }
+
+ /** Create a localized resource name based on a resource name and
+ * a locale.
+ * @param resource The desired resource
+ * @param locale The desired locale
+ * @return Localized resource name
+ */
+ public static String createResourceName(String resource, Locale locale) {
+ if (locale == null) {
+ return resource;
+ }
+ String resourceName = resource;
+ if (UtilValidate.isNotEmpty(locale.toString())) {
+ resourceName = resourceName + "_" + locale;
+ }
+ return resourceName;
+ }
+
+ protected static Set propertiesNotFound = FastSet.newInstance();
+ /** Resolve a properties file URL.
+ * This method uses the following strategy:
+ *
+ * - Locate the XML file specified in
resource (MyProps.xml)
+ * - Locate the file that starts with the name specified in
+ *
resource and ends with the locale's string and
+ * .xml (MyProps_en.xml)
+ * - Locate the file that starts with the name specified in
+ *
resource and ends with the locale's string and
+ * .properties (MyProps_en.properties)
+ * - Locate the file that starts with the name specified in
+ *
resource and ends with the locale's string (MyProps_en)
+ *
+ *
+ * The component:// protocol is supported in the
+ * resource parameter.
+ *
+ *
+ * @param resource The resource to resolve
+ * @param locale The desired locale
+ * @return A URL instance or null if not found.
+ */
+ public static URL resolvePropertiesUrl(String resource, Locale locale) {
+ if (UtilValidate.isEmpty(resource)) {
+ throw new IllegalArgumentException("resource cannot be null or empty");
+ }
+ if (locale == null) {
+ throw new IllegalArgumentException("locale cannot be null");
+ }
+ String resourceName = createResourceName(resource, locale);
+ if (propertiesNotFound.contains(resourceName)) {
+ return null;
+ }
+ URL url = null;
+ try {
+ // Check for complete URL first
+ if (resource.endsWith(".xml")) {
+ url = FlexibleLocation.resolveLocation(resource);
+ if (url != null) {
+ return url;
+ }
+ }
+ // Check for XML properties file next
+ url = FlexibleLocation.resolveLocation(resourceName + ".xml");
+ if (url != null) {
+ return url;
+ }
+ // Check for *.properties file
+ url = FlexibleLocation.resolveLocation(resourceName + ".properties");
+ if (url != null) {
+ return url;
+ }
+ url = FlexibleLocation.resolveLocation(resourceName);
+ if (url != null) {
+ return url;
+ }
+ } catch (Exception e) {
+ Debug.logInfo("Properties resolver: invalid URL - " + e.getMessage(), module);
+ }
+ if (propertiesNotFound.size() <= 300) {
+ // Sanity check - list could get quite large
+ propertiesNotFound.add(resourceName);
+ }
+ return null;
+ }
+
+ /** Convert XML property file to Properties instance. This method will convert
+ * both the Java XML properties file format and the OFBiz custom XML
+ * properties file format.
+ *
+ * The format of the custom XML properties file is:
+ *
+ *
* <resource>
* <property key="key">
* <value xml:lang="locale 1">Some value</value>
@@ -619,29 +765,80 @@ public class UtilProperties implements java.io.Serializable {
* ...
* </property>
* ...
- * </resource>
- * where "locale 1", "locale 2" are valid Locale strings.
+ * </resource>
where "locale 1", "locale 2" are valid Locale strings.
+ *
+ *
+ * @param in XML file InputStream
+ * @param locale The desired locale
+ * @param properties Optional Properties object to populate
+ * @return Properties instance or null if not found
*/
- public static class XmlResourceBundle extends ResourceBundle {
- protected Map propertyMap = null;
- protected String locale = null;
+ public static Properties xmlToProperties(InputStream in, Locale locale, Properties properties) throws IOException, InvalidPropertiesFormatException {
+ if (in == null) {
+ throw new IllegalArgumentException("InputStream cannot be null");
+ }
+ Document doc = null;
+ try {
+ // set validation true when we have a DTD for the custom XML format
+ doc = UtilXml.readXmlDocument(in, false, "XML Properties file");
+ in.close();
+ } catch (Exception e) {
+ in.close();
+ return null;
+ }
+ Element resourceElement = doc.getDocumentElement();
+ List extends Element> propertyList = UtilXml.childElementList(resourceElement, "property");
+ if (UtilValidate.isNotEmpty(propertyList)) {
+ // Custom XML properties file format
+ if (locale == null) {
+ throw new IllegalArgumentException("locale cannot be null");
+ }
+ String localeString = locale.toString();
+ for (Iterator extends Element> p = propertyList.iterator(); p.hasNext();) {
+ Element property = p.next();
+ Element value = UtilXml.firstChildElement(property, "value", "xml:lang", localeString);
+ if (value != null) {
+ if (properties == null) {
+ properties = new Properties();
+ }
+ properties.put(property.getAttribute("key"), UtilXml.elementValue(value));
+ }
+ }
+ return properties;
+ }
+ propertyList = UtilXml.childElementList(resourceElement, "entry");
+ if (UtilValidate.isEmpty(propertyList)) {
+ throw new InvalidPropertiesFormatException("XML properties file invalid or empty");
+ }
+ // Java XML properties file format
+ for (Iterator extends Element> p = propertyList.iterator(); p.hasNext();) {
+ Element property = p.next();
+ String value = UtilXml.elementValue(property);
+ if (value != null) {
+ if (properties == null) {
+ properties = new Properties();
+ }
+ properties.put(property.getAttribute("key"), value);
+ }
+ }
+ return properties;
+ }
- protected XmlResourceBundle() {}
+ /** Custom ResourceBundle class. This class extends ResourceBundle
+ * to add support for the OFBiz custom XML properties file format.
+ */
+ public static class UtilResourceBundle extends ResourceBundle {
+ protected Properties properties = null;
+ protected Locale locale = null;
+
+ protected UtilResourceBundle() {}
- public XmlResourceBundle(Map propertyMap, String locale, ResourceBundle parent) {
- this.propertyMap = propertyMap;
+ public UtilResourceBundle(Properties properties, Locale locale, ResourceBundle parent) {
+ this.properties = properties;
this.locale = locale;
- this.parent = parent;
+ setParent(parent);
}
- /**
- * Get ResourceBundle. This method override behaves differently than the
- * ResourceBundle method. The method calls ResourceBundle.getBundle(...) first
- * - to preserve the original behavior. If null is returned, then the method
- * attempts to get an XML resource. The resource search starts with the
- * specified locale, then the system locale, and finally the locale specified
- * in the general.properties locale.properties.fallback property.
- */
public static ResourceBundle getBundle(String resource, Locale locale, ClassLoader loader) throws MissingResourceException {
ResourceBundle bundle = null;
try {
@@ -652,107 +849,81 @@ public class UtilProperties implements java.io.Serializable {
if (bundle != null) {
return bundle;
}
- try {
- URL url = FlexibleLocation.resolveLocation(resource, loader);
- if (url == null) {
- url = FlexibleLocation.resolveLocation(resource + ".xml");
- }
- if (url != null) {
- Document doc = UtilXml.readXmlDocument(url);
- bundle = createBundle(doc, locale.toString());
- if (bundle == null && !Locale.getDefault().toString().equals(locale.toString())) {
- bundle = createBundle(doc, Locale.getDefault().toString());
- }
- if (bundle == null && !getFallbackLocale().toString().equals(locale.toString())) {
- bundle = createBundle(doc, getFallbackLocale().toString());
+ double startTime = System.currentTimeMillis();
+ FastList candidateLocales = (FastList) getCandidateLocales(locale);
+ ResourceBundle parentBundle = null;
+ synchronized (bundleLocaleCache) {
+ while (candidateLocales.size() > 0) {
+ Locale candidateLocale = candidateLocales.removeLast();
+ // ResourceBundles are connected together as a singly-linked list
+ String parentName = createResourceName(resource, candidateLocale);
+ ResourceBundle lookupBundle = bundleLocaleCache.get(parentName);
+ if (lookupBundle == null) {
+ Properties newProps = getProperties(resource, candidateLocale);
+ if (UtilValidate.isNotEmpty(newProps)) {
+ bundle = new UtilResourceBundle(newProps, candidateLocale, parentBundle);
+ bundleLocaleCache.put(parentName, bundle);
+ parentBundle = bundle;
+ }
+ } else {
+ parentBundle = bundle;
+ bundle = lookupBundle;
}
}
- } catch (Exception e) {
- throw new MissingResourceException(e.getMessage(), null, null);
}
+ double totalTime = System.currentTimeMillis() - startTime;
if (bundle == null) {
- throw new MissingResourceException("Resource " + resource + " not found", null, null);
+ throw new MissingResourceException("Resource " + resource + ", locale " + locale + " not found", null, null);
}
+ Debug.logInfo("ResourceBundle " + resource + " (" + locale + ") created in " + totalTime + " mS", module);
return bundle;
}
- /**
- * Creates a new XmlResourceBundle.
- * @param doc The XML Document instance to search
- * @param localeString
- * @return new ResourceBundle, or null if no matching resource found
- * @throws Exception Invalid properties XML file
- */
- protected static ResourceBundle createBundle(Document doc, String localeString) throws Exception {
- Map propertyMap = null;
- Element resourceElement = doc.getDocumentElement();
- List propertyList = UtilXml.childElementList(resourceElement, "property");
- if (propertyList == null) {
- throw new Exception("XML properties file invalid or empty");
- }
- int pos = 0;
- while (pos != -1) {
- for (Iterator p = propertyList.iterator(); p.hasNext();) {
- Element property = (Element) p.next();
- Element value = UtilXml.firstChildElement(property, "value", "xml:lang", localeString);
- if (value != null) {
- if (propertyMap == null) {
- propertyMap = FastMap.newInstance();
- }
- propertyMap.put(property.getAttribute("key"), UtilXml.elementValue(value));
- }
- }
- if (propertyMap != null) {
- return new XmlResourceBundle(propertyMap, localeString, null);
- }
- pos = localeString.lastIndexOf("_", localeString.length());
- if (pos != -1) {
- localeString = localeString.substring(0, pos);
- }
- }
- return null;
- }
-
public Locale getLocale() {
- return UtilMisc.ensureLocale(this.locale);
+ return this.locale;
}
protected Object handleGetObject(String key) {
- Object obj = propertyMap.get(key);
- if (obj == null && this.parent != null) {
- obj = parent.getObject(key);
- }
- return obj;
+ return properties.get(key);
}
- public Enumeration getKeys() {
- return new Enumeration() {
- Iterator i = propertyMap.keySet().iterator();
+ public Enumeration getKeys() {
+ return new Enumeration() {
+ Iterator i = properties.keySet().iterator();
public boolean hasMoreElements() {
return (i.hasNext());
}
- public Object nextElement() {
- return i.next();
+ public String nextElement() {
+ return (String) i.next();
}
};
}
- protected static Locale fallbackLocale = null;
- public static Locale getFallbackLocale() {
- if (fallbackLocale == null) {
- synchronized (XmlResourceBundle.class) {
- if (fallbackLocale == null) {
- String locale = UtilProperties.getPropertyValue("general", "locale.properties.fallback");
- if (UtilValidate.isNotEmpty(locale)) {
- fallbackLocale = UtilMisc.parseLocale(locale);
- }
- if (fallbackLocale == null) {
- fallbackLocale = UtilMisc.parseLocale("en");
- }
- }
- }
+ }
+
+ /** Custom Properties class. Extended from Properties to add support
+ * for the OFBiz custom XML file format.
+ */
+ @SuppressWarnings("serial")
+ public static class ExtendedProperties extends Properties {
+ public ExtendedProperties() {
+ super();
+ }
+ public ExtendedProperties(Properties defaults) {
+ super(defaults);
+ }
+ public ExtendedProperties(URL url, Locale locale) throws IOException, InvalidPropertiesFormatException {
+ InputStream in = new BufferedInputStream(url.openStream());
+ if (url.getFile().endsWith(".xml")) {
+ xmlToProperties(in, locale, this);
+ } else {
+ load(in);
}
- return fallbackLocale;
+ in.close();
+ }
+ public void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException {
+ xmlToProperties(in, null, this);
+ in.close();
}
}
}
diff --git a/framework/example/config/ExampleXmlUiLabels.xml b/framework/example/config/ExampleXmlUiLabels.xml
index a7ef08d01c..b2f5546a8e 100644
--- a/framework/example/config/ExampleXmlUiLabels.xml
+++ b/framework/example/config/ExampleXmlUiLabels.xml
@@ -31,8 +31,6 @@ under the License.
Part of the Open For Business Family of Open Source Software
- Part of the Open For Business Family of Open Source Software
- Part of the Open For Business Family of Open Source Software
Example ID
@@ -77,17 +75,13 @@ under the License.
Explicit Option
Opzione Esplicita
- Explicit Option
Form Widget Examples
- Form Widget Examples
- Form Widget Examples
Example Main Page
Pagina Principale Esempi
- Example Main Page
For something interesting make sure you are logged in, try username:admin, password:ofbiz.
@@ -106,8 +100,6 @@ under the License.
Original Example
- Original Example
- Original Example
Error: status change from [${lookedUpValue.statusId}] to [${parameters.statusId}] is not allowed.
@@ -138,8 +130,6 @@ under the License.
Form Widget Examples
- Form Widget Examples
- Form Widget Examples
Edit Example
@@ -211,11 +201,9 @@ under the License.
Another Date
Altra Data
- Another Date
Another Text
Altro Testo
- Another Text
diff --git a/framework/webtools/src/org/ofbiz/webtools/WebToolsServices.java b/framework/webtools/src/org/ofbiz/webtools/WebToolsServices.java
index 8d64ff8174..90a45bb097 100644
--- a/framework/webtools/src/org/ofbiz/webtools/WebToolsServices.java
+++ b/framework/webtools/src/org/ofbiz/webtools/WebToolsServices.java
@@ -51,7 +51,7 @@ import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.StringUtil;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilProperties;
-import org.ofbiz.base.util.UtilProperties.XmlResourceBundle;
+import org.ofbiz.base.util.UtilProperties.UtilResourceBundle;
import org.ofbiz.base.util.UtilURL;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.base.util.UtilDateTime;
@@ -661,7 +661,7 @@ public class WebToolsServices {
ResourceBundle bundle = null;
if (UtilValidate.isNotEmpty(entity.getDefaultResourceName())) {
try {
- bundle = XmlResourceBundle.getBundle(entity.getDefaultResourceName(), locale, loader);
+ bundle = UtilResourceBundle.getBundle(entity.getDefaultResourceName(), locale, loader);
} catch (Exception exception) {
Debug.logInfo(exception.getMessage(), module);
}