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 2025/07/24 16:29:56 UTC
(jackrabbit) branch 2.22 updated: JCR-5152 Add method to check if a (local) name is valid according to JCR spec
This is an automated email from the ASF dual-hosted git repository.
reschke pushed a commit to branch 2.22
in repository https://gitbox.apache.org/repos/asf/jackrabbit.git
The following commit(s) were added to refs/heads/2.22 by this push:
new 0b8185024 JCR-5152 Add method to check if a (local) name is valid according to JCR spec
0b8185024 is described below
commit 0b818502436cb8f8f3b76aabe2367b69f813d227
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Tue Jun 10 20:51:17 2025 +0200
JCR-5152 Add method to check if a (local) name is valid according to JCR
spec
---
.../main/java/org/apache/jackrabbit/util/Text.java | 32 ++++++++++++++++++++++
.../org/apache/jackrabbit/util/package-info.java | 2 +-
.../java/org/apache/jackrabbit/util/TextTest.java | 10 +++++++
3 files changed, 43 insertions(+), 1 deletion(-)
diff --git a/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java b/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java
index 0b860bc90..b6afc77c1 100644
--- a/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java
+++ b/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java
@@ -24,6 +24,7 @@ import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Properties;
+import java.util.Set;
/**
* This Class provides some text related utilities
@@ -41,6 +42,8 @@ public class Text {
*/
public static final char[] hexTable = "0123456789abcdef".toCharArray();
+ private static final Set<Character> INVALID_JCR_LOCAL_NAME_CHARS = Set.of( '/', ':', '[', ']', '*', '|');
+
/**
* Calculate an MD5 hash of the string given.
*
@@ -466,6 +469,10 @@ public class Text {
* char ::= nonspace | ' '
* nonspace ::= (* Any Unicode character except: '/', ':', '[', ']', '*', '|' or any whitespace character *)
* </pre>
+ * <p>
+ * Note that just using this method does not necessarily return a string which is a
+ * <a href="https://s.apache.org/jcr-2.0-spec/3_Repository_Model.html#3.2.2%20Local%20Names">valid local name</a>.
+ * You still have to take care of invalid <a href="https://www.w3.org/TR/xml/#NT-Char">XML characters</a>.
*
* @param name the name to escape
* @return the escaped name
@@ -567,6 +574,31 @@ public class Text {
return buffer.toString();
}
+ /**
+ * Checks if the given name is a valid JCR local name.
+ * <p>
+ * Note that the return value of {@link #escapeIllegalJcrChars(String)} is not necessarily a valid local name.
+ * You still have to take care of invalid <a href="https://www.w3.org/TR/xml/#NT-Char">XML characters</a>.
+ *
+ * @param localName the string value to check
+ * @return <code>true</code> if the name is valid, <code>false</code> otherwise.
+ * @see <a href="https://s.apache.org/jcr-2.0-spec/3_Repository_Model.html#3.2.2%20Local%20Names">JCR 2.0 Spec, ยง3.2.2 Local Names</a>
+ * @see #escapeIllegalJcrChars(String)
+ * @since 2.6.0 (Apache Jackrabbit 2.24.0)
+ */
+ public static boolean isValidJcrLocalName(String localName) {
+ if (localName == null || localName.isEmpty()) {
+ return false;
+ }
+ // self or parent are invalid
+ if (localName.equals(".") || localName.equals("..")) {
+ return false;
+ }
+ return localName.chars().noneMatch(c ->
+ INVALID_JCR_LOCAL_NAME_CHARS.contains((char) c) || !XMLChar.isValid(c)
+ );
+ }
+
/**
* Returns the name part of the path. If the given path is already a name
* (i.e. contains no slashes) it is returned.
diff --git a/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/package-info.java b/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/package-info.java
index ebb069361..ccfc068b2 100644
--- a/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/package-info.java
+++ b/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/package-info.java
@@ -14,5 +14,5 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-@org.osgi.annotation.versioning.Version("2.5.0")
+@org.osgi.annotation.versioning.Version("2.6.0")
package org.apache.jackrabbit.util;
diff --git a/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java b/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java
index 1375aff26..6d034c383 100644
--- a/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java
+++ b/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java
@@ -202,4 +202,14 @@ public class TextTest extends TestCase {
public void testEscapeHTML() {
assertEquals("&<>'"", Text.encodeIllegalHTMLCharacters("&<>'\""));
}
+
+ public void testIsValidJcrLocalName() {
+ assertTrue(Text.isValidJcrLocalName("valid%Name..\n\r test.\"'"));
+ assertFalse(Text.isValidJcrLocalName("invalid|name"));
+ assertFalse(Text.isValidJcrLocalName("some:name"));
+ // containing non XML characters (unicode control character)
+ assertFalse(Text.isValidJcrLocalName("\u000FinvalidName"));
+ assertFalse(Text.isValidJcrLocalName(".."));
+ assertFalse(Text.isValidJcrLocalName("."));
+ }
}