cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From andr...@apache.org
Subject svn commit: r1220753 - /cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/I18nTransformer.java
Date Mon, 19 Dec 2011 14:06:00 GMT
Author: andreas
Date: Mon Dec 19 14:06:00 2011
New Revision: 1220753

URL: http://svn.apache.org/viewvc?rev=1220753&view=rev
Log:
Support custom encodings for i18n resource bundles.

Modified:
    cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/I18nTransformer.java

Modified: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/I18nTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/I18nTransformer.java?rev=1220753&r1=1220752&r2=1220753&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/I18nTransformer.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/I18nTransformer.java
Mon Dec 19 14:06:00 2011
@@ -13,6 +13,11 @@
  */
 package org.apache.cocoon.sax.component;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.net.URLConnection;
 import java.text.DateFormat;
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
@@ -26,9 +31,12 @@ import java.util.HashSet;
 import java.util.Locale;
 import java.util.Map;
 import java.util.MissingResourceException;
+import java.util.PropertyResourceBundle;
 import java.util.ResourceBundle;
 import java.util.Set;
 import java.util.StringTokenizer;
+import java.util.ResourceBundle.Control;
+
 import org.apache.cocoon.pipeline.SetupException;
 import org.apache.cocoon.pipeline.caching.CacheKey;
 import org.apache.cocoon.pipeline.caching.ParameterCacheKey;
@@ -559,6 +567,16 @@ public class I18nTransformer extends Abs
     public static final String PARAM_UNTRANSLATED = "untranslated-text";
 
     /**
+     * The encoding of the resource bundle files.
+     */
+    public static final String PARAM_ENCODING = "encoding";
+
+    /**
+     * The default encoding of the resource bundle files.
+     */
+    public static final String DEFAULT_ENCODING = "ISO-8859-1";
+
+    /**
      * States of the transformer.
      */
     private enum TransformerState {
@@ -630,7 +648,7 @@ public class I18nTransformer extends Abs
      * Bundle name.
      */
     private String bundleName;
-
+    
     /**
      * Current (local) untranslated message value
      */
@@ -720,11 +738,11 @@ public class I18nTransformer extends Abs
 
         super();
 
-        this.init(locale, bundle, untranslated);
+        this.init(locale, bundle, untranslated, DEFAULT_ENCODING);
     }
 
     private void init(final Locale locale, final String bundle,
-            final String untranslated) {
+            final String untranslated, final String encoding) {
 
         LOG.debug("Initializing with locale={}, bundle={}, untraslated={}",
                 new Object[]{locale, bundle, untranslated});
@@ -736,7 +754,7 @@ public class I18nTransformer extends Abs
         this.locale = locale == null ? Locale.getDefault() : locale;
 
         try {
-            this.bundle = ResourceBundle.getBundle(bundle, this.locale);
+            this.bundle = ResourceBundle.getBundle(bundle, this.locale, new CustomEncodingControl(encoding));
         } catch (MissingResourceException e) {
             throw new SetupException(e);
         }
@@ -783,10 +801,14 @@ public class I18nTransformer extends Abs
         if (parameters == null || !parameters.containsKey(PARAM_BUNDLE)) {
             return;
         }
+        
+        final String encoding = (String) (parameters.containsKey(PARAM_ENCODING)
+                ? parameters.get(PARAM_ENCODING) : DEFAULT_ENCODING);
 
         this.init(parseLocale((String) parameters.get(PARAM_LOCALE)),
                 (String) parameters.get(PARAM_BUNDLE),
-                (String) parameters.get(PARAM_UNTRANSLATED));
+                (String) parameters.get(PARAM_UNTRANSLATED),
+                (String) parameters.get(PARAM_ENCODING));
     }
 
     public CacheKey constructCacheKey() {
@@ -1906,4 +1928,50 @@ public class I18nTransformer extends Abs
 
         return result;
     }
+
+    /**
+     * Support for custom encodings in bundle files.
+     */
+    public class CustomEncodingControl extends Control {
+        
+        private String encoding;
+
+        public CustomEncodingControl(final String encoding) {
+            this.encoding = encoding;
+        }
+        
+        public ResourceBundle newBundle(final String baseName, final Locale locale, final
String format,
+                final ClassLoader loader, final boolean reload) throws IllegalAccessException,
+                InstantiationException, IOException {
+            // This is a copy of the default implementation.
+            final String bundleName = toBundleName(baseName, locale);
+            final String resourceName = toResourceName(bundleName, "properties");
+            ResourceBundle bundle = null;
+            InputStream stream = null;
+            if (reload) {
+                // use parent classloader because the current classloader can't access
+                // the resource bundle in RCL mode
+                final URL url = loader.getParent().getResource(resourceName);
+                if (url != null) {
+                    final URLConnection connection = url.openConnection();
+                    if (connection != null) {
+                        connection.setUseCaches(false);
+                        stream = connection.getInputStream();
+                    }
+                }
+            } else {
+                stream = loader.getResourceAsStream(resourceName);
+            }
+            if (stream != null) {
+                try {
+                    // Only this line is changed to make it to read properties files with
custom encodings.
+                    bundle = new PropertyResourceBundle(new InputStreamReader(stream, this.encoding));
+                } finally {
+                    stream.close();
+                }
+            }
+            return bundle;
+        }
+        
+    }
 }



Mime
View raw message