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