package com.pavis.backend.slim.common.utils; import cn.hutool.core.date.DateUtil; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import java.io.FileInputStream; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @version: java version 1.8 * @Author: Guan H.J. * @description: * @date: 2023-11-01 14:25 */ @Slf4j public class CommonUtils { /** * 判断当前数据是否是一个JSON。 * * @param jsonStr * @return */ public static Boolean isJson(String jsonStr) { try { JSONObject jsonObject = JSON.parseObject(jsonStr); if (jsonObject != null) { return true; } else { log.info("该jsonStr不是一个JSON对象"); } } catch (Exception e) { log.info("ex -> 该jsonStr不是一个JSON对象:{}", e); e.printStackTrace(); } return false; } /** * 判断当前数据是否是一个JSONArray。 * * @param jsonStr * @return */ public static Boolean isJsonArray(String jsonStr) { try { Object json = JSON.parse(jsonStr); if (json instanceof JSONArray) { log.info("该jsonArrayStr是一个JSONArray对象"); JSONArray jsonArray = (JSONArray) json; return true; } else { log.info("该jsonArrayStr不是一个JSONArray对象"); } } catch (Exception e) { log.info("ex -> 该jsonStr不是一个JSONArray对象:{}", e); e.printStackTrace(); } return false; } public static boolean checkMd5(String filename, String md5) { try { FileInputStream fs = new FileInputStream(filename); String uploadFileMd5 = DigestUtils.md5Hex(fs); log.info("uploadFileMd5,{}", uploadFileMd5); if (StringUtils.equalsIgnoreCase(md5, uploadFileMd5)) { return true; } } catch (Exception e) { e.printStackTrace(); return false; } return false; } /** * 处理导入导出图谱schema时实体id的值设置为NULL. * @param jsonArray * @return */ public static JSONArray dealEntityId(JSONArray jsonArray){ // 处理所有的实体ID,设置null。 for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObject = JSON.parseObject(jsonArray.get(i).toString()); if (jsonObject.containsKey("entityId")){ jsonObject.put("entityId",null); jsonObject.put("createTime", DateUtil.date()); log.info("包含entityId:{}",JSON.toJSONString(jsonObject)); } jsonArray.set(i,jsonObject); } return jsonArray; } /** * 处理导入导出图谱schema时实体关系id的值设置为NULL. * @param jsonArray * @return */ public static JSONArray dealRelationId(JSONArray jsonArray){ // 处理所有的实体ID,设置null。 for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObject = JSON.parseObject(jsonArray.get(i).toString()); if (jsonObject.containsKey("relationId")){ jsonObject.put("relationId",null); log.info("包含entityId"); } jsonArray.set(i,jsonObject); } return jsonArray; } /** * 处理前端标注数据的颜色。 * @param textFront * @param entityName * @param colorStr * @return */ public static String dealTextFrontWithColor(String textFront, String entityName,String colorStr,String nameStr) { log.info("正则匹配替换颜色或名称 sourceName:{},upName:{}",entityName,nameStr); String regex = "]*data-before=\"" + entityName + "\"[^>]*>([^<]+)<\\/span>"; log.info("regex:{}", regex); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(textFront); StringBuffer upTextFront = new StringBuffer(); while (matcher.find()) { String matched = matcher.group(1); log.info("matched:{}",matched); log.info("matcher[0]:{}",matcher.group(0)); String replacedStr = matcher.group(0).replaceAll("--color: #[0-9a-fA-F]{6}", "--color:" + colorStr); replacedStr = replacedStr.replaceAll("--color:#[0-9a-fA-F]{6}", "--color:" + colorStr); if (null != nameStr && StringUtils.isNotEmpty(nameStr)){ replacedStr = replacedStr.replaceAll("data-before=\"" + entityName + "\"", "data-before=\"" + nameStr + "\""); } log.info("replacedStr:{}",replacedStr); matcher.appendReplacement(upTextFront, replacedStr); } matcher.appendTail(upTextFront); log.info("最终替换后的标注数据 upTextFront:{}",upTextFront.toString()); return upTextFront.toString(); } public static void main(String[] args) { String textFront = "王曼昱"; String entityName = "运动员"; String colorStr = "#ffffff"; String upTextFront = dealTextFrontWithColorName(textFront, entityName, colorStr,entityName + "哈哈哈"); log.info("upTextFront:{}",upTextFront); } public static String dealTextFrontWithColorName(String textFront, String entityName,String colorStr,String nameStr) { String regex = "]*data-before=\"" + entityName + "\"[^>]*>([^<]+)<\\/span>"; log.info("regex:{}", regex); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(textFront); StringBuffer upTextFront = new StringBuffer(); while (matcher.find()) { String matched = matcher.group(1); log.info("matched:{}",matched); log.info("matcher[0]:{}",matcher.group(0)); String replacedStr = matcher.group(0).replaceAll("--color: #[0-9a-fA-F]{6}", "--color:" + colorStr); replacedStr = replacedStr.replaceAll("--color:#[0-9a-fA-F]{6}", "--color:" + colorStr); replacedStr = replacedStr.replaceAll("data-before=\"" + entityName + "\"", "data-before=\"" + nameStr + "\""); log.info("replacedStr:{}",replacedStr); matcher.appendReplacement(upTextFront, replacedStr); } matcher.appendTail(upTextFront); log.info("最终替换后的标注数据 upTextFront:{}",upTextFront.toString()); return upTextFront.toString(); } }