You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2008/07/31 18:53:08 UTC

svn commit: r681416 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/internal/services/ test/java/org/apache/tapestry5/internal/services/ test/resources/org/apache/tapestry5/internal/services/

Author: hlship
Date: Thu Jul 31 09:53:07 2008
New Revision: 681416

URL: http://svn.apache.org/viewvc?rev=681416&view=rev
Log:
TAPESTRY-2525: Properties files in a message catalog should be read using UTF-8 encoding, rather than default encoding

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/MessagesSourceImplTest.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/utf8.properties
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/MessagesSourceImpl.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/MessagesSourceImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/MessagesSourceImpl.java?rev=681416&r1=681415&r2=681416&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/MessagesSourceImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/MessagesSourceImpl.java Thu Jul 31 09:53:07 2008
@@ -208,6 +208,18 @@
 
         tracker.add(resource.toURL());
 
+        try
+        {
+            return readPropertiesFromStream(resource.openStream());
+        }
+        catch (Exception ex)
+        {
+            throw new RuntimeException(ServicesMessages.failureReadingMessages(resource, ex), ex);
+        }
+    }
+
+    static Map<String, String> readPropertiesFromStream(InputStream propertiesFileStream) throws IOException
+    {
         Map<String, String> result = CollectionFactory.newCaseInsensitiveMap();
 
         Properties p = new Properties();
@@ -215,7 +227,8 @@
 
         try
         {
-            is = readStreamAsUTF8(resource.openStream());
+
+            is = readUTFStreamToEscapedASCII(propertiesFileStream);
 
             // Ok, now we have the content read into memory as UTF-8, not ASCII.
 
@@ -225,10 +238,6 @@
 
             is = null;
         }
-        catch (Exception ex)
-        {
-            throw new RuntimeException(ServicesMessages.failureReadingMessages(resource, ex), ex);
-        }
         finally
         {
             InternalUtils.close(is);
@@ -246,7 +255,13 @@
         return result;
     }
 
-    private InputStream readStreamAsUTF8(InputStream is) throws IOException
+
+    /**
+     * Reads a UTF-8 stream, performing a conversion to ASCII (i.e., ISO8859-1 encoding). Characters outside the normal
+     * range for ISO8859-1 are converted to unicode escapes. In effect, Tapestry is performing native2ascii on the
+     * files, on the fly.
+     */
+    private static InputStream readUTFStreamToEscapedASCII(InputStream is) throws IOException
     {
         Reader reader = new InputStreamReader(is, CHARSET);
 
@@ -259,12 +274,23 @@
 
             if (length < 0) break;
 
-            builder.append(buffer, 0, length);
+            for (int i = 0; i < length; i++)
+            {
+                char ch = buffer[i];
+
+                if (ch <= '\u007f')
+                {
+                    builder.append(ch);
+                    continue;
+                }
+
+                builder.append(String.format("\\u%04x", (int) ch));
+            }
         }
 
         reader.close();
 
-        byte[] resourceContent = builder.toString().getBytes(CHARSET);
+        byte[] resourceContent = builder.toString().getBytes();
 
         return new ByteArrayInputStream(resourceContent);
     }

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/MessagesSourceImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/MessagesSourceImplTest.java?rev=681416&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/MessagesSourceImplTest.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/MessagesSourceImplTest.java Thu Jul 31 09:53:07 2008
@@ -0,0 +1,36 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry5.internal.services;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import java.io.InputStream;
+import java.util.Map;
+
+public class MessagesSourceImplTest extends Assert
+{
+    @Test
+    public void read_utf() throws Exception
+    {
+        InputStream stream = getClass().getResourceAsStream("utf8.properties");
+
+        Map<String, String> properties = MessagesSourceImpl.readPropertiesFromStream(stream);
+
+        // Tapestry in Japanese is =??????
+
+        assertEquals(properties.get("tapestry"), "\u30bf\u30da\u30b9\u30c8\u30ea\u30fc");
+    }
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/utf8.properties
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/utf8.properties?rev=681416&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/utf8.properties (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/utf8.properties Thu Jul 31 09:53:07 2008
@@ -0,0 +1,2 @@
+tapestry=タペストリー
+version=5