Skip to content

Commit

Permalink
Just an attempt to keep structure
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 out) 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.

Not ideal, as Properties allows only strings. Unsure
why internal parameters are Properties and not Map<String, Object>
for example...
  • Loading branch information
cstamas committed Apr 30, 2024
1 parent c90d219 commit 7769a40
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
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,6 +31,7 @@
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;

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 @@ -89,7 +90,10 @@ protected void initialize(Model model, Properties parameters) throws ModelloExce

encoding = parameters.getProperty(ModelloParameterConstants.ENCODING);

licenseText = parameters.getProperty(ModelloParameterConstants.LICENSE_TEXT);
String licenseTextPacked = parameters.getProperty(ModelloParameterConstants.LICENSE_TEXT);
if (licenseTextPacked != null) {
licenseText = Arrays.asList(licenseTextPacked.split("\\|"));
}
}

protected Model getModel() {
Expand All @@ -112,25 +116,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
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
Expand Down Expand Up @@ -183,19 +185,21 @@ public void execute() throws MojoExecutionException {
}

if (licenseText != null || licenseFile != null) {
String license = "";
List<String> license = null;
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.setProperty(ModelloParameterConstants.LICENSE_TEXT, String.join("|", license));
}

customizeParameters(parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected JSourceWriter newJSourceWriter(String packageName, String className) t

private JComment getHeaderComment() {
JComment comment = new JComment();
comment.setComment(getHeader());
comment.setComment(String.join(System.lineSeparator(), getHeader()));
return comment;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@
*/
public abstract class AbstractXmlGenerator extends AbstractModelloGenerator {
protected void initHeader(XMLWriter w) {
XmlWriterUtil.writeComment(w, getHeader());
List<String> header = getHeader();
for (String headerLine : header) {
XmlWriterUtil.writeComment(w, headerLine);
}
}

/**
Expand Down

0 comments on commit 7769a40

Please sign in to comment.