desruisseaux commented on code in PR #422:
URL: https://github.com/apache/maven-archiver/pull/422#discussion_r4511397576


##########
src/main/java/org/apache/maven/shared/archiver/MavenArchiver.java:
##########


Review Comment:
   If the module name is specified by the user, we could be more conservative 
and accept any valid Java identifier. This was already done on the Maven 4 
branch of Maven JAR Plugin (`ToolExecutor` class). I copy here the 
implementation which was already done there:
   
   ```java
       /**
        * Sanitizes a candidate automatic module name by replacing all 
characters that are neither
        * valid Java identifier start/part characters nor {@code '.'} 
separators, then collapsing
        * consecutive separators and stripping leading/trailing dots.
        *
        * <p>Unlike the JDK's {@code ModulePath.cleanModuleName()} which is 
ASCII-only, this method
        * accepts any character that {@link Character#isJavaIdentifierStart} or
        * {@link Character#isJavaIdentifierPart} considers valid, including 
supplementary Unicode
        * code points (the encoding of {@code MANIFEST.MF} files is UTF-8).</p>
        *
        * @param  name the raw, potentially invalid module name
        * @return the sanitized name, or an empty string if nothing remains 
after sanitization
        */
       private static String cleanModuleName(final String name) {
           final int length = name.length();
           final var b = new StringBuilder(length);
           boolean wantStart = true;
           int n;
           for (int i = 0; i < length; i += n) {
               final int c = name.codePointAt(i);
               n = Character.charCount(c);
               if (wantStart) {
                   if (!Character.isJavaIdentifierStart(c)) {
                       continue;
                   }
                   wantStart = false;
               } else {
                   if (!Character.isJavaIdentifierPart(c)) {
                       wantStart = true;
                       b.append('.');
                       continue;
                   }
               }
               b.appendCodePoint(c);
           }
           int end = b.length() - 1;
           if (end >= 0 && b.charAt(end) == '.') {
               b.setLength(end);
           }
           return b.toString();
       }
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to