workaroud for snor issue
[vfc/nfvo/wfengine.git] / wso2 / baseservice-i18n / src / main / java / org / openo / baseservice / i18n / I18nImpl.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 org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Locale;
24 import java.util.Map;
25 import java.util.Map.Entry;
26
27 public class I18nImpl implements I18n {
28   private static Logger logger = LoggerFactory.getLogger(I18nImpl.class);
29
30   private String name;
31
32   private Map<String, I18nItem> items;
33
34   private Map<String, String> keyToValueMap;
35
36   private Map<String, String> valueToKeyMap;
37
38
39   public I18nImpl(String name, Map<String, I18nItem> items) {
40     this.name = name;
41     this.items = Collections.unmodifiableMap(items);
42
43     keyToValueMap = new HashMap<>(items.size());
44     valueToKeyMap = new HashMap<>(items.size() * 3);
45
46     try {
47       for (Entry<String, I18nItem> entry : items.entrySet()) {
48         String key = entry.getKey();
49         I18nItem item = entry.getValue();
50         String value = I18nJsonUtil.getInstance().writeToJson(item.getValues());
51
52         keyToValueMap.put(key, value);
53         for (Entry<String, String> valueEntry : item.getValues().entrySet()) {
54           valueToKeyMap.put(valueEntry.getValue(), key);
55         }
56       }
57     } catch (Exception e) {
58       logger.error("new I18nImpl failed:" + name, e);
59     }
60
61     keyToValueMap = Collections.unmodifiableMap(keyToValueMap);
62     valueToKeyMap = Collections.unmodifiableMap(valueToKeyMap);
63   }
64
65   @Override
66   public Map<String, String> getLabelValues(String labelKey) {
67     I18nItem item = items.get(labelKey);
68     if (item != null) {
69       return item.getValues();
70     }
71     return null;
72   }
73
74   @Override
75   public String getLabelValue(String labelKey, Locale theLocale) {
76     I18nItem item = items.get(labelKey);
77     if (item != null) {
78       Map<String, String> values = item.getValues();
79       return values.get(I18nLocaleTransfer.transfer(theLocale, values.keySet()));
80     }
81     return null;
82   }
83
84   @Override
85   public String getLabelValue(String labelKey, Locale theLocale, String[] arguments) {
86     I18nItem item = items.get(labelKey);
87     if (item != null) {
88       Map<String, String> values = item.getValues();
89       String value = values.get(I18nLocaleTransfer.transfer(theLocale, values.keySet()));
90       return replaceArguments(value, arguments);
91     }
92     return null;
93   }
94
95   @Override
96   public Map<String, String> getLabelValues(String labelKey, String[] arguments) {
97     I18nItem item = items.get(labelKey);
98     if (item != null) {
99       Map<String, String> map = new HashMap<String, String>();
100       for (Entry<String, String> entry : item.getValues().entrySet()) {
101         String value = entry.getValue();
102         map.put(entry.getKey(), replaceArguments(value, arguments));
103       }
104       return map;
105     }
106     return null;
107   }
108
109   @Override
110   public String getCanonicalLabelValues(String labelKey) {
111     return keyToValueMap.get(labelKey);
112   }
113
114   @Override
115   public String getKeyFromValue(String aValue) {
116     return valueToKeyMap.get(aValue);
117   }
118
119   private String replaceArguments(String value, String[] arguments) {
120     if (value == null) {
121       return null;
122     }
123     if (arguments != null) {
124       int i = 0;
125       for (String argument : arguments) {
126         if (argument == null) {
127           value = value.replaceAll("\\{\\s*" + i + "\\s*\\}", "");
128         } else {
129           value = value.replaceAll("\\{\\s*" + i + "\\s*\\}", argument);
130         }
131         i++;
132       }
133     }
134     return value.replaceAll("\\{\\s*\\d+\\s*\\}", "");
135   }
136
137 }