diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 77f8dbe..d5319c9 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -8,6 +8,13 @@ updates: maven: patterns: - "*" + ignore: + - dependency-name: org.slf4j:slf4j-api + version: + - ">= 2.0.0" + - dependency-name: org.slf4j:slf4j-simple + version: + - ">= 2.0.0" - package-ecosystem: github-actions directory: "/" schedule: diff --git a/pom.xml b/pom.xml index 3f7e16b..368d97d 100644 --- a/pom.xml +++ b/pom.xml @@ -76,6 +76,20 @@ SPDX-License-Identifier: EPL-2.0 + + com.mycila + license-maven-plugin + + + + + **/*-output.xml + **/*-expected.xml + + + + + diff --git a/src/main/java/net/revelc/code/formatter/xml/lib/CommentFormatter.java b/src/main/java/net/revelc/code/formatter/xml/lib/CommentFormatter.java index c0500ef..37d1cc2 100644 --- a/src/main/java/net/revelc/code/formatter/xml/lib/CommentFormatter.java +++ b/src/main/java/net/revelc/code/formatter/xml/lib/CommentFormatter.java @@ -8,50 +8,45 @@ package net.revelc.code.formatter.xml.lib; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class CommentFormatter { - private static final Pattern ORIGINAL_INDENT_PATTERN = Pattern.compile("^(?\\s*)-->"); + private static final Logger logger = LoggerFactory.getLogger(CommentFormatter.class); - public String format(String tagText, String indent, String lineDelimiter) { + public String format(String tagText, String indent, String lineDelimiter, FormattingPreferences prefs) { String[] lines = tagText.split(lineDelimiter, -1); - String originalIndent = resolveOriginalIndent(lines); - List newLines = new ArrayList<>(); - for (String line : lines) { - newLines.add(indent + removeOriginalIndent(line, originalIndent)); + if (logger.isDebugEnabled()) { + logger.debug("input: {}\n", Arrays.toString(lines)); } - return String.join(lineDelimiter, newLines); - } - - private String resolveOriginalIndent(String[] lines) { - // only multi-line comments need replace original indentation. - if (lines.length < 2) { - return null; + // Caller sets initial indents to method (no indent before ) + List newLines = new ArrayList<>(); + for (String line : lines) { + // do not trim leading space on multi-line comments (just add one indent) + if (lines.length < 2) { + newLines.add(indent + line); + } else if (line.trim().equals("") || line.contains("")) { - Matcher m = ORIGINAL_INDENT_PATTERN.matcher(line); - if (m.matches()) { - return m.group("indent"); - } - return null; - } - } - return null; + // Returned data to caller will be properly positioned by caller + return String.join(lineDelimiter, newLines); } - private static String removeOriginalIndent(String line, String indent) { - if (indent != null && line.startsWith(indent)) { - return line.substring(indent.length()); - } - return line; - } } diff --git a/src/main/java/net/revelc/code/formatter/xml/lib/XmlDocumentFormatter.java b/src/main/java/net/revelc/code/formatter/xml/lib/XmlDocumentFormatter.java index f27ba77..1053600 100644 --- a/src/main/java/net/revelc/code/formatter/xml/lib/XmlDocumentFormatter.java +++ b/src/main/java/net/revelc/code/formatter/xml/lib/XmlDocumentFormatter.java @@ -78,8 +78,8 @@ private void copyNode(Reader reader, FormatState state) throws IOException { } else if (tag instanceof CommentReader) { StringBuilder indentBuilder = new StringBuilder(30); indent(state.depth, indentBuilder); - state.out.append( - new CommentFormatter().format(tag.getTagText(), indentBuilder.toString(), fDefaultLineDelimiter)); + state.out.append(new CommentFormatter().format(tag.getTagText(), indentBuilder.toString(), + fDefaultLineDelimiter, prefs)); } else { String tagText = tag.getTagText(); if (!prefs.getDeleteBlankLines() @@ -452,9 +452,7 @@ protected String readTag() throws IOException { char c = (char) intChar; node.append(c); - // TODO logic incorrectly assumes that " is quote character - // when it could also be ' - if (c == '"') { + if (c == '"' || c == '\'') { insideQuote = !insideQuote; } if (c == '>' && !insideQuote) { @@ -468,6 +466,7 @@ protected String readTag() throws IOException { private static ErrorHandler errorHandler = new ErrorHandler() { @Override public void warning(SAXParseException e) throws SAXException { + // Do nothing on warning } @Override diff --git a/src/test/resources/default-output.xml b/src/test/resources/default-output.xml index 0c310c7..1aec106 100644 --- a/src/test/resources/default-output.xml +++ b/src/test/resources/default-output.xml @@ -1,11 +1,11 @@ + Make sure they have correct indentation after format.--> + apikitRef='api-v2-config' doc:name="API Autodiscovery" /> + Make sure they have correct indentation after format.--> diff --git a/src/test/resources/no-wrap-tags-output.xml b/src/test/resources/no-wrap-tags-output.xml index c4db383..436a5fd 100644 --- a/src/test/resources/no-wrap-tags-output.xml +++ b/src/test/resources/no-wrap-tags-output.xml @@ -1,11 +1,11 @@ + diff --git a/src/test/resources/test-input.xml b/src/test/resources/test-input.xml index 5a25e46..acddc91 100644 --- a/src/test/resources/test-input.xml +++ b/src/test/resources/test-input.xml @@ -36,7 +36,7 @@ http://www.mulesoft.org/schema/mule/api-platform-gw http://www.mulesoft.org/sche - + diff --git a/src/test/resources/test-space-expected.xml b/src/test/resources/test-space-expected.xml index 47ac738..2a754b2 100644 --- a/src/test/resources/test-space-expected.xml +++ b/src/test/resources/test-space-expected.xml @@ -1,11 +1,11 @@