1/* 2 * Copyright (c) 2023-2023 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 16import { Constants } from '../model/Constants'; 17import { GlobalThisHelper } from '../model/GlobalThisHelper'; 18import { GlobalThisStorageKey} from '../model/GlobalThisStorageKey'; 19import type common from '@ohos.app.ability.common'; 20import CheckEmptyUtils from './CheckEmptyUtils'; 21import { Log } from './Log'; 22 23export class StringUtil { 24 private static readonly DEFAULT_LEN_OF_COMMON_STR: number = 6; 25 private static readonly HALF_LEN_OF_COMMON_STR: number = 3; 26 private static readonly LINK_OF_COMMON_STR: string = '****'; 27 /** 28 * Get String from resources. 29 * 30 * @param {string} name - name of string.json 31 * @return {string} - string from res 32 */ 33 public static getString(name: string, ...args: Array<string | number>): string { 34 let abilityContext = GlobalThisHelper.getValue<common.UIAbilityContext>(GlobalThisStorageKey.KEY_MAIN_ABILITY_CONTEXT); 35 Log.info('getString abilityContext: ' +abilityContext) 36 if (abilityContext !== undefined && !CheckEmptyUtils.checkStrIsEmpty(name)) { 37 return <string> abilityContext?.resourceManager?.getStringByNameSync(name, ...args) ?? ''; 38 } 39 return ''; 40 } 41 /** 42 * Get String from resources. 43 * 44 * @param name string code 45 * @param context context 46 * @param args variable args 47 * @returns string value 48 */ 49 public static getStringByName(name: string, context: common.Context, ...args: Array<string | number>): string { 50 if (CheckEmptyUtils.isEmpty(context) || CheckEmptyUtils.checkStrIsEmpty(name)) { 51 return ''; 52 } 53 return context!.resourceManager?.getStringByNameSync(name, ...args) ?? ''; 54 } 55 /** 56 * get anonymization string 57 * 58 * @param {string} originStr - string 59 * @return {string} - anonymization string 60 */ 61 public static encodeCommonString(originStr: string):string { 62 if (!originStr) { 63 return originStr; 64 } 65 if (originStr.length <= this.DEFAULT_LEN_OF_COMMON_STR) { 66 return originStr; 67 } 68 return originStr.substring(0, this.HALF_LEN_OF_COMMON_STR) + 69 this.LINK_OF_COMMON_STR + 70 originStr.substring(originStr.length - this.HALF_LEN_OF_COMMON_STR); 71 } 72 73 /** 74 * get split mac 75 * 76 * @param {string} str - string 77 * @return {string} - anonymization mac string 78 */ 79 public static splitMac(str: string):string { 80 if (!str) { 81 return str; 82 } 83 let strSplit = str.split('://'); 84 if (strSplit.length > 1) { 85 return strSplit[0] + '://' + this.encodeCommonString(strSplit[1]); 86 } else if (str.length > this.DEFAULT_LEN_OF_COMMON_STR) { 87 return this.encodeCommonString(str); 88 } else { 89 return str; 90 } 91 } 92 93 public static getNetTypeByPrintId(printId: string):string { 94 if (!printId) { 95 return printId; 96 } 97 let printIdSplit = printId.split(':'); 98 let minLength = 2; 99 if (printIdSplit.length > minLength) { 100 return printIdSplit[1]; 101 } else { 102 return ''; 103 } 104 } 105 106}