You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/09/25 06:34:30 UTC

[shardingsphere] branch master updated: Add freemarker example engine. (#12595)

This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 c257936  Add freemarker example engine. (#12595)
c257936 is described below

commit c257936e0b0e9be443c4fb02a056e1b062dabf1f
Author: Guocheng Tang <to...@qq.com>
AuthorDate: Sat Sep 25 14:33:48 2021 +0800

    Add freemarker example engine. (#12595)
    
    * Add freemarker example engine.
    
    * Add freemarker example engine.
    
    * Remove useless configuration.
    
    * Adjustment method definition.
---
 examples/pom.xml                                   |  8 +++-
 examples/shardingsphere-sample/pom.xml             |  1 +
 .../{ => shardingsphere-example-engine}/pom.xml    | 16 +++----
 .../example/engine/ExampleGenerateEngine.java      | 55 ++++++++++++++++++++++
 4 files changed, 71 insertions(+), 9 deletions(-)

diff --git a/examples/pom.xml b/examples/pom.xml
index d9854c4..9e51956 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -67,12 +67,13 @@
         <junit.version>4.12</junit.version>
         <hamcrest.version>1.3</hamcrest.version>
         <mockito.version>2.7.21</mockito.version>
-
+        
         <apache-rat-plugin.version>0.12</apache-rat-plugin.version>
         <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
         <maven-resources-plugin.version>2.7</maven-resources-plugin.version>
         <maven-jar-plugin.version>2.6</maven-jar-plugin.version>
         <maven-source-plugin.version>2.4</maven-source-plugin.version>
+        <freemarker.version>2.3.31</freemarker.version>
         
         <javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable>
     </properties>
@@ -267,6 +268,11 @@
                 <version>${spring-framework.version}</version>
                 <scope>test</scope>
             </dependency>
+            <dependency>
+                <groupId>org.freemarker</groupId>
+                <artifactId>freemarker</artifactId>
+                <version>${freemarker.version}</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>
     
diff --git a/examples/shardingsphere-sample/pom.xml b/examples/shardingsphere-sample/pom.xml
index 782518d..41c98c8 100644
--- a/examples/shardingsphere-sample/pom.xml
+++ b/examples/shardingsphere-sample/pom.xml
@@ -31,6 +31,7 @@
     
     <modules>
         <module>shardingshpere-proxy-example</module>
+        <module>shardingsphere-example-engine</module>
     </modules>
 </project>
 
diff --git a/examples/shardingsphere-sample/pom.xml b/examples/shardingsphere-sample/shardingsphere-example-engine/pom.xml
similarity index 81%
copy from examples/shardingsphere-sample/pom.xml
copy to examples/shardingsphere-sample/shardingsphere-example-engine/pom.xml
index 782518d..513995c 100644
--- a/examples/shardingsphere-sample/pom.xml
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/pom.xml
@@ -21,16 +21,16 @@
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <parent>
         <groupId>org.apache.shardingsphere.example</groupId>
-        <artifactId>shardingsphere-example</artifactId>
+        <artifactId>shardingsphere-sample</artifactId>
         <version>5.0.0-RC1-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
-    <artifactId>shardingsphere-sample</artifactId>
-    <packaging>pom</packaging>
-    <name>${project.artifactId}</name>
+    <artifactId>shardingsphere-example-engine</artifactId>
     
-    <modules>
-        <module>shardingshpere-proxy-example</module>
-    </modules>
+    <dependencies>
+        <dependency>
+            <groupId>org.freemarker</groupId>
+            <artifactId>freemarker</artifactId>
+        </dependency>
+    </dependencies>
 </project>
-
diff --git a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/ExampleGenerateEngine.java b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/ExampleGenerateEngine.java
new file mode 100644
index 0000000..7905a19
--- /dev/null
+++ b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/java/org/apache/sharding/example/engine/ExampleGenerateEngine.java
@@ -0,0 +1,55 @@
+/*
+ * 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.sharding.example.engine;
+
+import freemarker.template.Configuration;
+import freemarker.template.Template;
+import freemarker.template.TemplateException;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+/**
+ * Example generate engine.
+ */
+public final class ExampleGenerateEngine {
+    
+    private static final Configuration CONFIGURATION = new Configuration(Configuration.VERSION_2_3_31);
+    
+    public ExampleGenerateEngine(final String path) throws IOException {
+        CONFIGURATION.setDirectoryForTemplateLoading(new File(path));
+        CONFIGURATION.setDefaultEncoding("UTF-8");
+    }
+    
+    /**
+     * Generate files based on data model.
+     * @param obj data model
+     * @param templateFile Equivalent to the template name of the template base directory.
+     * @param outputFile Output directory and file name
+     */
+    public void process(final Object obj, final String templateFile, final String outputFile) {
+        try {
+            Template template = CONFIGURATION.getTemplate(templateFile);
+            template.process(obj, new FileWriter(outputFile));
+        } catch (IOException | TemplateException e) {
+            e.printStackTrace();
+        }
+    }
+}
+