• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2016 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 /*
5  ******************************************************************************
6  * Copyright (C) 2007-2015, International Business Machines Corporation and
7  * others. All Rights Reserved.
8  ******************************************************************************
9  */
10 
11 package ohos.global.icu.impl.duration.impl;
12 
13 import java.io.BufferedReader;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.InputStreamReader;
17 import java.io.UnsupportedEncodingException;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.MissingResourceException;
25 
26 import ohos.global.icu.impl.ICUData;
27 import ohos.global.icu.util.ICUUncheckedIOException;
28 
29 /**
30  * A PeriodFormatterDataService that serves PeriodFormatterData objects based on
31  * data files stored as resources in this directory. These are text files named
32  * after the locale, for example, 'pfd_he_IL.txt' specifies an period formatter
33  * data file for Hebrew as spoken in Israel. Data is in a JSON-like format.
34  * @hide exposed on OHOS
35  */
36 public class ResourceBasedPeriodFormatterDataService extends
37         PeriodFormatterDataService {
38     private Collection<String> availableLocales; // of String
39 
40     private PeriodFormatterData lastData = null;
41     private String lastLocale = null;
42     private Map<String, PeriodFormatterData> cache = new HashMap<String, PeriodFormatterData>(); // String -> PeriodFormatterData
43     // private PeriodFormatterData fallbackFormatterData;
44 
45     private static final String PATH = "data/";
46 
47     private static final ResourceBasedPeriodFormatterDataService singleton = new ResourceBasedPeriodFormatterDataService();
48 
49     /**
50      * Returns the singleton instance of this class.
51      */
getInstance()52     public static ResourceBasedPeriodFormatterDataService getInstance() {
53         return singleton;
54     }
55 
56     /**
57      * Constructs the service.
58      */
ResourceBasedPeriodFormatterDataService()59     private ResourceBasedPeriodFormatterDataService() {
60         List<String> localeNames = new ArrayList<String>(); // of String
61         InputStream is = ICUData.getRequiredStream(getClass(), PATH
62                 + "index.txt");
63         try {
64             BufferedReader br = new BufferedReader(new InputStreamReader(is,
65                     "UTF-8"));
66             String string = null;
67             while (null != (string = br.readLine())) {
68                 string = string.trim();
69                 if (string.startsWith("#") || string.length() == 0) {
70                     continue;
71                 }
72                 localeNames.add(string);
73             }
74             br.close();
75         } catch (IOException e) {
76             throw new IllegalStateException("IO Error reading " + PATH
77                     + "index.txt: " + e.toString());
78         } finally {
79             try {
80                 is.close();
81             } catch (IOException ignored) {
82             }
83         }
84         availableLocales = Collections.unmodifiableList(localeNames);
85     }
86 
87     @Override
get(String localeName)88     public PeriodFormatterData get(String localeName) {
89         // remove tag info including calendar, we don't use the calendar
90         int x = localeName.indexOf('@');
91         if (x != -1) {
92             localeName = localeName.substring(0, x);
93         }
94 
95         synchronized (this) {
96             if (lastLocale != null && lastLocale.equals(localeName)) {
97                 return lastData;
98             }
99 
100             PeriodFormatterData ld = cache.get(localeName);
101             if (ld == null) {
102                 String ln = localeName;
103                 while (!availableLocales.contains(ln)) {
104                     int ix = ln.lastIndexOf("_");
105                     if (ix > -1) {
106                         ln = ln.substring(0, ix);
107                     } else if (!"test".equals(ln)) {
108                         ln = "test";
109                     } else {
110                         ln = null;
111                         break;
112                     }
113                 }
114                 if (ln != null) {
115                     String name = PATH + "pfd_" + ln + ".xml";
116                     try {
117                         InputStreamReader reader = new InputStreamReader(
118                                 ICUData.getRequiredStream(getClass(), name), "UTF-8");
119                         DataRecord dr = DataRecord.read(ln, new XMLRecordReader(reader));
120                         reader.close();
121                         if (dr != null) {
122                             // debug
123                             // if (false && ln.equals("ar_EG")) {
124                             // OutputStreamWriter osw = new
125                             // OutputStreamWriter(System.out, "UTF-8");
126                             // XMLRecordWriter xrw = new
127                             // XMLRecordWriter(osw);
128                             // dr.write(xrw);
129                             // osw.flush();
130                             // }
131                             ld = new PeriodFormatterData(localeName, dr);
132                         }
133                     } catch (UnsupportedEncodingException e) {
134                         throw new MissingResourceException(
135                                 "Unhandled encoding for resource " + name, name, "");
136                     } catch (IOException e) {
137                         throw new ICUUncheckedIOException(
138                                 "Failed to close() resource " + name, e);
139                     }
140                 } else {
141                     throw new MissingResourceException(
142                             "Duration data not found for  " + localeName, PATH,
143                             localeName);
144                 }
145 
146                 // if (ld == null) {
147                 // ld = getFallbackFormatterData();
148                 // }
149                 cache.put(localeName, ld);
150             }
151             lastData = ld;
152             lastLocale = localeName;
153 
154             return ld;
155         }
156     }
157 
158     @Override
getAvailableLocales()159     public Collection<String> getAvailableLocales() {
160         return availableLocales;
161     }
162 
163     // PeriodFormatterData getFallbackFormatterData() {
164     // synchronized (this) {
165     // if (fallbackFormatterData == null) {
166     // DataRecord dr = new DataRecord(); // hack, no default, will die if used
167     // fallbackFormatterData = new PeriodFormatterData(null, dr);
168     // }
169     // return fallbackFormatterData;
170     // }
171     // }
172 }
173