comparison java/src/java/util/ResourceBundle.d @ 0:6dd524f61e62

add dwt win and basic java stuff
author Frank Benoit <benoit@tionex.de>
date Mon, 02 Mar 2009 14:44:16 +0100
parents
children 712ffca654f3
comparison
equal deleted inserted replaced
-1:000000000000 0:6dd524f61e62
1 /**
2 * Authors: Frank Benoit <keinfarbton@googlemail.com>
3 */
4 module java.util.ResourceBundle;
5
6 import tango.text.Util;
7 import tango.io.Stdout;
8
9 import java.lang.util;
10 import java.lang.Integer;
11 import tango.io.device.File;
12 import tango.text.locale.Core;
13
14 class ResourceBundle {
15
16 String[ String ] map;
17
18 /++
19 + First entry is the default entry if no maching locale is found
20 +/
21 public this( ImportData[] data ){
22 char[] name = Culture.current().name.dup;
23 if( name.length is 5 && name[2] is '-' ){
24 name[2] = '_';
25 char[] end = "_" ~ name ~ ".properties";
26 foreach( entry; data ){
27 if( entry.name.length > end.length && entry.name[ $-end.length .. $ ] == end ){
28 //Trace.formatln( "ResourceBundle {}", entry.name );
29 initialize( cast(char[])entry.data );
30 return;
31 }
32 }
33 }
34 char[] end = "_" ~ name[0..2] ~ ".properties";
35 foreach( entry; data ){
36 if( entry.name.length > end.length && entry.name[ $-end.length .. $ ] == end ){
37 //Trace.formatln( "ResourceBundle {}", entry.name );
38 initialize( cast(char[])entry.data );
39 return;
40 }
41 }
42 //Trace.formatln( "ResourceBundle default" );
43 initialize( cast(char[])data[0].data );
44 }
45 public this( ImportData data ){
46 initialize( cast(char[])data.data );
47 }
48 public this( String data ){
49 initialize( data );
50 }
51 private void initialize( String data ){
52 String line;
53 int dataIndex;
54
55 //tango.io.Stdout.Stdout.formatln( "properties put ..." );
56 void readLine(){
57 line.length = 0;
58 char i = data[ dataIndex++ ];
59 while( dataIndex < data.length && i !is '\n' && i !is '\r' ){
60 line ~= i;
61 i = data[ dataIndex++ ];
62 }
63 }
64
65 //tango.io.Stdout.Stdout.formatln( "properties put {}", __LINE__ );
66 bool linecontinue = false;
67 bool iskeypart = true;
68 String key;
69 String value;
70 nextline:
71 while( dataIndex < data.length ){
72 //tango.io.Stdout.Stdout.formatln( "properties put {} startline", __LINE__ );
73 readLine();
74 line = java.lang.util.trim(line);
75 if( line.length is 0 ){
76 //tango.io.Stdout.Stdout.formatln( "properties put {} was 0 length", __LINE__ );
77 continue;
78 }
79 if( line[0] == '#' ){
80 //tango.io.Stdout.Stdout.formatln( "properties put {} was comment", __LINE__ );
81 continue;
82 }
83 int pos = 0;
84 bool esc = false;
85 if( !linecontinue ){
86 iskeypart = true;
87 key = null;
88 value = null;
89 }
90 else{
91 linecontinue = false;
92 }
93 while( pos < line.length ){
94 char[] c = line[pos .. pos +1];
95 if( esc ){
96 esc = false;
97 switch( c[0] ){
98 case 't' : c[0] = '\t'; break;
99 case 'n' : c[0] = '\n'; break;
100 case '\\': c[0] = '\\'; break;
101 case '\"': c[0] = '\"'; break;
102 case 'u' :
103 dchar d = Integer.parseInt( line[ pos+1 .. pos+5 ], 16 );
104 c = dcharToString(d);
105 pos += 4;
106 break;
107 default: break;
108 }
109 }
110 else{
111 if( c == "\\" ){
112 if( pos == line.length -1 ){
113 linecontinue = true;
114 goto nextline;
115 }
116 esc = true;
117 pos++;
118 continue;
119 }
120 else if( iskeypart && c == "=" ){
121 pos++;
122 iskeypart = false;
123 continue;
124 }
125 }
126 pos++;
127 if( iskeypart ){
128 key ~= c;
129 }
130 else{
131 value ~= c;
132 }
133 }
134 if( iskeypart ){
135 throw new RuntimeException( __FILE__, __LINE__, "Cannot find = in record" );
136 }
137 key = java.lang.util.trim(key);
138 value = java.lang.util.trim(value);
139 //tango.io.Stdout.Stdout.formatln( "properties put {}=>{}", key, value );
140
141 map[ key.dup ] = value.dup;
142 //tango.io.Stdout.Stdout.formatln( "properties put {}", __LINE__ );
143 }
144 }
145
146 public bool hasString( String key ){
147 return ( key in map ) !is null;
148 }
149
150 public String getString( String key ){
151 if( auto v = key in map ){
152 return (*v).dup;
153 }
154 throw new MissingResourceException( "key not found", this.classinfo.name, key );
155 }
156
157 public String[] getKeys(){
158 return map.keys;
159 }
160
161 public static ResourceBundle getBundle( ImportData[] data ){
162 return new ResourceBundle( data );
163 }
164 public static ResourceBundle getBundle( ImportData data ){
165 return new ResourceBundle( data );
166 }
167 public static ResourceBundle getBundle( String name ){
168 try{
169 return new ResourceBundle( cast(String) File.get(name) );
170 }
171 catch( IOException e){
172 e.msg ~= " file:" ~ name;
173 throw e;
174 }
175 }
176 public static ResourceBundle getBundleFromData( String data ){
177 return new ResourceBundle( data );
178 }
179 }
180
181