Resources.java
3.23 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
*/
package com.adobe.granite.rest.utils;
import com.adobe.granite.rest.filter.Filter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
public final class Resources {
private Resources() {
}
public static int getSize(Iterator<Resource> children, Filter<Resource> filter) {
LinkedList<Resource> filteredChildren = new LinkedList<Resource>();
Iterator<Resource> it = children;
while (it.hasNext()) {
Resource child = it.next();
ValueMap vm = (ValueMap)child.adaptTo(ValueMap.class);
if (vm != null && Boolean.TRUE.equals(vm.get("hidden", Boolean.class)) || filter != null && !filter.matches(child)) continue;
filteredChildren.add(child);
}
return filteredChildren.size();
}
public static Resource copy(Resource src, Resource dstParent, String name, int depth) throws PersistenceException {
HashSet<String> ignoreProperties = new HashSet<String>();
ignoreProperties.add("jcr:uuid");
return Resources.copy(src, dstParent, name, depth, ignoreProperties);
}
public static Resource copy(Resource src, Resource dstParent, String name, int depth, Set<String> ignoreProperties) throws PersistenceException {
if (Resources.isAncestorOrSameNode(src, dstParent)) {
throw new PersistenceException("Cannot copy ancestor " + src.getPath() + " to descendant " + dstParent.getPath());
}
ResourceResolver resolver = src.getResourceResolver();
if (name == null) {
name = src.getName();
}
if (dstParent.getChild(name) != null) {
Resource child = dstParent.getChild(name);
resolver.delete(child);
}
HashMap properties = new HashMap();
for (Map.Entry entry : ((ValueMap)src.adaptTo(ValueMap.class)).entrySet()) {
if (ignoreProperties.contains(entry.getKey())) continue;
properties.put(entry.getKey(), entry.getValue());
}
Resource dst = resolver.create(dstParent, name, properties);
if (depth == 0) {
return dst;
}
Iterator it = src.listChildren();
while (it.hasNext()) {
Resource child = (Resource)it.next();
Resources.copy(child, dst, null, --depth, ignoreProperties);
}
return dst;
}
private static boolean isAncestorOrSameNode(Resource src, Resource dest) {
if ("/".equals(src.getPath())) {
return true;
}
if (src.getPath().equals(dest.getPath())) {
return true;
}
if (dest.getPath().startsWith(src.getPath() + "/")) {
return true;
}
return false;
}
}