commit 189246a1a3a8588036d687760ed38fc20744d793 Author: hu645 <645326698@qq.com> Date: Sun Dec 1 19:18:17 2024 +0800 EnumGenerator diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..35f4d9b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/EnumGenerator-1.0-SNAPSHOT.jar b/EnumGenerator-1.0-SNAPSHOT.jar new file mode 100644 index 0000000..51daef3 Binary files /dev/null and b/EnumGenerator-1.0-SNAPSHOT.jar differ diff --git a/EnumGenerator.7z b/EnumGenerator.7z new file mode 100644 index 0000000..a8890cb Binary files /dev/null and b/EnumGenerator.7z differ diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml new file mode 100644 index 0000000..4ad225c --- /dev/null +++ b/dependency-reduced-pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + com.psbc + EnumGenerator + 1.0-SNAPSHOT + + + + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + com.psbc.Main + + + + + + + + + + 11 + 11 + UTF-8 + + diff --git a/output/EthicyEnum.java b/output/EthicyEnum.java new file mode 100644 index 0000000..131c905 --- /dev/null +++ b/output/EthicyEnum.java @@ -0,0 +1,42 @@ +public enum Ethicy { +1("1", "汉族"), +2("2", "回族"), +3("3", "维吾尔族"), +4("4", "苗族"), +5("5", "藏族") +; + +private final String code; +private final String value; + +Ethicy(String code, String value) { +this.code = code; +this.value = value; +} + +public String getCode() { +return code; +} + +public String getValue() { +return value; +} + +public static Ethicy findByCode(String code) { +for (Ethicy constant : values()) { +if (constant.key.equals(code)) { +return constant; +} +} +return null; // 或者抛出 IllegalArgumentException +} + +public static Ethicy findByValue(String value) { +for (Ethicy constant : values()) { +if (constant.value.equals(value)) { +return constant; +} +} +return null; // 或者抛出 IllegalArgumentException +} +} diff --git a/output/NationEnum.java b/output/NationEnum.java new file mode 100644 index 0000000..005b0d6 --- /dev/null +++ b/output/NationEnum.java @@ -0,0 +1,41 @@ +public enum Nation { +A("A", "阿根廷"), +B("B", "巴西"), +C("C", "加拿大"), +D("D", "丹麦") +; + +private final String code; +private final String value; + +Nation(String code, String value) { +this.code = code; +this.value = value; +} + +public String getCode() { +return code; +} + +public String getValue() { +return value; +} + +public static Nation findByCode(String code) { +for (Nation constant : values()) { +if (constant.key.equals(code)) { +return constant; +} +} +return null; // 或者抛出 IllegalArgumentException +} + +public static Nation findByValue(String value) { +for (Nation constant : values()) { +if (constant.value.equals(value)) { +return constant; +} +} +return null; // 或者抛出 IllegalArgumentException +} +} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..499ee45 --- /dev/null +++ b/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + com.psbc + EnumGenerator + 1.0-SNAPSHOT + + + 11 + 11 + UTF-8 + + + + org.freemarker + freemarker + 2.3.31 + + + com.alibaba + easyexcel + 3.3.4 + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + com.psbc.Main + + + + + + + + + + \ No newline at end of file diff --git a/run.bat b/run.bat new file mode 100644 index 0000000..b2c7882 --- /dev/null +++ b/run.bat @@ -0,0 +1,12 @@ +@echo off +REM 璁剧疆JAR鏂囦欢璺緞 +set JAR_PATH=D:\myDeployment\EnumGenerator\EnumGenerator-1.0-SNAPSHOT.jar + +REM 璁剧疆浼犻掔粰JAR鍖呯殑鍙傛暟 +set PARAM1=D:\myDeployment\EnumGenerator\test.xlsx +set PARAM2=D:\myDeployment\EnumGenerator\output\ +mkdir %PARAM2% +REM 鎵цJAR鍖呭苟浼犻掑弬鏁 +java -jar %JAR_PATH% %PARAM1% %PARAM2% + +pause diff --git a/run1.bat b/run1.bat new file mode 100644 index 0000000..8c9f647 --- /dev/null +++ b/run1.bat @@ -0,0 +1,12 @@ +@echo off +REM 璁剧疆JAR鏂囦欢璺緞 +set JAR_PATH=.\EnumGenerator-1.0-SNAPSHOT.jar + +REM 璁剧疆浼犻掔粰JAR鍖呯殑鍙傛暟 +set PARAM1=%1 +set PARAM2=%2 +mkdir .\%PARAM2% +REM 鎵цJAR鍖呭苟浼犻掑弬鏁 +java -jar %JAR_PATH% %PARAM1% %PARAM2% + +pause diff --git a/src/main/java/com/psbc/EnumGenerator.java b/src/main/java/com/psbc/EnumGenerator.java new file mode 100644 index 0000000..0e8deb9 --- /dev/null +++ b/src/main/java/com/psbc/EnumGenerator.java @@ -0,0 +1,51 @@ +package com.psbc; + +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateException; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.util.HashMap; +import java.util.Map; + +public class EnumGenerator { + + public static void main() { + // 瀹氫箟鏋氫妇鍚嶇О鍜屽父閲 + String enumName = "Color"; + Map constants = new HashMap<>(); + constants.put("RED", "Red Color"); + constants.put("GREEN", "Green Color"); + constants.put("BLUE", "Blue Color"); + + // 閰嶇疆FreeMarker + Configuration cfg = new Configuration(Configuration.VERSION_2_3_31); + try { + cfg.setDirectoryForTemplateLoading(new File("./src/templates")); + cfg.setDefaultEncoding("UTF-8"); + + // 鍔犺浇妯℃澘 + Template template = cfg.getTemplate("enumTemplate.ftl"); + + // 鍒涘缓鏁版嵁妯″瀷 + Map dataModel = new HashMap<>(); + dataModel.put("enumName", enumName); + dataModel.put("constants", constants.entrySet()); + + // 鎸囧畾杈撳嚭鏂囦欢璺緞 + String outputPath = "./src/output/" + enumName + "Enum.java"; + + // 鐢熸垚鏂囦欢 + try (Writer fileWriter = new FileWriter(outputPath)) { + template.process(dataModel, fileWriter); + System.out.println("Enum file generated at: " + outputPath); + } + + } catch (IOException | TemplateException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/psbc/Main.java b/src/main/java/com/psbc/Main.java new file mode 100644 index 0000000..8adb13e --- /dev/null +++ b/src/main/java/com/psbc/Main.java @@ -0,0 +1,99 @@ +package com.psbc; + +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.read.listener.PageReadListener; +import com.alibaba.excel.util.StringUtils; +import com.psbc.entity.DataOption; +import com.psbc.entity.FieldValue; +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateException; +import org.apache.poi.util.StringUtil; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.util.*; +import java.util.stream.Collectors; + +//TIP To Run code, press or +// click the icon in the gutter. +public class Main { + + public static String outputPath = ""; + public static void main(String[] args) throws TemplateException, IOException { + + String jarPathgetProtectionDomain = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + if(StringUtils.isEmpty(args[0])){ + System.out.println("璇疯緭鍏xcel璺緞"); + } + if(StringUtils.isEmpty(args[1])){ + System.out.println("璇疯緭鍏ユ灇涓剧被杈撳嚭璺緞"); + } + String excelPath = args[0]; + outputPath = args[1]; + getAllExcelData(excelPath); + } + + public static void getAllExcelData(String excelPath) throws TemplateException, IOException { + String[] inputDescs = {"姘戞棌浠g爜", "鍥藉鍦板尯浠g爜"}; + // 鐢ㄤ簬瀛樺偍鎵鏈塖heet椤电殑鏁版嵁 + List allDataOptions = new ArrayList<>(); + List fieldValues = new ArrayList<>(); + // 璇诲彇Excel鏂囦欢涓殑姣忎釜Sheet椤 + EasyExcel.read(excelPath, DataOption.class, new PageReadListener(allDataOptions::addAll)).sheet(0).doRead(); + EasyExcel.read(excelPath, FieldValue.class, new PageReadListener(fieldValues::addAll)).sheet(1).doRead(); + + +// // 杈撳嚭璇诲彇鍒扮殑鏁版嵁 +// for (DataOption data : allDataOptions) { +// System.out.println("Desc: " + data.getDesc() + ", enumName: " + data.getEnumName() + ", Filed: " + data.getField()); +// } +// for (FieldValue data : fieldValues) { +// System.out.println("Field: " + data.getField() + ", Code: " + data.getCode() + ", Value: " + data.getValue()); +// } + //灏嗗瓧绗︿覆杩涜鍖归厤 + for (String inputDesc : inputDescs) { + dataMatcher(allDataOptions, fieldValues, inputDesc); + } + + } + + private static void dataMatcher(List dataOptions, List fieldValues, String inputDesc) throws TemplateException, IOException { + Optional matchedData = dataOptions.stream() + .filter(data -> inputDesc.equals(data.getDesc())) + .findFirst(); + if (matchedData.isPresent()) { + Map constantsMap = fieldValues.stream().filter(item -> + item.getField().equals(matchedData.get().getField())).collect(Collectors.toMap(FieldValue::getCode, FieldValue::getValue)); + //鏈夊尮閰嶉」鍒欑敓鎴恊numClass + enumGenerator(matchedData.get().getEnumName(), constantsMap); + } else { + System.out.println("No matching data found for desc: " + inputDesc); + } + } + + private static void enumGenerator(String enumName, Map constants) throws IOException, TemplateException { + Configuration cfg = new Configuration(Configuration.VERSION_2_3_31); + cfg.setDirectoryForTemplateLoading(new File("./src/templates")); + cfg.setDefaultEncoding("UTF-8"); + + // 鍔犺浇妯℃澘 + Template template = cfg.getTemplate("enumTemplate.ftl"); + + // 鍒涘缓鏁版嵁妯″瀷 + Map dataModel = new HashMap<>(); + dataModel.put("enumName", enumName); + dataModel.put("constants", constants.entrySet()); + + // 鎸囧畾杈撳嚭鏂囦欢璺緞 + String output= outputPath + enumName + "Enum.java"; + + // 鐢熸垚鏂囦欢 + try (Writer fileWriter = new FileWriter(output)) { + template.process(dataModel, fileWriter); + System.out.println("Enum file generated at: " + output); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/psbc/entity/DataOption.java b/src/main/java/com/psbc/entity/DataOption.java new file mode 100644 index 0000000..f28dad3 --- /dev/null +++ b/src/main/java/com/psbc/entity/DataOption.java @@ -0,0 +1,37 @@ +package com.psbc.entity; + +import com.alibaba.excel.annotation.ExcelProperty; + +public class DataOption { + @ExcelProperty(index = 0) + private String desc; + @ExcelProperty(index = 1) + private String enumName; + @ExcelProperty(index = 2) + private String field; + + // Getters and Setters + public String getDesc() { + return desc; + } + + public String getEnumName() { + return enumName; + } + + public void setEnumName(String enumName) { + this.enumName = enumName; + } + + public void setDesc(String desc) { + this.desc = desc; + } + + public String getField() { + return field; + } + + public void setField(String value) { + this.field = value; + } +} diff --git a/src/main/java/com/psbc/entity/FieldValue.java b/src/main/java/com/psbc/entity/FieldValue.java new file mode 100644 index 0000000..7990546 --- /dev/null +++ b/src/main/java/com/psbc/entity/FieldValue.java @@ -0,0 +1,32 @@ +package com.psbc.entity; + +public class FieldValue { + private String field; + private String code; + private String value; + + public String getField() { + return field; + } + + public void setField(String field) { + this.field = field; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + +} diff --git a/src/templates/enumTemplate.ftl b/src/templates/enumTemplate.ftl new file mode 100644 index 0000000..93aa758 --- /dev/null +++ b/src/templates/enumTemplate.ftl @@ -0,0 +1,39 @@ +public enum ${enumName} { +<#list constants as constant> +${constant.key}("${constant.key}", "${constant.value}")<#if constant_has_next>, +; + +private final String code; +private final String value; + +${enumName}(String code, String value) { +this.code = code; +this.value = value; +} + +public String getCode() { +return code; +} + +public String getValue() { +return value; +} + +public static ${enumName} findByCode(String code) { +for (${enumName} constant : values()) { +if (constant.key.equals(code)) { +return constant; +} +} +return null; // 鎴栬呮姏鍑 IllegalArgumentException +} + +public static ${enumName} findByValue(String value) { +for (${enumName} constant : values()) { +if (constant.value.equals(value)) { +return constant; +} +} +return null; // 鎴栬呮姏鍑 IllegalArgumentException +} +} diff --git a/test.xlsx b/test.xlsx new file mode 100644 index 0000000..282bbbe Binary files /dev/null and b/test.xlsx differ