diff --git a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/DefPatternPool.java b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/DefPatternPool.java index fe16e15..356026e 100644 --- a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/DefPatternPool.java +++ b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/DefPatternPool.java @@ -32,6 +32,12 @@ public final class DefPatternPool { */ public final static Pattern SECURITY_PASSWORD = Pattern.compile("^\\S*(?=\\S{6,})(?=\\S*\\d)(?=\\S*[A-Z])(?=\\S*[a-z])(?=\\S*[!@#$%^&*?.])\\S*$"); + /** + * 占位符 /\$\{(\w+)\}/g + * ${xxxx} + */ + public final static Pattern PLACEHOLDER = Pattern.compile("\\$\\{([\\w\\.\\-\\/\\+\\$\\#\\@\\!\\^\\&\\(\\)]+)\\}"); + // ------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/MessUtil.java b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/MessUtil.java new file mode 100644 index 0000000..317c583 --- /dev/null +++ b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/utils/MessUtil.java @@ -0,0 +1,74 @@ +package org.opsli.common.utils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * 获得 占位符中的内容 + * + * @author Parker + * @date 2021年6月2日15:41:36 + */ +public class MessUtil { + + /** + * 获得默认占位符 集合 + * ${xxxx} + * + * @param soap 原始信息 + * @return List + */ + public static List getPlaceholderList(String soap) { + // 使用 正则池 + Matcher matcher = DefPatternPool.PLACEHOLDER.matcher(soap); + List list = new ArrayList<>(); + int i = 1; + while (matcher.find()) { + list.add(matcher.group(i)); + } + return list; + } + + /** + * 组装信息 + * @param soap 原始信息 + * @param regex 条件 占位符 + * @param startStr 删除掉的内容 + * @param endStr 删除掉的内容 + * @param map 根据原始信息占位符中的内容作为key,获取替换进占位符中的内容 + * @return String + */ + public static String getMes(String soap, String regex, String startStr, String endStr, Map map) { + List subUtil = getSubList(soap, regex); + for (String s : subUtil) { + if (map.containsKey(s) && null != map.get(s)) { + soap = soap.replace(startStr + s + endStr, map.get(s).toString()); + } + } + return soap; + } + + + /** + * 组装信息 + * @param soap 原始信息 + * @param regex 条件 占位符 + * @return List + */ + public static List getSubList(String soap, String regex) { + // 使用 正则池 + Pattern pattern = DefPatternPool.get(regex); + Matcher matcher = pattern.matcher(soap); + List list = new ArrayList<>(); + int i = 1; + while (matcher.find()) { + list.add(matcher.group(i)); + } + return list; + } + + +} \ No newline at end of file diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/ValidatorUtil.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/ValidatorUtil.java index 9a5f622..1c4625a 100644 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/ValidatorUtil.java +++ b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/ValidatorUtil.java @@ -16,15 +16,16 @@ package org.opsli.core.utils; import cn.hutool.core.convert.Convert; +import cn.hutool.core.lang.Validator; import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.ReflectUtil; import io.swagger.annotations.ApiModelProperty; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.opsli.common.enums.ValidatorType; import org.opsli.common.utils.DefPatternPool; import org.opsli.common.msg.ValidatorMsg; import org.opsli.api.wrapper.system.dict.DictModel; -import org.opsli.common.annotation.validator.Validator; import org.opsli.common.annotation.validator.ValidatorLenMax; import org.opsli.common.annotation.validator.ValidatorLenMin; import org.opsli.common.exception.ServiceException; @@ -52,10 +53,11 @@ public final class ValidatorUtil { Field[] fields = ReflectUtil.getFields(obj.getClass()); for (Field field : fields) { - // 获得 统一验证 注解 - Validator validator = field.getAnnotation(Validator.class); + // 获得 统一验证 注解 (起码冲突了) + org.opsli.common.annotation.validator.Validator validator = + field.getAnnotation(org.opsli.common.annotation.validator.Validator.class); if (validator != null) { - org.opsli.common.enums.ValidatorType[] types = validator.value(); + ValidatorType[] types = validator.value(); Object fieldValue = ReflectUtil.getFieldValue(obj, field); ValidatorUtil.check(field, types, fieldValue); } @@ -84,7 +86,7 @@ public final class ValidatorUtil { * @param types 类型数组 * @param fieldValue 字段值 */ - private static void check(Field field, org.opsli.common.enums.ValidatorType[] types, Object fieldValue){ + private static void check(Field field, ValidatorType[] types, Object fieldValue){ // 获得字段名 String fieldName = field.getName(); ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class); @@ -95,13 +97,13 @@ public final class ValidatorUtil { String value = Convert.toStr(fieldValue); // 循环验证 - for (org.opsli.common.enums.ValidatorType type : types) { + for (ValidatorType type : types) { try { boolean verifyRet; switch (type) { // 不能为空 case IS_NOT_NULL: { - verifyRet = cn.hutool.core.lang.Validator.isNotEmpty(fieldValue); + verifyRet = Validator.isNotEmpty(fieldValue); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_NOT_NULL; msg.setFieldName(fieldName); @@ -114,7 +116,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isGeneral(value); + verifyRet = Validator.isGeneral(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_GENERAL; msg.setFieldName(fieldName); @@ -168,7 +170,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isLetter(value); + verifyRet = Validator.isLetter(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_LETTER; msg.setFieldName(fieldName); @@ -181,7 +183,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isUpperCase(value); + verifyRet = Validator.isUpperCase(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_UPPER_CASE; msg.setFieldName(fieldName); @@ -194,7 +196,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isLowerCase(value); + verifyRet = Validator.isLowerCase(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_LOWER_CASE; msg.setFieldName(fieldName); @@ -207,7 +209,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isIpv4(value) || cn.hutool.core.lang.Validator.isIpv6(value) ; + verifyRet = Validator.isIpv4(value) || Validator.isIpv6(value) ; if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_IP; msg.setFieldName(fieldName); @@ -220,7 +222,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isIpv4(value); + verifyRet = Validator.isIpv4(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_IPV4; msg.setFieldName(fieldName); @@ -233,7 +235,7 @@ public final class ValidatorUtil { if(StringUtils.isEmpty(value)){ break; } - verifyRet = cn.hutool.core.lang.Validator.isIpv6(value); + verifyRet = Validator.isIpv6(value); if(!verifyRet){ ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_IPV6; msg.setFieldName(fieldName); @@ -246,7 +248,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isMoney(value); + verifyRet = Validator.isMoney(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_MONEY; msg.setFieldName(fieldName); @@ -259,7 +261,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isEmail(value); + verifyRet = Validator.isEmail(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_EMAIL; msg.setFieldName(fieldName); @@ -272,7 +274,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isMobile(value); + verifyRet = Validator.isMobile(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_MOBILE; msg.setFieldName(fieldName); @@ -285,7 +287,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isCitizenId(value); + verifyRet = Validator.isCitizenId(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_CITIZENID; msg.setFieldName(fieldName); @@ -298,7 +300,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isZipCode(value); + verifyRet = Validator.isZipCode(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_ZIPCODE; msg.setFieldName(fieldName); @@ -311,7 +313,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isUrl(value); + verifyRet = Validator.isUrl(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_URL; msg.setFieldName(fieldName); @@ -324,7 +326,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isChinese(value); + verifyRet = Validator.isChinese(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_CHINESE; msg.setFieldName(fieldName); @@ -337,7 +339,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isGeneralWithChinese(value); + verifyRet = Validator.isGeneralWithChinese(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_GENERAL_WITH_CHINESE; msg.setFieldName(fieldName); @@ -350,7 +352,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isMac(value); + verifyRet = Validator.isMac(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_MAC; msg.setFieldName(fieldName); @@ -363,7 +365,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isPlateNumber(value); + verifyRet = Validator.isPlateNumber(value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_PLATE_NUMBER; msg.setFieldName(fieldName); @@ -376,7 +378,7 @@ public final class ValidatorUtil { if (StringUtils.isEmpty(value)) { break; } - verifyRet = cn.hutool.core.lang.Validator.isMatchRegex( + verifyRet = Validator.isMatchRegex( DefPatternPool.SECURITY_PASSWORD, value); if (!verifyRet) { ValidatorMsg msg = ValidatorMsg.EXCEPTION_IS_SECURITY_PASSWORD; diff --git a/opsli-modulars/opsli-modulars-generator/src/main/java/org/opsli/plugins/generator/strategy/create/CodeBuilder.java b/opsli-modulars/opsli-modulars-generator/src/main/java/org/opsli/plugins/generator/strategy/create/CodeBuilder.java index e7cdb70..d430e2c 100644 --- a/opsli-modulars/opsli-modulars-generator/src/main/java/org/opsli/plugins/generator/strategy/create/CodeBuilder.java +++ b/opsli-modulars/opsli-modulars-generator/src/main/java/org/opsli/plugins/generator/strategy/create/CodeBuilder.java @@ -15,9 +15,14 @@ */ package org.opsli.plugins.generator.strategy.create; +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.collection.ListUtil; +import cn.hutool.core.convert.Convert; import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.StrUtil; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -25,6 +30,7 @@ import com.jfinal.kit.Kv; import lombok.extern.slf4j.Slf4j; import org.opsli.api.ApiFlag; import org.opsli.common.enums.DictType; +import org.opsli.common.utils.MessUtil; import org.opsli.common.utils.Props; import org.opsli.common.utils.ZipUtils; import org.opsli.modulars.generator.logs.wrapper.GenBuilderModel; @@ -138,23 +144,13 @@ public enum CodeBuilder { CodeType codeType = CodeType.getCodeType(templateModel.getType()); String typeName = codeType.getDesc(); - // 包名 - String packageName = builderModel.getPackageName(); - - // 模块名 - String moduleName = builderModel.getModuleName(); - - // 子模块名 - String subModuleName = builderModel.getSubModuleName(); - // 基础路径 String basePath = CodeBuilder.BASE_PATH + dataStr + FOLDER_PREFIX + typeName; // Path路径 String path = templateModel.getPath(); if(StrUtil.isNotEmpty(path)){ - path = StrUtil.replace(path, "${packageName}", packageName); - path = StrUtil.replace(path, "${moduleName}", moduleName); - path = StrUtil.replace(path, "${subModuleName}", subModuleName); + // 替换占位符 + path = handleReplace(path, builderModel); // 处理路径 前后加 [/] path = this.handlePath(path); } @@ -164,31 +160,23 @@ public enum CodeBuilder { this.createKv(builderModel) ); - // 获得模板文件名称 - String templateFileName = templateModel.getFileName(); - String[] templateFileNameArr = StrUtil.split(templateFileName, "."); - - // 模板文件前缀 - String templateFilePrefix = templateFileNameArr[0]; // 模板文件后缀 - String templateFileSuffix = templateFileNameArr[templateFileNameArr.length-1]; + String templateFileSuffix = FileUtil.getSuffix(templateModel.getFileName()); // 判断是否是Java 文件 - if(JAVA_SUFFIX.equals(templateFileNameArr[templateFileNameArr.length-1])){ + if(JAVA_SUFFIX.equals(templateFileSuffix)){ codeStr = GeneratorFactory.getJavaHeadAnnotation() + codeStr; } // 生成文件名 String fileName = builderModel.getModel().getTableHumpName(); - if(DictType.NO_YES_NO.getValue().equals(templateModel.getIgnoreFileName()) || - StrUtil.isEmpty(templateModel.getIgnoreFileName()) - ){ - if(templateFileNameArr.length > 1){ - // 判断是否忽略模板文件名 - fileName += templateFilePrefix; - } + + // 判断是否忽略文件名 (忽略默认采用 表名+后缀名) + if(DictType.NO_YES_YES.getValue().equals(templateModel.getIgnoreFileName())){ + fileName += StrUtil.prependIfMissing(templateFileSuffix, POINT_PREFIX); + }else { + fileName = handleReplace(templateModel.getFileName(), builderModel); } - fileName += StrUtil.prependIfMissing(templateFileSuffix, POINT_PREFIX); Map entityMap = Maps.newHashMapWithExpectedSize(3); entityMap.put(ZipUtils.FILE_PATH, basePath + path); @@ -214,7 +202,7 @@ public enum CodeBuilder { * @param path 路径 * @return String */ - protected String handlePath(String path) { + private String handlePath(String path) { if(StrUtil.isEmpty(path)){ return path; } @@ -232,6 +220,30 @@ public enum CodeBuilder { return StrUtil.replace(path, "//", FOLDER_PREFIX); } + /** + * 处理替换占位符 + * @param str 原字符串 + * @param builderModel 模型数据 + * @return String + */ + private String handleReplace(String str, GenBuilderModel builderModel){ + // 非法处理 + if(StrUtil.isEmpty(str) || builderModel == null){ + return str; + } + + String prefix = "${"; + String suffix = "}"; + List placeholderList = MessUtil.getPlaceholderList(str); + for (String placeholderField : placeholderList) { + Object property = BeanUtil.getProperty(builderModel, placeholderField); + str = StrUtil.replace(str, + prefix + placeholderField + suffix, + Convert.toStr(property)); + } + return str; + } + /** * 导出文件时为Writer生成OutputStream */ @@ -246,4 +258,13 @@ public enum CodeBuilder { return null; } + public static void main(String[] args) { + + String aaa = "aaaaaaaaab${bb}bbbbbbbb${bbaa}bbbbbbbccccccccccccc"; + List placeholderList = MessUtil.getPlaceholderList(aaa); + for (String s : placeholderList) { + System.out.println(s); + } + } + }