Skip to content

Commit 84a2d88

Browse files
authored
Merge pull request #184 from hazendaz/main
Resolve comment formatter issues
2 parents f00445d + b04de4d commit 84a2d88

File tree

9 files changed

+87
-72
lines changed

9 files changed

+87
-72
lines changed

.github/dependabot.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ updates:
88
maven:
99
patterns:
1010
- "*"
11+
ignore:
12+
- dependency-name: org.slf4j:slf4j-api
13+
version:
14+
- ">= 2.0.0"
15+
- dependency-name: org.slf4j:slf4j-simple
16+
version:
17+
- ">= 2.0.0"
1118
- package-ecosystem: github-actions
1219
directory: "/"
1320
schedule:

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ SPDX-License-Identifier: EPL-2.0
7676
</ignoredUnusedDeclaredDependencies>
7777
</configuration>
7878
</plugin>
79+
<plugin>
80+
<groupId>com.mycila</groupId>
81+
<artifactId>license-maven-plugin</artifactId>
82+
<configuration>
83+
<licenseSets>
84+
<licenseSet>
85+
<excludes combine.children="append">
86+
<exclude>**/*-output.xml</exclude>
87+
<exclude>**/*-expected.xml</exclude>
88+
</excludes>
89+
</licenseSet>
90+
</licenseSets>
91+
</configuration>
92+
</plugin>
7993
</plugins>
8094
</build>
8195
</project>

src/main/java/net/revelc/code/formatter/xml/lib/CommentFormatter.java

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,45 @@
88
package net.revelc.code.formatter.xml.lib;
99

1010
import java.util.ArrayList;
11+
import java.util.Arrays;
1112
import java.util.List;
12-
import java.util.regex.Matcher;
13-
import java.util.regex.Pattern;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
1415

1516
public class CommentFormatter {
1617

17-
private static final Pattern ORIGINAL_INDENT_PATTERN = Pattern.compile("^(?<indent>\\s*)-->");
18+
private static final Logger logger = LoggerFactory.getLogger(CommentFormatter.class);
1819

19-
public String format(String tagText, String indent, String lineDelimiter) {
20+
public String format(String tagText, String indent, String lineDelimiter, FormattingPreferences prefs) {
2021
String[] lines = tagText.split(lineDelimiter, -1);
21-
String originalIndent = resolveOriginalIndent(lines);
2222

23-
List<String> newLines = new ArrayList<>();
24-
for (String line : lines) {
25-
newLines.add(indent + removeOriginalIndent(line, originalIndent));
23+
if (logger.isDebugEnabled()) {
24+
logger.debug("input: {}\n", Arrays.toString(lines));
2625
}
2726

28-
return String.join(lineDelimiter, newLines);
29-
}
30-
31-
private String resolveOriginalIndent(String[] lines) {
32-
// only multi-line comments need replace original indentation.
33-
if (lines.length < 2) {
34-
return null;
27+
// Caller sets initial indents to method (no indent before <!-- and indent before -->)
28+
List<String> newLines = new ArrayList<>();
29+
for (String line : lines) {
30+
// do not trim leading space on multi-line comments (just add one indent)
31+
if (lines.length < 2) {
32+
newLines.add(indent + line);
33+
} else if (line.trim().equals("<!--") || line.trim().equals("-->") || line.contains("<!--")) {
34+
// add one indent for begin comment / end comment / line starting comment with text
35+
// allowing last line with comment to be indented twice on else
36+
newLines.add(indent + line.stripLeading());
37+
} else if (indent.equals("") && !line.stripLeading().equals("")) {
38+
// If indent is empty and line is not empty, use canonicalIndent
39+
newLines.add(prefs.getCanonicalIndent() + line.stripLeading());
40+
} else {
41+
// Add two intent for others
42+
newLines.add(indent + indent + line.stripLeading());
43+
}
3544
}
3645

37-
for (int i = lines.length - 1; i >= 0; i--) {
38-
String line = lines[i];
46+
logger.debug("output: {}\n", newLines);
3947

40-
if (line.trim().endsWith("-->")) {
41-
Matcher m = ORIGINAL_INDENT_PATTERN.matcher(line);
42-
if (m.matches()) {
43-
return m.group("indent");
44-
}
45-
return null;
46-
}
47-
}
48-
return null;
48+
// Returned data to caller will be properly positioned by caller
49+
return String.join(lineDelimiter, newLines);
4950
}
5051

51-
private static String removeOriginalIndent(String line, String indent) {
52-
if (indent != null && line.startsWith(indent)) {
53-
return line.substring(indent.length());
54-
}
55-
return line;
56-
}
5752
}

src/main/java/net/revelc/code/formatter/xml/lib/XmlDocumentFormatter.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ private void copyNode(Reader reader, FormatState state) throws IOException {
7878
} else if (tag instanceof CommentReader) {
7979
StringBuilder indentBuilder = new StringBuilder(30);
8080
indent(state.depth, indentBuilder);
81-
state.out.append(
82-
new CommentFormatter().format(tag.getTagText(), indentBuilder.toString(), fDefaultLineDelimiter));
81+
state.out.append(new CommentFormatter().format(tag.getTagText(), indentBuilder.toString(),
82+
fDefaultLineDelimiter, prefs));
8383
} else {
8484
String tagText = tag.getTagText();
8585
if (!prefs.getDeleteBlankLines()
@@ -452,9 +452,7 @@ protected String readTag() throws IOException {
452452
char c = (char) intChar;
453453

454454
node.append(c);
455-
// TODO logic incorrectly assumes that " is quote character
456-
// when it could also be '
457-
if (c == '"') {
455+
if (c == '"' || c == '\'') {
458456
insideQuote = !insideQuote;
459457
}
460458
if (c == '>' && !insideQuote) {
@@ -468,6 +466,7 @@ protected String readTag() throws IOException {
468466
private static ErrorHandler errorHandler = new ErrorHandler() {
469467
@Override
470468
public void warning(SAXParseException e) throws SAXException {
469+
// Do nothing on warning
471470
}
472471

473472
@Override

src/test/resources/default-output.xml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
This program and the accompanying materials are made
5-
available under the terms of the Eclipse Public License 2.0
6-
which is available at https://www.eclipse.org/legal/epl-2.0/
4+
This program and the accompanying materials are made
5+
available under the terms of the Eclipse Public License 2.0
6+
which is available at https://www.eclipse.org/legal/epl-2.0/
77
8-
SPDX-License-Identifier: EPL-2.0
8+
SPDX-License-Identifier: EPL-2.0
99
1010
-->
1111
<mule xmlns:api-platform-gw="http://www.mulesoft.org/schema/mule/api-platform-gw"
@@ -34,20 +34,20 @@ http://www.mulesoft.org/schema/mule/api-platform-gw http://www.mulesoft.org/sche
3434
<!--Here's a comment surrounded wrapped by empty lines to make sure they are kept-->
3535

3636
<!--
37-
Here's a comment block.
38-
Make sure they have correct indentation after format.
37+
Here's a comment block.
38+
Make sure they have correct indentation after format.
3939
-->
4040

4141
<!--
42-
Here's a comment block with leading spaces.
43-
Make sure they have correct indentation and keep the leading spaces after format.
42+
Here's a comment block with leading spaces.
43+
Make sure they have correct indentation and keep the leading spaces after format.
4444
-->
4545

4646
<!--Here's a comment block with unaligned block start/end.
47-
Make sure they have correct indentation after format.-->
47+
Make sure they have correct indentation after format.-->
4848

4949
<api-platform-gw:api apiName="${api-v2.name}" version="${api-v2.version}" flowRef="api-v2-main" create="true"
50-
apikitRef="api-v2-config" doc:name="API Autodiscovery" />
50+
apikitRef='api-v2-config' doc:name="API Autodiscovery" />
5151
<flow name="api-v2-main">
5252
<http:listener config-ref="api-httpsListenerConfig" path="/api/v2/*" doc:name="HTTP" />
5353
<set-variable variableName="traceId"

src/test/resources/multi-lined-attrs-output.xml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
This program and the accompanying materials are made
5-
available under the terms of the Eclipse Public License 2.0
6-
which is available at https://www.eclipse.org/legal/epl-2.0/
4+
This program and the accompanying materials are made
5+
available under the terms of the Eclipse Public License 2.0
6+
which is available at https://www.eclipse.org/legal/epl-2.0/
77
8-
SPDX-License-Identifier: EPL-2.0
8+
SPDX-License-Identifier: EPL-2.0
99
1010
-->
1111
<mule
@@ -57,24 +57,24 @@ http://www.mulesoft.org/schema/mule/api-platform-gw http://www.mulesoft.org/sche
5757
<!--Here's a comment surrounded wrapped by empty lines to make sure they are kept-->
5858

5959
<!--
60-
Here's a comment block.
61-
Make sure they have correct indentation after format.
60+
Here's a comment block.
61+
Make sure they have correct indentation after format.
6262
-->
6363

6464
<!--
65-
Here's a comment block with leading spaces.
66-
Make sure they have correct indentation and keep the leading spaces after format.
65+
Here's a comment block with leading spaces.
66+
Make sure they have correct indentation and keep the leading spaces after format.
6767
-->
6868

6969
<!--Here's a comment block with unaligned block start/end.
70-
Make sure they have correct indentation after format.-->
70+
Make sure they have correct indentation after format.-->
7171

7272
<api-platform-gw:api
7373
apiName="${api-v2.name}"
7474
version="${api-v2.version}"
7575
flowRef="api-v2-main"
7676
create="true"
77-
apikitRef="api-v2-config"
77+
apikitRef='api-v2-config'
7878
doc:name="API Autodiscovery" />
7979
<flow
8080
name="api-v2-main">

src/test/resources/no-wrap-tags-output.xml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
This program and the accompanying materials are made
5-
available under the terms of the Eclipse Public License 2.0
6-
which is available at https://www.eclipse.org/legal/epl-2.0/
4+
This program and the accompanying materials are made
5+
available under the terms of the Eclipse Public License 2.0
6+
which is available at https://www.eclipse.org/legal/epl-2.0/
77
8-
SPDX-License-Identifier: EPL-2.0
8+
SPDX-License-Identifier: EPL-2.0
99
1010
-->
1111
<mule xmlns:api-platform-gw="http://www.mulesoft.org/schema/mule/api-platform-gw" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:apikit="http://www.mulesoft.org/schema/mule/apikit" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
@@ -24,19 +24,19 @@ http://www.mulesoft.org/schema/mule/api-platform-gw http://www.mulesoft.org/sche
2424
<!--Here's a comment surrounded wrapped by empty lines to make sure they are kept-->
2525

2626
<!--
27-
Here's a comment block.
28-
Make sure they have correct indentation after format.
27+
Here's a comment block.
28+
Make sure they have correct indentation after format.
2929
-->
3030

3131
<!--
32-
Here's a comment block with leading spaces.
33-
Make sure they have correct indentation and keep the leading spaces after format.
32+
Here's a comment block with leading spaces.
33+
Make sure they have correct indentation and keep the leading spaces after format.
3434
-->
3535

3636
<!--Here's a comment block with unaligned block start/end.
37-
Make sure they have correct indentation after format.-->
37+
Make sure they have correct indentation after format.-->
3838

39-
<api-platform-gw:api apiName="${api-v2.name}" version="${api-v2.version}" flowRef="api-v2-main" create="true" apikitRef="api-v2-config" doc:name="API Autodiscovery" />
39+
<api-platform-gw:api apiName="${api-v2.name}" version="${api-v2.version}" flowRef="api-v2-main" create="true" apikitRef='api-v2-config' doc:name="API Autodiscovery" />
4040
<flow name="api-v2-main">
4141
<http:listener config-ref="api-httpsListenerConfig" path="/api/v2/*" doc:name="HTTP" />
4242
<set-variable variableName="traceId" value="#[message.inboundProperties.'correlationId' != null ? message.inboundProperties.'uber-trace-id' : java.util.UUID.randomUUID().toString()]" doc:name="Variable - traceId" mimeType="application/java" />

src/test/resources/test-input.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ http://www.mulesoft.org/schema/mule/api-platform-gw http://www.mulesoft.org/sche
3636
<!--Here's a comment block with unaligned block start/end.
3737
Make sure they have correct indentation after format.-->
3838

39-
<api-platform-gw:api apiName="${api-v2.name}" version="${api-v2.version}" flowRef="api-v2-main" create="true" apikitRef="api-v2-config" doc:name="API Autodiscovery"/>
39+
<api-platform-gw:api apiName="${api-v2.name}" version="${api-v2.version}" flowRef="api-v2-main" create="true" apikitRef='api-v2-config' doc:name="API Autodiscovery"/>
4040
<flow name="api-v2-main">
4141
<http:listener config-ref="api-httpsListenerConfig" path="/api/v2/*" doc:name="HTTP" />
4242
<set-variable variableName="traceId" value="#[message.inboundProperties.'correlationId' != null ? message.inboundProperties.'uber-trace-id' : java.util.UUID.randomUUID().toString()]" doc:name="Variable - traceId" mimeType="application/java"/>

src/test/resources/test-space-expected.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
This program and the accompanying materials are made
5-
available under the terms of the Eclipse Public License 2.0
6-
which is available at https://www.eclipse.org/legal/epl-2.0/
4+
This program and the accompanying materials are made
5+
available under the terms of the Eclipse Public License 2.0
6+
which is available at https://www.eclipse.org/legal/epl-2.0/
77
8-
SPDX-License-Identifier: EPL-2.0
8+
SPDX-License-Identifier: EPL-2.0
99
1010
-->
1111
<root>

0 commit comments

Comments
 (0)