You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by jl...@apache.org on 2017/10/04 21:15:00 UTC

incubator-netbeans-tools git commit: Allowing the license header to be in the second comment in the file, increasing the limit where it must start to 300.

Repository: incubator-netbeans-tools
Updated Branches:
  refs/heads/master 0ff2a41d4 -> 4ca5ac77e


Allowing the license header to be in the second comment in the file, increasing the limit where it must start to 300.

Project: http://git-wip-us.apache.org/repos/asf/incubator-netbeans-tools/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-netbeans-tools/commit/4ca5ac77
Tree: http://git-wip-us.apache.org/repos/asf/incubator-netbeans-tools/tree/4ca5ac77
Diff: http://git-wip-us.apache.org/repos/asf/incubator-netbeans-tools/diff/4ca5ac77

Branch: refs/heads/master
Commit: 4ca5ac77ecdf96297f91c8394409d825b5b98c13
Parents: 0ff2a41
Author: Jan Lahoda <jl...@netbeans.org>
Authored: Wed Oct 4 23:12:37 2017 +0200
Committer: Jan Lahoda <jl...@netbeans.org>
Committed: Wed Oct 4 23:12:37 2017 +0200

----------------------------------------------------------------------
 convert/src/convert/CategorizeLicenses.java     | 23 ++++++++++++++++----
 .../test/convert/CategorizeLicensesTest.java    | 14 ++++++++++++
 2 files changed, 33 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-netbeans-tools/blob/4ca5ac77/convert/src/convert/CategorizeLicenses.java
----------------------------------------------------------------------
diff --git a/convert/src/convert/CategorizeLicenses.java b/convert/src/convert/CategorizeLicenses.java
index 7f303a8..98bfd35 100644
--- a/convert/src/convert/CategorizeLicenses.java
+++ b/convert/src/convert/CategorizeLicenses.java
@@ -158,14 +158,23 @@ public class CategorizeLicenses {
               .orElse(null);
     }
 
-    private static Description snipLicense(String code, String commentStart, String commentEnd, String normalizeLines, CommentType commentType) {
+    public static Description snipLicense(String code, String commentStart, String commentEnd, String normalizeLines, CommentType commentType) {
         Matcher startM = Pattern.compile(commentStart).matcher(code);
-        if (!startM.find() || startM.start() > 100) //only first 100 characters
+        if (!startM.find() || startM.start() > LIMIT) //only first 150 characters
             return null;
         Matcher endM = Pattern.compile(commentEnd).matcher(code);
         if (!endM.find(startM.end()))
             return null;
         String lic = code.substring(startM.end(), endM.start());
+        if (!isLicenseText(lic)) {
+            startM = Pattern.compile(commentStart).matcher(code);
+            if (!startM.find(endM.end()) || startM.start() > LIMIT) //only first 150 characters
+                return null;
+            endM = Pattern.compile(commentEnd).matcher(code);
+            if (!endM.find(startM.end()))
+                return null;
+            lic = code.substring(startM.end(), endM.start());
+        }
         if (normalizeLines != null) {
             lic = Arrays.stream(lic.split("\n"))
                         .map(l -> l.replaceAll(normalizeLines, ""))
@@ -173,7 +182,9 @@ public class CategorizeLicenses {
         }
         return createUnifiedDescriptionOrNull(startM.start(), endM.end(), lic, commentType);
     }
-    
+
+    private static final int LIMIT = 300;
+
     public static Description snipLicenseBundle(String code, String firstLinePattern, String commentMarker, CommentType commentType) {
         StringBuilder res = new StringBuilder();
         boolean firstLine = true;
@@ -212,7 +223,7 @@ public class CategorizeLicenses {
     }
 
     private static Description createUnifiedDescriptionOrNull(int start, int end, String lic, CommentType commentType) {
-        if (lic != null && (lic.contains("CDDL") || lic.contains("Redistribution") || lic.contains("Apache License"))) {
+        if (lic != null && isLicenseText(lic)) {
             if (start == (-1)) {
                 throw new IllegalStateException();
             }
@@ -229,6 +240,10 @@ public class CategorizeLicenses {
         return null;
     }
 
+    private static boolean isLicenseText(String text) {
+        return text.contains("CDDL") || text.contains("Redistribution") || text.contains("Apache License");
+    }
+
     public static class Description {
         public final int start;
         public final int end;

http://git-wip-us.apache.org/repos/asf/incubator-netbeans-tools/blob/4ca5ac77/convert/test/convert/CategorizeLicensesTest.java
----------------------------------------------------------------------
diff --git a/convert/test/convert/CategorizeLicensesTest.java b/convert/test/convert/CategorizeLicensesTest.java
index 960b2a3..cc6c74a 100644
--- a/convert/test/convert/CategorizeLicensesTest.java
+++ b/convert/test/convert/CategorizeLicensesTest.java
@@ -48,4 +48,18 @@ public class CategorizeLicensesTest {
         assertEquals("#CDDL\n#\n#lic\n", code.substring(desc.start, desc.end));
     }
 
+    @Test
+    public void testSecondComment() {
+        final String code = "<!--first-->\n" +
+                            "<!--CDDL-->\n" +
+                            "\n";
+        CategorizeLicenses.Description desc =
+                CategorizeLicenses.snipLicense(code,
+                                               "<!--",
+                                               "-->",
+                                               null,
+                                               CategorizeLicenses.CommentType.XML);
+        assertEquals("CDDL", desc.header);
+    }
+
 }