Skip to content

Commit

Permalink
Replacing indentSchemaLocation=true with indentAttribute=schemaLocation.
Browse files Browse the repository at this point in the history
Deprecating usage of indentSchemaLocation
  • Loading branch information
Björn Ekryd committed May 1, 2024
1 parent 5bebfef commit 1a11c4c
Show file tree
Hide file tree
Showing 25 changed files with 225 additions and 139 deletions.
2 changes: 1 addition & 1 deletion maven-plugin/src/it/four-spaces-indent/expected_pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<expandEmptyElements>false</expandEmptyElements>
<nrOfIndentSpace>4</nrOfIndentSpace>
<indentBlankLines/>
<indentSchemaLocation>true</indentSchemaLocation>
<indentAttribute>schemaLocation</indentAttribute>
</configuration>
</execution>
</executions>
Expand Down
2 changes: 1 addition & 1 deletion maven-plugin/src/it/four-spaces-indent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<expandEmptyElements>false</expandEmptyElements>
<nrOfIndentSpace>4</nrOfIndentSpace>
<indentBlankLines />
<indentSchemaLocation>true</indentSchemaLocation>
<indentAttribute>schemaLocation</indentAttribute>
</configuration>
</execution>
</executions>
Expand Down
22 changes: 16 additions & 6 deletions maven-plugin/src/it/mojo-description/expected_description.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,20 @@ sortpom:sort
User property: sort.ignoreLineSeparators
Ignore line separators when comparing current POM with sorted one

indentAttribute
User property: sort.indentAttribute
Should the xml attributes be indented. Can be either 'schemaLocation' or
'all'. The attribute will be indented (2 * nrOfIndentSpace) characters.
'schemaLocation' only indents the schemaLocation attribute in the project
element.

indentBlankLines (Default: false)
User property: sort.indentBlankLines
Should blank lines (if preserved) have indentation.

indentSchemaLocation (Default: false)
User property: sort.indentSchemaLocation
Should the schema location attribute of project (top level xml element)
be placed on a new line. The attribute will be indented (2 *
nrOfIndentSpace) characters.
Deprecated! Use indentAttribute=schemaLocation instead.

keepBlankLines (Default: true)
User property: sort.keepBlankLines
Expand Down Expand Up @@ -195,15 +200,20 @@ sortpom:verify
User property: sort.ignoreLineSeparators
Ignore line separators when comparing current POM with sorted one

indentAttribute
User property: sort.indentAttribute
Should the xml attributes be indented. Can be either 'schemaLocation' or
'all'. The attribute will be indented (2 * nrOfIndentSpace) characters.
'schemaLocation' only indents the schemaLocation attribute in the project
element.

indentBlankLines (Default: false)
User property: sort.indentBlankLines
Should blank lines (if preserved) have indentation.

indentSchemaLocation (Default: false)
User property: sort.indentSchemaLocation
Should the schema location attribute of project (top level xml element)
be placed on a new line. The attribute will be indented (2 *
nrOfIndentSpace) characters.
Deprecated! Use indentAttribute=schemaLocation instead.

keepBlankLines (Default: true)
User property: sort.keepBlankLines
Expand Down
13 changes: 9 additions & 4 deletions maven-plugin/src/main/java/sortpom/AbstractParentMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ abstract class AbstractParentMojo extends AbstractMojo {
@Parameter(property = "sort.indentBlankLines", defaultValue = "false")
boolean indentBlankLines;

/**
* Should the schema location attribute of project (top level xml element) be placed on a new
* line. The attribute will be indented (2 * nrOfIndentSpace) characters.
*/
/** Deprecated! Use indentAttribute=schemaLocation instead. */
@Parameter(property = "sort.indentSchemaLocation", defaultValue = "false")
boolean indentSchemaLocation;

/**
* Should the xml attributes be indented. Can be either 'schemaLocation' or 'all'. The attribute
* will be indented (2 * nrOfIndentSpace) characters. 'schemaLocation' only indents the
* schemaLocation attribute in the project element.
*/
@Parameter(property = "sort.indentAttribute")
String indentAttribute;

/** Choose between a number of predefined sort order files. */
@Parameter(property = "sort.predefinedSortOrder", defaultValue = "recommended_2008_06")
String predefinedSortOrder;
Expand Down
19 changes: 18 additions & 1 deletion maven-plugin/src/main/java/sortpom/SortMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class SortMojo extends AbstractParentMojo {

public void setup(SortPomLogger mavenLogger) throws MojoFailureException {
warnAboutDeprecatedArguments(mavenLogger, indentSchemaLocation);
new ExceptionConverter(
() -> {
var pluginParameters =
Expand All @@ -30,7 +31,10 @@ public void setup(SortPomLogger mavenLogger) throws MojoFailureException {
spaceBeforeCloseEmptyElement,
keepBlankLines,
endWithNewline)
.setIndent(nrOfIndentSpace, indentBlankLines, indentSchemaLocation)
.setIndent(
nrOfIndentSpace,
indentBlankLines,
getProcessedIndentAttribute(indentAttribute, indentSchemaLocation))
.setSortOrder(sortOrderFile, predefinedSortOrder)
.setSortEntities(
sortDependencies,
Expand All @@ -51,4 +55,17 @@ public void setup(SortPomLogger mavenLogger) throws MojoFailureException {
protected void sortPom() throws MojoFailureException {
new ExceptionConverter(sortPomImpl::sortPom).executeAndConvertException();
}

static String getProcessedIndentAttribute(String indentAttribute, boolean indentSchemaLocation) {
return indentAttribute != null
? indentAttribute
: (indentSchemaLocation ? "schemaLocation" : null);
}

static void warnAboutDeprecatedArguments(SortPomLogger log, boolean indentSchemaLocation) {
if (indentSchemaLocation) {
log.warn(
"[DEPRECATED] The parameter 'indentSchemaLocation' is no longer used. Please use <indentAttribute>schemaLocation</indentAttribute> instead. In the next major version, using 'indentSchemaLocation' will cause an error!");
}
}
}
9 changes: 8 additions & 1 deletion maven-plugin/src/main/java/sortpom/VerifyMojo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package sortpom;

import static sortpom.SortMojo.getProcessedIndentAttribute;
import static sortpom.SortMojo.warnAboutDeprecatedArguments;

import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -36,6 +39,7 @@ public class VerifyMojo extends AbstractParentMojo {
private String violationFilename;

public void setup(SortPomLogger mavenLogger) throws MojoFailureException {
warnAboutDeprecatedArguments(mavenLogger, indentSchemaLocation);
new ExceptionConverter(
() -> {
var pluginParameters =
Expand All @@ -50,7 +54,10 @@ public void setup(SortPomLogger mavenLogger) throws MojoFailureException {
spaceBeforeCloseEmptyElement,
keepBlankLines,
endWithNewline)
.setIndent(nrOfIndentSpace, indentBlankLines, indentSchemaLocation)
.setIndent(
nrOfIndentSpace,
indentBlankLines,
getProcessedIndentAttribute(indentAttribute, indentSchemaLocation))
.setSortOrder(sortOrderFile, predefinedSortOrder)
.setSortEntities(
sortDependencies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import java.io.File;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -197,12 +198,6 @@ void parameterIndentBlankLineShouldEndUpInXmlProcessor() {
"indentBlankLines", xmlOutputGenerator);
}

@Test
void parameterIndentSchemaLocationShouldEndUpInXmlProcessor() {
assertParameterMoveFromMojoToRestOfApplicationForBoolean(
"indentSchemaLocation", xmlOutputGenerator);
}

@Test
void xmlProcessingInstructionParserShouldGetLogger() throws MojoFailureException {
var expectedLogger = new MavenLogger(null, false);
Expand All @@ -212,6 +207,16 @@ void xmlProcessingInstructionParserShouldGetLogger() throws MojoFailureException
assertThat(logger, sameInstance(expectedLogger));
}

@Test
void indentSchemaLocationShouldWarnForDeprecation() throws MojoFailureException {
var logger = mock(SortPomLogger.class);
new ReflectionHelper(sortMojo).setField("indentSchemaLocation", true);
sortMojo.setup(logger);
verify(logger)
.warn(
"[DEPRECATED] The parameter 'indentSchemaLocation' is no longer used. Please use <indentAttribute>schemaLocation</indentAttribute> instead. In the next major version, using 'indentSchemaLocation' will cause an error!");
}

private void assertParameterMoveFromMojoToRestOfApplication(
String parameterName, Object parameterValue, Object... whereParameterCanBeFound) {
new ReflectionHelper(sortMojo).setField(parameterName, parameterValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import java.io.File;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -14,6 +15,7 @@
import sortpom.SortPomImpl;
import sortpom.SortPomService;
import sortpom.VerifyMojo;
import sortpom.logger.SortPomLogger;
import sortpom.output.XmlOutputGenerator;
import sortpom.parameter.VerifyFailType;
import sortpom.util.FileUtil;
Expand Down Expand Up @@ -191,9 +193,13 @@ void parameterIndentBlankLineShouldEndUpInXmlProcessor() {
}

@Test
void parameterIndentSchemaLocationShouldEndUpInXmlProcessor() {
assertParameterMoveFromMojoToRestOfApplicationForBoolean(
"indentSchemaLocation", xmlOutputGenerator);
void indentSchemaLocationShouldWarnForDeprecation() throws MojoFailureException {
var logger = mock(SortPomLogger.class);
new ReflectionHelper(verifyMojo).setField("indentSchemaLocation", true);
verifyMojo.setup(logger);
verify(logger)
.warn(
"[DEPRECATED] The parameter 'indentSchemaLocation' is no longer used. Please use <indentAttribute>schemaLocation</indentAttribute> instead. In the next major version, using 'indentSchemaLocation' will cause an error!");
}

@Test
Expand Down
9 changes: 5 additions & 4 deletions sorter/src/main/java/sortpom/output/PatchedXMLWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.DefaultText;
import sortpom.content.NewlineText;
import sortpom.parameter.IndentAttribute;

/** Overriding XMLWriter to be able to handle SortPom formatting options */
class PatchedXMLWriter extends XMLWriter {

private final OutputFormat format;
private final boolean indentBlankLines;
private final boolean indentSchemaLocation;
private final IndentAttribute indentAttribute;
private final boolean spaceBeforeCloseEmptyElement;
private final boolean endWithNewline;

Expand All @@ -27,12 +28,12 @@ public PatchedXMLWriter(
OutputFormat format,
boolean spaceBeforeCloseEmptyElement,
boolean indentBlankLines,
boolean indentSchemaLocation,
IndentAttribute indentAttribute,
boolean endWithNewline) {
super(writer, format);
this.format = format;
this.indentBlankLines = indentBlankLines;
this.indentSchemaLocation = indentSchemaLocation;
this.indentAttribute = indentAttribute;
this.spaceBeforeCloseEmptyElement = spaceBeforeCloseEmptyElement;
this.endWithNewline = endWithNewline;
}
Expand Down Expand Up @@ -119,7 +120,7 @@ private void writeTrimmedText(Node node) throws IOException {
@Override
protected void writeAttribute(Attribute attribute) throws IOException {
var qualifiedName = attribute.getQualifiedName();
if (indentSchemaLocation && "xsi:schemaLocation".equals(qualifiedName)) {
if (indentAttribute.indentSchemaLocation() && "xsi:schemaLocation".equals(qualifiedName)) {
writePrintln();
writeString(format.getIndent());
writeString(format.getIndent());
Expand Down
7 changes: 4 additions & 3 deletions sorter/src/main/java/sortpom/output/XmlOutputGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import sortpom.exception.FailureException;
import sortpom.parameter.IndentAttribute;
import sortpom.parameter.PluginParameters;
import sortpom.util.StringLineSeparatorWriter;

Expand All @@ -16,7 +17,7 @@ public class XmlOutputGenerator {
private String indentCharacters;
private boolean expandEmptyElements;
private boolean indentBlankLines;
private boolean indentSchemaLocation;
private IndentAttribute indentAttribute;
private boolean spaceBeforeCloseEmptyElement;
private String lineSeparator;
private boolean endWithNewline;
Expand All @@ -27,7 +28,7 @@ public void setup(PluginParameters pluginParameters) {
this.encoding = pluginParameters.encoding;
this.expandEmptyElements = pluginParameters.expandEmptyElements;
this.indentBlankLines = pluginParameters.indentBlankLines;
this.indentSchemaLocation = pluginParameters.indentSchemaLocation;
this.indentAttribute = pluginParameters.indentAttribute;
this.spaceBeforeCloseEmptyElement = pluginParameters.spaceBeforeCloseEmptyElement;
this.lineSeparator = pluginParameters.lineSeparatorUtil.toString();
this.endWithNewline = pluginParameters.endWithNewline;
Expand All @@ -47,7 +48,7 @@ public String getSortedXml(Document newDocument) {
createPrettyFormat(),
spaceBeforeCloseEmptyElement,
indentBlankLines,
indentSchemaLocation,
indentAttribute,
endWithNewline);
xmlWriter.write(newDocument);
writer.writeDelayedNewline();
Expand Down
28 changes: 28 additions & 0 deletions sorter/src/main/java/sortpom/parameter/IndentAttribute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package sortpom.parameter;

import sortpom.exception.FailureException;

public enum IndentAttribute {
NONE,
SCHEMA_LOCATION,
ALL;

static IndentAttribute fromString(String indentAttribute) {
if (indentAttribute == null) {
return NONE;
}
if ("schemaLocation".equalsIgnoreCase(indentAttribute)) {
return SCHEMA_LOCATION;
}
if ("all".equalsIgnoreCase(indentAttribute)) {
return ALL;
}
throw new FailureException(
String.format(
"verifyFail must be either SCHEMA_LOCATION or ALL. Was: %s", indentAttribute));
}

public boolean indentSchemaLocation() {
return this != NONE;
}
}
14 changes: 7 additions & 7 deletions sorter/src/main/java/sortpom/parameter/PluginParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class PluginParameters {
public final boolean keepBlankLines;
public final boolean endWithNewline;
public final boolean indentBlankLines;
public final boolean indentSchemaLocation;
public final IndentAttribute indentAttribute;
public final VerifyFailType verifyFailType;
public final VerifyFailOnType verifyFailOn;
public final boolean ignoreLineSeparators;
Expand All @@ -44,7 +44,7 @@ private PluginParameters(
boolean endWithNewline,
String indentCharacters,
boolean indentBlankLines,
boolean indentSchemaLocation,
IndentAttribute indentAttribute,
String predefinedSortOrder,
String customSortOrderFile,
DependencySortOrder sortDependencies,
Expand Down Expand Up @@ -79,7 +79,7 @@ private PluginParameters(
this.keepBlankLines = keepBlankLines;
this.endWithNewline = endWithNewline;
this.indentBlankLines = indentBlankLines;
this.indentSchemaLocation = indentSchemaLocation;
this.indentAttribute = indentAttribute;
this.verifyFailType = verifyFailType;
this.verifyFailOn = verifyFailOn;
this.ignoreLineSeparators = ignoreLineSeparators;
Expand All @@ -101,7 +101,7 @@ public static class Builder {
private LineSeparatorUtil lineSeparatorUtil;
private String indentCharacters;
private boolean indentBlankLines;
private boolean indentSchemaLocation;
private IndentAttribute indentAttribute = IndentAttribute.NONE;
private boolean expandEmptyElements;
private boolean spaceBeforeCloseEmptyElement;
private String predefinedSortOrder;
Expand Down Expand Up @@ -164,10 +164,10 @@ public Builder setFormatting(

/** Sets indent information that is used when the pom file is sorted */
public Builder setIndent(
final int nrOfIndentSpace, final boolean indentBlankLines, boolean indentSchemaLocation) {
final int nrOfIndentSpace, final boolean indentBlankLines, String indentAttribute) {
this.indentCharacters = new IndentCharacters(nrOfIndentSpace).getIndentCharacters();
this.indentBlankLines = indentBlankLines;
this.indentSchemaLocation = indentSchemaLocation;
this.indentAttribute = IndentAttribute.fromString(indentAttribute);
return this;
}

Expand Down Expand Up @@ -226,7 +226,7 @@ public PluginParameters build() {
endWithNewline,
indentCharacters,
indentBlankLines,
indentSchemaLocation,
indentAttribute,
predefinedSortOrder,
customSortOrderFile,
sortDependencies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void writeXmlWithDocType() throws Exception {
reader.setIncludeInternalDTDDeclarations(true);

var wr = new StringWriter();
var writer = new PatchedXMLWriter(wr, new OutputFormat(), false, false, false, false);
var writer = new PatchedXMLWriter(wr, new OutputFormat(), false, false, null, false);
writer.write(reader.read(new StringReader(xml)));

assertEquals(xml, wr.toString());
Expand Down
Loading

0 comments on commit 1a11c4c

Please sign in to comment.