workaroud for snor issue
[vfc/nfvo/wfengine.git] / wso2 / baseservice-i18n / src / main / java / org / openo / baseservice / i18n / I18nJsonUtil.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openo.baseservice.i18n;
17
18 import com.fasterxml.jackson.core.JsonParseException;
19 import com.fasterxml.jackson.core.JsonProcessingException;
20 import com.fasterxml.jackson.databind.JsonMappingException;
21 import com.fasterxml.jackson.databind.ObjectMapper;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Locale;
26 import java.util.Set;
27 import java.util.concurrent.locks.Lock;
28 import java.util.concurrent.locks.ReentrantLock;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 final class I18nJsonUtil {
33
34     private static I18nJsonUtil util;
35     private static Lock lock = new ReentrantLock();
36     private ObjectMapper objectMapper;
37
38     public I18nJsonUtil(ObjectMapper objectMapper) {
39         this.objectMapper = objectMapper;
40     }
41
42     <T extends Object> T readFromJson(InputStream ins, Class<T> clazz)
43         throws JsonParseException, JsonMappingException, IOException {
44         ByteArrayOutputStream baos = new ByteArrayOutputStream();
45         byte[] bs = new byte[1024 * 10];
46         int length = -1;
47         while ((length = ins.read(bs)) > 0) {
48             baos.write(bs, 0, length);
49         }
50         bs = baos.toByteArray();
51
52         byte[] ubs = null;
53         if (bs.length > 3) {
54             // 删除bom头 -17, -69, -65
55             if (bs[0] == -17 && bs[1] == -69 && bs[2] == -65) {
56                 ubs = new byte[bs.length - 3];
57                 System.arraycopy(bs, 3, ubs, 0, ubs.length);
58             }
59         }
60         if (ubs == null) {
61             ubs = bs;
62         }
63         return objectMapper.readValue(ubs, clazz);
64     }
65
66     <T extends Object> T readFromJson(String str, Class<T> clazz)
67         throws JsonParseException, JsonMappingException, IOException {
68         return objectMapper.readValue(str, clazz);
69     }
70
71     String writeToJson(Object obj) throws JsonProcessingException {
72         return objectMapper.writeValueAsString(obj);
73     }
74
75     static I18nJsonUtil getInstance(ObjectMapper objectMapper) {
76         if (util == null) {
77             lock.lock();
78             try {
79                 if (util == null) {
80                     if (objectMapper == null) {
81                         objectMapper = new ObjectMapper();
82                     }
83                     util = new I18nJsonUtil(objectMapper);
84                 }
85             } finally {
86                 lock.unlock();
87             }
88         }
89         return util;
90     }
91
92     static I18nJsonUtil getInstance() {
93         return getInstance(null);
94     }
95 }
96
97
98 /**
99  * 国际化转换工具
100  *
101  * @author 10163976
102  */
103 class I18nLocaleTransfer {
104
105     private static Logger LOG = LoggerFactory.getLogger(I18nLocaleTransfer.class);
106
107     /**
108      * 方言转换<br> 如果存在直接返回<br> 如果不存在,则获取语言进行模糊匹配,未匹配上则返回默认方言<br>
109      *
110      * @param theLocale 待转换方言
111      * @param locales 存在的方言
112      * @return 转换后的方言
113      */
114     public static String transfer(Locale theLocale, Set<String> locales) {
115         if (locales == null || locales.isEmpty()) {
116             LOG.debug("locales is NULL or empty");
117             return null;
118         }
119         if (theLocale == null) {
120             String result = fetchDefault(locales);
121             LOG.debug("transfer NULL --> " + result + " in " + locales);
122             return result;
123         }
124         String locale = theLocale.toString();
125         if (locale.isEmpty()) {
126             String result = fetchDefault(locales);
127             LOG.debug("transfer EMPTY --> " + result + " in " + locales);
128             return result;
129         }
130         // 精确匹配
131         if (locales.contains(locale)) {
132             return locale;
133         }
134
135         // 根据语言模糊匹配
136         String language = theLocale.getLanguage();
137         if (locales.contains(language)) {
138             LOG.debug("transfer " + locale + " --> " + language + " in " + locales);
139             return language;
140         }
141
142         language = language + "_";
143         for (String temp : locales) {
144             if (temp.startsWith(language)) {
145                 LOG.debug("transfer " + locale + " --> " + temp + " in " + locales);
146                 return temp;
147             }
148         }
149         String result = fetchDefault(locales);
150         LOG.debug("transfer " + locale + " --> " + result + " in " + locales);
151         return result;
152     }
153
154     /**
155      * 返回默认方言,优先级为en,en_US,zh,zh_CN,如果都不存在,则随机返回一个
156      */
157     private static String fetchDefault(Set<String> locales) {
158         if (locales.contains("en")) {
159             return "en";
160         } else if (locales.contains("en_US")) {
161             return "en_US";
162         }
163         if (locales.contains("zh")) {
164             return "zh";
165         } else if (locales.contains("zh_CN")) {
166             return "zh_CN";
167         }
168         return locales.iterator().next();
169     }
170 }