Skip to content

Commit

Permalink
Keep license structure (#440)
Browse files Browse the repository at this point in the history
What happens is that license is handled as string, that later deep below (in XmlWriterUtil, from plexus-utils) is stripped by Line Separators (hence paragraph lines are gone) and spit into string.

Instead, make this a list of strings, and just write them out line by line, as XML comment structure allows it.

Also change config from Properties to Map<String, Object> and now it is possible to configure List<String> for license.
  • Loading branch information
cstamas committed May 2, 2024
1 parent c90d219 commit 18d2f87
Show file tree
Hide file tree
Showing 82 changed files with 322 additions and 300 deletions.
6 changes: 3 additions & 3 deletions modello-core/src/main/java/org/codehaus/modello/Modello.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import java.io.Reader;
import java.io.Writer;
import java.util.Properties;
import java.util.Map;

import org.codehaus.modello.core.ModelloCore;
import org.codehaus.modello.model.Model;
Expand All @@ -44,14 +44,14 @@ public Modello(ModelloCore core) {
this.core = core;
}

public void generate(Reader modelReader, String outputType, Properties parameters)
public void generate(Reader modelReader, String outputType, Map<String, Object> parameters)
throws ModelloException, ModelValidationException {
Model model = core.loadModel(modelReader);

core.generate(model, outputType, parameters);
}

public void translate(Reader reader, Writer writer, String outputType, Properties parameters)
public void translate(Reader reader, Writer writer, String outputType, Map<String, Object> parameters)
throws ModelloException, ModelValidationException {
Model model = core.translate(reader, outputType, parameters);

Expand Down
17 changes: 9 additions & 8 deletions modello-core/src/main/java/org/codehaus/modello/ModelloCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
*/

import java.io.File;
import java.util.Properties;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.util.StringUtils;
Expand All @@ -37,7 +38,7 @@ public class ModelloCli {

private static String outputType;

private static Properties parameters;
private static Map<String, Object> parameters;

public static void main(String[] args) throws Exception {
Modello modello = new DefaultPlexusContainer().lookup(Modello.class);
Expand All @@ -58,7 +59,7 @@ public static void parseArgumentsFromCommandLine(String[] args) throws Exception

outputType = args[1];

parameters = new Properties();
parameters = new HashMap<>();

String outputDirectory = args[2];

Expand All @@ -70,7 +71,7 @@ public static void parseArgumentsFromCommandLine(String[] args) throws Exception
System.exit(1);
}

parameters.setProperty(ModelloParameterConstants.OUTPUT_DIRECTORY, outputDirectory);
parameters.put(ModelloParameterConstants.OUTPUT_DIRECTORY, outputDirectory);

String modelVersion = args[3];

Expand All @@ -82,7 +83,7 @@ public static void parseArgumentsFromCommandLine(String[] args) throws Exception
System.exit(1);
}

parameters.setProperty(ModelloParameterConstants.VERSION, modelVersion);
parameters.put(ModelloParameterConstants.VERSION, modelVersion);

String packageWithVersion = args[4];

Expand All @@ -94,7 +95,7 @@ public static void parseArgumentsFromCommandLine(String[] args) throws Exception
System.exit(1);
}

parameters.setProperty(ModelloParameterConstants.PACKAGE_WITH_VERSION, packageWithVersion);
parameters.put(ModelloParameterConstants.PACKAGE_WITH_VERSION, packageWithVersion);

String javaSource = args[5];

Expand All @@ -106,10 +107,10 @@ public static void parseArgumentsFromCommandLine(String[] args) throws Exception
System.exit(1);
}

parameters.setProperty(ModelloParameterConstants.OUTPUT_JAVA_SOURCE, javaSource);
parameters.put(ModelloParameterConstants.OUTPUT_JAVA_SOURCE, javaSource);

if (args.length > 6) {
parameters.setProperty(ModelloParameterConstants.ENCODING, args[6]);
parameters.put(ModelloParameterConstants.ENCODING, args[6]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class ModelloParameterConstants {
public static final String XSD_ENFORCE_MANDATORY_ELEMENTS = "modello.xsd.enforce.mandatory.element";

/**
* The license text as string, to be added to generated files, if needed.
* The license text as list of strings, to be added to generated files, if needed.
*
* @since 2.3.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import java.io.Writer;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.codehaus.modello.ModelloException;
import org.codehaus.modello.ModelloRuntimeException;
Expand Down Expand Up @@ -251,11 +251,11 @@ public void saveModel(Model model, Writer writer) throws ModelloException {
throw new ModelloRuntimeException("Not implemented.");
}

public Model translate(Reader reader, String inputType, Properties parameters) throws ModelloException {
public Model translate(Reader reader, String inputType, Map<String, Object> parameters) throws ModelloException {
throw new ModelloRuntimeException("Not implemented.");
}

public void generate(Model model, String outputType, Properties parameters) throws ModelloException {
public void generate(Model model, String outputType, Map<String, Object> parameters) throws ModelloException {
if (model == null) {
throw new ModelloRuntimeException("Illegal argument: model == null.");
}
Expand All @@ -265,7 +265,7 @@ public void generate(Model model, String outputType, Properties parameters) thro
}

if (parameters == null) {
parameters = new Properties();
parameters = new HashMap<>();
}

ModelloGenerator generator = generatorPluginManager.getGeneratorPlugin(outputType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;
import java.util.Map;

import org.codehaus.modello.ModelloException;
import org.codehaus.modello.model.Model;
Expand All @@ -46,8 +46,8 @@ public interface ModelloCore {

void saveModel(Model model, Writer writer) throws ModelloException;

Model translate(Reader reader, String inputType, Properties parameters)
Model translate(Reader reader, String inputType, Map<String, Object> parameters)
throws ModelloException, ModelValidationException;

void generate(Model model, String outputType, Properties parameters) throws ModelloException;
void generate(Model model, String outputType, Map<String, Object> parameters) throws ModelloException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Map;

import org.codehaus.modello.ModelloException;
import org.codehaus.modello.ModelloParameterConstants;
Expand Down Expand Up @@ -66,7 +67,7 @@ public abstract class AbstractModelloGenerator implements ModelloGenerator {

private String encoding;

private String licenseText;
private List<String> licenseText;

@Inject
private BuildContext buildContext;
Expand All @@ -75,21 +76,21 @@ protected Logger getLogger() {
return logger;
}

protected void initialize(Model model, Properties parameters) throws ModelloException {
protected void initialize(Model model, Map<String, Object> parameters) throws ModelloException {
this.model = model;

outputDirectory = new File(getParameter(parameters, ModelloParameterConstants.OUTPUT_DIRECTORY));
outputDirectory = new File(requireParameter(parameters, ModelloParameterConstants.OUTPUT_DIRECTORY));

String version = getParameter(parameters, ModelloParameterConstants.VERSION);
String version = requireParameter(parameters, ModelloParameterConstants.VERSION);

generatedVersion = new Version(version);

packageWithVersion =
Boolean.parseBoolean(getParameter(parameters, ModelloParameterConstants.PACKAGE_WITH_VERSION));
Boolean.parseBoolean(requireParameter(parameters, ModelloParameterConstants.PACKAGE_WITH_VERSION));

encoding = parameters.getProperty(ModelloParameterConstants.ENCODING);
encoding = (String) parameters.get(ModelloParameterConstants.ENCODING);

licenseText = parameters.getProperty(ModelloParameterConstants.LICENSE_TEXT);
licenseText = (List<String>) parameters.get(ModelloParameterConstants.LICENSE_TEXT);
}

protected Model getModel() {
Expand All @@ -112,25 +113,29 @@ protected String getEncoding() {
return encoding;
}

protected String getHeader() {
String header = getLicenseHeader();
if (header == null) {
return getGeneratedHeader();
protected List<String> getHeader() {
List<String> header = new ArrayList<>();
List<String> license = getLicenseHeader();
if (license != null) {
header.addAll(license);
}
List<String> generated = getGeneratedHeader();
if (generated != null) {
header.addAll(generated);
}
header += System.lineSeparator();
header += getGeneratedHeader();
return header;
}

protected String getGeneratedHeader() {
protected List<String> getGeneratedHeader() {
String version = getClass().getPackage().getImplementationVersion();
return "=================== DO NOT EDIT THIS FILE ====================" + System.lineSeparator()
+ "Generated by Modello" + ((version == null) ? "" : (' ' + version)) + "," + System.lineSeparator()
+ "any modifications will be overwritten." + System.lineSeparator()
+ "==============================================================";
return Arrays.asList(
"=================== DO NOT EDIT THIS FILE ====================",
"Generated by Modello" + ((version == null) ? "" : (' ' + version)) + ",",
"any modifications will be overwritten.",
"==============================================================");
}

protected String getLicenseHeader() {
protected List<String> getLicenseHeader() {
return licenseText;
}

Expand Down Expand Up @@ -229,19 +234,8 @@ protected boolean isEmpty(String string) {
// Parameter utils
// ----------------------------------------------------------------------

/**
* @deprecated Use {@link #getParameter(Properties, String)} instead
* @param name parameter name
* @param parameters the properties
* @return the parameter value
*/
@Deprecated
protected String getParameter(String name, Properties parameters) {
return getParameter(parameters, name);
}

protected String getParameter(Properties parameters, String name) {
String value = parameters.getProperty(name);
protected String requireParameter(Map<String, Object> parameters, String name) {
String value = (String) parameters.get(name);

if (value == null) {
throw new ModelloRuntimeException("Missing parameter '" + name + "'.");
Expand All @@ -250,8 +244,8 @@ protected String getParameter(Properties parameters, String name) {
return value;
}

protected String getParameter(Properties parameters, String name, String defaultValue) {
return parameters.getProperty(name, defaultValue);
protected String getParameter(Map<String, Object> parameters, String name, String defaultValue) {
return (String) parameters.getOrDefault(name, defaultValue);
}

protected BuildContext getBuildContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE.
*/

import java.util.Properties;
import java.util.Map;

import org.codehaus.modello.ModelloException;
import org.codehaus.modello.model.Model;
Expand All @@ -31,5 +31,5 @@
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
*/
public interface ModelloGenerator {
void generate(Model model, Properties parameters) throws ModelloException;
void generate(Model model, Map<String, Object> parameters) throws ModelloException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
Expand Down Expand Up @@ -142,8 +145,8 @@ protected boolean producesResources() {
*
* @return the parameters
*/
protected Properties createParameters() {
return new Properties();
protected Map<String, Object> createParameters() {
return new HashMap<>();
}

/**
Expand All @@ -154,7 +157,7 @@ protected Properties createParameters() {
*
* @param parameters the parameters to customize
*/
protected void customizeParameters(Properties parameters) {}
protected void customizeParameters(Map<String, Object> parameters) {}

// ----------------------------------------------------------------------
//
Expand All @@ -169,33 +172,34 @@ public void execute() throws MojoExecutionException {
// Initialize the parameters
// ----------------------------------------------------------------------

Properties parameters = createParameters();
Map<String, Object> parameters = createParameters();

parameters.setProperty(ModelloParameterConstants.OUTPUT_DIRECTORY, outputDirectory);
parameters.put(ModelloParameterConstants.OUTPUT_DIRECTORY, outputDirectory);

parameters.setProperty(ModelloParameterConstants.VERSION, version);
parameters.put(ModelloParameterConstants.VERSION, version);

parameters.setProperty(ModelloParameterConstants.PACKAGE_WITH_VERSION, Boolean.toString(packageWithVersion));
parameters.put(ModelloParameterConstants.PACKAGE_WITH_VERSION, Boolean.toString(packageWithVersion));

if (!packagedVersions.isEmpty()) {
parameters.setProperty(
ModelloParameterConstants.ALL_VERSIONS, StringUtils.join(packagedVersions.iterator(), ","));
parameters.put(ModelloParameterConstants.ALL_VERSIONS, StringUtils.join(packagedVersions.iterator(), ","));
}

if (licenseText != null || licenseFile != null) {
String license = "";
List<String> license;
if (licenseText != null) {
// licenseText prevails
license = licenseText;
license = Arrays.asList(licenseText.split(System.lineSeparator()));
} else {
try {
// load it up and hard fail if cannot, as it is user misconfiguration
license = String.join(System.lineSeparator(), Files.readAllLines(licenseFile.toPath()));
license = Files.readAllLines(licenseFile.toPath()).stream()
.map(l -> StringUtils.stripEnd(l, null))
.collect(Collectors.toList());
} catch (IOException e) {
throw new MojoExecutionException("Could not load up license text from " + licenseFile, e);
}
}
parameters.setProperty(ModelloParameterConstants.LICENSE_TEXT, license);
parameters.put(ModelloParameterConstants.LICENSE_TEXT, license);
}

customizeParameters(parameters);
Expand Down Expand Up @@ -223,7 +227,7 @@ public void execute() throws MojoExecutionException {
/**
* Performs execute on a single specified model.
*/
private void doExecute(String modelStr, String outputDirectory, Properties parameters)
private void doExecute(String modelStr, String outputDirectory, Map<String, Object> parameters)
throws MojoExecutionException {
if (!buildContext.hasDelta(modelStr)) {
getLog().debug("Skipping unchanged model: " + modelStr);
Expand All @@ -243,9 +247,9 @@ private void doExecute(String modelStr, String outputDirectory, Properties param
modelloCore.generate(model, getGeneratorType(), parameters);

for (String version : packagedVersions) {
parameters.setProperty(ModelloParameterConstants.VERSION, version);
parameters.put(ModelloParameterConstants.VERSION, version);

parameters.setProperty(ModelloParameterConstants.PACKAGE_WITH_VERSION, Boolean.toString(true));
parameters.put(ModelloParameterConstants.PACKAGE_WITH_VERSION, Boolean.toString(true));

getLog().info("Generating packaged version: " + version);
modelloCore.generate(model, getGeneratorType(), parameters);
Expand Down

0 comments on commit 18d2f87

Please sign in to comment.