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; 17 18 import java.io.InputStream; 19 import java.util.ArrayList; 20 import java.util.List; 21 import java.util.HashMap; 22 import java.util.Map; 23 24 import java.util.Locale; 25 26 public class ModuleJsonUtil { 27 private static final String VERSION_CODE = "\"versionCode\""; 28 private static final String VERSION_NAME = "\"versionName\""; 29 private static final Log LOG = new Log(ModuleJsonUtil.class.toString()); 30 31 /** 32 * get the versionCode from json file. 33 * 34 * @param jsonStr uncompress json object 35 * @return the result 36 */ getVersion(String jsonString)37 public static Version getVersion(String jsonString) throws BundleException { 38 Version version = new Version(); 39 try { 40 if (jsonString != null) { 41 // find versionName 42 int indexOfVersionName = jsonString.indexOf(VERSION_NAME); 43 if (indexOfVersionName <= 0) { 44 LOG.error("ModuleJsonUtil::getVersion obtain index of VERSION_NAME failed"); 45 return version; 46 } 47 indexOfVersionName += VERSION_NAME.length(); 48 int nameStart = jsonString.indexOf("\"", indexOfVersionName); 49 int nameEnd = nameStart + 1; 50 while(jsonString.charAt(nameEnd) != '\"') { 51 ++nameEnd; 52 } 53 version.versionName = jsonString.substring(nameStart + 1, nameEnd).trim(); 54 //find versionCode 55 int indexOfVersionCode = jsonString.indexOf(VERSION_CODE); 56 if (indexOfVersionCode <= 0) { 57 LOG.error("ModuleJsonUtil::getVersion obtain index of VERSION_CODE failed"); 58 return version; 59 } 60 indexOfVersionCode += VERSION_CODE.length(); 61 while(!Character.isDigit(jsonString.charAt(indexOfVersionCode))) { 62 ++indexOfVersionCode; 63 } 64 String versionCode = ""; 65 while(Character.isDigit(jsonString.charAt(indexOfVersionCode))) { 66 versionCode += jsonString.charAt(indexOfVersionCode); 67 ++indexOfVersionCode; 68 } 69 version.versionCode = Integer.parseInt(versionCode.trim()); 70 } 71 } catch (NumberFormatException exception) { 72 LOG.error("ModuleJsonUtil::getVersion NumberFormatException exception: " + exception.getMessage()); 73 throw new BundleException("ModuleJsonUtil::getVersion failed"); 74 } 75 return version; 76 } 77 } 78