gt:         return key -> {
context: /*
   * This library is part of OpenCms -
   * the Open Source Content Management System
   *
   * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * For further information about Alkacon Software, please see the
   * company website: http://www.alkacon.com
   *
   * For further information about OpenCms, please see the
   * project website: http://www.opencms.org
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   */
  
  package org.opencms.ade.containerpage;
  
  import org.opencms.ade.configuration.CmsADEConfigData;
  import org.opencms.ade.configuration.CmsElementView;
  import org.opencms.ade.configuration.CmsResourceTypeConfig;
  import org.opencms.ade.containerpage.inherited.CmsContainerConfigurationCache;
  import org.opencms.ade.containerpage.inherited.CmsInheritanceReference;
  import org.opencms.ade.containerpage.inherited.CmsInheritanceReferenceParser;
  import org.opencms.ade.containerpage.inherited.CmsInheritedContainerState;
  import org.opencms.ade.containerpage.shared.CmsContainer;
  import org.opencms.ade.containerpage.shared.CmsContainerElement;
  import org.opencms.ade.containerpage.shared.CmsContainerElement.ModelGroupState;
  import org.opencms.ade.containerpage.shared.CmsContainerElementData;
  import org.opencms.ade.containerpage.shared.CmsElementLockInfo;
  import org.opencms.ade.containerpage.shared.CmsElementSettingsConfig;
  import org.opencms.ade.containerpage.shared.CmsFormatterConfig;
  import org.opencms.ade.containerpage.shared.CmsFormatterConfigCollection;
  import org.opencms.ade.containerpage.shared.CmsInheritanceInfo;
  import org.opencms.ade.detailpage.CmsDetailPageResourceHandler;
  import org.opencms.configuration.CmsConfigurationException;
  import org.opencms.db.CmsResourceState;
  import org.opencms.file.CmsFile;
  import org.opencms.file.CmsObject;
  import org.opencms.file.CmsRequestContext;
  import org.opencms.file.CmsResource;
  import org.opencms.file.CmsResourceFilter;
  import org.opencms.file.types.CmsResourceTypeXmlContainerPage;
  import org.opencms.file.types.CmsResourceTypeXmlContent;
  import org.opencms.file.types.I_CmsResourceType;
  import org.opencms.gwt.CmsDefaultResourceStatusProvider;
  import org.opencms.gwt.CmsIconUtil;
  import org.opencms.gwt.shared.CmsAdditionalInfoBean;
  import org.opencms.gwt.shared.CmsPermissionInfo;
  import org.opencms.i18n.CmsMessageContainer;
  import org.opencms.i18n.CmsMessages;
  import org.opencms.jsp.util.CmsJspStandardContextBean;
  import org.opencms.jsp.util.CmsJspStandardContextBean.TemplateBean;
  import org.opencms.loader.CmsTemplateContextManager;
  import org.opencms.lock.CmsLock;
  import org.opencms.main.CmsException;
  import org.opencms.main.CmsLog;
  import org.opencms.main.OpenCms;
  import org.opencms.relations.CmsRelation;
  import org.opencms.relations.CmsRelationFilter;
  import org.opencms.relations.CmsRelationType;
  import org.opencms.search.galleries.CmsGallerySearch;
  import org.opencms.search.galleries.CmsGallerySearchResult;
  import org.opencms.util.CmsDateUtil;
  import org.opencms.util.CmsStringUtil;
  import org.opencms.util.CmsUUID;
  import org.opencms.workplace.CmsWorkplaceMessages;
  import org.opencms.workplace.editors.CmsWorkplaceEditorManager;
  import org.opencms.workplace.editors.directedit.CmsAdvancedDirectEditProvider;
  import org.opencms.workplace.editors.directedit.CmsDirectEditMode;
  import org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider;
  import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
  import org.opencms.workplace.explorer.CmsResourceUtil;
  import org.opencms.xml.containerpage.CmsADESessionCache;
  import org.opencms.xml.containerpage.CmsContainerBean;
  import org.opencms.xml.containerpage.CmsContainerElementBean;
  import org.opencms.xml.containerpage.CmsContainerPageBean;
  import org.opencms.xml.containerpage.CmsFormatterConfiguration;
  import org.opencms.xml.containerpage.CmsGroupContainerBean;
  import org.opencms.xml.containerpage.CmsXmlContainerPage;
  import org.opencms.xml.containerpage.CmsXmlContainerPageFactory;
  import org.opencms.xml.containerpage.CmsXmlGroupContainer;
  import org.opencms.xml.containerpage.CmsXmlGroupContainerFactory;
  import org.opencms.xml.containerpage.I_CmsFormatterBean;
  import org.opencms.xml.content.CmsXmlContent;
  import org.opencms.xml.content.CmsXmlContentFactory;
  import org.opencms.xml.content.CmsXmlContentProperty;
  import org.opencms.xml.content.CmsXmlContentPropertyHelper;
  
  import java.io.IOException;
  import java.text.DateFormat;
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Collections;
  import java.util.Date;
  import java.util.HashMap;
  import java.util.LinkedHashMap;
  import java.util.LinkedHashSet;
  import java.util.List;
  import java.util.Locale;
  import java.util.Map;
  import java.util.Map.Entry;
  import java.util.Set;
  import java.util.function.Function;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.apache.commons.logging.Log;
  
  import com.google.common.base.Supplier;
  import com.google.common.base.Suppliers;
  import com.google.common.collect.Sets;
  
  /**
   * Utility class to generate the element data objects used within the container-page editor.<p>
   *
   * @since 8.0.0
   */
  public class CmsElementUtil {
  
      /** The maximum number of nested container levels. */
      public static final int MAX_NESTING_LEVEL = 7;
  
      /** Static reference to the log. */
      private static final Log LOG = CmsLog.getLog(org.opencms.ade.containerpage.CmsElementUtil.class);
  
      /** The ADE configuration data for the current page URI. */
      private CmsADEConfigData m_adeConfig;
  
      /** The cms context. */
      private CmsObject m_cms;
  
      /** The current page uri. */
      private String m_currentPageUri;
  
      /** The content locale. */
      private Locale m_locale;
  
      /** The current container page. */
      private CmsResource m_page;
  
      /** The request parameters to use while rendering the elements. */
      @SuppressWarnings("unused")
      private Map<String, Object> m_parameterMap;
  
      /** The http request. */
      private HttpServletRequest m_req;
  
      /** The http response. */
      private HttpServletResponse m_res;
  
      /** The standard context bean. */
      private CmsJspStandardContextBean m_standardContext;
  
      /**
       * Creates a new instance.<p>
       * Use this constructor to set the current container page state.<p>
       *
       * @param cms the cms context
       * @param currentPageUri the current page uri
       * @param containerPage the container page bean with the current container state
       * @param detailContentId the detail content structure id
       * @param req the http request
       * @param res the http response
       * @param isDragMode if the page is in drag mode
       * @param locale the content locale
       *
       * @throws CmsException if something goes wrong
       */
      public CmsElementUtil(
          CmsObject cms,
          String currentPageUri,
          CmsContainerPageBean containerPage,
          CmsUUID detailContentId,
          HttpServletRequest req,
          HttpServletResponse res,
          boolean isDragMode,
          Locale locale)
      throws CmsException {
  
          m_cms = OpenCms.initCmsObject(cms);
          String contextPath = (String)cms.getRequestContext().getAttribute(CmsRequestContext.ATTRIBUTE_ADE_CONTEXT_PATH);
          if (contextPath != null) {
              m_cms.getRequestContext().setAttribute(CmsRequestContext.ATTRIBUTE_ADE_CONTEXT_PATH, contextPath);
          }
          m_req = req;
          m_res = res;
          m_currentPageUri = currentPageUri;
          m_locale = locale;
          // initializing request for standard context bean
          req.setAttribute(CmsJspStandardContextBean.ATTRIBUTE_CMS_OBJECT, m_cms);
          if (detailContentId != null) {
              CmsResource detailRes = m_cms.readResource(
                  detailContentId,
                  CmsResourceFilter.ignoreExpirationOffline(m_cms));
              req.setAttribute(CmsDetailPageResourceHandler.ATTR_DETAIL_CONTENT_RESOURCE, detailRes);
          }
          m_standardContext = CmsJspStandardContextBean.getInstance(req);
          m_page = m_cms.readResource(currentPageUri, CmsResourceFilter.ignoreExpirationOffline(cms));
          m_standardContext.setPage(containerPage);
          m_standardContext.setDragMode(isDragMode);
      }
  
      /**
       * Creates a new instance.<p>
       *
       * @param cms the cms context
       * @param currentPageUri the current page uri
       * @param detailContentId the detail content structure id
       * @param req the http request
       * @param res the http response
       * @param locale the content locale
       *
       * @throws CmsException if something goes wrong
       */
      public CmsElementUtil(
          CmsObject cms,
          String currentPageUri,
          CmsUUID detailContentId,
          HttpServletRequest req,
          HttpServletResponse res,
          Locale locale)
      throws CmsException {
  
          m_cms = OpenCms.initCmsObject(cms);
          m_req = req;
          m_res = res;
          m_currentPageUri = currentPageUri;
          m_locale = locale;
          // initializing request for standard context bean
          req.setAttribute(CmsJspStandardContextBean.ATTRIBUTE_CMS_OBJECT, m_cms);
          if (detailContentId != null) {
              CmsResource detailRes = m_cms.readResource(detailContentId, CmsResourceFilter.ignoreExpirationOffline(cms));
              req.setAttribute(CmsDetailPageResourceHandler.ATTR_DETAIL_CONTENT_RESOURCE, detailRes);
          }
          m_standardContext = CmsJspStandardContextBean.getInstance(req);
          m_page = m_cms.readResource(currentPageUri);
          CmsXmlContainerPage xmlContainerPage = CmsXmlContainerPageFactory.unmarshal(cms, m_page, req);
          CmsContainerPageBean containerPage = xmlContainerPage.getContainerPage(cms);
          m_standardContext.setPage(containerPage);
      }
  
      /**
       * Creates a new instance.<p>
       *
       * @param cms the cms context
       * @param currentPageUri the current page uri
       * @param detailContentId the detail content structure id
       * @param requestParameters the request parameters to use while rendering the elements
       * @param req the http request
       * @param res the http response
       * @param locale the content locale
       *
       * @throws CmsException if something goes wrong
       */
      public CmsElementUtil(
          CmsObject cms,
          String currentPageUri,
          CmsUUID detailContentId,
          String requestParameters,
          HttpServletRequest req,
          HttpServletResponse res,
          Locale locale)
      throws CmsException {
  
          this(cms, currentPageUri, detailContentId, req, res, locale);
          m_parameterMap = parseRequestParameters(requestParameters);
      }
  
      /**
       * Checks if a group element is allowed in a container with a given type.<p>
       *
       * @param containerType the container type spec (comma separated)
       * @param groupContainer the group
       *
       * @return true if the group is allowed in the container
       */
      public static boolean checkGroupAllowed(String containerType, CmsGroupContainerBean groupContainer) {
  
          return !Sets.intersection(CmsContainer.splitType(containerType), groupContainer.getTypes()).isEmpty();
      }
  
      public static CmsContainerBean clientToServerContainer(
          CmsContainer container,
          List<CmsContainerElementBean> elements) {
  
          return new CmsContainerBean(
              container.getName(),
              container.getType(),
              container.getParentInstanceId(),
              container.isRootContainer(),
              container.getMaxElements(),
              elements);
  
      }
  
      /**
       * Helper method to create a string template source for a given formatter and content.
       *
       * @param formatter the formatter
       * @param contentSupplier the content supplier
       *
       * @return the string template provider
       */
      public static Function<String, String> createStringTemplateSource(
          I_CmsFormatterBean formatter,
          Supplier<CmsXmlContent> contentSupplier) {
  
  