]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/impl/ICUResourceBundleImpl.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / core / src / com / ibm / icu / impl / ICUResourceBundleImpl.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2004-2011, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.impl;
8
9 import java.nio.ByteBuffer;
10 import java.util.HashMap;
11 import java.util.Set;
12 import java.util.TreeSet;
13
14 import com.ibm.icu.util.UResourceBundle;
15 import com.ibm.icu.util.UResourceBundleIterator;
16 import com.ibm.icu.util.UResourceTypeMismatchException;
17
18 class ICUResourceBundleImpl extends ICUResourceBundle {
19     protected ICUResourceBundleImpl(ICUResourceBundleReader reader, String key, String resPath, int resource,
20                                     ICUResourceBundleImpl container) {
21         super(reader, key, resPath, resource, container);
22     }
23     protected final ICUResourceBundle createBundleObject(String _key,
24                                                          int _resource,
25                                                          HashMap<String, String> table,
26                                                          UResourceBundle requested,
27                                                          boolean[] isAlias) {
28         if (isAlias != null) {
29             isAlias[0] = false;
30         }
31         String _resPath = resPath + "/" + _key;
32         switch(ICUResourceBundleReader.RES_GET_TYPE(_resource)) {
33         case STRING :
34         case STRING_V2:
35             return new ICUResourceBundleImpl.ResourceString(reader, _key, _resPath, _resource, this);
36         case BINARY:
37             return new ICUResourceBundleImpl.ResourceBinary(reader, _key, _resPath, _resource, this);
38         case ALIAS:
39             if (isAlias != null) {
40                 isAlias[0] = true;
41             }
42             return findResource(_key, _resPath, _resource, table, requested);
43         case INT:
44             return new ICUResourceBundleImpl.ResourceInt(reader, _key, _resPath, _resource, this);
45         case INT_VECTOR:
46             return new ICUResourceBundleImpl.ResourceIntVector(reader, _key, _resPath, _resource, this);
47         case ARRAY:
48         case ARRAY16:
49             return new ICUResourceBundleImpl.ResourceArray(reader, _key, _resPath, _resource, this);
50         case TABLE:
51         case TABLE16:
52         case TABLE32:
53             return new ICUResourceBundleImpl.ResourceTable(reader, _key, _resPath, _resource, this);
54         default :
55             throw new IllegalStateException("The resource type is unknown");
56         }
57     }
58
59     // Scalar values ------------------------------------------------------- ***
60
61     private static final class ResourceBinary extends ICUResourceBundleImpl {
62         public ByteBuffer getBinary() {
63             return reader.getBinary(resource);
64         }
65         public byte [] getBinary(byte []ba) {
66             return reader.getBinary(resource, ba);
67         }
68         ResourceBinary(ICUResourceBundleReader reader, String key, String resPath, int resource,
69                        ICUResourceBundleImpl container) {
70             super(reader, key, resPath, resource, container);
71         }
72     }
73     private static final class ResourceInt extends ICUResourceBundleImpl {
74         public int getInt() {
75             return ICUResourceBundleReader.RES_GET_INT(resource);
76         }
77         public int getUInt() {
78             return ICUResourceBundleReader.RES_GET_UINT(resource);
79         }
80         ResourceInt(ICUResourceBundleReader reader, String key, String resPath, int resource,
81                     ICUResourceBundleImpl container) {
82             super(reader, key, resPath, resource, container);
83         }
84     }
85     private static final class ResourceString extends ICUResourceBundleImpl {
86         private String value;
87         public String getString() {
88             return value;
89         }
90         ResourceString(ICUResourceBundleReader reader, String key, String resPath, int resource,
91                        ICUResourceBundleImpl container) {
92             super(reader, key, resPath, resource, container);
93             value = reader.getString(resource);
94         }
95     }
96     private static final class ResourceIntVector extends ICUResourceBundleImpl {
97         private int[] value;
98         public int[] getIntVector() {
99             return value;
100         }
101         ResourceIntVector(ICUResourceBundleReader reader, String key, String resPath, int resource,
102                           ICUResourceBundleImpl container) {
103             super(reader, key, resPath, resource, container);
104             value = reader.getIntVector(resource);
105         }
106     }
107
108     // Container values ---------------------------------------------------- ***
109
110     private static class ResourceContainer extends ICUResourceBundleImpl {
111         protected ICUResourceBundleReader.Container value;
112
113         public int getSize() {
114             return value.getSize();
115         }
116         protected int getContainerResource(int index) {
117             return value.getContainerResource(index);
118         }
119         protected UResourceBundle createBundleObject(int index, String resKey, HashMap<String, String> table,
120                                                      UResourceBundle requested, boolean[] isAlias) {
121             int item = getContainerResource(index);
122             if (item == RES_BOGUS) {
123                 throw new IndexOutOfBoundsException();
124             }
125             return createBundleObject(resKey, item, table, requested, isAlias);
126         }
127         ResourceContainer(ICUResourceBundleReader reader, String key, String resPath, int resource,
128                           ICUResourceBundleImpl container) {
129             super(reader, key, resPath, resource, container);
130         }
131     }
132     private static class ResourceArray extends ResourceContainer {
133         protected String[] handleGetStringArray() {
134             String[] strings = new String[value.getSize()];
135             UResourceBundleIterator iter = getIterator();
136             int i = 0;
137             while (iter.hasNext()) {
138                 strings[i++] = iter.next().getString();
139             }
140             return strings;
141         }
142         public String[] getStringArray() {
143             return handleGetStringArray();
144         }
145         protected UResourceBundle handleGetImpl(String indexStr, HashMap<String, String> table,
146                                                 UResourceBundle requested,
147                                                 int[] index, boolean[] isAlias) {
148             int i = indexStr.length() > 0 ? Integer.valueOf(indexStr).intValue() : -1;
149             if(index != null) {
150                 index[0] = i;
151             }
152             if (i < 0) {
153                 throw new UResourceTypeMismatchException("Could not get the correct value for index: "+ indexStr);
154             }
155             return createBundleObject(i, indexStr, table, requested, isAlias);
156         }
157         protected UResourceBundle handleGetImpl(int index, HashMap<String, String> table,
158                                                 UResourceBundle requested, boolean[] isAlias) {
159             return createBundleObject(index, Integer.toString(index), table, requested, isAlias);
160         }
161         ResourceArray(ICUResourceBundleReader reader, String key, String resPath, int resource,
162                       ICUResourceBundleImpl container) {
163             super(reader, key, resPath, resource, container);
164             value = reader.getArray(resource);
165             createLookupCache(); // Use bundle cache to access array entries
166         }
167     }
168     static class ResourceTable extends ResourceContainer {
169         protected String getKey(int index) {
170             return ((ICUResourceBundleReader.Table)value).getKey(index);
171         }
172         protected Set<String> handleKeySet() {
173             TreeSet<String> keySet = new TreeSet<String>();
174             ICUResourceBundleReader.Table table = (ICUResourceBundleReader.Table)value;
175             for (int i = 0; i < table.getSize(); ++i) {
176                 keySet.add(table.getKey(i));
177             }
178             return keySet;
179         }
180         protected int getTableResource(String resKey) {
181             return ((ICUResourceBundleReader.Table)value).getTableResource(resKey);
182         }
183         protected int getTableResource(int index) {
184             return getContainerResource(index);
185         }
186         protected UResourceBundle handleGetImpl(String resKey, HashMap<String, String> table,
187                                                 UResourceBundle requested,
188                                                 int[] index, boolean[] isAlias) {
189             int i = ((ICUResourceBundleReader.Table)value).findTableItem(resKey);
190             if(index != null) {
191                 index[0] = i;
192             }
193             if (i < 0) {
194                 return null;
195             }
196             return createBundleObject(i, resKey, table, requested, isAlias);
197         }
198         protected UResourceBundle handleGetImpl(int index, HashMap<String, String> table,
199                                                 UResourceBundle requested, boolean[] isAlias) {
200             String itemKey = ((ICUResourceBundleReader.Table)value).getKey(index);
201             if (itemKey == null) {
202                 throw new IndexOutOfBoundsException();
203             }
204             return createBundleObject(index, itemKey, table, requested, isAlias);
205         }
206         ResourceTable(ICUResourceBundleReader reader, String key, String resPath, int resource,
207                       ICUResourceBundleImpl container) {
208             super(reader, key, resPath, resource, container);
209             value = reader.getTable(resource);
210             createLookupCache(); // Use bundle cache to access table entries
211         }
212     }
213 }