mbaedke commented on code in PR #2237: URL: https://github.com/apache/jackrabbit-oak/pull/2237#discussion_r2075215947
########## oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/NamespaceHelperPrototypeTest.java: ########## @@ -0,0 +1,190 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.oak.commons; + +// experimental location for prototyping namespace related support functions + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.junit.Test; + +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Locale; +import java.util.Map; +import java.util.UUID; +import java.util.function.Function; + +import static org.junit.Assert.assertEquals; + +public class NamespaceHelperPrototypeTest { + + // map with 'optimal' prefix mappings; hard-wired should be ok + private static final Map<String, String> KNOWN_PREFIXES = + Map.of("http://purl.org/dc/terms/", "dc", + "http://ns.adobe.com/exif/1.0/", "exif"); + + // public API: suggest an available prefix for the provided namespace + // (while considering pre-existing mappings) + private static @NotNull String suggestPrefix(@NotNull String namespace, + @NotNull Function<String, String> lookupPrefix) { + // try hard-wired map + String known = KNOWN_PREFIXES.get(namespace); + + // lookup using supplied mapper as well + String lookedUpNamespace = known != null ? lookupPrefix.apply(known) : null; + + // return hardwired prefix if unused or mapper has the prefix mapped to the same namespace + if (known != null && (lookedUpNamespace == null || namespace.equals(lookedUpNamespace))) { + return known; + } else { + return makeUpPrefix(namespace, lookupPrefix); + } + } + + // public API: suggest an available prefix for the provided namespace + public static @NotNull String suggestPrefix(String namespace) { + return suggestPrefix(namespace, n -> null); + } + + // suggest an available prefix for the provided namespace, based on the characters + // in the namespace name + // (while considering pre-existing mappings) + private static @NotNull String makeUpPrefix(@NotNull String namespace, + @NotNull Function<String, String> lookupNamespace) { Review Comment: Shouldn't it be rather named getRegisteredPrefix() here? -- 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: oak-dev-unsubscr...@jackrabbit.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org