FrameworkContentExporterUtils.java
6.4 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.Filter
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageFilter
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ValueMap
*/
package com.adobe.cq.mobile.angular.data.util;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.day.cq.commons.Filter;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageFilter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
public class FrameworkContentExporterUtils {
public static final String NG_PAGE_RESOURCE_TYPE = "mobileapps/components/angular/ng-page";
public static final String NG_FRAMEWORK_TYPE_ID = "angular";
private static final String JCR_CONTENT = "jcr:content/";
public static String getRelativePathToDescendantResource(Resource parentResource, Resource descendantResource) {
return FrameworkContentExporterUtils.getRelativePathToDescendantPath(parentResource, descendantResource.getPath());
}
public static String getRelativePathToDescendantPath(Resource parentResource, String absoluteDescendantPath) {
String parentPath = parentResource.getPath();
if (absoluteDescendantPath.indexOf(parentPath) == 0) {
String relativeResourcePath = parentResource.getName() + absoluteDescendantPath.substring(parentPath.length());
return relativeResourcePath;
}
return null;
}
public static String getPathToAsset(Resource parentResource, String absoluteDescendantAssetPath, boolean appExport) {
if (!appExport) {
return absoluteDescendantAssetPath;
}
String relativeResourcePath = FrameworkContentExporterUtils.getRelativePathToDescendantPath(parentResource, absoluteDescendantAssetPath);
relativeResourcePath = FrameworkContentExporterUtils.replaceJcrContent(relativeResourcePath);
return relativeResourcePath;
}
public static Resource getTopLevelAppResource(Resource resource) {
Resource parent = resource.getParent();
if (parent == null || !"cq:Page".equals(parent.getResourceType())) {
return resource;
}
MobileResource parentMobileRes = (MobileResource)parent.adaptTo(MobileResource.class);
if (parentMobileRes.isA(MobileResourceType.CONTENT.getType(), MobileResourceType.INSTANCE.getType())) {
return resource;
}
return FrameworkContentExporterUtils.getTopLevelAppResource(parent);
}
public static boolean isTopLevelAppResource(Resource resource) {
if (resource == null) {
return false;
}
return resource.equals((Object)FrameworkContentExporterUtils.getTopLevelAppResource(resource));
}
public static long getMillisecondsSinceUnixEpoch() {
return System.currentTimeMillis();
}
public static List<Page> getAllAngularDescendantPages(Page page, PageFilter pageFilter) {
ArrayList<Page> pageList = new ArrayList<Page>();
FrameworkContentExporterUtils.getAllDescendantsOfResourceType(page, "mobileapps/components/angular/ng-page", pageFilter, pageList);
return pageList;
}
public static String getJsFriendlyResourceName(String resourcePath) {
if (resourcePath == null) {
return null;
}
String uniqueName = resourcePath;
String key = "jcr:content/";
int indexOfKey = uniqueName.indexOf(key);
if (indexOfKey > 0) {
uniqueName = uniqueName.substring(indexOfKey + key.length());
}
return uniqueName.replaceAll("[^A-Za-z0-9]", "");
}
public static String getRelativePathToRootLevel(Resource resource) {
String path = "";
Resource ancestor = resource.getParent();
while ((ancestor = ancestor.getParent()) != null) {
path = path + "../";
}
return path;
}
public static List<Resource> getAllAngularPageComponents(Resource pageContentResource) {
ArrayList<Resource> angularComponents = new ArrayList<Resource>();
FrameworkContentExporterUtils.getAllAngularPageComponentsHelper(pageContentResource, angularComponents);
return angularComponents;
}
public static Resource getAncestorTemplateResource(Resource resource, String resourceType) {
if (resource == null || resourceType == null) {
return null;
}
Resource contentResource = resource.getChild("jcr:content");
if (contentResource != null && contentResource.isResourceType(resourceType)) {
return resource;
}
return FrameworkContentExporterUtils.getAncestorTemplateResource(resource.getParent(), resourceType);
}
public static String getRelativeComponentPath(String path) {
if (path == null || !path.contains("jcr:content/")) {
return path;
}
return path.substring(path.indexOf("jcr:content/") + "jcr:content/".length());
}
private static void getAllAngularPageComponentsHelper(Resource resource, List<Resource> components) {
Iterator resourceChildrenIter = resource.listChildren();
while (resourceChildrenIter.hasNext()) {
Resource childResource = (Resource)resourceChildrenIter.next();
ValueMap childProperties = (ValueMap)childResource.adaptTo(ValueMap.class);
if ("angular".equals(childProperties.get("frameworkType", String.class))) {
components.add(childResource);
}
FrameworkContentExporterUtils.getAllAngularPageComponentsHelper(childResource, components);
}
}
private static void getAllDescendantsOfResourceType(Page page, String resourceType, PageFilter pageFilter, List<Page> list) {
Iterator children = page.listChildren((Filter)pageFilter, true);
while (children.hasNext()) {
Page child = (Page)children.next();
Resource contentResource = child.getContentResource();
if (contentResource == null || !contentResource.isResourceType(resourceType)) continue;
list.add(child);
}
}
private static String replaceJcrContent(String path) {
if (path == null) {
return null;
}
return path.replaceAll("/jcr:content/", "/jcr_content/");
}
}