1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package ohos.global.i18n; 17 18 import java.util.HashMap; 19 import java.io.BufferedReader; 20 import java.io.IOException; 21 import java.io.InputStreamReader; 22 import java.io.File; 23 import java.io.FileInputStream; 24 import java.net.URISyntaxException; 25 import java.nio.charset.StandardCharsets; 26 import java.nio.file.FileSystems; 27 import java.util.logging.Logger; 28 import java.util.logging.Level; 29 30 /** 31 * This class is used to extract measure format pattern data related to a locale 32 * 33 * @since 2022-8-22 34 */ 35 public class MeasureFormatPatternFetcher { 36 private static MeasureFormatPatternFetcher patternFetcher = new MeasureFormatPatternFetcher(); 37 private static final Logger logger = Logger.getLogger("MeasureFormatPatternFetcher"); 38 private static final String PATH = "/resource" + FileSystems.getDefault().getSeparator() + 39 "measure_format_patterns.txt"; 40 41 static { patternFetcher.init()42 patternFetcher.init(); 43 } 44 45 private HashMap<String, String> locale2Pattern; 46 MeasureFormatPatternFetcher()47 private MeasureFormatPatternFetcher() {} 48 49 /** 50 * Return singleton instance; 51 * 52 * @return patternFetcher; 53 */ getInstance()54 public static MeasureFormatPatternFetcher getInstance() { 55 return patternFetcher; 56 } 57 58 /** 59 * Get measure format pattern related to locale. 60 * 61 * @param locale Indicates which locale's data will be retrived. 62 * @return measure format pattern related to locale. 63 */ get(String locale)64 public String get(String locale) { 65 String pattern = locale2Pattern.get(locale); 66 if (pattern == null) { 67 return ""; 68 } 69 return pattern; 70 } 71 init()72 private void init() { 73 try (BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream( 74 new File(MeasureFormatPatternFetcher.class.getResource(PATH).toURI())), StandardCharsets.UTF_8))) { 75 locale2Pattern = new HashMap<>(); 76 String line = ""; 77 while ((line = fin.readLine()) != null) { 78 String[] temp = getPatterns(line); 79 if (temp.length == 2) { 80 locale2Pattern.put(temp[0], temp[1]); 81 } 82 } 83 } catch (IOException | URISyntaxException e) { 84 logger.log(Level.SEVERE, "Init error"); 85 } 86 } 87 getPatterns(String line)88 private String[] getPatterns(String line) { 89 String[] result = new String[2]; 90 String trimedLine = line.trim(); 91 String[] localeAndPatterns = trimedLine.split(" ", 2); // Split into 2 parts 92 if (localeAndPatterns.length != 2) { 93 logger.log(Level.SEVERE, "Init error"); 94 return new String[0]; 95 } 96 result[0] = localeAndPatterns[0]; 97 String[] patterns = localeAndPatterns[1].split(", "); 98 StringBuilder sb = new StringBuilder(); 99 for (int i = 0; i < patterns.length; ++i) { 100 if (patterns[i].length() > 2) { 101 int length = patterns[i].length(); 102 sb.append(patterns[i].substring(1, length - 1)); 103 } 104 if (i != patterns.length) { 105 sb.append(FileConfig.SEP); 106 } 107 } 108 result[1] = sb.toString(); 109 return result; 110 } 111 }