cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject svn commit: r474847 - in /cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon: i18n/I18nUtils.java transformation/I18nTransformer.java
Date Tue, 14 Nov 2006 16:15:24 GMT
Author: vgritsenko
Date: Tue Nov 14 08:15:23 2006
New Revision: 474847

URL: http://svn.apache.org/viewvc?view=rev&rev=474847
Log:
from branch - remove cache-on-startup, add preload.

Modified:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/i18n/I18nUtils.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/transformation/I18nTransformer.java

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/i18n/I18nUtils.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/i18n/I18nUtils.java?view=diff&rev=474847&r1=474846&r2=474847
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/i18n/I18nUtils.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/i18n/I18nUtils.java Tue
Nov 14 08:15:23 2006
@@ -23,7 +23,6 @@
 import org.apache.cocoon.environment.Request;
 import org.apache.cocoon.environment.Session;
 import org.apache.cocoon.environment.Response;
-import org.apache.cocoon.transformation.I18nTransformer;
 import org.apache.cocoon.util.Deprecation;
 
 import java.util.Enumeration;
@@ -38,7 +37,9 @@
  */
 public class I18nUtils {
 
-    // Locale string delimiter
+    /**
+     * Locale string delimiter
+     */
     private static final String LOCALE_DELIMITER = "_-@.";
 
     /**
@@ -47,6 +48,19 @@
      */
     private static boolean deprecationFound = false;
 
+    /**
+     * The namespace for i18n is "http://apache.org/cocoon/i18n/2.1".
+     */
+    public static final String NAMESPACE_URI =
+            "http://apache.org/cocoon/i18n/2.1";
+
+    /**
+     * The old namespace for i18n is "http://apache.org/cocoon/i18n/2.0".
+     */
+    public static final String OLD_NAMESPACE_URI =
+            "http://apache.org/cocoon/i18n/2.0";
+
+
     private I18nUtils() {
         // Disable instantiation
     }
@@ -249,13 +263,13 @@
     }
 
     public static boolean matchesI18nNamespace(String uri) {
-        if (I18nTransformer.I18N_NAMESPACE_URI.equals(uri)) {
+        if (NAMESPACE_URI.equals(uri)) {
             return true;
-        } else if (I18nTransformer.I18N_OLD_NAMESPACE_URI.equals(uri)) {
+        } else if (OLD_NAMESPACE_URI.equals(uri)) {
             if (!deprecationFound) {
                 deprecationFound = true;
-                Deprecation.logger.warn("The namespace <" + I18nTransformer.I18N_OLD_NAMESPACE_URI
+
-                                        "> is deprecated, use: <" + I18nTransformer.I18N_NAMESPACE_URI
+ ">");
+                Deprecation.logger.warn("The namespace <" + OLD_NAMESPACE_URI +
+                                        "> is deprecated, use: <" + NAMESPACE_URI +
">");
             }
             return true;
         }

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/transformation/I18nTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/transformation/I18nTransformer.java?view=diff&rev=474847&r1=474846&r2=474847
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/transformation/I18nTransformer.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/transformation/I18nTransformer.java
Tue Nov 14 08:15:23 2006
@@ -70,11 +70,11 @@
  * @cocoon.sitemap.component.documentation.caching TBD
  * @cocoon.sitemap.component.logger sitemap.transformer.i18n
  *
- * <h3>i18n transformer</h3>
+ * <h3>I18n Transformer</h3>
  * <p>The i18n transformer works by finding a translation for the user's locale
- * in the configured catalogues. Locale is determined based on the request,
- * session, or a cookie data. See {@link org.apache.cocoon.acting.LocaleAction}
- * for details.</p>
+ * in the configured catalogues. Locale is passed as parameter to the transformer,
+ * and it can be determined based on the request, session, or a cookie data by
+ * the {@link org.apache.cocoon.acting.LocaleAction}.</p>
  *
  * <p>For the passed local it then attempts to find a message catalogue that
  * satisifies the locale, and uses it for for processing text replacement
@@ -156,7 +156,7 @@
  * The value of this attribute should be the id of the catalogue to use
  * (see sitemap configuration).
  *
- * <h3>Sitemap configuration</h3>
+ * <h3>Sitemap Configuration</h3>
  * <pre>
  * &lt;map:transformer name="i18n"
  *     src="org.apache.cocoon.transformation.I18nTransformer"&gt;
@@ -169,7 +169,8 @@
  *       ...
  *     &lt;/catalogues&gt;
  *     &lt;untranslated-text&gt;untranslated&lt;/untranslated-text&gt;
- *     &lt;cache-at-startup&gt;true&lt;/cache-at-startup&gt;
+ *     &lt;preload&gt;en_US&lt;/preload&gt;
+ *     &lt;preload catalogue="someId"&gt;fr_CA&lt;/preload&gt;
  * &lt;/map:transformer&gt;
  * </pre>
  * Where:
@@ -193,8 +194,10 @@
  *      cocoon:/test/messages_en.xml and cocoon:/test/messages.xml.
  *  <li><strong>untranslated-text</strong>: text used for
  *      untranslated keys (default is to output the key name).
- *  <li><strong>cache-at-startup</strong>: flag whether to cache
- *      messages at startup (false by default).
+ *  <li><strong>preload</strong>: locale of the catalogue to preload. Will
attempt
+ *      to resolve all configured catalogues for specified locale. If optional
+ *      <code>catalogue</code> attribute is present, will preload only specified
+ *      catalogue. Multiple <code>preload</code> elements can be specified.
  * </ul>
  *
  * <h3>Pipeline Usage</h3>
@@ -271,14 +274,12 @@
     /**
      * The namespace for i18n is "http://apache.org/cocoon/i18n/2.1".
      */
-    public static final String I18N_NAMESPACE_URI =
-            "http://apache.org/cocoon/i18n/2.1";
+    public static final String I18N_NAMESPACE_URI = I18nUtils.NAMESPACE_URI;
 
     /**
      * The old namespace for i18n is "http://apache.org/cocoon/i18n/2.0".
      */
-    public static final String I18N_OLD_NAMESPACE_URI =
-            "http://apache.org/cocoon/i18n/2.0";
+    public static final String I18N_OLD_NAMESPACE_URI = I18nUtils.OLD_NAMESPACE_URI;
 
     //
     // i18n elements
@@ -714,10 +715,10 @@
     public static final String I18N_UNTRANSLATED        = "untranslated-text";
 
     /**
-     * This configuration parameter specifies if the message catalog should be
-     * cached at startup.
+     * This configuration parameter specifies locale for which catalogues should
+     * be preloaded.
      */
-    public static final String I18N_CACHE_STARTUP       = "cache-at-startup";
+    public static final String I18N_PRELOAD             = "preload";
 
     /**
      * <code>fraction-digits</code> attribute is used with
@@ -963,8 +964,8 @@
         // Read in the config options from the transformer definition
         Configuration cataloguesConf = conf.getChild("catalogues", false);
         if (cataloguesConf == null) {
-            throw new ConfigurationException("I18nTransformer requires <catalogues>
configuration at " +
-                                             conf.getLocation());
+            throw new ConfigurationException("Required <catalogues> configuration is
missing.",
+                                             conf);
         }
 
         // new configuration style
@@ -976,14 +977,13 @@
 
             String[] locations;
             String location = catalogueConfs[i].getAttribute("location", null);
-            Configuration[] locationConf =
-                catalogueConfs[i].getChildren("location");
+            Configuration[] locationConf = catalogueConfs[i].getChildren("location");
             if (location != null) {
                 if (locationConf.length > 0) {
-                    String msg = "I18nTransformer: Location attribute cannot be " +
+                    String msg = "Location attribute cannot be " +
                                  "specified with location elements";
                     getLogger().error(msg);
-                    throw new ConfigurationException(msg);
+                    throw new ConfigurationException(msg, catalogueConfs[i]);
                 }
 
                 if (getLogger().isDebugEnabled()) {
@@ -994,10 +994,10 @@
                 locations[0] = location;
             } else {
                 if (locationConf.length == 0) {
-                    String msg = "I18nTransformer: A location attribute or location " +
+                    String msg = "A location attribute or location " +
                                  "elements must be specified";
                     getLogger().error(msg);
-                    throw new ConfigurationException(msg);
+                    throw new ConfigurationException(msg, catalogueConfs[i]);
                 }
 
                 locations = new String[locationConf.length];
@@ -1014,8 +1014,8 @@
             try {
                 catalogueInfo = new CatalogueInfo(name, locations);
             } catch (PatternException e) {
-                throw new ConfigurationException("I18nTransformer: Error in name or location
" +
-                                                 "attribute on catalogue element with id
" + id, e);
+                throw new ConfigurationException("Error in name or location attribute on
catalogue " +
+                                                 "element with id " + id, catalogueConfs[i],
e);
             }
             catalogues.put(id, catalogueInfo);
         }
@@ -1023,8 +1023,8 @@
         String defaultCatalogueId = cataloguesConf.getAttribute("default");
         defaultCatalogue = (CatalogueInfo) catalogues.get(defaultCatalogueId);
         if (defaultCatalogue == null) {
-            throw new ConfigurationException("I18nTransformer: Default catalogue id '" +
-                                             defaultCatalogueId + "' denotes a nonexisting
catalogue");
+            throw new ConfigurationException("Default catalogue id '" + defaultCatalogueId
+
+                                             "' denotes a nonexisting catalogue", cataloguesConf);
         }
 
         // Obtain default text to use for untranslated messages
@@ -1032,6 +1032,38 @@
         if (getLogger().isDebugEnabled()) {
             getLogger().debug("Default untranslated text is '" + defaultUntranslated + "'");
         }
+
+        // Preload specified catalogues (if any)
+        Configuration[] preloadConfs = conf.getChildren(I18N_PRELOAD);
+        for (int i = 0; i < preloadConfs.length; i++) {
+            String localeStr = preloadConfs[i].getValue();
+            this.locale = I18nUtils.parseLocale(localeStr);
+
+            String id = preloadConfs[i].getAttribute("catalogue", null);
+            if (id != null) {
+                CatalogueInfo catalogueInfo = (CatalogueInfo) catalogues.get(id);
+                if (catalogueInfo == null) {
+                    throw new ConfigurationException("Invalid catalogue id '" + id +
+                                                     "' in preload element.", preloadConfs[i]);
+                }
+
+                try {
+                    catalogueInfo.getCatalogue();
+                } finally {
+                    catalogueInfo.releaseCatalog();
+                }
+            } else {
+                for (Iterator j = catalogues.values().iterator(); j.hasNext(); ) {
+                    CatalogueInfo catalogueInfo = (CatalogueInfo) j.next();
+                    try {
+                        catalogueInfo.getCatalogue();
+                    } finally {
+                        catalogueInfo.releaseCatalog();
+                    }
+                }
+            }
+        }
+        this.locale = null;
     }
 
     /**



Mime
View raw message