Skip to content

Commit

Permalink
mathieucarbou#242: replace the test for further header lines with an …
Browse files Browse the repository at this point in the history
…optional regex.
  • Loading branch information
cns-solutions-admin committed Nov 19, 2021
1 parent 4400a75 commit 7e3b285
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,19 @@ 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
*/
@Parameter(required = true)
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.util.regex.Pattern;

import com.mycila.maven.plugin.license.util.StringUtils;

/**
* The <code>HeaderDefinition</code> 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
Expand All @@ -34,6 +36,7 @@ public final class HeaderDefinition {

private Pattern skipLinePattern;
private Pattern firstLineDetectionPattern;
private Pattern otherLineDetectionPattern;
private Pattern lastLineDetectionPattern;
private Boolean isMultiline;

Expand All @@ -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.
*/
Expand All @@ -61,13 +65,40 @@ 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 <code>HeaderDefinition</code> 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;
this.endLine = endLine;
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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"});
}


}
4 changes: 4 additions & 0 deletions license-maven-plugin/src/test/resources/doc/java1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/***********************
** Copyright ACME
**********************/
package com.mycila.maven.plugin.license.header;
4 changes: 4 additions & 0 deletions license-maven-plugin/src/test/resources/doc/java2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/***********************
* Copyright ACME
**********************/
package com.mycila.maven.plugin.license.header;
4 changes: 4 additions & 0 deletions license-maven-plugin/src/test/resources/doc/java3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/***********************
* Copyright ACME *
***********************/
package com.mycila.maven.plugin.license.header;

0 comments on commit 7e3b285

Please sign in to comment.