matthiasblaesing commented on code in PR #9310:
URL: https://github.com/apache/netbeans/pull/9310#discussion_r3011918276


##########
platform/core.netigso/src/org/netbeans/core/netigso/Netigso.java:
##########
@@ -291,7 +291,8 @@ protected Set<String> createLoader(ModuleInfo m, 
ProxyClassLoader pcl, File jar)
                         }
                     }
                     if (exported instanceof String) {
-                        for (String p : exported.toString().split(",")) { // 
NOI18N
+                        String regexp = ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"; 
// NOI18N

Review Comment:
   The intention here is, that quotes must come in pairs? I see two problems 
here:
   
   1. This is expensive. For each comma found in the string the whole pair 
matching will happen again and again.
   2. This breaks the moment `\"` is present in the string.
   
   The syntax for the `Export-Package` is given in the OSGI specification:
   
   - Base definitions: 
https://docs.osgi.org/specification/osgi.core/7.0.0/ch01.html#framework.general.syntax
   - Definiton for "Export-Package": 
https://docs.osgi.org/specification/osgi.core/7.0.0/framework.module.html#framework.module.exportpackage
   
   Crucial here is the definition of `quoted-string`. This explicitly allows 
`\"` in a quoted string. This makes sense as otherwise you could not express 
quotes in directives/attributes.
   
   A linear scan of the string with a look ahead of one char should be able to 
do this. Compare the two approaches:
   
   ```java
   
   import java.util.ArrayList;
   import java.util.Arrays;
   import java.util.List;
   import java.util.regex.Pattern;
   
   public class Test2 {
   
       public static void main() {
           String regexp = ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)";
           String test = "x.y.z,a.b.c;a=\"\\\"\",d.e.f";
           Pattern matcher = Pattern.compile(regexp);
           String[] result = matcher.split("x.y.z,a.b.c;a=\"\\\"\",d.e.f");
           System.out.println(result.length);
           System.out.println(Arrays.toString(result));
   
           System.out.println("======");
   
           List<String> elements = new ArrayList<>();
           StringBuilder buffer = new StringBuilder();
           boolean inQuotedString = false;
           for (int i = 0; i < test.length(); i++) {
               char nextChar = test.charAt(i);
               switch(nextChar) {
                   case '"' -> {
                       inQuotedString = ! inQuotedString;
                       buffer.append(nextChar);
                   }
                   case ',' -> {
                       if (inQuotedString) {
                           buffer.append(nextChar);
                       } else {
                           elements.add(buffer.toString());
                           buffer.setLength(0);
                       }
                   }
                   case '\\' -> {
                       buffer.append(nextChar);
                       if (inQuotedString) {
                           if((i + 1) == test.length()) {
                               throw new IllegalStateException("Invalid escape 
sequence");
                           }
                           nextChar = test.charAt(i + 1);
                           i++;
                           if (nextChar == '"' || nextChar == '\\') {
                               buffer.append(nextChar);
                           } else {
                               throw new IllegalStateException("Invalid escape 
sequence");
                           }
                       }
                   }
                   default -> buffer.append(nextChar);
               }
           }
           if(! buffer.isEmpty()){
               elements.add(buffer.toString());
           }
   
           System.out.println(elements.size());
           System.out.println(elements);
       }
   }
   ```
   
   Given that this broke (I agree with your assessment on that), we need a test 
to check parsing. I suggest to pull the parsing of the `Export-Package` entry 
into a package visible static method, that can trivially be tested.
   



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to