]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/impl/CalendarData.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / impl / CalendarData.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2004-2009, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.impl;\r
8 \r
9 import java.util.ArrayList;\r
10 import java.util.MissingResourceException;\r
11 \r
12 import com.ibm.icu.util.ULocale;\r
13 import com.ibm.icu.util.UResourceBundle;\r
14 import com.ibm.icu.util.UResourceBundleIterator;\r
15 \r
16 /**\r
17  * This class abstracts access to calendar (Calendar and DateFormat) data.\r
18  * @internal ICU 3.0\r
19  */\r
20 public class CalendarData {\r
21     /**\r
22      * Construct a CalendarData from the given locale.\r
23      * @param loc locale to use. The 'calendar' keyword will be ignored.\r
24      * @param type calendar type. NULL indicates the gregorian calendar. \r
25      * No default lookup is done.\r
26      */\r
27     public CalendarData(ULocale loc, String type) {\r
28         this((ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, loc), type);\r
29     }\r
30     \r
31     public CalendarData(ICUResourceBundle b, String type) {\r
32         fBundle = b;\r
33         if((type == null) || (type.equals("")) || (type.equals("gregorian"))) {\r
34             fMainType = "gregorian";\r
35             fFallbackType = null;\r
36         } else {\r
37             fMainType = type;\r
38             fFallbackType ="gregorian";\r
39         }\r
40     }\r
41     \r
42     /**\r
43      * Load data for calendar. Note, this object owns the resources, do NOT call ures_close()!\r
44      *\r
45      * @param key Resource key to data\r
46      * @internal\r
47      */\r
48     public ICUResourceBundle get(String key) {\r
49         try {\r
50             return fBundle.getWithFallback("calendar/" + fMainType + "/" + key);\r
51         } catch(MissingResourceException m) {\r
52             if(fFallbackType != null) {\r
53                 return fBundle.getWithFallback("calendar/" + fFallbackType + "/" + key);\r
54             }\r
55             throw m;\r
56             \r
57         }       \r
58     }\r
59 \r
60     /**\r
61      * Load data for calendar. Note, this object owns the resources, do NOT call ures_close()!\r
62      * There is an implicit key of 'format'\r
63      * data is located in:   "calendar/key/format/subKey"\r
64      * for example,  calendar/dayNames/format/abbreviated\r
65      *\r
66      * @param key Resource key to data\r
67      * @param subKey Resource key to data\r
68      * @internal\r
69      */\r
70     public ICUResourceBundle get(String key, String subKey) {\r
71         try {\r
72             return fBundle.getWithFallback("calendar/" + fMainType + "/" + key + "/format/" + subKey);\r
73         } catch(MissingResourceException m) {\r
74             if(fFallbackType != null) {\r
75                 return fBundle.getWithFallback("calendar/" + fFallbackType + "/" + key + "/format/" + subKey);\r
76             }\r
77             throw m;\r
78             \r
79         }       \r
80     }\r
81 \r
82     /**\r
83      * Load data for calendar. Note, this object owns the resources, do NOT call ures_close()!\r
84      * data is located in:   "calendar/key/contextKey/subKey"\r
85      * for example,  calendar/dayNames/stand-alone/narrow\r
86      *\r
87      * @param key Resource key to data\r
88      * @param contextKey Resource key to data\r
89      * @param subKey Resource key to data\r
90      * @internal\r
91      */\r
92     public ICUResourceBundle get(String key, String contextKey, String subKey) {\r
93         try {\r
94             return fBundle.getWithFallback("calendar/" + fMainType + "/" + key + "/" + contextKey + "/" + subKey);\r
95         } catch(MissingResourceException m) {\r
96             if(fFallbackType != null) {\r
97                 return fBundle.getWithFallback("calendar/" + fFallbackType + "/" + key + "/" + contextKey + "/" + subKey);\r
98             }\r
99             throw m;\r
100             \r
101         }       \r
102     }\r
103     \r
104     public String[] getStringArray(String key) {\r
105         return get(key).getStringArray();\r
106     }\r
107 \r
108     public String[] getStringArray(String key, String subKey) {\r
109         return get(key, subKey).getStringArray();\r
110     }\r
111 \r
112     public String[] getStringArray(String key, String contextKey, String subKey) {\r
113         return get(key, contextKey, subKey).getStringArray();\r
114     }\r
115     public String[] getEras(String subkey){\r
116         ICUResourceBundle bundle = get("eras/"+subkey);\r
117         return bundle.getStringArray();\r
118     }\r
119     public String[] getDateTimePatterns(){\r
120         ICUResourceBundle bundle = get("DateTimePatterns");\r
121         ArrayList<String> list = new ArrayList<String>();\r
122         UResourceBundleIterator iter = bundle.getIterator();\r
123         while (iter.hasNext()) {\r
124             UResourceBundle patResource = iter.next();\r
125             int resourceType = patResource.getType();\r
126             switch (resourceType) {\r
127                 case UResourceBundle.STRING:\r
128                     list.add(patResource.getString());\r
129                     break;\r
130                 case UResourceBundle.ARRAY:\r
131                     String[] items = patResource.getStringArray();\r
132                     list.add(items[0]);\r
133                     break;\r
134             }\r
135         }\r
136 \r
137         return list.toArray(new String[list.size()]);\r
138     }\r
139 \r
140     public String[] getOverrides(){\r
141         ICUResourceBundle bundle = get("DateTimePatterns");\r
142         ArrayList<String> list = new ArrayList<String>();\r
143         UResourceBundleIterator iter = bundle.getIterator();\r
144         while (iter.hasNext()) {\r
145             UResourceBundle patResource = iter.next();\r
146             int resourceType = patResource.getType();\r
147             switch (resourceType) {\r
148                 case UResourceBundle.STRING:\r
149                     list.add(null);\r
150                     break;\r
151                 case UResourceBundle.ARRAY:\r
152                     String[] items = patResource.getStringArray();\r
153                     list.add(items[1]);\r
154                     break;\r
155             }\r
156         }\r
157         return list.toArray(new String[list.size()]);\r
158     }\r
159 \r
160     public ULocale getULocale() {\r
161         return fBundle.getULocale();\r
162     }\r
163 \r
164     private ICUResourceBundle fBundle;\r
165     private String fMainType;\r
166     private String fFallbackType;\r
167 }\r