EnumGenerator

This commit is contained in:
hu645 2024-12-01 19:18:17 +08:00
commit 189246a1a3
20 changed files with 651 additions and 0 deletions

38
.gitignore vendored Normal file
View File

@ -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

8
.idea/.gitignore vendored Normal file
View File

@ -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

7
.idea/encodings.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

14
.idea/misc.xml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="corretto-11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

124
.idea/uiDesigner.xml Normal file
View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

Binary file not shown.

BIN
EnumGenerator.7z Normal file

Binary file not shown.

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.psbc</groupId>
<artifactId>EnumGenerator</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer>
<mainClass>com.psbc.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

42
output/EthicyEnum.java Normal file
View File

@ -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
}
}

41
output/NationEnum.java Normal file
View File

@ -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
}
}

54
pom.xml Normal file
View File

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.psbc</groupId>
<artifactId>EnumGenerator</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.psbc.Main</mainClass> <!-- 替换为你的主类 -->
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

12
run.bat Normal file
View File

@ -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

12
run1.bat Normal file
View File

@ -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

View File

@ -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<String, String> 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<String, Object> 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();
}
}
}

View File

@ -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 <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> 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("请输入excel路径");
}
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 = {"民族代码", "国家地区代码"};
// 用于存储所有Sheet页的数据
List<DataOption> allDataOptions = new ArrayList<>();
List<FieldValue> fieldValues = new ArrayList<>();
// 读取Excel文件中的每个Sheet页
EasyExcel.read(excelPath, DataOption.class, new PageReadListener<DataOption>(allDataOptions::addAll)).sheet(0).doRead();
EasyExcel.read(excelPath, FieldValue.class, new PageReadListener<FieldValue>(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<DataOption> dataOptions, List<FieldValue> fieldValues, String inputDesc) throws TemplateException, IOException {
Optional<DataOption> matchedData = dataOptions.stream()
.filter(data -> inputDesc.equals(data.getDesc()))
.findFirst();
if (matchedData.isPresent()) {
Map<String, String> constantsMap = fieldValues.stream().filter(item ->
item.getField().equals(matchedData.get().getField())).collect(Collectors.toMap(FieldValue::getCode, FieldValue::getValue));
//有匹配项则生成enumClass
enumGenerator(matchedData.get().getEnumName(), constantsMap);
} else {
System.out.println("No matching data found for desc: " + inputDesc);
}
}
private static void enumGenerator(String enumName, Map<String, String> 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<String, Object> 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);
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,39 @@
public enum ${enumName} {
<#list constants as constant>
${constant.key}("${constant.key}", "${constant.value}")<#if constant_has_next>,</#if>
</#list>;
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
}
}

BIN
test.xlsx Normal file

Binary file not shown.