AssetLanguageCopyReferenceProvider.java
3.94 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.references.Reference
* com.adobe.granite.references.ReferenceProvider
* com.day.cq.commons.LanguageUtil
* com.day.text.Text
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.core.impl;
import com.adobe.granite.references.Reference;
import com.adobe.granite.references.ReferenceProvider;
import com.day.cq.commons.LanguageUtil;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={ReferenceProvider.class})
public class AssetLanguageCopyReferenceProvider
implements ReferenceProvider {
public static final String REFERENCE_TYPE = "assetLanguageCopy";
private static final Logger log = LoggerFactory.getLogger(AssetLanguageCopyReferenceProvider.class);
public List<Reference> getReferences(Resource resource) {
ArrayList<Reference> references = new ArrayList<Reference>();
ResourceResolver resolver = resource.getResourceResolver();
Session session = (Session)resolver.adaptTo(Session.class);
Collection<Resource> languageRoots = this.getLanguageRoots(resolver, resource.getPath());
String path = resource.getPath();
String langRoot = LanguageUtil.getLanguageRoot((String)path);
String contentPath = "";
for (Resource languageRoot2 : languageRoots) {
String languageRootPath = languageRoot2.getPath();
if (!path.startsWith(languageRootPath) || langRoot != null && !langRoot.equals(LanguageUtil.getLanguageRoot((String)languageRootPath))) continue;
contentPath = path.replaceFirst(languageRootPath, "");
break;
}
try {
for (Resource languageRoot2 : languageRoots) {
String targetPath = languageRoot2.getPath() + contentPath;
if (!session.nodeExists(targetPath)) continue;
references.add(new Reference(resource, resolver.getResource(targetPath), "assetLanguageCopy"));
}
}
catch (RepositoryException e) {
log.error("unable to get all language copy references for resource: " + path + ". Cause: {}", (Object)e.getMessage(), (Object)e);
}
return references;
}
private Collection<Resource> getLanguageRoots(ResourceResolver resolver, String path) {
String parent = null;
String root = LanguageUtil.getLanguageRoot((String)path);
if (root == null) {
return new LinkedList<Resource>();
}
parent = Text.getRelativeParent((String)root, (int)1);
Resource parentResource = resolver.getResource(parent);
if (parentResource == null) {
return Collections.emptySet();
}
LinkedList<Resource> assets = new LinkedList<Resource>();
Iterator iter = resolver.listChildren(parentResource);
while (iter.hasNext()) {
Resource r = (Resource)iter.next();
String name = Text.getName((String)r.getPath());
Locale locale = LanguageUtil.getLocale((String)name);
if (locale == null) continue;
assets.add(r);
}
return assets;
}
public String getType() {
return "assetLanguageCopy";
}
}