PathSchemeHelper.java
4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* 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;
}
}