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 anonymization string 43 * 44 * @param {string} originStr - string 45 * @return {string} - anonymization string 46 */ 47 public static encodeCommonString(originStr: string):string { 48 if (!originStr) { 49 return originStr; 50 } 51 if (originStr.length <= this.DEFAULT_LEN_OF_COMMON_STR) { 52 return originStr; 53 } 54 return originStr.substring(0, this.HALF_LEN_OF_COMMON_STR) + 55 this.LINK_OF_COMMON_STR + 56 originStr.substring(originStr.length - this.HALF_LEN_OF_COMMON_STR); 57 } 58 59 /** 60 * get split mac 61 * 62 * @param {string} str - string 63 * @return {string} - anonymization mac string 64 */ 65 public static splitMac(str: string):string { 66 if (!str) { 67 return str; 68 } 69 let strSplit = str.split('://'); 70 if (strSplit.length > 1) { 71 return strSplit[0] + '://' + this.encodeCommonString(strSplit[1]); 72 } else if (str.length > this.DEFAULT_LEN_OF_COMMON_STR) { 73 return this.encodeCommonString(str); 74 } else { 75 return str; 76 } 77 } 78 79 public static getNetTypeByPrintId(printId: string):string { 80 if (!printId) { 81 return printId; 82 } 83 let printIdSplit = printId.split(':'); 84 let minLength = 2; 85 if (printIdSplit.length > minLength) { 86 return printIdSplit[1]; 87 } else { 88 return ''; 89 } 90 } 91 92}