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.util.ArrayList;
10 import java.util.MissingResourceException;
12 import com.ibm.icu.util.ULocale;
13 import com.ibm.icu.util.UResourceBundle;
14 import com.ibm.icu.util.UResourceBundleIterator;
17 * This class abstracts access to calendar (Calendar and DateFormat) data.
20 public class CalendarData {
22 * Construct a CalendarData from the given locale.
23 * @param loc locale to use. The 'calendar' keyword will be ignored.
24 * @param type calendar type. NULL indicates the gregorian calendar.
25 * No default lookup is done.
27 public CalendarData(ULocale loc, String type) {
28 this((ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, loc), type);
31 public CalendarData(ICUResourceBundle b, String type) {
33 if((type == null) || (type.equals("")) || (type.equals("gregorian"))) {
34 fMainType = "gregorian";
38 fFallbackType = "gregorian";
43 * Load data for calendar. Note, this object owns the resources, do NOT call ures_close()!
45 * @param key Resource key to data
48 public ICUResourceBundle get(String key) {
50 return fBundle.getWithFallback("calendar/" + fMainType + "/" + key);
51 } catch(MissingResourceException m) {
52 if(fFallbackType != null) {
53 return fBundle.getWithFallback("calendar/" + fFallbackType + "/" + key);
61 * Load data for calendar. Note, this object owns the resources, do NOT call ures_close()!
62 * There is an implicit key of 'format'
63 * data is located in: "calendar/key/format/subKey"
64 * for example, calendar/dayNames/format/abbreviated
66 * @param key Resource key to data
67 * @param subKey Resource key to data
70 public ICUResourceBundle get(String key, String subKey) {
72 return fBundle.getWithFallback("calendar/" + fMainType + "/" + key + "/format/" + subKey);
73 } catch(MissingResourceException m) {
74 if(fFallbackType != null) {
75 return fBundle.getWithFallback("calendar/" + fFallbackType + "/" + key + "/format/" + subKey);
83 * Load data for calendar. Note, this object owns the resources, do NOT call ures_close()!
84 * data is located in: "calendar/key/contextKey/subKey"
85 * for example, calendar/dayNames/stand-alone/narrow
87 * @param key Resource key to data
88 * @param contextKey Resource key to data
89 * @param subKey Resource key to data
92 public ICUResourceBundle get(String key, String contextKey, String subKey) {
94 return fBundle.getWithFallback("calendar/" + fMainType + "/" + key + "/" + contextKey + "/" + subKey);
95 } catch(MissingResourceException m) {
96 if(fFallbackType != null) {
97 return fBundle.getWithFallback("calendar/" + fFallbackType + "/" + key + "/" + contextKey + "/" + subKey);
104 public String[] getStringArray(String key) {
105 return get(key).getStringArray();
108 public String[] getStringArray(String key, String subKey) {
109 return get(key, subKey).getStringArray();
112 public String[] getStringArray(String key, String contextKey, String subKey) {
113 return get(key, contextKey, subKey).getStringArray();
115 public String[] getEras(String subkey){
116 ICUResourceBundle bundle = get("eras/"+subkey);
117 return bundle.getStringArray();
119 public String[] getDateTimePatterns(){
120 ICUResourceBundle bundle = get("DateTimePatterns");
121 ArrayList<String> list = new ArrayList<String>();
122 UResourceBundleIterator iter = bundle.getIterator();
123 while (iter.hasNext()) {
124 UResourceBundle patResource = iter.next();
125 int resourceType = patResource.getType();
126 switch (resourceType) {
127 case UResourceBundle.STRING:
128 list.add(patResource.getString());
130 case UResourceBundle.ARRAY:
131 String[] items = patResource.getStringArray();
137 return list.toArray(new String[list.size()]);
140 public String[] getOverrides(){
141 ICUResourceBundle bundle = get("DateTimePatterns");
142 ArrayList<String> list = new ArrayList<String>();
143 UResourceBundleIterator iter = bundle.getIterator();
144 while (iter.hasNext()) {
145 UResourceBundle patResource = iter.next();
146 int resourceType = patResource.getType();
147 switch (resourceType) {
148 case UResourceBundle.STRING:
151 case UResourceBundle.ARRAY:
152 String[] items = patResource.getStringArray();
157 return list.toArray(new String[list.size()]);
160 public ULocale getULocale() {
161 return fBundle.getULocale();
164 private ICUResourceBundle fBundle;
165 private String fMainType;
166 private String fFallbackType;