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 65232bf Split ExampleGenerator to Main and generator (#14934)
65232bf is described below
commit 65232bfb5f57dd9644a81b1485214d2f16b32c72
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Jan 20 11:49:22 2022 +0800
Split ExampleGenerator to Main and generator (#14934)
---
...leGenerateEngine.java => ExampleGenerator.java} | 39 ++++++++++----------
.../example/generator/ExampleGeneratorMain.java | 42 ++++++++++++++++++++++
2 files changed, 62 insertions(+), 19 deletions(-)
diff --git
a/examples/shardingsphere-sample/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/ExampleGenerateEngine.java
b/examples/shardingsphere-sample/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/ExampleGenerator.java
similarity index 71%
rename from
examples/shardingsphere-sample/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/ExampleGenerateEngine.java
rename to
examples/shardingsphere-sample/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/ExampleGenerator.java
index 46b29cf..2a0502a 100644
---
a/examples/shardingsphere-sample/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/ExampleGenerateEngine.java
+++
b/examples/shardingsphere-sample/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/ExampleGenerator.java
@@ -33,11 +33,9 @@ import java.util.Map.Entry;
import java.util.Objects;
/**
- * Example generate engine.
+ * Example generator.
*/
-public final class ExampleGenerateEngine {
-
- private static final Configuration TEMPLATE_CONFIG = new
Configuration(Configuration.VERSION_2_3_31);
+public final class ExampleGenerator {
private static final String DATA_MODEL_PATH =
"/data-model/data-model.yaml";
@@ -62,48 +60,51 @@ public final class ExampleGenerateEngine {
private static final String RESOURCES_PATH = "resources";
- static {
- try {
- TEMPLATE_CONFIG.setDirectoryForTemplateLoading(new
File(Objects.requireNonNull(ExampleGenerateEngine.class.getClassLoader().getResource("template")).getFile()));
- TEMPLATE_CONFIG.setDefaultEncoding("UTF-8");
- } catch (final IOException ex) {
- ex.printStackTrace();
- }
+ private final Configuration templateConfig;
+
+ public ExampleGenerator() throws IOException {
+ templateConfig = createTemplateConfiguration();
+ }
+
+ private Configuration createTemplateConfiguration() throws IOException {
+ Configuration result = new Configuration(Configuration.VERSION_2_3_31);
+ result.setDirectoryForTemplateLoading(new
File(Objects.requireNonNull(ExampleGenerator.class.getClassLoader().getResource("template")).getFile()));
+ result.setDefaultEncoding("UTF-8");
+ return result;
}
/**
* Generate file.
*
- * @param args args
* @throws IOException IO exception
* @throws TemplateException template exception
*/
@SuppressWarnings("unchecked")
- public static void main(final String[] args) throws IOException,
TemplateException {
- try (InputStream input =
ExampleGenerateEngine.class.getResourceAsStream(DATA_MODEL_PATH)) {
+ public void generate() throws IOException, TemplateException {
+ try (InputStream input =
ExampleGenerator.class.getResourceAsStream(DATA_MODEL_PATH)) {
Map<String, String> dataModel = new Yaml().loadAs(input,
Map.class);
generateFile(dataModel,
ExampleTemplateFactory.getJavaClassTemplateMap(dataModel), JAVA_CLASS_PATH);
generateFile(dataModel,
ExampleTemplateFactory.getResourceTemplateMap(dataModel), RESOURCES_PATH);
}
}
- private static void generateFile(final Map<String, String> dataModel,
final Map<String, String> templateMap, final String outputRelativePath) throws
IOException, TemplateException {
+ private void generateFile(final Map<String, String> dataModel, final
Map<String, String> templateMap, final String outputRelativePath) throws
IOException, TemplateException {
String outputPath = generatePath(dataModel, OUTPUT_PATH +
outputRelativePath);
for (Entry<String, String> entry : templateMap.entrySet()) {
processFile(dataModel, entry.getKey(), outputPath + "/" +
entry.getValue());
}
}
- private static String generatePath(final Object model, final String
relativePath) throws IOException, TemplateException {
+ private String generatePath(final Object model, final String relativePath)
throws IOException, TemplateException {
try (StringWriter result = new StringWriter()) {
- new Template("path", relativePath, TEMPLATE_CONFIG).process(model,
result);
+ new Template("path", relativePath, templateConfig).process(model,
result);
return result.toString();
}
}
- private static void processFile(final Object model, final String
templateFile, final String outputFile) throws IOException, TemplateException {
+ private 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);
+ templateConfig.getTemplate(templateFile).process(model, writer);
}
}
}
diff --git
a/examples/shardingsphere-sample/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/ExampleGeneratorMain.java
b/examples/shardingsphere-sample/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/ExampleGeneratorMain.java
new file mode 100644
index 0000000..1c26b85
--- /dev/null
+++
b/examples/shardingsphere-sample/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/ExampleGeneratorMain.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.example.generator;
+
+import freemarker.template.TemplateException;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.io.IOException;
+
+/**
+ * Example generator entrance.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ExampleGeneratorMain {
+
+ /**
+ * Main entrance.
+ *
+ * @param args args
+ * @throws IOException IO exception
+ * @throws TemplateException template exception
+ */
+ public static void main(final String[] args) throws IOException,
TemplateException {
+ new ExampleGenerator().generate();
+ }
+}