AssetUtils.java
3.43 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* javax.jcr.Value
* javax.jcr.nodetype.NodeDefinition
* javax.jcr.nodetype.NodeType
* javax.jcr.nodetype.PropertyDefinition
* org.apache.sling.api.resource.Resource
*/
package com.adobe.granite.asset.core.impl;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.nodetype.NodeDefinition;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.PropertyDefinition;
import org.apache.sling.api.resource.Resource;
public final class AssetUtils {
public static Item copy(Item src, Node dstParent, String name) throws RepositoryException {
if (src.isNode()) {
return AssetUtils.copy((Node)src, dstParent, name);
}
return AssetUtils.copy((Property)src, dstParent, name);
}
public static Node copy(Node src, Node dstParent, String name) throws RepositoryException {
return AssetUtils.copy(src, dstParent, name, false);
}
public static Node copy(Node src, Node dstParent, String name, boolean bestEffort) throws RepositoryException {
if (name == null) {
name = src.getName();
}
if (dstParent.hasNode(name)) {
dstParent.getNode(name).remove();
}
Node dst = dstParent.addNode(name, src.getPrimaryNodeType().getName());
for (NodeType mix : src.getMixinNodeTypes()) {
try {
dst.addMixin(mix.getName());
continue;
}
catch (RepositoryException e) {
if (bestEffort) continue;
throw e;
}
}
PropertyIterator iter = src.getProperties();
while (iter.hasNext()) {
try {
AssetUtils.copy(iter.nextProperty(), dst, null);
continue;
}
catch (RepositoryException e) {
if (bestEffort) continue;
throw e;
}
}
iter = src.getNodes();
while (iter.hasNext()) {
Node n = iter.nextNode();
if (n.getDefinition().isProtected()) continue;
try {
AssetUtils.copy(n, dst, null, bestEffort);
continue;
}
catch (RepositoryException e) {
if (bestEffort) continue;
throw e;
}
}
return dst;
}
public static Property copy(Property src, Node dstParent, String name) throws RepositoryException {
if (!src.getDefinition().isProtected()) {
if (name == null) {
name = src.getName();
}
if (dstParent.hasProperty(name)) {
dstParent.getProperty(name).remove();
}
if (src.getDefinition().isMultiple()) {
return dstParent.setProperty(name, src.getValues());
}
return dstParent.setProperty(name, src.getValue());
}
return null;
}
public static boolean isAsset(Resource resource) throws RepositoryException {
Node node = (Node)resource.adaptTo(Node.class);
return node.isNodeType("dam:Asset");
}
}