From 9e50f0fb2f8bea2bed2ae1535091bfecfa1e944a Mon Sep 17 00:00:00 2001
From: Gaetan Boismal <gaetan.boismal@forgerock.com>
Date: Wed, 24 Feb 2016 16:21:31 +0000
Subject: [PATCH] OPENDJ-2689 Improve copyright-maven-plugin

---
 opendj-sdk/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/UpdateCopyrightMojo.java   |   37 +++-----
 opendj-sdk/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/CopyrightAbstractMojo.java |  183 +++++++++++++++++++++++++++++++--------------
 2 files changed, 138 insertions(+), 82 deletions(-)

diff --git a/opendj-sdk/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/CopyrightAbstractMojo.java b/opendj-sdk/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/CopyrightAbstractMojo.java
index 19adfc0..f76d085 100644
--- a/opendj-sdk/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/CopyrightAbstractMojo.java
+++ b/opendj-sdk/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/CopyrightAbstractMojo.java
@@ -92,14 +92,10 @@
 
     /** The file extensions to test. */
     public static final List<String> CHECKED_EXTENSIONS = Arrays.asList(
-            "bat", "c", "h", "html", "java", "ldif", "Makefile", "mc", "properties", "sh", "txt", "xml", "xsd", "xsl");
-
-    private static final List<String> EXCLUDED_END_COMMENT_BLOCK_TOKEN = Arrays.asList("*/", "-->");
-    private static final List<String> SUPPORTED_COMMENT_MIDDLE_BLOCK_TOKEN = Arrays.asList("*", "#", "rem", "!");
-    private static final List<String> SUPPORTED_START_BLOCK_COMMENT_TOKEN = Arrays.asList("/*", "<!--");
+            "bat", "c", "fml", "h", "html", "java", "java.stub", "ldif", "mc",
+            "md", "properties", "security", "sh", "txt", "xjb", "xml", "xml.vm", "xsd", "xsl");
 
     private final class CustomGitExeScmProvider extends GitExeScmProvider {
-
         @Override
         protected GitCommand getDiffCommand() {
             return new CustomGitDiffCommand();
@@ -107,7 +103,6 @@
     }
 
     private class CustomGitDiffCommand extends GitDiffCommand implements GitCommand {
-
         @Override
         protected DiffScmResult executeDiffCommand(ScmProviderRepository repo, ScmFileSet fileSet,
                 ScmVersion unused, ScmVersion unused2) throws ScmException {
@@ -125,6 +120,118 @@
 
     }
 
+    private static String resolveExtension(final String filePath) {
+        int firstPeriodPos = filePath.indexOf('.');
+        if (firstPeriodPos > 0) {
+            return filePath.substring(firstPeriodPos + 1);
+        }
+        return "";
+    }
+
+    enum CommentParser {
+        BAT("rem"),
+        DEFAULT("#", "*", "!", "//"),
+        JAVA("/*", "*/", Arrays.asList("*", "//", "")),
+        MC(";"),
+        SH("#"),
+        XML("<!--", "-->", Arrays.asList("!", "~", ""));
+
+        static CommentParser createParserForFile(final String filePath) {
+            switch (resolveExtension(filePath)) {
+            case "java":
+            case "c":
+            case "h":
+                return JAVA;
+            case "fml":
+            case "html":
+            case "md":
+            case "xjb":
+            case "xml":
+            case "xml.vm":
+            case "xsd":
+            case "xsl":
+                return XML;
+            case "bat":
+                return BAT;
+            case "security":
+            case "sh":
+            case "ldif":
+            case "properties":
+                return SH;
+            case "mc":
+                return MC;
+            default:
+                return DEFAULT;
+            }
+        }
+
+        private final String startBlock;
+        private final String endBlock;
+        private final List<String> middleBlockTokens;
+        private String currentLine = "";
+        private boolean supportCommentBlocks;
+        private boolean commentBlockOpened;
+
+        CommentParser(final String... middleBlockTokens) {
+            this(null, null, Arrays.asList(middleBlockTokens));
+        }
+
+        CommentParser(final String startBlock, final String endBlock, final List<String> middleBlockTokens) {
+            this.startBlock = startBlock;
+            this.endBlock = endBlock;
+            this.middleBlockTokens = middleBlockTokens;
+            this.supportCommentBlocks = startBlock != null && endBlock != null;
+        }
+
+        void consumeLine(final String line) {
+            if (line != null) {
+                if (supportCommentBlocks && currentLine.endsWith(endBlock)) {
+                    commentBlockOpened = false;
+                }
+                currentLine = line.trim();
+                if (supportCommentBlocks && currentLine.startsWith(startBlock)) {
+                    commentBlockOpened = true;
+                }
+            }
+        }
+
+        boolean isCommentLine() {
+            return commentBlockOpened
+                    || startsWithCommentLineToken();
+        }
+
+        private boolean startsWithCommentLineToken() {
+            return getCommentTokenInLine(currentLine) != null;
+        }
+
+        private String getCommentTokenInLine(final String line) {
+            for (final String token : middleBlockTokens) {
+                if (line.startsWith(token)) {
+                    return token;
+                }
+            }
+            return null;
+        }
+
+        String getNewCommentedLine(final String line) throws Exception {
+            final String commentToken = getCommentTokenInLine(line.trim());
+            if (commentToken == null) {
+                throw new Exception("Incompatibles comments lines in the file.");
+            }
+
+            String resultLine = "";
+            if (commentToken.isEmpty()) {
+                resultLine = " ";
+            }
+            return resultLine + line.substring(0, line.indexOf(commentToken) + commentToken.length());
+        }
+
+        boolean isNonEmptyCommentedLine() {
+            final String commentToken = getCommentTokenInLine(currentLine);
+            return commentToken == null || !commentToken.equals(currentLine);
+        }
+    }
+
     /** The string representation of the current year. */
     Integer currentYear = Calendar.getInstance().get(Calendar.YEAR);
 
@@ -262,20 +369,12 @@
     /** Examines the provided files list to determine whether each changed file copyright is acceptable. */
     void checkCopyrights() throws MojoExecutionException, MojoFailureException {
         for (final File changedFile : getChangedFiles()) {
-            if (!changedFile.exists() || !changedFile.isFile()) {
-                continue;
-            }
-
-            final String changedFileName = changedFile.getPath();
-            int lastPeriodPos = changedFileName.lastIndexOf('.');
-            if (lastPeriodPos > 0) {
-                String extension = changedFileName.substring(lastPeriodPos + 1);
-                if (!CHECKED_EXTENSIONS.contains(extension.toLowerCase())) {
-                    continue;
-                }
-            } else if (fileNameEquals("bin", changedFile.getParentFile())
-                    && fileNameEquals("resource", changedFile.getParentFile().getParentFile())) {
-                // ignore resource/bin directory.
+            if (!changedFile.exists()
+                    || !changedFile.isFile()
+                    || !CHECKED_EXTENSIONS.contains(resolveExtension(changedFile.getPath()).toLowerCase())
+                    || (fileNameEquals("bin", changedFile.getParentFile())
+                           && fileNameEquals("resource", changedFile.getParentFile().getParentFile()))) {
+                // Verify that the file must be checked (ignore bin/resource directory)
                 continue;
             }
 
@@ -295,11 +394,12 @@
      */
     private boolean checkCopyrightForFile(File changedFile) throws MojoExecutionException {
         try (BufferedReader reader = new BufferedReader(new FileReader(changedFile))) {
+            final CommentParser commentParser = CommentParser.createParserForFile(changedFile.getPath());
             String line;
             while ((line = reader.readLine()) != null) {
-                String lowerLine = line.toLowerCase().trim();
-                if (isCommentLine(lowerLine)
-                        && lowerLine.contains("copyright")
+                commentParser.consumeLine(line);
+                if (commentParser.isCommentLine()
+                        && line.toLowerCase().trim().contains("copyright")
                         && line.contains(currentYear.toString())
                         && line.contains(copyrightOwnerToken)) {
                     return true;
@@ -312,39 +412,4 @@
                     + " to check copyright date. No further copyright date checking will be performed.");
         }
     }
-
-    private String getCommentToken(String line, boolean includesStartBlock) {
-        final List<String> supportedTokens = new ArrayList<>(SUPPORTED_COMMENT_MIDDLE_BLOCK_TOKEN);
-        if (includesStartBlock) {
-            supportedTokens.addAll(SUPPORTED_START_BLOCK_COMMENT_TOKEN);
-        }
-
-        if (trimmedLineStartsWith(line, EXCLUDED_END_COMMENT_BLOCK_TOKEN) != null) {
-            return null;
-        }
-
-        return trimmedLineStartsWith(line, supportedTokens);
-    }
-
-    private String trimmedLineStartsWith(String line, List<String> supportedTokens) {
-        for (String token : supportedTokens) {
-            if (line.trim().startsWith(token)) {
-                return token;
-            }
-        }
-        return null;
-    }
-
-    boolean isNonEmptyCommentedLine(String line) {
-        String commentToken = getCommentTokenInBlock(line);
-        return commentToken == null || !commentToken.equals(line.trim());
-    }
-
-    String getCommentTokenInBlock(String line) {
-        return getCommentToken(line, false);
-    }
-
-    boolean isCommentLine(String line) {
-        return getCommentToken(line, true) != null;
-    }
 }
diff --git a/opendj-sdk/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/UpdateCopyrightMojo.java b/opendj-sdk/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/UpdateCopyrightMojo.java
index a362ae3..a3d6abc 100644
--- a/opendj-sdk/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/UpdateCopyrightMojo.java
+++ b/opendj-sdk/opendj-copyright-maven-plugin/src/main/java/org/forgerock/maven/UpdateCopyrightMojo.java
@@ -106,8 +106,8 @@
         private boolean commentBlockEnded;
         private boolean portionsCopyrightNeeded;
         private boolean copyrightSectionPresent;
+        private CommentParser commentParser;
         private String curLine;
-        private String curLowerLine;
         private Integer startYear;
         private Integer endYear;
         private final BufferedReader reader;
@@ -116,6 +116,7 @@
         private UpdateCopyrightFile(String filePath) throws IOException {
             this.filePath = filePath;
             reader = new BufferedReader(new FileReader(filePath));
+            commentParser = CommentParser.createParserForFile(filePath);
             final File tmpFile = new File(filePath + ".tmp");
             if (!tmpFile.exists()) {
                 tmpFile.createNewFile();
@@ -174,12 +175,17 @@
                 previousLine = bufferedLines.get(indexAdd);
             }
             indexAdd++;
+            final String newCommentedLine = commentParser.getNewCommentedLine(previousLine);
             if (!portionsCopyrightNeeded) {
                 for (int i = 0; i < nbLinesToSkip; i++) {
-                    bufferedLines.add(indexAdd++, getNewCommentedLine());
+                    if (!bufferedLines.get(indexAdd).equals(newCommentedLine)) {
+                        // We have a blank line already so do not add a new one.
+                        bufferedLines.add(indexAdd, newCommentedLine);
+                    }
+                    indexAdd++;
                 }
             }
-            final String newCopyrightLine = getNewCommentedLine()
+            final String newCopyrightLine = newCommentedLine
                     + indent() + (portionsCopyrightNeeded ? newPortionsCopyrightLabel : newCopyrightLabel)
                     + " " + currentYear + " " + forgeRockCopyrightLabel;
             bufferedLines.add(indexAdd, newCopyrightLine);
@@ -219,7 +225,7 @@
             nextLine();
             while (curLine != null) {
                 if (curLineMatches(lineBeforeCopyrightCompiledRegExp)) {
-                    if (!isCommentLine(curLowerLine)) {
+                    if (!commentParser.isCommentLine()) {
                         throw new Exception("The line before copyright token must be a commented line");
                     }
                     lineBeforeCopyrightReaded = true;
@@ -236,7 +242,7 @@
             while (curLine != null) {
                 if (isOldCopyrightOwnerLine()) {
                     return true;
-                } else if (isNonEmptyCommentedLine(curLine)
+                } else if (commentParser.isNonEmptyCommentedLine()
                             || isCopyrightLine()
                             || commentBlockEnded) {
                     return false;
@@ -250,7 +256,7 @@
             while (curLine != null) {
                 if (isCopyrightLine()) {
                     return true;
-                } else if ((isNonEmptyCommentedLine(curLine) && !isOldCopyrightOwnerLine())
+                } else if ((commentParser.isNonEmptyCommentedLine() && !isOldCopyrightOwnerLine())
                             || commentBlockEnded) {
                     return false;
                 }
@@ -277,6 +283,7 @@
 
         private void nextLine() throws Exception {
             curLine = reader.readLine();
+            commentParser.consumeLine(curLine);
             if (curLine == null && !copyrightUpdated) {
                 throw new Exception("unexpected end of file while trying to read copyright");
             } else  if (curLine != null) {
@@ -284,27 +291,11 @@
             }
 
             if (!copyrightUpdated) {
-                curLowerLine = curLine.trim().toLowerCase();
-                if (lineBeforeCopyrightReaded && !isCommentLine(curLowerLine)) {
+                if (lineBeforeCopyrightReaded && !commentParser.isCommentLine()) {
                     commentBlockEnded = true;
                 }
             }
         }
-
-        private String getNewCommentedLine() throws Exception {
-            int indexCommentToken = 1;
-            String commentToken = null;
-            String linePattern = null;
-            while (bufferedLines.size() > indexCommentToken && commentToken == null) {
-                linePattern = bufferedLines.get(indexCommentToken++);
-                commentToken = getCommentTokenInBlock(linePattern);
-            }
-            if (commentToken == null) {
-                throw new Exception("Incompatibles comments lines in the file.");
-            }
-            return linePattern.substring(0, linePattern.indexOf(commentToken) + 1);
-        }
-
     }
 
     private static final Pattern OLD_COPYRIGHT_REGEXP = Pattern.compile(".*copyright.*", CASE_INSENSITIVE);

--
Gitblit v1.10.0