]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/impl/ICUResourceBundleImpl.java
go
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / impl / ICUResourceBundleImpl.java
1 //##header J2SE15
2 /*
3  *******************************************************************************
4  * Copyright (C) 2004-2009, International Business Machines Corporation and    *
5  * others. All Rights Reserved.                                                *
6  *******************************************************************************
7  */
8 package com.ibm.icu.impl;
9
10 import java.util.HashMap;
11 import java.util.MissingResourceException;
12
13 import com.ibm.icu.util.ULocale;
14 import com.ibm.icu.util.UResourceBundle;
15 import com.ibm.icu.util.UResourceBundleIterator;
16 import com.ibm.icu.util.UResourceTypeMismatchException;
17
18 //#if defined(FOUNDATION10) || defined(J2SE13) || defined(ECLIPSE_FRAGMENT)
19 //#else
20 import java.nio.ByteBuffer;
21 //#endif
22
23 class ICUResourceBundleImpl {
24
25     static final class ResourceArray extends ICUResourceBundle {
26         protected String[] handleGetStringArray() {
27             String[] strings = new String[size];
28             UResourceBundleIterator iter = getIterator();
29             int i = 0;
30             while (iter.hasNext()) {
31                 strings[i++] = iter.next().getString();
32             }
33             return strings;
34         }
35         /**
36          * @internal ICU 3.0
37          */
38         public String[] getStringArray() {
39             return handleGetStringArray();
40         }
41
42         protected UResourceBundle handleGetImpl(String indexStr, HashMap table, UResourceBundle requested,
43                 int[] index, boolean[] isAlias) {
44             index[0] = getIndex(indexStr);
45             if (index[0] > -1) {
46                 return handleGetImpl(index[0], table, requested, isAlias);
47             }
48             throw new UResourceTypeMismatchException("Could not get the correct value for index: "+ index);
49         }
50
51         protected UResourceBundle handleGetImpl(int index, HashMap table, UResourceBundle requested,
52                 boolean[] isAlias) {
53             if (index > size) {
54                 throw new IndexOutOfBoundsException();
55             }
56             int offset = RES_GET_OFFSET(resource);
57             int itemOffset = offset + getIntOffset(index + 1);
58             long itemResource = (UNSIGNED_INT_MASK) & ICUResourceBundle.getInt(rawData,itemOffset);
59             String path = (isTopLevel == true) ? Integer.toString(index) : resPath + "/" + index;
60
61             return createBundleObject(Integer.toString(index), itemResource, path, table, requested, this, isAlias);
62         }
63         private int countItems() {
64             int offset = RES_GET_OFFSET(resource);
65             int value = getInt(rawData,offset);
66             return value;
67         }
68         ResourceArray(String key, String resPath, long resource, ICUResourceBundle bundle) {
69             assign(this, bundle);
70             this.resource = resource;
71             this.key = key;
72             this.size = countItems();
73             this.resPath = resPath;
74             createLookupCache(); // Use bundle cache to access array entries
75         }
76     }
77     static final class ResourceBinary extends ICUResourceBundle {
78         private byte[] value;
79         public ByteBuffer getBinary() {
80             return ByteBuffer.wrap(value);
81         }
82         public byte [] getBinary(byte []ba) {
83             return value;
84         }
85         private byte[] getValue() {
86             int offset = RES_GET_OFFSET(resource);
87             int length = ICUResourceBundle.getInt(rawData,offset);
88             int byteOffset = offset + getIntOffset(1);
89             byte[] dst = new byte[length];
90             //if (ASSERT) Assert.assrt("byteOffset+length < rawData.length", byteOffset+length < rawData.length);
91             System.arraycopy(rawData, byteOffset, dst, 0, length);
92             return dst;
93         }
94         ResourceBinary(String key, String resPath, long resource, ICUResourceBundle bundle) {
95             assign(this, bundle);
96             this.resource = resource;
97             this.key = key;
98             this.resPath = resPath;
99             value = getValue();
100
101         }
102     }
103     static final class ResourceInt extends ICUResourceBundle {
104         public int getInt() {
105             return RES_GET_INT(resource);
106         }
107         public int getUInt() {
108             long ret = RES_GET_UINT(resource);
109             return (int) ret;
110         }
111         ResourceInt(String key, String resPath, long resource, ICUResourceBundle bundle) {
112             assign(this, bundle);
113             this.key = key;
114             this.resource = resource;
115             this.resPath = resPath;
116         }
117     }
118
119     static final class ResourceString extends ICUResourceBundle {
120         private String value;
121         public String getString() {
122             return value;
123         }
124         ResourceString(String key, String resPath, long resource, ICUResourceBundle bundle) {
125             assign(this, bundle);
126             value = getStringValue(resource);
127             this.key = key;
128             this.resource = resource;
129             this.resPath = resPath;
130         }
131     }
132
133     static final class ResourceIntVector extends ICUResourceBundle {
134         private int[] value;
135         public int[] getIntVector() {
136             return value;
137         }
138         private int[] getValue() {
139             int offset = RES_GET_OFFSET(resource);
140             int length = ICUResourceBundle.getInt(rawData,offset);
141             int intOffset = offset + getIntOffset(1);
142             int[] val = new int[length];
143             //int byteLength = getIntOffset(length);
144
145             //if (ASSERT) Assert.assrt("(intOffset+byteLength)<rawData.length", (intOffset+byteLength)<rawData.length);
146
147             for(int i=0; i<length;i++){
148                 val[i]=ICUResourceBundle.getInt(rawData, intOffset+getIntOffset(i));
149             }
150             return val;
151         }
152         ResourceIntVector(String key, String resPath, long resource, ICUResourceBundle bundle) {
153             assign(this, bundle);
154             this.key = key;
155             this.resource = resource;
156             this.size = 1;
157             this.resPath = resPath;
158             value = getValue();
159         }
160     }
161
162     static final class ResourceTable extends ICUResourceBundle {
163
164         protected UResourceBundle handleGetImpl(String resKey, HashMap table, UResourceBundle requested,
165                 int[] index, boolean[] isAlias) {
166             if(size<=0){
167                 return null;
168             }
169             int offset = RES_GET_OFFSET(resource);
170             // offset+0 contains number of entries
171             // offset+1 contains the keyOffset
172             int currentOffset = (offset) + getCharOffset(1);
173             //int keyOffset = rawData.getChar(currentOffset);
174             /* do a binary search for the key */
175             index[0] = findKey(size, currentOffset, this, resKey);
176             if (index[0] == -1) {
177                 //throw new MissingResourceException(ICUResourceBundleReader.getFullName(baseName, localeID),
178                 //                                    localeID,
179                 //                                    key);
180                 return null;
181             }
182             currentOffset += getCharOffset(size + (~size & 1))
183                     + getIntOffset(index[0]);
184             long resOffset = (UNSIGNED_INT_MASK) & ICUResourceBundle.getInt(rawData, currentOffset);
185             String path = (isTopLevel == true) ? resKey : resPath + "/" + resKey;
186
187             return createBundleObject(resKey, resOffset, path, table, requested, this, isAlias);
188         }
189
190         public int getOffset(int currentOffset, int index) {
191             return getChar(rawData, currentOffset + getCharOffset(index));
192         }
193         protected UResourceBundle handleGetImpl(int index, HashMap table, UResourceBundle requested,
194                 boolean[] isAlias) {
195             if (index > size) {
196                 throw new IndexOutOfBoundsException();
197             }
198             int offset = RES_GET_OFFSET(resource);
199             // offset+0 contains number of entries
200             // offset+1 contains the keyOffset
201             int currentOffset = (offset) + getCharOffset(1);
202             int betterOffset = getOffset(currentOffset, index);
203             String itemKey = RES_GET_KEY(rawData, betterOffset).toString();
204             currentOffset += getCharOffset(size + (~size & 1))
205                     + getIntOffset(index);
206             long resOffset = (UNSIGNED_INT_MASK) & ICUResourceBundle.getInt(rawData,currentOffset);
207             String path = (isTopLevel == true) ? itemKey : resPath + "/" + itemKey;
208
209             return createBundleObject(itemKey, resOffset, path, table, requested, this, isAlias);
210         }
211         private int countItems() {
212             int offset = RES_GET_OFFSET(resource);
213             int value = getChar(rawData,offset);
214             return value;
215         }
216         ResourceTable(String key, String resPath, long resource, ICUResourceBundle bundle) {
217             this(key, resPath, resource, bundle, false);
218         }
219         ResourceTable(ICUResourceBundleReader reader, String baseName, String localeID, ClassLoader loader) {
220
221             this.rawData = reader.getData();
222             this.rootResource = (UNSIGNED_INT_MASK) & reader.getRootResource();
223             this.noFallback = reader.getNoFallback();
224             this.baseName = baseName;
225             this.localeID = localeID;
226             this.ulocale = new ULocale(localeID);
227             this.loader = loader;
228             initialize(null, "", rootResource, null, isTopLevel);
229         }
230         void initialize(String resKey, String resourcePath, long resOffset,
231                 ICUResourceBundle bundle, boolean topLevel){
232             if(bundle!=null){
233                 assign(this, bundle);
234             }
235             key = resKey;
236             resource = resOffset;
237             isTopLevel = topLevel;
238             size = countItems();
239             resPath = resourcePath;
240             createLookupCache(); // Use bundle cache to access nested resources
241         }
242         ResourceTable(String key, String resPath, long resource,
243                 ICUResourceBundle bundle, boolean isTopLevel) {
244             initialize(key, resPath, resource, bundle, isTopLevel);
245         }
246     }
247     static final class ResourceTable32 extends ICUResourceBundle{
248
249         protected UResourceBundle handleGetImpl(String resKey, HashMap table, UResourceBundle requested,
250                 int[] index, boolean[] isAlias) {
251             int offset = RES_GET_OFFSET(resource);
252             // offset+0 contains number of entries
253             // offset+1 contains the keyOffset
254             int currentOffset = (offset) + getIntOffset(1);
255             //int keyOffset = rawData.getChar(currentOffset);
256             /* do a binary search for the key */
257             index[0] = findKey(size, currentOffset, this, resKey);
258             if (index[0] == -1) {
259                 throw new MissingResourceException(
260                         "Could not find resource ",
261                         ICUResourceBundleReader.getFullName(baseName, localeID),
262                         resKey);
263             }
264             currentOffset += getIntOffset(size) + getIntOffset(index[0]);
265             long resOffset = (UNSIGNED_INT_MASK) & ICUResourceBundle.getInt(rawData,currentOffset);
266             String path = (isTopLevel == true) ? resKey : resPath + "/" + resKey;
267
268             return createBundleObject(resKey, resOffset, path, table, requested, this, isAlias);
269         }
270
271         public int getOffset(int currentOffset, int index) {
272             return ICUResourceBundle.getInt(rawData, currentOffset + getIntOffset(index));
273         }
274         protected UResourceBundle handleGetImpl(int index, HashMap table, UResourceBundle requested,
275                 boolean[] isAlias) {
276             if(size<=0){
277                 return null;
278             }
279             if (index > size) {
280                 throw new IndexOutOfBoundsException();
281             }
282             int offset = RES_GET_OFFSET(resource);
283             // offset+0 contains number of entries
284             // offset+1 contains the keyOffset
285             int currentOffset = (offset) + getIntOffset(1)
286                     + getIntOffset(index);
287             int betterOffset = getOffset(currentOffset, 0);
288             String itemKey = RES_GET_KEY(rawData, betterOffset).toString();
289             currentOffset += getIntOffset(size);
290             long resOffset = (UNSIGNED_INT_MASK) & ICUResourceBundle.getInt(rawData,currentOffset);
291             String path = (isTopLevel == true) ? Integer.toString(index) : resPath + "/" + index;
292
293             return createBundleObject(itemKey, resOffset, path, table, requested, this, isAlias);
294         }
295         private int countItems() {
296             int offset = RES_GET_OFFSET(resource);
297             int value = ICUResourceBundle.getInt(rawData, offset);
298             return value;
299         }
300         ResourceTable32(String key, String resPath, long resource, ICUResourceBundle bundle) {
301             this(key, resPath, resource, bundle, false);
302         }
303         ResourceTable32(ICUResourceBundleReader reader, String baseName, String localeID, ClassLoader loader) {
304
305             this.rawData = reader.getData();
306             this.rootResource = (UNSIGNED_INT_MASK) & reader.getRootResource();
307             this.noFallback = reader.getNoFallback();
308             this.baseName = baseName;
309             this.localeID = localeID;
310             this.ulocale = new ULocale(localeID);
311             this.loader = loader;
312             initialize(null, "", rootResource, null, isTopLevel);
313         }
314         void initialize(String resKey, String resourcePath, long resOffset,
315                 ICUResourceBundle bundle, boolean topLevel){
316             if(bundle!=null){
317                 assign(this, bundle);
318             }
319             key = resKey;
320             resource = resOffset;
321             isTopLevel = topLevel;
322             size = countItems();
323             resPath = resourcePath;
324             createLookupCache(); // Use bundle cache to access nested resources
325         }
326         ResourceTable32(String key, String resPath, long resource,
327                 ICUResourceBundle bundle, boolean isTopLevel) {
328             initialize(key, resPath, resource, bundle, isTopLevel);
329         }
330     }
331 }