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