package customization.test; import aiyh.utils.Util; import aiyh.utils.fileUtil.ProperUtil; import cn.hutool.core.util.URLUtil; import com.alibaba.fastjson.JSON; import com.api.aiyh_pcn.patentWall.service.PatentWallService; import com.api.aiyh_pcn.patentWall.vo.PatentVO; import com.api.aiyh_pcn.patentWall.vo.SearchInputVO; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; import weaver.common.util.string.StringUtil; import weaver.general.GCONST; import java.io.*; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author EBU7-dev1-ayh * @create 2021/11/29 0029 10:16 */ public class NewUtilTest { @Before public void before() throws Exception { GCONST.setServerName("ecology"); GCONST.setRootPath("H:\\e-cology-dev\\web\\"); } @AfterClass public static void after() { System.out.println("单元测试结束!"); } @Test public void testKey() { String key = "aiyh.patent.config[11].public"; String pattern = "^(aiyh.patent\\.)(?(\\w+))$"; String pattern1 = "^(aiyh.patent\\.)(?(?(\\w+))\\.(\\S)+(?!\\[))"; String pattern2 = "^(aiyh.patent\\.)(?(\\w+))(\\[([0-9])+])$"; String pattern3 = "^(aiyh.patent\\.)(?(\\w+))(\\[(?([0-9])+)])\\.(?(\\S)+)$"; Pattern compile = Pattern.compile(pattern); Matcher matcher = compile.matcher(key); while (matcher.find()) { System.out.println("单一值"); System.out.println(matcher.group("key")); } compile = Pattern.compile(pattern2); matcher = compile.matcher(key); while (matcher.find()) { System.out.println("数组"); System.out.println(matcher.group("key")); } compile = Pattern.compile(pattern3); matcher = compile.matcher(key); while (matcher.find()) { System.out.println("对象数组"); System.out.println(matcher.group("key")); System.out.println(matcher.group("objKey")); System.out.println(matcher.group("index")); } compile = Pattern.compile(pattern1); matcher = compile.matcher(key); while (matcher.find()) { System.out.println("多个"); System.out.println(matcher.group("key")); System.out.println(matcher.group("objKey")); } } @Test public void testGetPro() { String key = "aiyh.patent.config[0]"; String prefix = "aiyh.patent"; String value = "第一个元素"; getPro(prefix, key, value); } public void getPro(String prefix, String key, Object value) { Map result = new HashMap<>(); // result = keyHandler(prefix, key, value, result); String key2 = "aiyh.patent.config[1]"; // result = keyHandler(prefix, key2, "第二个元素", result); System.out.println(JSON.toJSONString(result)); } @Test public void redPro() { String fileName = "test"; String prefix = "aiyh.patentWall"; Map result = Util.readProperties2Map(fileName, prefix); System.out.println(JSON.toJSONString(result)); } public Map getProperties2Map(String fileName, String prefix) { String propertyPath = GCONST.getPropertyPath(); if (StringUtil.isNullOrEmpty(fileName)) { return null; } if (fileName.contains(".properties")) { fileName.replace(".properties", ""); } String path = propertyPath + "prop2map" + File.separator + fileName + ".properties"; ProperUtil prop = new ProperUtil(); Map map = new HashMap<>(); InputStream inputStream = null; try { inputStream = new BufferedInputStream(new FileInputStream(path)); prop.load(inputStream); // Enumeration enumeration = prop.propertyNames(); // 顺序读取 Enumeration enumeration = prop.keys(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); String value = prop.getProperty(key); System.out.println(key); keyHandler(prefix, key, value, map); ; } } catch (IOException e) { throw new RuntimeException("找不到文件:" + path); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return map; } public Map keyHandler(String prePrefix, String key, Object value, Map preResult) { String objRegex = "^(" + prePrefix + "\\.)(?(\\w+))$"; Pattern compile = Pattern.compile(objRegex); Matcher matcher = compile.matcher(key); if (matcher.find()) { // 只匹配前缀.key=value模式的 String resultKey = matcher.group("key"); preResult.put(resultKey, prop2MapPutValue(value)); } String moreKey = "^(" + prePrefix + "\\.)(?(?(\\w+))\\.(\\S)+(?!\\[))"; compile = Pattern.compile(moreKey); matcher = compile.matcher(key); if (matcher.find()) { // 匹配前缀.key1.key2=value模式的 String objKey = matcher.group("objKey"); String prefixStr = prePrefix + "." + objKey; Map valueMap; if (preResult.containsKey(objKey)) { valueMap = (Map) preResult.get(objKey); keyHandler(prefixStr, key, value, valueMap); return null; } valueMap = new HashMap<>(); keyHandler(prefixStr, key, value, valueMap); preResult.put(objKey, valueMap); return null; } String strList = "^(" + prePrefix + "\\.)(?(\\w+))(\\[(?([0-9])+)])$"; compile = Pattern.compile(strList); matcher = compile.matcher(key); if (matcher.find()) { // 匹配前缀.key[0]=value模式的 String objKey = matcher.group("key"); int index = Integer.parseInt(matcher.group("index")); if (preResult.containsKey(objKey)) { // 存在值 List valueList = (List) preResult.get(objKey); if (index >= valueList.size()) { valueList.add(prop2MapPutValue(value)); } else { valueList.set(index, prop2MapPutValue(value)); } return null; } List valueList = new ArrayList<>(); valueList.add(prop2MapPutValue(value)); preResult.put(objKey, valueList); return null; } String objArray = "^(" + prePrefix + "\\.)(?(\\w+))(\\[(?([0-9])+)])\\.(?(\\S)+)$"; // String objArray = "^("+prePrefix+"\\.)(?(\\w+))(\\[(?([0-9])+)])(\\.(?(\\S)+))+"; compile = Pattern.compile(objArray); matcher = compile.matcher(key); if (matcher.find()) { // 匹配前缀.key[0].name=value的模式 String arrKey = matcher.group("arrKey"); String objKey = matcher.group("objKey"); int index = Integer.parseInt(matcher.group("index")); List> mapList; if (preResult.containsKey(arrKey)) { // 存在 mapList = (List>) preResult.get(arrKey); // mapList Map valueMap; if (index >= mapList.size()) { valueMap = new HashMap<>(); valueMap.put(objKey, prop2MapPutValue(value)); mapList.add(valueMap); return null; } valueMap = mapList.get(index); String arrMoreKey = "(?(\\w+))\\.(\\S)+(?!\\[)"; Pattern arrMoreKeyCompile = Pattern.compile(arrMoreKey); Matcher arrMoreKeyMatcher = arrMoreKeyCompile.matcher(objKey); if (arrMoreKeyMatcher.find()) { String arrMoreObjKey = arrMoreKeyMatcher.group("key"); Map arrMoreValue; if (valueMap.containsKey(arrMoreObjKey)) { arrMoreValue = (Map) valueMap.get(arrMoreObjKey); keyHandler(arrMoreObjKey, objKey, value, arrMoreValue); return null; } arrMoreValue = new HashMap<>(); keyHandler(arrMoreObjKey, objKey, value, arrMoreValue); valueMap.put(arrMoreObjKey, arrMoreValue); return null; } arrMoreKey = "(?(\\w+))(\\[(?([0-9])+)])$"; arrMoreKeyCompile = Pattern.compile(arrMoreKey); arrMoreKeyMatcher = arrMoreKeyCompile.matcher(objKey); if (arrMoreKeyMatcher.find()) { String arrMoreArrKey = arrMoreKeyMatcher.group("key"); int arrMoreIndex = Integer.parseInt(arrMoreKeyMatcher.group("index")); List arrMoreListValue; if (valueMap.containsKey(arrMoreArrKey)) { // 存在值 arrMoreListValue = (List) valueMap.get(arrMoreArrKey); if (arrMoreIndex >= arrMoreListValue.size()) { arrMoreListValue.add(prop2MapPutValue(value)); } else { arrMoreListValue.set(arrMoreIndex, prop2MapPutValue(value)); } return null; } arrMoreListValue = new ArrayList<>(); arrMoreListValue.add(prop2MapPutValue(value)); valueMap.put(arrMoreArrKey, arrMoreListValue); return null; } // 直接添加 valueMap.put(objKey, prop2MapPutValue(value)); return null; } // 不存在 mapList = new ArrayList<>(); Map valueMap = new HashMap<>(); valueMap.put(objKey, prop2MapPutValue(value)); mapList.add(valueMap); preResult.put(arrKey, mapList); } return null; } public Object prop2MapPutValue(Object value) { if (value == null) { return null; } String valueStr = String.valueOf(value).trim(); if (valueStr.startsWith("\"") && valueStr.endsWith("\"")) { return valueStr.substring(1, valueStr.length() - 1); } if (valueStr.contains(",")) { return valueStr.split(","); } return value; } @Test public void testSubstr() { System.out.println("\"这个是文本框\"".trim().substring(1, "\"这个是文本框\"".trim().length() - 1)); } @Test public void testSplit() { String str = "12,343,89\\,99"; String str1 = "abcd'efg'hig?'klmn"; String[] data = str1.split("(?(\\w*|\\W*|[\\u4e00-\\u9fa5]*))(`~`)"; Pattern compile = Pattern.compile(pattern); Matcher matcher = compile.matcher(selectName); if (matcher.find()) { showName = matcher.group("lable"); } } System.out.println(showName); } @Test public void testPatentServer() { PatentWallService patentWallService = new PatentWallService(); List inputList = patentWallService.getSearchList("patentWall", 8); System.out.println(JSON.toJSONString(inputList)); List patentWallList = patentWallService.getList(null, "patentWall"); System.out.println(JSON.toJSONString(patentWallList)); } @Test public void testRegx() { String pattern = "\\$\\{(?(\\s|\\S)+?)}"; Pattern compile = Pattern.compile(pattern); Matcher matcher = compile.matcher("${id}"); if (matcher.find()) { System.out.println(matcher.group("field")); } } @Test public void testSplic(){ List list = new ArrayList<>(); list.add(0); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); System.out.println(list); System.out.println(list.subList(0,2)); list.removeAll(list.subList(0,2)); System.out.println(list); } @Test public void test() { String bizContent = "{\"contractExtension\":\".pdf\",\"contractTitle\":\"PCN Template - Non-Disclosure Agreement (Corporate).pdf\",\"uploadType\":1,\"customerNo\":\"ecef845ba95a409393cb66271a41b0a6\"}"; System.out.println(cn.hutool.core.util.URLUtil.encodeAll(bizContent, Charset.forName("UTF-8"))); } }