123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- 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 = "<span[^>]*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 = "<span class=\"entityhig\" style=\"border-bottom: 8px solid rgb(68, 141, 255); --data-wih: translate(0px,30px); line-height: 30px; --color: #448DFF; background: none; box-shadow: none;\" data-before=\"运动员\" id=\"10\">王曼昱</span>";
- 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 = "<span[^>]*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();
- }
- }
|