You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by sg...@apache.org on 2020/04/05 22:03:50 UTC
[freemarker-generator] 01/11: FREEMARKER-140 freemarker-cli: Expose
DataSources directly in the data model
This is an automated email from the ASF dual-hosted git repository.
sgoeschl pushed a commit to branch feature/FREEMARKER-140
in repository https://gitbox.apache.org/repos/asf/freemarker-generator.git
commit 64cd0929f7a217d4043e508300cde7a69ce517d0
Author: Siegfried Goeschl <si...@gmail.com>
AuthorDate: Sun Apr 5 17:20:27 2020 +0200
FREEMARKER-140 freemarker-cli: Expose DataSources directly in the data model
---
.../base/datamodel/DataModelsSupplier.java | 64 ++++++++++++++++++++++
.../org/apache/freemarker/generator/cli/Main.java | 6 +-
.../freemarker/generator/cli/config/Settings.java | 25 ++++++++-
.../freemarker/generator/cli/config/Suppliers.java | 5 ++
.../generator/cli/task/FreeMarkerTask.java | 28 +++++++---
.../freemarker/generator/cli/ManualTest.java | 4 +-
6 files changed, 118 insertions(+), 14 deletions(-)
diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/datamodel/DataModelsSupplier.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/datamodel/DataModelsSupplier.java
new file mode 100644
index 0000000..8f9f268
--- /dev/null
+++ b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/datamodel/DataModelsSupplier.java
@@ -0,0 +1,64 @@
+/*
+ * 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.freemarker.generator.base.datamodel;
+
+import org.apache.freemarker.generator.base.datasource.DataSource;
+import org.apache.freemarker.generator.base.datasource.DataSourceFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Supplier;
+
+import static java.util.stream.Collectors.toList;
+
+/**
+ * Create a list of <code>DataSource</code> based on a list of sources consisting of
+ * URIs, named URIs or files.
+ */
+public class DataModelsSupplier implements Supplier<Map<String, Object>> {
+
+ /** List of source files, named URIs and URIs */
+ private final Collection<String> sources;
+
+ /**
+ * Constructor.
+ *
+ * @param sources List of sources
+ */
+ public DataModelsSupplier(Collection<String> sources) {
+ this.sources = new ArrayList<>(sources);
+ }
+
+ @Override
+ public Map<String, Object> get() {
+ final List<DataSource> dataModels = sources.stream().map(this::resolve).collect(toList());
+ return Collections.emptyMap();
+ }
+
+ /**
+ * Resolve a <code>source</code> to a <code>DataSource</code>.
+ *
+ * @param source the source being a file name, an <code>URI</code> or <code>NamedUri</code>
+ * @return list of <code>DataSource</code>
+ */
+ protected DataSource resolve(String source) {
+ return DataSourceFactory.create(source);
+ }
+}
diff --git a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/Main.java b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/Main.java
index e890120..6522c85 100644
--- a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/Main.java
+++ b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/Main.java
@@ -86,7 +86,7 @@ public class Main implements Callable<Integer> {
@Option(names = { "-l", "--locale" }, description = "Locale being used for the output, e.g. 'en_US'")
String locale;
- @Option(names = { "-m", "--mode" }, description = "[template|datasource]", defaultValue = "TEMPLATE")
+ @Option(names = { "--mode" }, description = "[template|datasource]", defaultValue = "TEMPLATE")
GeneratorMode mode;
@Option(names = { "-o", "--output" }, description = "Output file")
@@ -95,6 +95,9 @@ public class Main implements Callable<Integer> {
@Option(names = { "-P", "--param" }, description = "Set parameter")
Map<String, String> parameters;
+ @Option(names = { "-m", "--data-model" }, description = "Data model used for rendering")
+ List<String> dataModels;
+
@Option(names = { "--config" }, defaultValue = FREEMARKER_CLI_PROPERTY_FILE, description = "FreeMarker CLI configuration file")
String configFile;
@@ -219,6 +222,7 @@ public class Main implements Callable<Integer> {
.setOutputFile(outputFile)
.setParameters(parameters != null ? parameters : new HashMap<>())
.setDataSources(getCombindedDataSources())
+ .setDataModels(dataModels)
.setSystemProperties(systemProperties != null ? systemProperties : new Properties())
.setTemplateDirectories(templateDirectories)
.setTemplateName(templateSourceOptions.template)
diff --git a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/Settings.java b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/Settings.java
index 30f6d9c..51bbae8 100644
--- a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/Settings.java
+++ b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/Settings.java
@@ -85,9 +85,12 @@ public class Settings {
/** Expose environment variables globally in the data model? */
private final boolean isEnvironmentExposed;
- /** User-supplied list of source files or directories */
+ /** User-supplied list of data sources or directories */
private final List<String> dataSources;
+ /** User-supplied list of data sources directly exposed in the data model */
+ private List<String> dataModels;
+
/** User-supplied parameters */
private final Map<String, String> parameters;
@@ -113,6 +116,7 @@ public class Settings {
boolean isReadFromStdin,
boolean isEnvironmentExposed,
List<String> dataSources,
+ List<String> dataModels,
Map<String, String> parameters,
Properties sytemProperties,
Writer writer) {
@@ -134,6 +138,7 @@ public class Settings {
this.isReadFromStdin = isReadFromStdin;
this.isEnvironmentExposed = isEnvironmentExposed;
this.dataSources = requireNonNull(dataSources);
+ this.dataModels = requireNonNull(dataModels);
this.parameters = requireNonNull(parameters);
this.sytemProperties = requireNonNull(sytemProperties);
this.configuration = requireNonNull(configuration);
@@ -208,6 +213,10 @@ public class Settings {
return dataSources;
}
+ public List<String> getDataModels() {
+ return dataModels;
+ }
+
public Map<String, String> getParameters() {
return parameters;
}
@@ -289,6 +298,7 @@ public class Settings {
private boolean isReadFromStdin;
private boolean isEnvironmentExposed;
private List<String> dataSources;
+ private List<String> dataModels;
private Map<String, String> parameters;
private Properties systemProperties;
private Properties configuration;
@@ -303,6 +313,7 @@ public class Settings {
this.setInputEncoding(DEFAULT_CHARSET.name());
this.setOutputEncoding(DEFAULT_CHARSET.name());
this.dataSources = emptyList();
+ this.dataModels = emptyList();
this.templateDirectories = emptyList();
}
@@ -381,7 +392,16 @@ public class Settings {
}
public SettingsBuilder setDataSources(List<String> dataSources) {
- this.dataSources = dataSources;
+ if (dataSources != null) {
+ this.dataSources = dataSources;
+ }
+ return this;
+ }
+
+ public SettingsBuilder setDataModels(List<String> dataModels) {
+ if (dataModels != null) {
+ this.dataModels = dataModels;
+ }
return this;
}
@@ -433,6 +453,7 @@ public class Settings {
isReadFromStdin,
isEnvironmentExposed,
dataSources,
+ dataModels,
parameters,
systemProperties,
writer
diff --git a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/Suppliers.java b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/Suppliers.java
index 0766ad3..41cd94a 100644
--- a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/Suppliers.java
+++ b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/Suppliers.java
@@ -17,6 +17,7 @@
package org.apache.freemarker.generator.cli.config;
import freemarker.cache.TemplateLoader;
+import org.apache.freemarker.generator.base.datamodel.DataModelsSupplier;
import org.apache.freemarker.generator.base.datasource.DataSourcesSupplier;
import org.apache.freemarker.generator.base.file.PropertiesClassPathSupplier;
import org.apache.freemarker.generator.base.file.PropertiesFileSystemSupplier;
@@ -56,6 +57,10 @@ public class Suppliers {
settings.getInputEncoding());
}
+ public static DataModelsSupplier dataModelsSupplier(Settings settings) {
+ return new DataModelsSupplier(settings.getDataModels());
+ }
+
public static PropertiesSupplier propertiesSupplier(String fileName) {
return new PropertiesSupplier(
new PropertiesFileSystemSupplier(fileName),
diff --git a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/task/FreeMarkerTask.java b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/task/FreeMarkerTask.java
index 0fa4482..d9fe61b 100644
--- a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/task/FreeMarkerTask.java
+++ b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/task/FreeMarkerTask.java
@@ -43,6 +43,7 @@ import static org.apache.freemarker.generator.base.FreeMarkerConstants.DEFAULT_G
import static org.apache.freemarker.generator.base.FreeMarkerConstants.Location.STDIN;
import static org.apache.freemarker.generator.base.FreeMarkerConstants.Model.DATASOURCES;
import static org.apache.freemarker.generator.cli.config.Suppliers.configurationSupplier;
+import static org.apache.freemarker.generator.cli.config.Suppliers.dataModelsSupplier;
import static org.apache.freemarker.generator.cli.config.Suppliers.dataSourcesSupplier;
import static org.apache.freemarker.generator.cli.config.Suppliers.toolsSupplier;
@@ -56,19 +57,22 @@ public class FreeMarkerTask implements Callable<Integer> {
private final Settings settings;
private final Supplier<Map<String, Object>> toolsSupplier;
private final Supplier<List<DataSource>> dataSourcesSupplier;
+ private final Supplier<Map<String, Object>> dataModelsSupplier;
private final Supplier<Configuration> configurationSupplier;
public FreeMarkerTask(Settings settings) {
- this(settings, toolsSupplier(settings), dataSourcesSupplier(settings), configurationSupplier(settings));
+ this(settings, toolsSupplier(settings), dataSourcesSupplier(settings), dataModelsSupplier(settings), configurationSupplier(settings));
}
public FreeMarkerTask(Settings settings,
Supplier<Map<String, Object>> toolsSupplier,
Supplier<List<DataSource>> dataSourcesSupplier,
+ Supplier<Map<String, Object>> dataModelsSupplier,
Supplier<Configuration> configurationSupplier) {
this.settings = requireNonNull(settings);
this.toolsSupplier = requireNonNull(toolsSupplier);
this.dataSourcesSupplier = requireNonNull(dataSourcesSupplier);
+ this.dataModelsSupplier = requireNonNull(dataModelsSupplier);
this.configurationSupplier = requireNonNull(configurationSupplier);
}
@@ -76,7 +80,7 @@ public class FreeMarkerTask implements Callable<Integer> {
public Integer call() {
final Template template = template(settings, configurationSupplier);
try (Writer writer = settings.getWriter(); DataSources dataSources = dataSources(settings, dataSourcesSupplier)) {
- final Map<String, Object> dataModel = dataModel(settings, dataSources, toolsSupplier);
+ final Map<String, Object> dataModel = dataModel(settings, dataSources, dataModelsSupplier, toolsSupplier);
template.process(dataModel, writer);
return SUCCESS;
} catch (RuntimeException e) {
@@ -126,20 +130,26 @@ public class FreeMarkerTask implements Callable<Integer> {
}
}
- private static Map<String, Object> dataModel(Settings settings, DataSources dataSources, Supplier<Map<String, Object>> tools) {
- final Map<String, Object> dataModel = new HashMap<>();
+ private static Map<String, Object> dataModel(
+ Settings settings,
+ DataSources dataSources,
+ Supplier<Map<String, Object>> dataModelsSupplier,
+ Supplier<Map<String, Object>> tools) {
+ final Map<String, Object> result = new HashMap<>();
- dataModel.put(DATASOURCES, dataSources);
+ result.putAll(dataModelsSupplier.get());
+ result.put(DATASOURCES, dataSources);
+ // TODO rework based on FREEMARKER-140
if (settings.isEnvironmentExposed()) {
// add all system & user-supplied properties as top-level entries
- dataModel.putAll(System.getenv());
- dataModel.putAll(settings.getParameters());
+ result.putAll(System.getenv());
+ result.putAll(settings.getParameters());
}
- dataModel.putAll(tools.get());
+ result.putAll(tools.get());
- return dataModel;
+ return result;
}
private static boolean isAbsoluteTemplateFile(Settings settings) {
diff --git a/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ManualTest.java b/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ManualTest.java
index 0d8ca31..67e8e39 100644
--- a/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ManualTest.java
+++ b/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ManualTest.java
@@ -35,12 +35,12 @@ public class ManualTest {
// private static final String CMD = "-i ${JsoupTool.parse(DataSources.first).select('a')[0]} site/sample/html/dependencies.html";
// private static final String CMD = "-b ./src/test -t templates/properties/csv/locker-test-users.ftl site/sample/properties";
// private static final String CMD = "-b ./src/test -e UTF-8 -l de_AT -Dcolumn=Order%20ID -Dvalues=226939189,957081544 -Dformat=DEFAULT -Ddelimiter=COMMA -t templates/csv/md/filter.ftl site/sample/csv/sales-records.csv";
- // private static final String CMD = "-E -b ./src/test -t templates/environment.ftl";
+ private static final String CMD = "-E -b ./src/test -t templates/environment.ftl";
// private static final String CMD = "-b ./src/test -l de_AT -DFOO=foo -DBAR=bar -t templates/info.ftl -d user:admin=site/sample/csv/contract.csv#charset=UTF-16 google:www=https://www.google.com?foo=bar#contenttype=application/json";
// private static final String CMD = "-b ./src/test -t templates/info.ftl -d :user=site/sample/properties -d contract=site/sample/csv/contract.csv";
// private static final String CMD = "-b ./src/test -t site/sample/ftl/nginx/nginx.conf.ftl -d env=site/sample/ftl/nginx/nginx.env";
// private static final String CMD = "-b ./src/test -t templates/info.ftl -d env=site/sample/ftl/nginx/nginx.env";
- private static final String CMD = "-b ./src/test -t templates/json/yaml/transform.ftl site/sample/json/swagger-spec.json";
+ // private static final String CMD = "-b ./src/test -t templates/json/yaml/transform.ftl site/sample/json/swagger-spec.json";
// private static final String CMD = "-b ./src/test -t templates/yaml/json/transform.ftl site/sample/yaml/swagger-spec.yaml";
public static void main(String[] args) {