You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by re...@apache.org on 2023/12/21 09:07:47 UTC

(jackrabbit) branch 2.20 updated: JCR-4983: jackrabbit-spi-commons: improve diagnostics for invalid names

This is an automated email from the ASF dual-hosted git repository.

reschke pushed a commit to branch 2.20
in repository https://gitbox.apache.org/repos/asf/jackrabbit.git


The following commit(s) were added to refs/heads/2.20 by this push:
     new 60bf2a027 JCR-4983: jackrabbit-spi-commons: improve diagnostics for invalid names
60bf2a027 is described below

commit 60bf2a027486788a6db2a0dfeb5caff14438e2a8
Author: Julian Reschke <re...@apache.org>
AuthorDate: Thu Nov 2 13:02:34 2023 +0000

    JCR-4983: jackrabbit-spi-commons: improve diagnostics for invalid names
    
    git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/trunk@1913533 13f79535-47bb-0310-9956-ffa450edef68
---
 .../spi/commons/conversion/NameParser.java         | 66 +++++++++++++++-------
 .../spi/commons/conversion/NameParserTest.java     | 16 +++++-
 2 files changed, 61 insertions(+), 21 deletions(-)

diff --git a/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/conversion/NameParser.java b/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/conversion/NameParser.java
index a33909a0c..aa7c0f877 100644
--- a/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/conversion/NameParser.java
+++ b/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/conversion/NameParser.java
@@ -52,13 +52,18 @@ public class NameParser {
      */
     public static Name parse(String jcrName, NamespaceResolver resolver, NameFactory factory)
             throws IllegalNameException, NamespaceException {
-        // trivial check
-        int len = jcrName == null ? 0 : jcrName.length();
+
+        if (jcrName == null) {
+            complainAndThrow("name is null", "");
+        }
+
+        int len = jcrName.length();
         if (len == 0) {
-            throw new IllegalNameException("empty name");
+            complainAndThrow("empty name", jcrName);
         }
+
         if (".".equals(jcrName) || "..".equals(jcrName)) {
-            throw new IllegalNameException(jcrName);
+            complainAndThrow("illegal name", jcrName);
         }
 
         // parse the name
@@ -73,36 +78,36 @@ public class NameParser {
             char c = jcrName.charAt(i);
             if (c == ':') {
                 if (state == STATE_PREFIX_START) {
-                    throw new IllegalNameException("Prefix must not be empty");
+                    complainAndThrow("Prefix must not be empty", jcrName, i);
                 } else if (state == STATE_PREFIX) {
                     if (trailingSpaces) {
-                        throw new IllegalNameException("Trailing spaces not allowed");
+                        complainAndThrow("Trailing spaces not allowed", jcrName, i);
                     }
                     prefix = jcrName.substring(0, i);
                     if (!XMLChar.isValidNCName(prefix)) {
-                        throw new IllegalNameException("Invalid name prefix: " + prefix);
+                        complainAndThrow("Invalid name prefix: " + prefix, jcrName, i);
                     }
                     state = STATE_NAME_START;
                 } else if (state == STATE_URI) {
                     // ignore -> validation of uri later on.
                 } else {
-                    throw new IllegalNameException(asDisplayableString(c) + " not allowed in name");
+                    complainAndThrow("'" + asDisplayableString(c) + "' not allowed in name", jcrName, i);
                 }
                 trailingSpaces = false;
             } else if (c == ' ') {
                 if (state == STATE_PREFIX_START || state == STATE_NAME_START) {
-                    throw new IllegalNameException(asDisplayableString(c) + " not valid name start");
+                    complainAndThrow("'" + asDisplayableString(c) + "' not valid name start", jcrName, i);
                 }
                 trailingSpaces = true;
             } else if (c == '[' || c == ']' || c == '*' || c == '|') {
-                throw new IllegalNameException(asDisplayableString(c) + " not allowed in name");
+                complainAndThrow("'" + asDisplayableString(c) + "' not allowed in name", jcrName, i);
             } else if (Character.isWhitespace(c) && c < 128) {
-                throw new IllegalNameException("Whitespace character " + asDisplayableString(c) + " not allowed in name");
+                complainAndThrow("Whitespace character '" + asDisplayableString(c) + "' not allowed in name", jcrName, i);
             } else if (c == '/') {
                 if (state == STATE_URI_START) {
                     state = STATE_URI;
                 } else if (state != STATE_URI) {
-                    throw new IllegalNameException(asDisplayableString(c) + " not allowed in name");
+                    complainAndThrow("'" + asDisplayableString(c) + "' not allowed in name", jcrName, i);
                 }
                 trailingSpaces = false;
             } else if (c == '{') {
@@ -140,10 +145,7 @@ public class NameParser {
                         state = STATE_NAME;
                         nameStart = 0;
                     } else {
-                        throw new IllegalNameException(
-                                "The URI prefix of the name " + jcrName
-                                + " is neither a valid URI nor a valid part"
-                                + " of a local name.");
+                        complainAndThrow("The URI prefix is neither a valid URI nor a valid part of a local name", jcrName);
                     }
                 } else if (state == STATE_PREFIX_START) {
                     state = STATE_PREFIX; // prefix start -> validation later on will fail.
@@ -168,14 +170,14 @@ public class NameParser {
         // take care of qualified jcrNames starting with '{' that are not having
         // a terminating '}' -> make sure there are no illegal characters present.
         if (state == STATE_URI && (jcrName.indexOf(':') > -1 || jcrName.indexOf('/') > -1)) {
-            throw new IllegalNameException("Local name may not contain ':' nor '/'");
+            complainAndThrow("Local name may not contain ':' nor '/'", jcrName);
         }
 
         if (nameStart == len || state == STATE_NAME_START) {
-            throw new IllegalNameException("Local name must not be empty");
+            complainAndThrow("Local name must not be empty", jcrName);
         }
         if (trailingSpaces) {
-            throw new IllegalNameException("Trailing spaces not allowed");
+            complainAndThrow("Trailing spaces not allowed", jcrName);
         }
 
         // if namespace is null, this is just a check for format. this can only
@@ -206,11 +208,37 @@ public class NameParser {
             return "\\r";
         } else if (c == '\t') {
             return "\\t";
+        } else if (c == '\'') {
+            return "\\'";
+        } else if (c == '"') {
+            return "\\\"";
         } else {
             return String.format("\\u%04x", (int) c);
         }
     }
 
+    private static String formatNameForDisplay(String name) {
+        StringBuilder b = new StringBuilder();
+        for (int i = 0; i < name.length(); i++) {
+            b.append(asDisplayableString(name.charAt(i)));
+        }
+        return b.toString();
+    }
+
+    private static void complainAndThrow(String reason, String name) throws IllegalNameException {
+        complainAndThrow(reason, name, -1);
+    }
+
+    private static void complainAndThrow(String reason, String name, int index) throws IllegalNameException{
+        String msg;
+        if (index == -1) {
+            msg = String.format("%s (name: \"%s\")", reason, formatNameForDisplay(name));
+        } else {
+            msg = String.format("%s (name: \"%s\", at position: %d)", reason, formatNameForDisplay(name), index);
+        }
+        throw new IllegalNameException(msg);
+    }
+
     /**
      * Parses an array of <code>jcrName</code> and returns the respective
      * array of <code>Name</code>.
diff --git a/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/conversion/NameParserTest.java b/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/conversion/NameParserTest.java
index 6dc0ec235..d2cfee6e5 100644
--- a/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/conversion/NameParserTest.java
+++ b/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/conversion/NameParserTest.java
@@ -241,12 +241,24 @@ public class NameParserTest extends TestCase {
         }
     }
 
-    public void testMessage() {
+    public void testMessageTab() {
         try {
             NameParser.checkFormat("horizontal\ttab");
             fail("should fail with IllegalNameException");
         } catch (IllegalNameException ex) {
-            assertTrue("message should contain '\\t'", ex.getMessage().indexOf("\\t") >= 0);
+            assertTrue("message should contain '\\t', was: >>>" + ex.getMessage() + "<<<", ex.getMessage().indexOf("\\t") >= 0);
+            assertTrue("message should contain 'horizontal', was: >>>" + ex.getMessage() + "<<<",
+                    ex.getMessage().indexOf("horizontal") >= 0);
+        }
+    }
+
+    public void testMessageWithNonAscii() {
+        try {
+            NameParser.checkFormat("\uD83D\uDCA9[]");
+            fail("should fail with IllegalNameException");
+        } catch (IllegalNameException ex) {
+            assertTrue("message should contain '\\ud83d\\udca9', was: >>>" + ex.getMessage() + "<<<",
+                    ex.getMessage().indexOf("\\ud83d\\udca9") >= 0);
         }
     }