You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/01/19 14:42:53 UTC
[shardingsphere] branch master updated: Refactor ExampleGenerateEngine (#14919)
This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new ee76192 Refactor ExampleGenerateEngine (#14919)
ee76192 is described below
commit ee7619246b6a6a15433d7cebce0e5f2ebcc5a920
Author: Liang Zhang <te...@163.com>
AuthorDate: Wed Jan 19 22:40:54 2022 +0800
Refactor ExampleGenerateEngine (#14919)
---
.../example/engine/ExampleGenerateEngine.java | 120 +++++++++------------
1 file changed, 51 insertions(+), 69 deletions(-)
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/shardingsphere/example/engine/ExampleGenerateEngine.java b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/shardingsphere/example/engine/ExampleGenerateEngine.java
index 195a4e0..fa19923 100644
--- a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/shardingsphere/example/engine/ExampleGenerateEngine.java
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/shardingsphere/example/engine/ExampleGenerateEngine.java
@@ -37,7 +37,22 @@ import java.util.Objects;
*/
public final class ExampleGenerateEngine {
- private static final Configuration CONFIGURATION = new Configuration(Configuration.VERSION_2_3_31);
+ private static final Configuration TEMPLATE_CONFIG = new Configuration(Configuration.VERSION_2_3_31);
+
+ private static final String DATA_MODEL_PATH = "/data-model/data-model.yaml";
+
+ private static final String FILE_NAME_PREFIX = "${mode?cap_first}${transaction?cap_first}"
+ + "<#assign featureName=\"\">"
+ + "<#if feature?split(\",\")?size gt 1>"
+ + "<#assign featureName=\"Mixed\">"
+ + "<#else>"
+ + "<#list feature?split(\"-\") as item>"
+ + "<#assign featureName=featureName + item?cap_first>"
+ + "</#list></#if>${featureName}"
+ + "<#assign frameworkName=\"\">"
+ + "<#list framework?split(\"-\") as item>"
+ + "<#assign frameworkName=frameworkName + item?cap_first>"
+ + "</#list>${frameworkName}";
private static final String OUTPUT_PATH = "./examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-${mode}-example"
+ "<#assign package=\"\">"
@@ -60,21 +75,6 @@ public final class ExampleGenerateEngine {
private static final String RESOURCES_PATH = "resources";
- private static final String FILE_NAME_PREFIX = "${mode?cap_first}${transaction?cap_first}"
- + "<#assign featureName=\"\">"
- + "<#if feature?split(\",\")?size gt 1>"
- + "<#assign featureName=\"Mixed\">"
- + "<#else>"
- + "<#list feature?split(\"-\") as item>"
- + "<#assign featureName=featureName + item?cap_first>"
- + "</#list></#if>${featureName}"
- + "<#assign frameworkName=\"\">"
- + "<#list framework?split(\"-\") as item>"
- + "<#assign frameworkName=frameworkName + item?cap_first>"
- + "</#list>${frameworkName}";
-
- private static final String DATA_MODEL_PATH = "/data-model/data-model.yaml";
-
private static Map<String, String> renameTemplateMap;
private static Map<String, String> unRenameTemplateMap;
@@ -83,10 +83,10 @@ public final class ExampleGenerateEngine {
static {
try {
- CONFIGURATION.setDirectoryForTemplateLoading(new File(Objects.requireNonNull(ExampleGenerateEngine.class.getClassLoader().getResource("")).getFile()));
- CONFIGURATION.setDefaultEncoding("UTF-8");
- } catch (IOException e) {
- e.printStackTrace();
+ TEMPLATE_CONFIG.setDirectoryForTemplateLoading(new File(Objects.requireNonNull(ExampleGenerateEngine.class.getClassLoader().getResource("")).getFile()));
+ TEMPLATE_CONFIG.setDefaultEncoding("UTF-8");
+ } catch (final IOException ex) {
+ ex.printStackTrace();
}
}
@@ -94,72 +94,54 @@ public final class ExampleGenerateEngine {
* Generate file.
*
* @param args args
+ * @throws IOException IO exception
+ * @throws TemplateException template exception
*/
- public static void main(final String[] args) {
- Yaml yaml = new Yaml();
- InputStream in = ExampleGenerateEngine.class.getResourceAsStream(DATA_MODEL_PATH);
- Map<String, String> dataModel = yaml.loadAs(in, Map.class);
- fillTemplateMap(dataModel);
- generateJavaCode(dataModel);
- generateResourcesFile(dataModel);
- }
-
- /**
- * Generate files based on data model.
- *
- * @param model data model
- * @param templateFile Equivalent to the template name of the template base directory.
- * @param outputFile Output directory and file name.
- */
- public static void processFile(final Object model, final String templateFile, final String outputFile) {
- try (Writer writer = new FileWriter(outputFile)) {
- Template template = CONFIGURATION.getTemplate(templateFile);
- template.process(model, writer);
- } catch (TemplateException | IOException e) {
- e.printStackTrace();
+ @SuppressWarnings("unchecked")
+ public static void main(final String[] args) throws IOException, TemplateException {
+ try (InputStream input = ExampleGenerateEngine.class.getResourceAsStream(DATA_MODEL_PATH)) {
+ Map<String, String> dataModel = new Yaml().loadAs(input, Map.class);
+ fillTemplateMap(dataModel);
+ generateJavaCodes(dataModel);
+ generateResourcesFile(dataModel);
}
}
- /**
- * Placeholder replacement.
- *
- * @param model data model
- * @param templateString String template
- * @return Replace the placeholder string
- */
- public static String processString(final Object model, final String templateString) {
- try (StringWriter result = new StringWriter();
- StringReader reader = new StringReader(templateString)) {
- Template t = new Template("string", reader, CONFIGURATION);
- t.process(model, result);
- return result.toString();
- } catch (IOException | TemplateException e) {
- e.printStackTrace();
- }
- return null;
- }
-
private static void fillTemplateMap(final Map<String, String> dataModel) {
renameTemplateMap = ExampleTemplateFactory.getRenameTemplate(dataModel);
unRenameTemplateMap = ExampleTemplateFactory.getUnReNameTemplate(dataModel);
resourceTemplateMap = ExampleTemplateFactory.getResourceTemplate(dataModel);
}
- private static void generateJavaCode(final Map<String, String> dataModel) {
+ private static void generateJavaCodes(final Map<String, String> dataModel) throws IOException, TemplateException {
String fileName = processString(dataModel, FILE_NAME_PREFIX);
String outputPath = processString(dataModel, OUTPUT_PATH + JAVA_CLASS_PATH);
- for (String key : renameTemplateMap.keySet()) {
- processFile(dataModel, "/template/" + renameTemplateMap.get(key), outputPath + "/" + fileName + key + ".java");
+ for (String each : renameTemplateMap.keySet()) {
+ processFile(dataModel, "/template/" + renameTemplateMap.get(each), outputPath + "/" + fileName + each + ".java");
}
- for (String key : unRenameTemplateMap.keySet()) {
- processFile(dataModel, "/template/" + key + ".ftl", outputPath + "/" + unRenameTemplateMap.get(key));
+ for (String each : unRenameTemplateMap.keySet()) {
+ processFile(dataModel, "/template/" + each + ".ftl", outputPath + "/" + unRenameTemplateMap.get(each));
}
}
- private static void generateResourcesFile(final Map<String, String> dataModel) {
+ private static void generateResourcesFile(final Map<String, String> dataModel) throws IOException, TemplateException {
String outputPath = processString(dataModel, OUTPUT_PATH + RESOURCES_PATH);
- for (String key : resourceTemplateMap.keySet()) {
- processFile(dataModel, "/template/" + key + ".ftl", outputPath + "/" + resourceTemplateMap.get(key));
+ for (String each : resourceTemplateMap.keySet()) {
+ processFile(dataModel, "/template/" + each + ".ftl", outputPath + "/" + resourceTemplateMap.get(each));
+ }
+ }
+
+ private static String processString(final Object model, final String templateString) throws IOException, TemplateException {
+ try (StringWriter result = new StringWriter();
+ StringReader reader = new StringReader(templateString)) {
+ new Template("string", reader, TEMPLATE_CONFIG).process(model, result);
+ return result.toString();
+ }
+ }
+
+ private static void processFile(final Object model, final String templateFile, final String outputFile) throws IOException, TemplateException {
+ try (Writer writer = new FileWriter(outputFile)) {
+ TEMPLATE_CONFIG.getTemplate(templateFile).process(model, writer);
}
}
}