Template.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2014 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.wcm.commons.util;

  21. import java.util.EnumSet;
  22. import java.util.regex.Matcher;
  23. import java.util.regex.Pattern;

  24. import org.apache.commons.lang3.StringUtils;
  25. import org.jetbrains.annotations.NotNull;
  26. import org.jetbrains.annotations.Nullable;
  27. import org.osgi.annotation.versioning.ProviderType;

  28. import com.day.cq.wcm.api.NameConstants;
  29. import com.day.cq.wcm.api.Page;

  30. /**
  31.  * Template utility methods
  32.  */
  33. @ProviderType
  34. @SuppressWarnings({ "null", "java:S2589" }) // extra null checks for backward compatibility
  35. public final class Template {

  36.   static final Pattern TEMPLATE_PATH_PATTERN = Pattern.compile("^/(apps|libs)/(.+)/templates(/.*)?/([^/]+)$");

  37.   private Template() {
  38.     // static methods only
  39.   }

  40.   /**
  41.    * Gets the resource type for a given template path.
  42.    * <p>
  43.    * This is based on the assumption that:
  44.    * </p>
  45.    * <ul>
  46.    * <li>Given a template path is <code>/apps/{app_path}/templates/{optional_path}/{template_path}</code></li>
  47.    * <li>Then the resource path is at <code>{app_path}/components/{optional_path}/page/{template_path}</code></li>
  48.    * </ul>
  49.    * @param templatePath Template path
  50.    * @return Resource type path or null if template path did not match expectations
  51.    */
  52.   @SuppressWarnings("unused")
  53.   public static @Nullable String getResourceTypeFromTemplatePath(@NotNull String templatePath) {
  54.     if (templatePath == null) {
  55.       return null;
  56.     }
  57.     String resource = null;
  58.     Matcher matcher = TEMPLATE_PATH_PATTERN.matcher(templatePath);
  59.     if (matcher.matches()) {
  60.       resource = matcher.group(2) + "/components"
  61.           + StringUtils.defaultString(matcher.group(3)) + "/page/" + matcher.group(4);
  62.     }
  63.     return resource;
  64.   }

  65.   /**
  66.    * Checks if the given page uses a specific template.
  67.    * @param page AEM page
  68.    * @param templates Template(s)
  69.    * @return true if the page uses the template
  70.    */
  71.   public static boolean is(@NotNull Page page, @NotNull TemplatePathInfo @NotNull... templates) {
  72.     if (page == null || templates == null || templates.length == 0) {
  73.       return false;
  74.     }
  75.     String templatePath = page.getProperties().get(NameConstants.PN_TEMPLATE, String.class);
  76.     for (TemplatePathInfo template : templates) {
  77.       if (template.getTemplatePath().equals(templatePath)) {
  78.         return true;
  79.       }
  80.     }
  81.     return false;
  82.   }

  83.   /**
  84.    * Checks if the given page uses a specific template.
  85.    * @param page AEM page
  86.    * @param templatePaths Template path(s)
  87.    * @return true if the page uses the template
  88.    */
  89.   public static boolean is(@NotNull Page page, @NotNull String @NotNull... templatePaths) {
  90.     if (page == null || templatePaths == null || templatePaths.length == 0) {
  91.       return false;
  92.     }
  93.     String templatePath = page.getProperties().get(NameConstants.PN_TEMPLATE, String.class);
  94.     for (String givenTemplatePath : templatePaths) {
  95.       if (StringUtils.equals(templatePath, givenTemplatePath)) {
  96.         return true;
  97.       }
  98.     }
  99.     return false;
  100.   }

  101.   /**
  102.    * Lookup a template by the given template path.
  103.    * @param templatePath Path of template
  104.    * @param templates Templates
  105.    * @return The {@link TemplatePathInfo} instance or null for unknown template paths
  106.    */
  107.   public static @Nullable TemplatePathInfo forTemplatePath(@NotNull String templatePath, @NotNull TemplatePathInfo @NotNull... templates) {
  108.     if (templatePath == null || templates == null || templates.length == 0) {
  109.       return null;
  110.     }
  111.     for (TemplatePathInfo template : templates) {
  112.       if (StringUtils.equals(template.getTemplatePath(), templatePath)) {
  113.         return template;
  114.       }
  115.     }
  116.     return null;
  117.   }

  118.   /**
  119.    * Lookup a template by the given template path.
  120.    * @param templatePath Path of template
  121.    * @param templateEnums Templates
  122.    * @param <E> Template enum
  123.    * @return The {@link TemplatePathInfo} instance or null for unknown template paths
  124.    */
  125.   @SafeVarargs
  126.   public static @Nullable <E extends Enum<E> & TemplatePathInfo> TemplatePathInfo forTemplatePath(@NotNull String templatePath,
  127.       @NotNull Class<E> @NotNull... templateEnums) {
  128.     if (templatePath == null || templateEnums == null) {
  129.       return null;
  130.     }
  131.     for (Class<E> templateEnum : templateEnums) {
  132.       for (E template : EnumSet.allOf(templateEnum)) {
  133.         if (StringUtils.equals(template.getTemplatePath(), templatePath)) {
  134.           return template;
  135.         }
  136.       }
  137.     }
  138.     return null;
  139.   }

  140.   /**
  141.    * Lookup template for given page.
  142.    * @param page Page
  143.    * @param templates Templates
  144.    * @return The {@link TemplatePathInfo} instance or null for unknown template paths
  145.    */
  146.   public static @Nullable TemplatePathInfo forPage(@NotNull Page page, @NotNull TemplatePathInfo @NotNull... templates) {
  147.     if (page == null || templates == null) {
  148.       return null;
  149.     }
  150.     String templatePath = page.getProperties().get(NameConstants.PN_TEMPLATE, String.class);
  151.     if (templatePath == null) {
  152.       return null;
  153.     }
  154.     return forTemplatePath(templatePath, templates);
  155.   }

  156.   /**
  157.    * Lookup template for given page.
  158.    * @param page Page
  159.    * @param templateEnums Templates
  160.    * @param <E> Template enum
  161.    * @return The {@link TemplatePathInfo} instance or null for unknown template paths
  162.    */
  163.   @SafeVarargs
  164.   public static @Nullable <E extends Enum<E> & TemplatePathInfo> TemplatePathInfo forPage(@NotNull Page page, @NotNull Class<E> @NotNull... templateEnums) {
  165.     if (page == null || templateEnums == null) {
  166.       return null;
  167.     }
  168.     String templatePath = page.getProperties().get(NameConstants.PN_TEMPLATE, String.class);
  169.     if (templatePath == null) {
  170.       return null;
  171.     }
  172.     return forTemplatePath(templatePath, templateEnums);
  173.   }

  174. }