masterpages.jsp 3.28 KB
<%--
  ADOBE CONFIDENTIAL

  Copyright 2015 Adobe Systems Incorporated
  All Rights Reserved.

  NOTICE:  All information contained herein is, and remains
  the property of Adobe Systems Incorporated and its suppliers,
  if any.  The intellectual and technical concepts contained
  herein are proprietary to Adobe Systems Incorporated and its
  suppliers and may be covered by U.S. and Foreign Patents,
  patents in process, and are protected by trade secret or copyright law.
  Dissemination of this information or reproduction of this material
  is strictly forbidden unless prior written permission is obtained
  from Adobe Systems Incorporated.
--%><%
%><%@include file="/libs/granite/ui/global.jsp" %><%
%><%@page session="false"
          import="java.util.Map,
                  java.util.Map.Entry,
                  java.util.HashMap,
                  java.util.Iterator,
                  java.util.Locale,
                  org.apache.commons.collections.Transformer,
                  org.apache.commons.collections.iterators.TransformIterator,
                  org.apache.jackrabbit.util.Text,
                  org.apache.sling.api.resource.ResourceMetadata,
                  org.apache.sling.api.resource.ResourceResolver,
                  org.apache.sling.api.wrappers.ValueMapDecorator,
                  com.adobe.granite.ui.components.Config,
                  com.adobe.granite.ui.components.ds.DataSource,
                  com.adobe.granite.ui.components.ds.SimpleDataSource,
                  com.adobe.granite.ui.components.ds.ValueMapResource,
                  com.day.cq.commons.Language,
                  com.day.cq.commons.LanguageUtil,
                  com.day.cq.wcm.api.LanguageManager,
                  com.day.cq.wcm.api.Page,
                  com.day.cq.wcm.msm.api.Blueprint,
                  com.day.cq.wcm.msm.api.BlueprintManager" %><%

// This is the same implementation as com.day.cq.wcm.core.impl.servlets.ContentLanguageServlet.

final ResourceResolver resolver = resourceResolver;

final Config cfg = new Config(resource.getChild(Config.DATASOURCE));

final BlueprintManager bpm = resolver.adaptTo(BlueprintManager.class);
if (bpm == null) {
    return;
}

final String blueprintPath = cmp.getExpressionHelper().getString(cfg.get("blueprint", String.class));
if (blueprintPath == null) {
    return;
}

final Blueprint blueprint = bpm.getBlueprint(blueprintPath);

final Resource site = resolver.getResource(blueprint.getSitePath());
if (site == null) {
    return;
}

final Page rootPage = site.adaptTo(Page.class);
if (rootPage == null) {
    return;
}

@SuppressWarnings("unchecked")
final Iterator<Resource> it = new TransformIterator(rootPage.listChildren(), new Transformer() {
    public Object transform(Object o) {
        Page masterPage = (Page) o;

        String pageTitle = masterPage.getPageTitle();
        if (pageTitle == null || pageTitle.isEmpty()) {
            pageTitle = masterPage.getTitle();
        }
        ValueMap vm = new ValueMapDecorator(new HashMap<String, Object>());
        vm.put("value", masterPage.getPath());
        vm.put("text", pageTitle);
        vm.put("selected", true);

        return new ValueMapResource(resolver, new ResourceMetadata(), "nt:unstructured", vm);
    }
});

request.setAttribute(DataSource.class.getName(), new SimpleDataSource(it));
%>