changeset 114:46539f5c5993

Added implementation of ResourceBundle.
author kntroh
date Fri, 08 Apr 2011 20:12:20 +0900
parents fb3aa8075988
children b31c27b8e86d
files base/src/java/lang/Integer.d base/src/java/lang/String.d base/src/java/nonstandard/Locale.d base/src/java/util/ResourceBundle.d
diffstat 4 files changed, 134 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/base/src/java/lang/Integer.d	Wed Apr 06 21:57:23 2011 +0200
+++ b/base/src/java/lang/Integer.d	Fri Apr 08 20:12:20 2011 +0900
@@ -95,8 +95,12 @@
                 throw new NumberFormatException( e );
             }
         } else { // Phobos
-            implMissing( __FILE__, __LINE__ );
-            return 0;
+            try{
+            	return std.conv.parse!(int)( s, radix );
+            }
+            catch( std.conv.ConvException e ){
+                throw new NumberFormatException( e );
+            }
         }
     }
 
--- a/base/src/java/lang/String.d	Wed Apr 06 21:57:23 2011 +0200
+++ b/base/src/java/lang/String.d	Fri Apr 08 20:12:20 2011 +0900
@@ -158,8 +158,7 @@
         buf[0] = key;
         return tango.text.convert.Utf.toString( buf );
     } else { // Phobos
-        implMissing( __FILE__, __LINE__ );
-        return null;
+        return std.conv.to!(string)(key);
     }
 }
 
@@ -702,8 +701,7 @@
     version(Tango){
         return tango.text.Util.trim( str ).dup;
     } else { // Phobos
-        implMissing( __FILE__, __LINE__ );
-        return null;
+        return std.string.strip( str.idup );
     }
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/base/src/java/nonstandard/Locale.d	Fri Apr 08 20:12:20 2011 +0900
@@ -0,0 +1,110 @@
+/**
+ * Locale.d
+ * Information of a locale.
+ * Author: knt.roh
+ * License: Public Domain
+ */
+module java.nonstandard.locale;
+
+import java.lang.String;
+import java.lang.util : implMissing;
+
+version(Tango){
+    private import tango.text.locale.Core;
+} else { // Phobos
+    private import std.conv;
+    private import std.exception;
+}
+version (Windows) {
+    private import std.c.string;
+    private import std.c.windows.windows;
+    private bool W_VERSION;
+    static this() {
+        W_VERSION = GetVersion < 0x80000000;
+    }
+    private extern (Windows) {
+        enum LCID : DWORD {
+            /// The default locale for the user or process.
+            LOCALE_USER_DEFAULT     = 0x0400,
+        }
+        enum LCTYPE : DWORD {
+            /// ISO639 language name.
+            LOCALE_SISO639LANGNAME  = 0x0059,
+            /// ISO3166 country name.
+            LOCALE_SISO3166CTRYNAME = 0x005A
+        }
+        /// Retrieves information about a locale specified by identifier.
+        /// See_Also: GetLocaleInfo Function (Windows)
+        ///           (http://msdn.microsoft.com/en-us/library/dd318101%28VS.85%29.aspx)
+        INT GetLocaleInfoW(
+            LCID Locale,
+            LCTYPE LCType,
+            LPWSTR lpLCData,
+            INT cchData
+        );
+        /// ditto
+        INT GetLocaleInfoA(
+            LCID Locale,
+            LCTYPE LCType,
+            LPCSTR lpLCData,
+            INT cchData
+        );
+    }
+    /// A purpose of this templete is switch of W or A in Windows.
+    private String caltureNameImpl(Char, alias GetLocalInfo)() {
+        INT len;
+        Char[] res;
+        Char[] buf;
+        len = GetLocalInfo(LCID.LOCALE_USER_DEFAULT,
+            LCTYPE.LOCALE_SISO639LANGNAME, null, 0);
+        enforce(len, new Exception("LOCALE_SISO639LANGNAME (len)", __FILE__, __LINE__));
+        buf.length = len;
+        len = GetLocalInfo(LCID.LOCALE_USER_DEFAULT,
+            LCTYPE.LOCALE_SISO639LANGNAME, buf.ptr, buf.length);
+        enforce(len, new Exception("LOCALE_SISO639LANGNAME", __FILE__, __LINE__));
+        res ~= buf[0 .. len - 1];
+        res ~= "-";
+        len = GetLocalInfo(LCID.LOCALE_USER_DEFAULT,
+            LCTYPE.LOCALE_SISO3166CTRYNAME, null, 0);
+        enforce(len, new Exception("LOCALE_SISO3166CTRYNAME (len)", __FILE__, __LINE__));
+        buf.length = len;
+        len = GetLocalInfo(LCID.LOCALE_USER_DEFAULT,
+            LCTYPE.LOCALE_SISO3166CTRYNAME, buf.ptr, buf.length);
+        enforce(len, new Exception("LOCALE_SISO3166CTRYNAME", __FILE__, __LINE__));
+        res ~= buf[0 .. len - 1];
+        return to!(String)(res);
+    }
+} else version (Posix) {
+    private import std.process : getenv;
+    private import std.string : indexOf, replace;
+}
+
+/// Get a omitted calture name. for example: "en-US"
+String caltureName() {
+    version(Tango){
+        return Culture.current.name;
+    } else { // Phobos
+        version (Windows) {
+            if (W_VERSION) {
+                return caltureNameImpl!(wchar, GetLocaleInfoW)();
+            } else {
+                return caltureNameImpl!(char, GetLocaleInfoA)();
+            }
+        } else version (Posix) {
+            // LC_ALL is override to settings of all category.
+            // This is undefined in almost case.
+            String res = .getenv("LC_ALL");
+            if (!res || !res.length) {
+                // LANG is basic Locale setting.
+                // A settings of each category override this. 
+                res = .getenv("LANG");
+            }
+            int dot = .indexOf(res, '.');
+            if (dot != -1) res = res[0 .. dot];
+            return .replace(res, "_", "-");
+        } else {
+            implMissing(__FILE__, __LINE__);
+            return "";
+        }
+    }
+}
--- a/base/src/java/util/ResourceBundle.d	Wed Apr 06 21:57:23 2011 +0200
+++ b/base/src/java/util/ResourceBundle.d	Fri Apr 08 20:12:20 2011 +0900
@@ -8,10 +8,10 @@
 import java.lang.exceptions;
 import java.util.MissingResourceException;
 import java.util.Enumeration;
+import java.nonstandard.locale;
 version(Tango){
     //import tango.text.Util;
     import tango.io.device.File;
-    import tango.text.locale.Core;
 } else { // Phobos
     import std.file;
 }
@@ -25,32 +25,28 @@
      + First entry is the default entry if no maching locale is found
      +/
     public this( ImportData[] data ){
-        version(Tango){
-            char[] name = Culture.current().name.dup;
-            if( name.length is 5 && name[2] is '-' ){
-                name[2] = '_';
-                char[] end = "_" ~ name ~ ".properties";
-                foreach( entry; data ){
-                    if( entry.name.length > end.length && entry.name[ $-end.length .. $ ] == end ){
-                        //Trace.formatln( "ResourceBundle {}", entry.name );
-                        initialize( cast(char[])entry.data );
-                        return;
-                    }
-                }
-            }
-            char[] end = "_" ~ name[0..2] ~ ".properties";
+        char[] name = caltureName.dup;
+        if( name.length is 5 && name[2] is '-' ){
+            name[2] = '_';
+            char[] end = "_" ~ name ~ ".properties";
             foreach( entry; data ){
                 if( entry.name.length > end.length && entry.name[ $-end.length .. $ ] == end ){
                     //Trace.formatln( "ResourceBundle {}", entry.name );
-                    initialize( cast(char[])entry.data );
+                    initialize( cast(String)entry.data );
                     return;
                 }
             }
-            //Trace.formatln( "ResourceBundle default" );
-            initialize( cast(char[])data[0].data );
-        } else { // Phobos
-            implMissing(__FILE__,__LINE__);
         }
+        char[] end = "_" ~ name[0..2] ~ ".properties";
+        foreach( entry; data ){
+            if( entry.name.length > end.length && entry.name[ $-end.length .. $ ] == end ){
+                //Trace.formatln( "ResourceBundle {}", entry.name );
+                initialize( cast(String)entry.data );
+                return;
+            }
+        }
+        //Trace.formatln( "ResourceBundle default" );
+        initialize( cast(String)data[0].data );
     }
     public this( ImportData data ){
         initialize( cast(String)data.data );