-
-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add regex for line comparison #243
base: master
Are you sure you want to change the base?
Changes from 4 commits
7e3b285
8c6c33b
bbcf1ee
b207766
8603463
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -34,6 +36,7 @@ public final class HeaderDefinition { | |
|
||
private Pattern skipLinePattern; | ||
private Pattern firstLineDetectionPattern; | ||
private Pattern otherLineDetectionPattern; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer that the name is called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Except it is for all lines except the first line - the last line must also pass this pattern (or the beforeEachLine test). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why ? There is a pattern for the last line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But the code still checks beforeEachLine. I didn't want to change the logic and the RegEx happens when the startsWith(beforeEachLine) check happens. |
||
private Pattern lastLineDetectionPattern; | ||
private Boolean isMultiline; | ||
|
||
|
@@ -61,13 +64,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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you would need to update |
||
this.lastLineDetectionPattern = compile(lastLineDetectionPattern); | ||
this.allowBlankLines = allowBlankLines; | ||
this.isMultiline = isMultiline; | ||
|
@@ -141,12 +171,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 +257,7 @@ public void validate() { | |
check("endLine", this.endLine); | ||
check("afterEachLine", this.afterEachLine); | ||
check("firstLineDetectionPattern", this.firstLineDetectionPattern); | ||
// other line detection pattern can be null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for this comment: it's like |
||
check("lastLineDetectionPattern", this.lastLineDetectionPattern); | ||
check("isMultiline", this.isMultiline); | ||
check("allowBlankLines", this.allowBlankLines); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"}); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You would need also to have a more complete test (like |
||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/*********************** | ||
** Copyright ACME | ||
**********************/ | ||
package com.mycila.maven.plugin.license.header; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/*********************** | ||
* Copyright ACME | ||
**********************/ | ||
package com.mycila.maven.plugin.license.header; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/*********************** | ||
* Copyright ACME * | ||
***********************/ | ||
package com.mycila.maven.plugin.license.header; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove any change regarding multiline: I've correctly fixed in #247