This is an automated email from the ASF dual-hosted git repository. sgoeschl pushed a commit to branch FREEMARKER-129 in repository https://gitbox.apache.org/repos/asf/freemarker-generator.git
commit 846145f47bc85ee5ba99b27330afedd4f232185b Author: Siegfried Goeschl <[email protected]> AuthorDate: Thu Feb 6 21:10:58 2020 +0100 FREEMARKER-129 Cleanup source code --- freemarker-generator-base/pom.xml | 4 +- .../generator/base/util/PropertiesTransformer.java | 41 +++++++++++++++ .../generator/util/PropertiesTransformerTest.java | 48 ++++++++++++++++++ freemarker-generator-cli/pom.xml | 23 +-------- .../src/main/assembly/small.xml | 59 ---------------------- .../org/apache/freemarker/generator/cli/Main.java | 8 +-- .../generator/cli/impl/ToolsSupplier.java | 17 ++++--- freemarker-generator-maven-plugin/pom.xml | 14 +---- freemarker-generator-tools/pom.xml | 4 +- 9 files changed, 106 insertions(+), 112 deletions(-) diff --git a/freemarker-generator-base/pom.xml b/freemarker-generator-base/pom.xml index d86e3d3..e67c1a3 100644 --- a/freemarker-generator-base/pom.xml +++ b/freemarker-generator-base/pom.xml @@ -26,10 +26,8 @@ </parent> <artifactId>freemarker-generator-base</artifactId> - <packaging>jar</packaging> <name>Apache FreeMarker Generator: Base</name> - <description>Command-line client for Apache FreeMarker</description> - <inceptionYear>2020</inceptionYear> + <description>Common functionality</description> <dependencies> <dependency> diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/util/PropertiesTransformer.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/util/PropertiesTransformer.java new file mode 100644 index 0000000..6da5ecf --- /dev/null +++ b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/util/PropertiesTransformer.java @@ -0,0 +1,41 @@ +/* + * 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.util; + +import java.util.Properties; + +/** + * Helper class to transform <code>java.util.Properties</code> + */ +public class PropertiesTransformer { + + /** + * Create a new <code>java.util.Properties</code> instance having the matching key prefix removed + * + * @param properties the properties + * @param prefix prefix to be removed from the matching keys + * @return Properties + */ + public static Properties removeKeyPrefix(Properties properties, String prefix) { + final Properties result = new Properties(); + properties.entrySet() + .stream() + .filter(entry -> entry.getKey().toString().startsWith(prefix)) + .forEach(entry -> result.put(entry.getKey().toString().substring(prefix.length()), entry.getValue())); + return result; + } +} diff --git a/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/util/PropertiesTransformerTest.java b/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/util/PropertiesTransformerTest.java new file mode 100644 index 0000000..710307a --- /dev/null +++ b/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/util/PropertiesTransformerTest.java @@ -0,0 +1,48 @@ +/* + * 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.util; + +import org.apache.freemarker.generator.base.util.PropertiesTransformer; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import static org.junit.Assert.assertEquals; + +public class PropertiesTransformerTest { + + private final Map<String, Object> settings = new HashMap<>(); + + @Test + public void shouldCreateToolWithDefaultConstructor() { + final Properties properties = PropertiesTransformer.removeKeyPrefix(properties(), "freemarker.tools."); + + assertEquals(2, properties.size()); + assertEquals("o.a.f.g.t.commonscsv.CommonsCSVTool", properties.getProperty("CSVTool")); + assertEquals("o.a.f.g.t.commonscsv.ExecTool", properties.getProperty("ExecTool")); + } + + private Properties properties() { + final Properties properties = new Properties(); + properties.put("foo", "bar"); + properties.put("freemarker.tools.CSVTool", "o.a.f.g.t.commonscsv.CommonsCSVTool"); + properties.put("freemarker.tools.ExecTool", "o.a.f.g.t.commonscsv.ExecTool"); + return properties; + } +} diff --git a/freemarker-generator-cli/pom.xml b/freemarker-generator-cli/pom.xml index 68b27d1..520a00b 100644 --- a/freemarker-generator-cli/pom.xml +++ b/freemarker-generator-cli/pom.xml @@ -26,13 +26,11 @@ </parent> <artifactId>freemarker-generator-cli</artifactId> - <packaging>jar</packaging> <name>Apache FreeMarker Generator: CLI</name> <description>Command-line client for Apache FreeMarker</description> - <inceptionYear>2016</inceptionYear> <build> - <defaultGoal>clean package</defaultGoal> + <defaultGoal>package</defaultGoal> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> @@ -93,25 +91,6 @@ <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> - <phase>package</phase> - <goals> - <goal>single</goal> - </goals> - <configuration> - <archive> - <manifest> - <mainClass>org.apache.freemarker.generator.cli.Main</mainClass> - </manifest> - </archive> - <descriptorRefs> - <descriptorRef>jar-with-dependencies</descriptorRef> - </descriptorRefs> - <descriptors> - <descriptor>src/main/assembly/small.xml</descriptor> - </descriptors> - </configuration> - </execution> - <execution> <id>make-assembly</id> <phase>package</phase> <configuration> diff --git a/freemarker-generator-cli/src/main/assembly/small.xml b/freemarker-generator-cli/src/main/assembly/small.xml deleted file mode 100644 index 3fa92e6..0000000 --- a/freemarker-generator-cli/src/main/assembly/small.xml +++ /dev/null @@ -1,59 +0,0 @@ -<!-- - 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. ---> -<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"> - <id>small</id> - <formats> - <format>jar</format> - </formats> - <includeBaseDirectory>false</includeBaseDirectory> - <dependencySets> - <dependencySet> - <outputDirectory>${file.separator}</outputDirectory> - <useProjectArtifact>true</useProjectArtifact> - <unpack>true</unpack> - <scope>runtime</scope> - <excludes> - <!-- Exclude Apache POI --> - <exclude>com.github.virtuald:curvesapi</exclude> - <exclude>commons-codec:commons-codec</exclude> - <exclude>org.apache.commons:commons-collections4</exclude> - <exclude>org.apache.commons:commons-compress</exclude> - <exclude>org.apache.commons:commons-lang3</exclude> - <exclude>org.apache.commons:commons-math3</exclude> - <exclude>org.apache.xmlbeans:xmlbeans</exclude> - <exclude>org.apache.poi:poi</exclude> - <exclude>org.apache.poi:poi-ooxml</exclude> - <exclude>org.apache.poi:poi-ooxml-schemas</exclude> - <exclude>org.apache.poi:poi-ooxml-schemas</exclude> - <!-- Exclude Grok --> - <exclude>io.krakens:java-grok</exclude> - <!-- Exclude JSoup --> - <exclude>org.jsoup:jsoup</exclude> - </excludes> - </dependencySet> - </dependencySets> - <fileSets> - <fileSet> - <outputDirectory>${file.separator}</outputDirectory> - <directory>${project.build.outputDirectory}</directory> - </fileSet> - </fileSets> -</assembly> \ No newline at end of file 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 46299b1..b5277a4 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 @@ -16,9 +16,9 @@ */ package org.apache.freemarker.generator.cli; +import org.apache.freemarker.generator.base.file.PropertiesFileSupplier; import org.apache.freemarker.generator.base.util.ClosableUtils; import org.apache.freemarker.generator.base.util.StringUtils; -import org.apache.freemarker.generator.base.file.PropertiesFileSupplier; import org.apache.freemarker.generator.cli.impl.TemplateDirectorySupplier; import org.apache.freemarker.generator.cli.model.Settings; import org.apache.freemarker.generator.cli.picocli.GitVersionProvider; @@ -49,7 +49,7 @@ public class Main implements Callable<Integer> { private static final String FREEMARKER_CLI_PROPERTY_FILE = "freemarker-cli.properties"; - @ArgGroup(exclusive = true, multiplicity = "1") + @ArgGroup(multiplicity = "1") private TemplateSourceOptions templateSourceOptions; private static final class TemplateSourceOptions { @@ -67,7 +67,7 @@ public class Main implements Callable<Integer> { private Map<String, String> properties; @Option(names = { "-e", "--input-encoding" }, description = "Encoding of input file", defaultValue = "UTF-8") - String inputEncoding; + private String inputEncoding; @Option(names = { "-E", "--expose-env" }, description = "Expose environment variables and user-supplied properties globally") private boolean isEnvironmentExposed; @@ -85,7 +85,7 @@ public class Main implements Callable<Integer> { private String include; @Option(names = { "--output-encoding" }, description = "Encoding of output file, e.g. UTF-8", defaultValue = "UTF-8") - String outputEncoding; + private String outputEncoding; @Option(names = { "--stdin" }, description = "Read input document from stdin") private boolean readFromStdin; diff --git a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/impl/ToolsSupplier.java b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/impl/ToolsSupplier.java index d76e5ef..977cd1c 100644 --- a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/impl/ToolsSupplier.java +++ b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/impl/ToolsSupplier.java @@ -24,6 +24,7 @@ import java.util.function.Supplier; import static java.util.Objects.requireNonNull; import static java.util.stream.Collectors.toMap; +import static org.apache.freemarker.generator.base.util.PropertiesTransformer.removeKeyPrefix; public class ToolsSupplier implements Supplier<Map<String, Object>> { @@ -45,17 +46,17 @@ public class ToolsSupplier implements Supplier<Map<String, Object>> { @Override public Map<String, Object> get() { - return configuration.stringPropertyNames().stream() - .filter(name -> name.startsWith(FREEMARKER_TOOLS_PREFIX)) - .filter(name -> toolClassCanBeLoaded(configuration.getProperty(name))) - .collect(toMap(ToolsSupplier::toolName, name -> ToolsFactory.create(configuration.getProperty(name), settings))); + final Properties properties = toolsProperties(); + return properties.stringPropertyNames().stream() + .filter(key -> toolExists(properties.getProperty(key))) + .collect(toMap(key -> key, key -> ToolsFactory.create(properties.getProperty(key), settings))); } - private boolean toolClassCanBeLoaded(String clazzName) { - return ToolsFactory.exists(clazzName); + private Properties toolsProperties() { + return removeKeyPrefix(this.configuration, FREEMARKER_TOOLS_PREFIX); } - private static String toolName(String propertyName) { - return propertyName.substring(FREEMARKER_TOOLS_PREFIX.length()); + private static boolean toolExists(String clazzName) { + return ToolsFactory.exists(clazzName); } } diff --git a/freemarker-generator-maven-plugin/pom.xml b/freemarker-generator-maven-plugin/pom.xml index ad4c32c..a17ddc9 100644 --- a/freemarker-generator-maven-plugin/pom.xml +++ b/freemarker-generator-maven-plugin/pom.xml @@ -12,7 +12,7 @@ <packaging>maven-plugin</packaging> <name>Apache FreeMarker Generator: Maven Plugin</name> - <url>http://freemarker.apache.org/</url> + <description>Maven plugin for Apache FreeMarker</description> <properties> <maven-core.version>3.5.2</maven-core.version> @@ -116,22 +116,10 @@ <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> - <configuration> - <source>1.8</source> - <target>1.8</target> - </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> - <executions> - <execution> - <id>attach-sources</id> - <goals> - <goal>jar</goal> - </goals> - </execution> - </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> diff --git a/freemarker-generator-tools/pom.xml b/freemarker-generator-tools/pom.xml index 7c4096f..4b67e50 100644 --- a/freemarker-generator-tools/pom.xml +++ b/freemarker-generator-tools/pom.xml @@ -26,10 +26,8 @@ </parent> <artifactId>freemarker-generator-tools</artifactId> - <packaging>jar</packaging> <name>Apache FreeMarker Generator: Tools</name> - <description>Tools for Apache FreeMarker</description> - <inceptionYear>2020</inceptionYear> + <description>Document processing tools for Apache FreeMarker Generator</description> <dependencies> <dependency>
