]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/classes/core/src/com/ibm/icu/impl/CalendarData.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / classes / core / src / com / ibm / icu / impl / CalendarData.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.util.ArrayList;
10 import java.util.MissingResourceException;
11
12 import com.ibm.icu.util.ULocale;
13 import com.ibm.icu.util.UResourceBundle;
14 import com.ibm.icu.util.UResourceBundleIterator;
15
16 /**
17  * This class abstracts access to calendar (Calendar and DateFormat) data.
18  * @internal ICU 3.0
19  */
20 public class CalendarData {
21     /**
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.
26      */
27     public CalendarData(ULocale loc, String type) {
28         this((ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, loc), type);
29     }
30     
31     public CalendarData(ICUResourceBundle b, String type) {
32         fBundle = b;
33         if((type == null) || (type.equals("")) || (type.equals("gregorian"))) {
34             fMainType = "gregorian";
35             fFallbackType = null;
36         } else {
37             fMainType = type;
38             fFallbackType = "gregorian";
39         }
40     }
41     
42     /**
43      * Load data for calendar. Note, this object owns the resources, do NOT call ures_close()!
44      *
45      * @param key Resource key to data
46      * @internal
47      */
48     public ICUResourceBundle get(String key) {
49         try {
50             return fBundle.getWithFallback("calendar/" + fMainType + "/" + key);
51         } catch(MissingResourceException m) {
52             if(fFallbackType != null) {
53                 return fBundle.getWithFallback("calendar/" + fFallbackType + "/" + key);
54             }
55             throw m;
56             
57         }       
58     }
59
60     /**
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
65      *
66      * @param key Resource key to data
67      * @param subKey Resource key to data
68      * @internal
69      */
70     public ICUResourceBundle get(String key, String subKey) {
71         try {
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);
76             }
77             throw m;
78             
79         }       
80     }
81
82     /**
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
86      *
87      * @param key Resource key to data
88      * @param contextKey Resource key to data
89      * @param subKey Resource key to data
90      * @internal
91      */
92     public ICUResourceBundle get(String key, String contextKey, String subKey) {
93         try {
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);
98             }
99             throw m;
100             
101         }       
102     }
103     
104     public String[] getStringArray(String key) {
105         return get(key).getStringArray();
106     }
107
108     public String[] getStringArray(String key, String subKey) {
109         return get(key, subKey).getStringArray();
110     }
111
112     public String[] getStringArray(String key, String contextKey, String subKey) {
113         return get(key, contextKey, subKey).getStringArray();
114     }
115     public String[] getEras(String subkey){
116         ICUResourceBundle bundle = get("eras/"+subkey);
117         return bundle.getStringArray();
118     }
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());
129                     break;
130                 case UResourceBundle.ARRAY:
131                     String[] items = patResource.getStringArray();
132                     list.add(items[0]);
133                     break;
134             }
135         }
136
137         return list.toArray(new String[list.size()]);
138     }
139
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:
149                     list.add(null);
150                     break;
151                 case UResourceBundle.ARRAY:
152                     String[] items = patResource.getStringArray();
153                     list.add(items[1]);
154                     break;
155             }
156         }
157         return list.toArray(new String[list.size()]);
158     }
159
160     public ULocale getULocale() {
161         return fBundle.getULocale();
162     }
163
164     private ICUResourceBundle fBundle;
165     private String fMainType;
166     private String fFallbackType;
167 }