NameUtil.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2020 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.tooling.commons.contentpackagebuilder;

  21. import java.util.regex.Pattern;

  22. import org.apache.commons.lang3.StringUtils;
  23. import org.jetbrains.annotations.Nullable;

  24. /**
  25.  * Validates JCR names.
  26.  */
  27. final class NameUtil {

  28.   // list of chars that are invalid for JCR names (from org.apache.jackrabbit.util.Text)
  29.   private static final String ILLEGAL_CHARS = "%/[]*|\t\r\n";

  30.   // we allow a single colon (:) as it separates the namespace
  31.   private static final char NAMESPACE_SEPARATOR = ':';

  32.   private static final Pattern ILLEGAL_CHARS_PATTERN;
  33.   static {
  34.     StringBuilder sb = new StringBuilder();
  35.     sb.append("(");
  36.     for (int i = 0; i < ILLEGAL_CHARS.length(); i++) {
  37.       if (i > 0) {
  38.         sb.append("|");
  39.       }
  40.       sb.append(Pattern.quote(ILLEGAL_CHARS.substring(i, i + 1)));
  41.     }
  42.     sb.append(")");
  43.     ILLEGAL_CHARS_PATTERN = Pattern.compile(sb.toString());
  44.   }

  45.   private NameUtil() {
  46.     // static methods only
  47.   }

  48.   /**
  49.    * Checks if the name is a valid JCR name.
  50.    * @param name Name
  51.    * @return true if valid
  52.    */
  53.   public static boolean isValidName(@Nullable String name) {
  54.     if (StringUtils.isEmpty(name)) {
  55.       return false;
  56.     }
  57.     int numberOfColons = StringUtils.countMatches(name, NAMESPACE_SEPARATOR);
  58.     if (numberOfColons > 1) {
  59.       return false;
  60.     }
  61.     return !ILLEGAL_CHARS_PATTERN.matcher(name).find();
  62.   }

  63.   /**
  64.    * Ensures that all parts of the path are valid JCR names.
  65.    * @param path Path.
  66.    */
  67.   public static void ensureValidPath(String path) {
  68.     String relativePath;
  69.     if (StringUtils.startsWith(path, "/")) {
  70.       relativePath = path.substring(1);
  71.     }
  72.     else {
  73.       relativePath = path;
  74.     }
  75.     String[] pathParts = StringUtils.split(relativePath, "/");
  76.     for (String pathPart : pathParts) {
  77.       if (!isValidName(pathPart)) {
  78.         throw new IllegalArgumentException("Path contains illegal node names: " + path);
  79.       }
  80.     }
  81.   }

  82. }