1 /*
2  * Copyright (c) 2021-2024 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 org.codehaus.jackson.map.ObjectMapper;
19 import org.codehaus.jackson.JsonNode;
20 import org.codehaus.jackson.annotate.JsonIgnoreProperties;
21 
22 import java.io.File;
23 import java.io.IOException;
24 import java.net.URISyntaxException;
25 import java.util.Iterator;
26 import java.util.ArrayList;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 
30 /**
31  * Resource configuration defined in the reource_items.json
32  *
33  * @since 2022-8-22
34  */
35 public class ResourceConfiguration {
36     private static final Logger LOG = Logger.getLogger("ResourceConfiguration");
37 
ResourceConfiguration()38     private ResourceConfiguration() {}
39 
40     /**
41      * method used to parse resource_items.json
42      *
43      * @return the resource configuration extracted from resource_items.json
44      */
parse()45     public static ArrayList<ResourceConfiguration.ConfigItem> parse() {
46         ArrayList<ResourceConfiguration.ConfigItem> ret = new ArrayList<>();
47         try {
48             File json = new File(ResourceConfiguration.class.getResource("/resource/resource_items.json").toURI());
49             ObjectMapper mapper = new ObjectMapper();
50             JsonNode jsonNode = mapper.readTree(json);
51             Iterator<JsonNode> jsonNodes = jsonNode.getElements();
52             int count = 0;
53             while (jsonNodes.hasNext()) {
54                 JsonNode node = jsonNodes.next();
55                 ConfigItem item = mapper.treeToValue(node, ConfigItem.class);
56                 if (count != item.index) {
57                     throw new IllegalStateException("not consecutive index for index " + count);
58                 }
59                 ++count;
60                 ret.add(item);
61             }
62         } catch (IOException | URISyntaxException e) {
63             LOG.log(Level.SEVERE, "Generated errors when read resource_items.json file.");
64         }
65         return ret;
66     }
67 
68     /**
69      * Configuration item defined in resource_items.json
70      */
71     @JsonIgnoreProperties(ignoreUnknown = true)
72     public static class ConfigItem {
73         int index;
74         String method;
75         int lengths;
76         int offsets;
77         String pub;
78         String sep;
79         String pointer;
80         String type;
81         Element[] elements;
82 
83         /**
84          * get index
85          *
86          * @return index
87          */
getIndex()88         public int getIndex() {
89             return index;
90         }
91 
92         /**
93          * get method
94          *
95          * @return method
96          */
getMethod()97         public String getMethod() {
98             return method;
99         }
100 
101         /**
102          * get lengths
103          *
104          * @return length
105          */
getLengths()106         public int getLengths() {
107             return lengths;
108         }
109 
110         /**
111          * get offsets
112          *
113          * @return offsets
114          */
getOffsets()115         public int getOffsets() {
116             return offsets;
117         }
118 
119         /**
120          * get pub
121          *
122          * @return pub
123          */
getPub()124         public String getPub() {
125             return pub;
126         }
127 
128         /**
129          * get sep
130          *
131          * @return sep
132          */
getSep()133         public String getSep() {
134             return sep;
135         }
136 
137         /**
138          * get pointer
139          *
140          * @return Pointer
141          */
getPointer()142         public String getPointer() {
143             return pointer;
144         }
145 
146         /**
147          * get type
148          *
149          * @return type
150          */
getType()151         public String getType() {
152             return type;
153         }
154 
155         /**
156          * get elements
157          *
158          * @return elements
159          */
getElements()160         public Element[] getElements() {
161             return elements;
162         }
163     }
164 
165     /**
166      * element pattern
167      */
168     public static class Element {
169         String availableFormat;
170         String skeleton;
171         int enumIndex;
172         int index;
173 
174         /**
175          * get availableFormat
176          *
177          * @return availableFormat
178          */
getAvailableFormat()179         public String getAvailableFormat() {
180             return availableFormat;
181         }
182 
183         /**
184          * get skeleton
185          *
186          * @return return skeleton
187          */
getSkeleton()188         public String getSkeleton() {
189             return skeleton;
190         }
191 
192         /**
193          * get enumIndex
194          *
195          * @return enumIndex
196          */
getEnumIndex()197         public int getEnumIndex() {
198             return enumIndex;
199         }
200 
201         /**
202          * get index
203          *
204          * @return index
205          */
getIndex()206         public int getIndex() {
207             return index;
208         }
209     }
210 }
211