2 *******************************************************************************
3 * Copyright (C) 2004-2011, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.impl;
9 import java.nio.ByteBuffer;
10 import java.util.HashMap;
12 import java.util.TreeSet;
14 import com.ibm.icu.util.UResourceBundle;
15 import com.ibm.icu.util.UResourceBundleIterator;
16 import com.ibm.icu.util.UResourceTypeMismatchException;
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);
23 protected final ICUResourceBundle createBundleObject(String _key,
25 HashMap<String, String> table,
26 UResourceBundle requested,
28 if (isAlias != null) {
31 String _resPath = resPath + "/" + _key;
32 switch(ICUResourceBundleReader.RES_GET_TYPE(_resource)) {
35 return new ICUResourceBundleImpl.ResourceString(reader, _key, _resPath, _resource, this);
37 return new ICUResourceBundleImpl.ResourceBinary(reader, _key, _resPath, _resource, this);
39 if (isAlias != null) {
42 return findResource(_key, _resPath, _resource, table, requested);
44 return new ICUResourceBundleImpl.ResourceInt(reader, _key, _resPath, _resource, this);
46 return new ICUResourceBundleImpl.ResourceIntVector(reader, _key, _resPath, _resource, this);
49 return new ICUResourceBundleImpl.ResourceArray(reader, _key, _resPath, _resource, this);
53 return new ICUResourceBundleImpl.ResourceTable(reader, _key, _resPath, _resource, this);
55 throw new IllegalStateException("The resource type is unknown");
59 // Scalar values ------------------------------------------------------- ***
61 private static final class ResourceBinary extends ICUResourceBundleImpl {
62 public ByteBuffer getBinary() {
63 return reader.getBinary(resource);
65 public byte [] getBinary(byte []ba) {
66 return reader.getBinary(resource, ba);
68 ResourceBinary(ICUResourceBundleReader reader, String key, String resPath, int resource,
69 ICUResourceBundleImpl container) {
70 super(reader, key, resPath, resource, container);
73 private static final class ResourceInt extends ICUResourceBundleImpl {
75 return ICUResourceBundleReader.RES_GET_INT(resource);
77 public int getUInt() {
78 return ICUResourceBundleReader.RES_GET_UINT(resource);
80 ResourceInt(ICUResourceBundleReader reader, String key, String resPath, int resource,
81 ICUResourceBundleImpl container) {
82 super(reader, key, resPath, resource, container);
85 private static final class ResourceString extends ICUResourceBundleImpl {
87 public String getString() {
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);
96 private static final class ResourceIntVector extends ICUResourceBundleImpl {
98 public int[] getIntVector() {
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);
108 // Container values ---------------------------------------------------- ***
110 private static class ResourceContainer extends ICUResourceBundleImpl {
111 protected ICUResourceBundleReader.Container value;
113 public int getSize() {
114 return value.getSize();
116 protected int getContainerResource(int index) {
117 return value.getContainerResource(index);
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();
125 return createBundleObject(resKey, item, table, requested, isAlias);
127 ResourceContainer(ICUResourceBundleReader reader, String key, String resPath, int resource,
128 ICUResourceBundleImpl container) {
129 super(reader, key, resPath, resource, container);
132 private static class ResourceArray extends ResourceContainer {
133 protected String[] handleGetStringArray() {
134 String[] strings = new String[value.getSize()];
135 UResourceBundleIterator iter = getIterator();
137 while (iter.hasNext()) {
138 strings[i++] = iter.next().getString();
142 public String[] getStringArray() {
143 return handleGetStringArray();
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;
153 throw new UResourceTypeMismatchException("Could not get the correct value for index: "+ indexStr);
155 return createBundleObject(i, indexStr, table, requested, isAlias);
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);
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
168 static class ResourceTable extends ResourceContainer {
169 protected String getKey(int index) {
170 return ((ICUResourceBundleReader.Table)value).getKey(index);
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));
180 protected int getTableResource(String resKey) {
181 return ((ICUResourceBundleReader.Table)value).getTableResource(resKey);
183 protected int getTableResource(int index) {
184 return getContainerResource(index);
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);
196 return createBundleObject(i, resKey, table, requested, isAlias);
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();
204 return createBundleObject(index, itemKey, table, requested, isAlias);
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