cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject svn commit: r474832 - in /cocoon/branches/BRANCH_2_1_X: src/java/org/apache/cocoon/i18n/I18nUtils.java src/java/org/apache/cocoon/transformation/I18nTransformer.java src/webapp/samples/i18n/sitemap.xmap src/webapp/samples/sitemap.xmap status.xml
Date Tue, 14 Nov 2006 15:56:44 GMT
Author: vgritsenko
Date: Tue Nov 14 07:56:43 2006
New Revision: 474832

URL: http://svn.apache.org/viewvc?view=rev&rev=474832
Log:
    <action dev="VG" type="add">
      Core: Add ability to pre-load i18n catalogues in I18nTransformer.
      Removed references to defunct cache-on-startup configuration.
    </action>


Modified:
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/i18n/I18nUtils.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/I18nTransformer.java
    cocoon/branches/BRANCH_2_1_X/src/webapp/samples/i18n/sitemap.xmap
    cocoon/branches/BRANCH_2_1_X/src/webapp/samples/sitemap.xmap
    cocoon/branches/BRANCH_2_1_X/status.xml

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/i18n/I18nUtils.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/i18n/I18nUtils.java?view=diff&rev=474832&r1=474831&r2=474832
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/i18n/I18nUtils.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/i18n/I18nUtils.java Tue Nov 14
07:56:43 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;
@@ -40,7 +39,9 @@
  */
 public class I18nUtils {
 
-    // Locale string delimiter
+    /**
+     * Locale string delimiter
+     */
     private static final String LOCALE_DELIMITER = "_-@.";
 
     /**
@@ -49,6 +50,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
     }
@@ -251,13 +265,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/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/I18nTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/I18nTransformer.java?view=diff&rev=474832&r1=474831&r2=474832
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/I18nTransformer.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/I18nTransformer.java
Tue Nov 14 07:56:43 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>
@@ -266,7 +269,7 @@
  * @author <a href="mailto:mattam@netcourrier.com">Matthieu Sozeau</a>
  * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
  * @author <a href="mailto:Michael.Enke@wincor-nixdorf.com">Michael Enke</a>
- * @version CVS $Id$
+ * @version $Id$
  */
 public class I18nTransformer extends AbstractTransformer
                              implements CacheableProcessingComponent,
@@ -275,14 +278,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
@@ -718,10 +719,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
@@ -967,8 +968,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
@@ -980,14 +981,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()) {
@@ -998,10 +998,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];
@@ -1018,8 +1018,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);
         }
@@ -1027,8 +1027,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
@@ -1036,6 +1036,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;
     }
 
     /**

Modified: cocoon/branches/BRANCH_2_1_X/src/webapp/samples/i18n/sitemap.xmap
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/webapp/samples/i18n/sitemap.xmap?view=diff&rev=474832&r1=474831&r2=474832
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/webapp/samples/i18n/sitemap.xmap (original)
+++ cocoon/branches/BRANCH_2_1_X/src/webapp/samples/i18n/sitemap.xmap Tue Nov 14 07:56:43
2006
@@ -24,23 +24,30 @@
   <map:components>
     <map:generators default="file">
       <!-- redefine the generators to remove their labels (otherwise aggregated parts
also go through views) -->
-      <map:generator name="file" src="org.apache.cocoon.generation.FileGenerator" logger="sitemap.generator.file"/>
-      <map:generator name="serverpages" src="org.apache.cocoon.generation.ServerPagesGenerator"
logger="sitemap.generator.serverpages"/>
+      <map:generator name="file"
+                     logger="sitemap.generator.file"
+                     src="org.apache.cocoon.generation.FileGenerator"/>
+      <map:generator name="serverpages"
+                     logger="sitemap.generator.serverpages"
+                     src="org.apache.cocoon.generation.ServerPagesGenerator"/>
     </map:generators>
+
     <map:transformers default="xslt">
       <!-- Configure i18n transformer -->
-      <map:transformer name="i18n" logger="sitemap.transformer.i18n" src="org.apache.cocoon.transformation.I18nTransformer">
+      <map:transformer name="i18n"
+                       logger="sitemap.transformer.i18n"
+                       src="org.apache.cocoon.transformation.I18nTransformer">
         <catalogues default="messages">
           <catalogue id="messages" name="messages" location="translations"/>
           <catalogue id="menu" name="menu" location="translations"/>
           <catalogue id="tiered" name="messages">
-        <location>translations/tiered</location>
-        <location>translations</location>
-      </catalogue>
+            <location>translations/tiered</location>
+            <location>translations</location>
+          </catalogue>
         </catalogues>
-        <cache-at-startup>true</cache-at-startup>
       </map:transformer>
     </map:transformers>
+
     <map:actions>
       <!-- LocaleAction is used to set/get current users locale. See Javadocs for details.
-->
       <map:action name="locale" logger="sitemap.action.locale" src="org.apache.cocoon.acting.LocaleAction">

Modified: cocoon/branches/BRANCH_2_1_X/src/webapp/samples/sitemap.xmap
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/webapp/samples/sitemap.xmap?view=diff&rev=474832&r1=474831&r2=474832
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/webapp/samples/sitemap.xmap (original)
+++ cocoon/branches/BRANCH_2_1_X/src/webapp/samples/sitemap.xmap Tue Nov 14 07:56:43 2006
@@ -32,7 +32,6 @@
         <catalogues default="messages">
           <catalogue id="messages" name="messages" location="common/i18n"/>
         </catalogues>
-        <cache-at-startup>true</cache-at-startup>
       </map:transformer>
     </map:transformers>
   </map:components>

Modified: cocoon/branches/BRANCH_2_1_X/status.xml
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/status.xml?view=diff&rev=474832&r1=474831&r2=474832
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/status.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/status.xml Tue Nov 14 07:56:43 2006
@@ -184,6 +184,10 @@
   <release version="@version@" date="@date@">
 -->
   <release version="2.1.10" date="TBD">
+    <action dev="VG" type="add">
+      Core: Add ability to pre-load i18n catalogues in I18nTransformer.
+      Removed references to defunct cache-on-startup configuration.
+    </action>
     <action dev="JJ" type="add" fixes-bug="COCOON-1879">
       CForms: Added @whitespace attribute to fd:field widget, to control how leading
       and trailing whitespace characters are handled when reading submitted values.



Mime
View raw message