Skip to content

Commit f28bf8f

Browse files
author
Björn Ekryd
committed
Change name of ThrowAwayNewlineWrapper to better indicate what it does.
Simplify? the regex to determine if newline should be kept or not (removes sonar warning)
1 parent 5e57e8b commit f28bf8f

File tree

9 files changed

+65
-46
lines changed

9 files changed

+65
-46
lines changed

sorter/src/main/java/sortpom/content/NewlineText.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import org.dom4j.tree.AbstractText;
44

55
/**
6-
* The NewlineText is not really a special case of text. The special handling of NewlineText is done
7-
* in XmlProcessor.PatchedXMLWriter
6+
* The NewlineText is not really a special case of text, it is a placeholder that we want to keep a
7+
* newline i the pom. The special handling of NewlineText is done in XmlProcessor.PatchedXMLWriter
88
*/
99
public class NewlineText extends AbstractText {
1010
private static final long serialVersionUID = -7552189498553321263L;
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package sortpom.wrapper;
22

3+
import static sortpom.wrapper.content.ThrowAwayNewlineWrapper.THROW_AWAY_NEWLINE_INSTANCE;
4+
import static sortpom.wrapper.content.UnsortedWrapper.*;
5+
36
import java.util.regex.Pattern;
47
import org.dom4j.Element;
58
import org.dom4j.Node;
69
import org.dom4j.Text;
710
import sortpom.parameter.PluginParameters;
8-
import sortpom.wrapper.content.SingleNewlineInTextWrapper;
911
import sortpom.wrapper.content.UnsortedWrapper;
1012
import sortpom.wrapper.content.Wrapper;
1113

@@ -15,25 +17,18 @@
1517
*/
1618
public class TextWrapperCreator {
1719

18-
public static final Pattern REGEX_ONE_NEWLINE =
19-
Pattern.compile("^[\\t ]*(\\r|\\n|\\r\\n)[\\t ]*$");
20-
public static final Pattern REGEX_ONE_OR_MORE_NEWLINE = Pattern.compile("^\\s*?([\\r\\n])\\s*$");
20+
public static final Pattern REGEX_ONE_OR_MORE_NEWLINE = Pattern.compile("(\\r\\n|\\r|\\n)+?");
2121
private boolean keepBlankLines;
2222

2323
public void setup(PluginParameters pluginParameters) {
2424
keepBlankLines = pluginParameters.keepBlankLines;
2525
}
2626

2727
Wrapper<Node> createWrapper(Text text) {
28-
if (isElementSpacePreserved(text.getParent())) {
28+
if (!text.getText().isBlank() || isElementSpacePreserved(text.getParent())) {
2929
return new UnsortedWrapper<>(text);
3030
}
31-
if (isSingleNewLine(text)) {
32-
return SingleNewlineInTextWrapper.INSTANCE;
33-
} else if (isBlankLineOrLines(text)) {
34-
return UnsortedWrapper.NEWLINE_TEXT_WRAPPER_INSTANCE;
35-
}
36-
return new UnsortedWrapper<>(text);
31+
return blankTextNode(text);
3732
}
3833

3934
boolean isElementSpacePreserved(Element element) {
@@ -47,14 +42,17 @@ boolean isElementSpacePreserved(Element element) {
4742
&& "preserve".equals(attr.getText());
4843
}
4944

50-
private boolean isSingleNewLine(Text content) {
51-
return REGEX_ONE_NEWLINE.matcher(content.getText()).matches();
52-
}
53-
54-
boolean isBlankLineOrLines(Text content) {
45+
Wrapper<Node> blankTextNode(Text text) {
5546
if (!keepBlankLines) {
56-
return false;
47+
return THROW_AWAY_NEWLINE_INSTANCE;
48+
}
49+
var textContent = text.getText();
50+
var newLineCount = REGEX_ONE_OR_MORE_NEWLINE.matcher(textContent).results().count();
51+
if (newLineCount <= 1) {
52+
// One newline is just the linebreak between two XML elements
53+
return THROW_AWAY_NEWLINE_INSTANCE;
5754
}
58-
return REGEX_ONE_OR_MORE_NEWLINE.matcher(content.getText()).matches();
55+
// Multiple linebreaks between XML elements indicate that a newline should be kept
56+
return KEEP_NEWLINE_INSTANCE;
5957
}
6058
}

sorter/src/main/java/sortpom/wrapper/content/SingleNewlineInTextWrapper.java renamed to sorter/src/main/java/sortpom/wrapper/content/ThrowAwayNewlineWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
*
99
* @author Bjorn
1010
*/
11-
public enum SingleNewlineInTextWrapper implements Wrapper<Node> {
12-
INSTANCE;
11+
public enum ThrowAwayNewlineWrapper implements Wrapper<Node> {
12+
THROW_AWAY_NEWLINE_INSTANCE;
1313

1414
@Override
1515
public Text getContent() {

sorter/src/main/java/sortpom/wrapper/content/UnsortedWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/** A wrapper that lets is element be unsorted */
88
public class UnsortedWrapper<T extends Node> implements Wrapper<T> {
9-
public static final UnsortedWrapper<Node> NEWLINE_TEXT_WRAPPER_INSTANCE =
9+
public static final UnsortedWrapper<Node> KEEP_NEWLINE_INSTANCE =
1010
new UnsortedWrapper<>(new NewlineText());
1111

1212
/** The wrapped dom content. */

sorter/src/main/java/sortpom/wrapper/operation/HierarchyWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.List;
55
import org.dom4j.Element;
66
import org.dom4j.Node;
7-
import sortpom.wrapper.content.SingleNewlineInTextWrapper;
7+
import sortpom.wrapper.content.ThrowAwayNewlineWrapper;
88
import sortpom.wrapper.content.Wrapper;
99

1010
/**
@@ -35,7 +35,7 @@ void createWrappedStructure(WrapperFactory factory) {
3535
HierarchyWrapper currentWrapper = null;
3636
for (var child : elementContent.getContent().content()) {
3737
Wrapper<?> wrapper = factory.create(child);
38-
if (wrapper instanceof SingleNewlineInTextWrapper) {
38+
if (wrapper instanceof ThrowAwayNewlineWrapper) {
3939
continue;
4040
}
4141
if (currentWrapper == null) {

sorter/src/test/java/sortpom/wrapper/TextWrapperCreatorTest.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package sortpom.wrapper;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
34
import static org.junit.jupiter.api.Assertions.assertFalse;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
55

66
import org.dom4j.dom.DOMText;
77
import org.dom4j.tree.DefaultText;
88
import org.junit.jupiter.api.BeforeEach;
99
import org.junit.jupiter.api.Test;
1010
import sortpom.parameter.PluginParameters;
11+
import sortpom.wrapper.content.ThrowAwayNewlineWrapper;
12+
import sortpom.wrapper.content.UnsortedWrapper;
1113

1214
/**
1315
* @author bjorn
@@ -27,19 +29,34 @@ void setup() {
2729

2830
@Test
2931
void testIsEmptyLine() {
30-
assertFalse(textWrapperCreator.isBlankLineOrLines(new DefaultText("\n sortpom\n ")));
31-
assertFalse(textWrapperCreator.isBlankLineOrLines(new DefaultText("sortpom")));
32-
assertTrue(textWrapperCreator.isBlankLineOrLines(new DefaultText("\n ")));
33-
assertTrue(textWrapperCreator.isBlankLineOrLines(new DefaultText(" \n ")));
34-
assertTrue(textWrapperCreator.isBlankLineOrLines(new DefaultText(" \n\n ")));
35-
assertTrue(textWrapperCreator.isBlankLineOrLines(new DefaultText("\n\n")));
36-
assertTrue(textWrapperCreator.isBlankLineOrLines(new DefaultText(" \r ")));
37-
assertTrue(textWrapperCreator.isBlankLineOrLines(new DefaultText(" \r\r ")));
38-
assertTrue(textWrapperCreator.isBlankLineOrLines(new DefaultText("\r\r")));
39-
assertTrue(textWrapperCreator.isBlankLineOrLines(new DefaultText(" \r\n ")));
40-
assertTrue(textWrapperCreator.isBlankLineOrLines(new DefaultText(" \r\n\r\n ")));
41-
assertTrue(textWrapperCreator.isBlankLineOrLines(new DefaultText("\r\n\r\n")));
42-
assertFalse(textWrapperCreator.isBlankLineOrLines(new DefaultText(" ")));
32+
assertKeepNewline("\n \n ");
33+
assertKeepNewline(" \n\n ");
34+
assertKeepNewline("\n\n");
35+
assertKeepNewline("\n\n\n");
36+
assertKeepNewline("\n\n\n\n\n\n\n");
37+
assertKeepNewline(" \r\r ");
38+
assertKeepNewline("\r\r");
39+
assertKeepNewline(" \r\n\r\n ");
40+
assertKeepNewline("\r\n\r\n");
41+
42+
assertNoSpecialNewline("");
43+
assertNoSpecialNewline("\n ");
44+
assertNoSpecialNewline(" \n ");
45+
assertNoSpecialNewline(" \r ");
46+
assertNoSpecialNewline(" \r\n ");
47+
assertNoSpecialNewline(" ");
48+
}
49+
50+
private void assertKeepNewline(String text) {
51+
assertEquals(
52+
UnsortedWrapper.KEEP_NEWLINE_INSTANCE,
53+
textWrapperCreator.blankTextNode(new DefaultText(text)));
54+
}
55+
56+
private void assertNoSpecialNewline(String text) {
57+
assertEquals(
58+
ThrowAwayNewlineWrapper.THROW_AWAY_NEWLINE_INSTANCE,
59+
textWrapperCreator.blankTextNode(new DefaultText(text)));
4360
}
4461

4562
@Test

sorter/src/test/java/sortpom/wrapper/SingleNewlineInTextWrapperTest.java renamed to sorter/src/test/java/sortpom/wrapper/ThrowAwayNewlineWrapperTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import org.junit.jupiter.api.Test;
99
import org.junit.jupiter.api.function.Executable;
10-
import sortpom.wrapper.content.SingleNewlineInTextWrapper;
10+
import sortpom.wrapper.content.ThrowAwayNewlineWrapper;
1111

1212
/**
1313
* All method should throw exception since the element should be throw away, except for the toString
@@ -16,11 +16,11 @@
1616
* @author bjorn
1717
* @since 2012-06-14
1818
*/
19-
class SingleNewlineInTextWrapperTest {
19+
class ThrowAwayNewlineWrapperTest {
2020

2121
@Test
2222
void testGetContent() {
23-
Executable testMethod = SingleNewlineInTextWrapper.INSTANCE::getContent;
23+
Executable testMethod = ThrowAwayNewlineWrapper.THROW_AWAY_NEWLINE_INSTANCE::getContent;
2424

2525
var thrown = assertThrows(UnsupportedOperationException.class, testMethod);
2626

@@ -29,7 +29,8 @@ void testGetContent() {
2929

3030
@Test
3131
void testIsBefore() {
32-
Executable testMethod = () -> SingleNewlineInTextWrapper.INSTANCE.isBefore(null);
32+
Executable testMethod =
33+
() -> ThrowAwayNewlineWrapper.THROW_AWAY_NEWLINE_INSTANCE.isBefore(null);
3334

3435
var thrown = assertThrows(UnsupportedOperationException.class, testMethod);
3536

@@ -38,7 +39,7 @@ void testIsBefore() {
3839

3940
@Test
4041
void testIsContentElement() {
41-
Executable testMethod = SingleNewlineInTextWrapper.INSTANCE::isContentElement;
42+
Executable testMethod = ThrowAwayNewlineWrapper.THROW_AWAY_NEWLINE_INSTANCE::isContentElement;
4243

4344
var thrown = assertThrows(UnsupportedOperationException.class, testMethod);
4445

@@ -47,7 +48,7 @@ void testIsContentElement() {
4748

4849
@Test
4950
void testIsResortable() {
50-
Executable testMethod = SingleNewlineInTextWrapper.INSTANCE::isSortable;
51+
Executable testMethod = ThrowAwayNewlineWrapper.THROW_AWAY_NEWLINE_INSTANCE::isSortable;
5152

5253
var thrown = assertThrows(UnsupportedOperationException.class, testMethod);
5354

@@ -57,6 +58,7 @@ void testIsResortable() {
5758
@Test
5859
void testToString() {
5960
assertThat(
60-
SingleNewlineInTextWrapper.INSTANCE.toString(" "), is(" SingleNewlineInTextWrapper"));
61+
ThrowAwayNewlineWrapper.THROW_AWAY_NEWLINE_INSTANCE.toString(" "),
62+
is(" SingleNewlineInTextWrapper"));
6163
}
6264
}

sorter/src/test/resources/PreserveContent_input.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
MESSAGE!
1818
</message>
1919
<mapEnum>asEnum</mapEnum>
20+
<noPreserve> </noPreserve>
2021
<indentString xml:space="preserve"> </indentString>
2122
<noFileComment>true</noFileComment>
2223
<double xml:space="preserve">

sorter/src/test/resources/PreserveContent_output.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
MESSAGE!
1818
</message>
1919
<mapEnum>asEnum</mapEnum>
20+
<noPreserve></noPreserve>
2021
<indentString xml:space="preserve"> </indentString>
2122
<noFileComment>true</noFileComment>
2223
<double xml:space="preserve">

0 commit comments

Comments
 (0)