PathSchemeHelper.java 4.74 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.designer.Designer
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 */
package com.day.cq.wcm.designimporter.impl.common;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.designer.Designer;
import com.day.cq.wcm.designimporter.DesignImporterContext;
import com.day.cq.wcm.designimporter.util.ImporterUtil;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;

public class PathSchemeHelper {
    public static String getCanvasComponentPath(Resource resource) {
        return "/apps/" + PathSchemeHelper.getCanvasResourceType(resource);
    }

    public static String getCanvasResourceType(Resource resource) {
        if (ImporterUtil.isCanvas(resource)) {
            return resource.getResourceType();
        }
        if (ImporterUtil.isImporter(resource)) {
            Resource canvas = resource.getChild("canvas");
            if (canvas != null) {
                return canvas.getResourceType();
            }
            return PathSchemeHelper.getImporterCanvasResourceType(resource);
        }
        throw new IllegalArgumentException("The passed resource is neither a canvas nor an importer");
    }

    public static String getCanvasDesignPath(Resource resource) {
        if (resource != null) {
            PageManager pageManager = (PageManager)resource.getResourceResolver().adaptTo(PageManager.class);
            Page containingPage = pageManager.getContainingPage(resource);
            Designer designer = (Designer)resource.getResourceResolver().adaptTo(Designer.class);
            String containingPageDesignPath = designer.getDesignPath(containingPage);
            String resourcePath = resource.getPath();
            if (ImporterUtil.isCanvas(resource)) {
                if (ImporterUtil.isImporter(resource.getParent())) {
                    resourcePath = resource.getParent().getPath();
                    return containingPageDesignPath + "/canvas" + resourcePath;
                }
                return containingPageDesignPath;
            }
            if (ImporterUtil.isImporter(resource)) {
                return containingPageDesignPath + "/canvas" + resourcePath;
            }
        }
        throw new IllegalArgumentException("The passed resource is neither a canvas nor an importer");
    }

    public static String getCanvasComponentPath(DesignImporterContext designImporterContext) {
        return "/apps/" + PathSchemeHelper.getCanvasResourceType(designImporterContext);
    }

    public static String getCanvasResourceType(DesignImporterContext designImporterContext) {
        Resource resource = designImporterContext.getImporter();
        if (ImporterUtil.isImporter(resource)) {
            return PathSchemeHelper.getImporterCanvasResourceType(resource);
        }
        return PathSchemeHelper.getCanvaspageCanvasResourceType(resource, designImporterContext.htmlName);
    }

    public static String getCanvasDesignPath(DesignImporterContext designImporterContext) {
        try {
            return designImporterContext.designNode.getPath();
        }
        catch (RepositoryException var1_1) {
            return null;
        }
    }

    private static String getImporterCanvasResourceType(Resource resource) {
        String resourcePath;
        String resourceType = resourcePath = resource.getPath();
        Matcher m = Pattern.compile("/content/(?:campaigns/)?(.*?)/(.*)").matcher(resourcePath);
        if (m.find()) {
            String brand = m.group(1);
            String abridgedResPath = m.group(2);
            resourceType = brand + "/components/canvas/" + abridgedResPath;
        }
        return resourceType.replaceAll(":", "_");
    }

    private static String getCanvaspageCanvasResourceType(Resource resource, String htmlName) {
        PageManager pageManager = (PageManager)resource.getResourceResolver().adaptTo(PageManager.class);
        Page canvasPage = pageManager.getContainingPage(resource);
        String pagePath = canvasPage.getPath();
        Matcher m = Pattern.compile("/content/(?:campaigns/)?(.*?)/(.*)").matcher(pagePath);
        if (m.find()) {
            String brand = m.group(1);
            String abridgedPagePath = m.group(2);
            String uniqueId = abridgedPagePath.replace('/', '-') + "-" + htmlName;
            return brand + "/components/canvaspage/" + uniqueId + "-" + "canvas";
        }
        return null;
    }
}