From 7e3b285d313de1d9dfdfd325139f5b13bf825729 Mon Sep 17 00:00:00 2001 From: VLCEK Martin Date: Fri, 19 Nov 2021 14:46:26 +0100 Subject: [PATCH] https://github.com/mathieucarbou/license-maven-plugin/issues/242: replace the test for further header lines with an optional regex. --- .../maven/plugin/license/HeaderStyle.java | 8 ++- .../license/header/HeaderDefinition.java | 54 ++++++++++++++++++- .../plugin/license/header/HeaderParser.java | 2 +- .../license/header/HeaderParserTest.java | 29 ++++++++++ .../src/test/resources/doc/java1.txt | 4 ++ .../src/test/resources/doc/java2.txt | 4 ++ .../src/test/resources/doc/java3.txt | 4 ++ 7 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 license-maven-plugin/src/test/resources/doc/java1.txt create mode 100644 license-maven-plugin/src/test/resources/doc/java2.txt create mode 100644 license-maven-plugin/src/test/resources/doc/java3.txt diff --git a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/HeaderStyle.java b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/HeaderStyle.java index 8be5d7891..6b0b1e274 100644 --- a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/HeaderStyle.java +++ b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/HeaderStyle.java @@ -94,6 +94,12 @@ public class HeaderStyle { @Parameter(required = true) public String firstLineDetectionPattern; + /** + * The regex used to detect the other lines of a header section or line + */ + @Parameter(required = false) + public String otherLineDetectionPattern; + /** * The regex used to detect the end of a header section or line */ @@ -101,6 +107,6 @@ public class HeaderStyle { public String lastLineDetectionPattern; public HeaderDefinition toHeaderDefinition() { - return new HeaderDefinition(name, firstLine, beforeEachLine, endLine, afterEachLine, skipLinePattern, firstLineDetectionPattern, lastLineDetectionPattern, allowBlankLines, multiline, padLines); + return new HeaderDefinition(name != null ? name.toLowerCase() : null, firstLine, beforeEachLine, endLine, afterEachLine, skipLinePattern, firstLineDetectionPattern, otherLineDetectionPattern, lastLineDetectionPattern, allowBlankLines, multiline, padLines); } } diff --git a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderDefinition.java b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderDefinition.java index 8a8cb5a6e..119d82552 100755 --- a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderDefinition.java +++ b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderDefinition.java @@ -17,6 +17,8 @@ import java.util.regex.Pattern; +import com.mycila.maven.plugin.license.util.StringUtils; + /** * The HeaderDefinition class defines what is needed to output a header text into the of the given file * type and what is needed to match the first line as well as the last line of a previous header of the given file @@ -34,6 +36,7 @@ public final class HeaderDefinition { private Pattern skipLinePattern; private Pattern firstLineDetectionPattern; + private Pattern otherLineDetectionPattern; private Pattern lastLineDetectionPattern; private Boolean isMultiline; @@ -52,6 +55,7 @@ public final class HeaderDefinition { * @param skipLinePattern The pattern of lines to skip before being allowed to output this header or null * if it can be outputted from the line of the file. * @param firstLineDetectionPattern The pattern to detect the first line of a previous header. + * @param otherLineDetectionPattern The pattern to detect any following line of a previous header. * @param lastLineDetectionPattern The pattern to detect the last line of a previous header. * @throws IllegalArgumentException If the type name is null. */ @@ -61,6 +65,32 @@ public HeaderDefinition(String type, String skipLinePattern, String firstLineDetectionPattern, String lastLineDetectionPattern, boolean allowBlankLines, boolean isMultiline, boolean padLines) { + this(type, firstLine, beforeEachLine, endLine, afterEachLine, skipLinePattern, firstLineDetectionPattern, null, lastLineDetectionPattern, allowBlankLines, isMultiline, padLines); + } + + /** + * Constructs a new HeaderDefinition object with every header definition properties. + * + * @param type The type name for this header definition. + * @param firstLine The string to output before the content of the first line of this header. + * @param beforeEachLine The string to output before the content of each line of this header (except + * firstLine and endLine). + * @param endLine The string to output before the content of the last line of this header. + * @param afterEachLine The string to output after the content of each line of this header (except + * firstLine and endLine). + * @param skipLinePattern The pattern of lines to skip before being allowed to output this header or null + * if it can be outputted from the line of the file. + * @param firstLineDetectionPattern The pattern to detect the first line of a previous header. + * @param otherLineDetectionPattern The pattern to detect any following line of a previous header. + * @param lastLineDetectionPattern The pattern to detect the last line of a previous header. + * @throws IllegalArgumentException If the type name is null. + */ + public HeaderDefinition(String type, + String firstLine, String beforeEachLine, + String endLine, String afterEachLine, + String skipLinePattern, + String firstLineDetectionPattern, String otherLineDetectionPattern, String lastLineDetectionPattern, + boolean allowBlankLines, boolean isMultiline, boolean padLines) { this(type); this.firstLine = firstLine; this.beforeEachLine = beforeEachLine; @@ -68,6 +98,7 @@ public HeaderDefinition(String type, this.afterEachLine = afterEachLine; this.skipLinePattern = compile(skipLinePattern); this.firstLineDetectionPattern = compile(firstLineDetectionPattern); + this.otherLineDetectionPattern = compile(otherLineDetectionPattern); this.lastLineDetectionPattern = compile(lastLineDetectionPattern); this.allowBlankLines = allowBlankLines; this.isMultiline = isMultiline; @@ -141,12 +172,30 @@ public boolean isSkipLine(String line) { * Tells if the given content line is the first line of a possible header of this definition kind. * * @param line The line to test. - * @return true if the first line of a header have been recognized or false. + * @return true if the first line of a header has been recognized or false. */ public boolean isFirstHeaderLine(String line) { return firstLineDetectionPattern != null && line != null && firstLineDetectionPattern.matcher(line).matches(); } - + + /** + * Tells if the given content line is another than the first line of a possible header of this definition kind. + * + * @param line The line to test. + * @return true if another than the first line of a header has been recognized or false. + */ + public boolean isOtherHeaderLine(String line) { + if (line == null) { + return false; + } else if (otherLineDetectionPattern != null) { + return otherLineDetectionPattern.matcher(line).matches(); + } else if ("".equals(StringUtils.rtrim(beforeEachLine)) && !isMultiLine()) { + return line.startsWith(beforeEachLine); + } else { + return line.startsWith(StringUtils.rtrim(beforeEachLine)); + } + } + /** * Tells if the given content line is the last line of a possible header of this definition kind. * @@ -209,6 +258,7 @@ public void validate() { check("endLine", this.endLine); check("afterEachLine", this.afterEachLine); check("firstLineDetectionPattern", this.firstLineDetectionPattern); + // other line detection pattern can be null check("lastLineDetectionPattern", this.lastLineDetectionPattern); check("isMultiline", this.isMultiline); check("allowBlankLines", this.allowBlankLines); diff --git a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderParser.java b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderParser.java index a7bd6efe5..ac4bbf1d6 100755 --- a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderParser.java +++ b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderParser.java @@ -179,7 +179,7 @@ private boolean hasHeader() { foundEnd = true; } else { - while ((line = fileContent.nextLine()) != null && line.startsWith(before)) { + while ((line = fileContent.nextLine()) != null && headerDefinition.isOtherHeaderLine(line)) { inPlaceHeader.append(line.toLowerCase()); if (headerDefinition.isMultiLine() && headerDefinition.isLastHeaderLine(line)) { foundEnd = true; diff --git a/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/header/HeaderParserTest.java b/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/header/HeaderParserTest.java index 6e9711807..0fdc830ed 100755 --- a/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/header/HeaderParserTest.java +++ b/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/header/HeaderParserTest.java @@ -101,4 +101,33 @@ public void test_parsing_xml6() throws Exception { assertEquals(parser.getBeginPosition(), 45); assertEquals(parser.getEndPosition(), 864); } + + @Test + public void test_parsing_java1() throws Exception { + HeaderParser parser = getCustomJavadocHeaderParser("src/test/resources/doc/java1.txt"); + assertTrue(parser.gotAnyHeader()); + assertEquals("package", parser.getFileContent().getContent().substring(parser.getEndPosition()).trim().substring(0, 7)); + } + + @Test + public void test_parsing_java2() throws Exception { + HeaderParser parser = getCustomJavadocHeaderParser("src/test/resources/doc/java2.txt"); + assertTrue(parser.gotAnyHeader()); + assertEquals("package", parser.getFileContent().getContent().substring(parser.getEndPosition()).trim().substring(0, 7)); + } + + @Test + public void test_parsing_java3() throws Exception { + HeaderParser parser = getCustomJavadocHeaderParser("src/test/resources/doc/java3.txt"); + assertTrue(parser.gotAnyHeader()); + assertEquals("package", parser.getFileContent().getContent().substring(parser.getEndPosition()).trim().substring(0, 7)); + } + + private HeaderParser getCustomJavadocHeaderParser(final String fileName) { + final HeaderDefinition headerDefinition = new HeaderDefinition( + "javadoc2_style", "/**", " ** ", " **/", "", null, "^\\s*/\\*.*$", "^\\s*\\*.*$", "^.*\\*/\\s*$", false, true, false); + return new HeaderParser(new FileContent(new File(fileName), System.getProperty("file.encoding")), headerDefinition, new String[]{"copyright"}); + } + + } diff --git a/license-maven-plugin/src/test/resources/doc/java1.txt b/license-maven-plugin/src/test/resources/doc/java1.txt new file mode 100644 index 000000000..fd20c720f --- /dev/null +++ b/license-maven-plugin/src/test/resources/doc/java1.txt @@ -0,0 +1,4 @@ +/*********************** + ** Copyright ACME + **********************/ +package com.mycila.maven.plugin.license.header; diff --git a/license-maven-plugin/src/test/resources/doc/java2.txt b/license-maven-plugin/src/test/resources/doc/java2.txt new file mode 100644 index 000000000..905d6f54c --- /dev/null +++ b/license-maven-plugin/src/test/resources/doc/java2.txt @@ -0,0 +1,4 @@ +/*********************** + * Copyright ACME + **********************/ +package com.mycila.maven.plugin.license.header; diff --git a/license-maven-plugin/src/test/resources/doc/java3.txt b/license-maven-plugin/src/test/resources/doc/java3.txt new file mode 100644 index 000000000..ff8b3cf14 --- /dev/null +++ b/license-maven-plugin/src/test/resources/doc/java3.txt @@ -0,0 +1,4 @@ +/*********************** +* Copyright ACME * +***********************/ +package com.mycila.maven.plugin.license.header;