JSR283Helper.java
7.92 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* 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.Session
* javax.jcr.Value
* javax.jcr.nodetype.NodeType
* javax.jcr.nodetype.PropertyDefinition
* org.apache.jackrabbit.commons.JcrUtils
* org.apache.jackrabbit.util.Text
*/
package com.day.crx.explorer.impl.util;
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.Session;
import javax.jcr.Value;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.PropertyDefinition;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.util.Text;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class JSR283Helper {
private static final String NS_SV_URI = "http://www.jcp.org/jcr/sv/1.0";
public static Node getNode(Session session, String path) throws RepositoryException {
Node root = session.getRootNode();
if (path.equals("") || path.equals("/")) {
return root;
}
path = path.endsWith("/") ? path.substring(1, path.length() - 1) : path.substring(1);
return root.getNode(path);
}
public static Property getProperty(Session session, String path) throws RepositoryException {
return session.getRootNode().getProperty(path.substring(1));
}
public static String getExtendedPath(Property property) throws RepositoryException {
Node parent = property.getParent();
StringBuffer path = new StringBuffer();
if (parent.getDepth() > 0) {
path.append(parent.getPath());
}
path.append("/./");
path.append(property.getName());
return path.toString();
}
public static Item getItem(Session session, String path) throws RepositoryException {
String relPath;
Node root = session.getRootNode();
if (path.equals("/")) {
return root;
}
if (path.endsWith("/") && root.hasNode(relPath = path.substring(1, path.length() - 1))) {
return root.getNode(relPath);
}
if (JSR283Helper.addressesProperty(path) && root.hasProperty(relPath = path.substring(1))) {
return root.getProperty(relPath);
}
return session.getItem(path);
}
public static boolean addressesProperty(String path) {
if (path == null || path.equals("") || path.equals("/")) {
return false;
}
String parent = Text.getRelativeParent((String)path, (int)1);
return Text.getName((String)parent).equals(".");
}
public static boolean addressesNode(String path) {
return path != null && (path.equals("") || path.endsWith("/"));
}
public static Node setPrimaryType(Node node, String type) throws RepositoryException {
if (node.getPrimaryNodeType().getName().equals(type)) {
return node;
}
Session session = node.getSession();
String uuid = node.isNodeType("mix:referenceable") ? node.getUUID() : null;
String name = node.getName();
Node parent = node.getParent();
NodeType[] mix = node.getMixinNodeTypes();
Node tmpNode = session.getRootNode().addNode("tmp" + System.currentTimeMillis(), "nt:unstructured");
NodeIterator iter = node.getNodes();
while (iter.hasNext()) {
Node child = iter.nextNode();
session.move(child.getPath(), tmpNode.getPath() + "/" + child.getName());
}
PropertyIterator piter = node.getProperties();
while (piter.hasNext()) {
Property p = piter.nextProperty();
if (p.getDefinition().isProtected()) continue;
if (p.getDefinition().isMultiple()) {
tmpNode.setProperty(p.getName(), p.getValues());
continue;
}
tmpNode.setProperty(p.getName(), p.getValue());
}
node.remove();
node = uuid == null ? parent.addNode(name, type) : JSR283Helper.addNode(parent, name, type, uuid);
for (int i = 0; i < mix.length; ++i) {
node.addMixin(mix[i].getName());
}
iter = tmpNode.getNodes();
while (iter.hasNext()) {
Node child = iter.nextNode();
session.move(child.getPath(), node.getPath() + "/" + child.getName());
}
piter = tmpNode.getProperties();
while (piter.hasNext()) {
Property p = piter.nextProperty();
if (p.getDefinition().isProtected()) continue;
if (p.getDefinition().isMultiple()) {
node.setProperty(p.getName(), p.getValues());
continue;
}
node.setProperty(p.getName(), p.getValue());
}
tmpNode.remove();
return node;
}
public static Node addNode(Node parent, String name, String type, String uuid) throws RepositoryException {
try {
Session session = parent.getSession();
ContentHandler handler = session.getImportContentHandler(parent.getPath(), 3);
String[] prefixes = session.getNamespacePrefixes();
handler.startDocument();
for (int i = 0; i < prefixes.length; ++i) {
String prefix = prefixes[i];
handler.startPrefixMapping(prefix, session.getNamespaceURI(prefix));
}
AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute("http://www.jcp.org/jcr/sv/1.0", "name", "sv:name", "CDATA", name);
handler.startElement("http://www.jcp.org/jcr/sv/1.0", "node", "sv:node", attrs);
attrs = new AttributesImpl();
attrs.addAttribute("http://www.jcp.org/jcr/sv/1.0", "name", "sv:name", "CDATA", "jcr:primaryType");
attrs.addAttribute("http://www.jcp.org/jcr/sv/1.0", "type", "sv:type", "CDATA", "Name");
handler.startElement("http://www.jcp.org/jcr/sv/1.0", "property", "sv:property", attrs);
handler.startElement("http://www.jcp.org/jcr/sv/1.0", "value", "sv:value", null);
handler.characters(type.toCharArray(), 0, type.length());
handler.endElement("http://www.jcp.org/jcr/sv/1.0", "value", "sv:value");
handler.endElement("http://www.jcp.org/jcr/sv/1.0", "property", "sv:property");
if (uuid != null) {
attrs = new AttributesImpl();
attrs.addAttribute("http://www.jcp.org/jcr/sv/1.0", "name", "sv:name", "CDATA", "jcr:uuid");
attrs.addAttribute("http://www.jcp.org/jcr/sv/1.0", "type", "sv:type", "CDATA", "String");
handler.startElement("http://www.jcp.org/jcr/sv/1.0", "property", "sv:property", attrs);
handler.startElement("http://www.jcp.org/jcr/sv/1.0", "value", "sv:value", null);
handler.characters(uuid.toCharArray(), 0, uuid.length());
handler.endElement("http://www.jcp.org/jcr/sv/1.0", "value", "sv:value");
handler.endElement("http://www.jcp.org/jcr/sv/1.0", "property", "sv:property");
}
handler.endElement("http://www.jcp.org/jcr/sv/1.0", "node", "sv:node");
handler.endDocument();
return parent.getNode(name);
}
catch (SAXException e) {
Exception root = e.getException();
if (root instanceof RepositoryException) {
throw (RepositoryException)root;
}
if (root instanceof RuntimeException) {
throw (RuntimeException)root;
}
throw new RepositoryException("Error while creating node", (Throwable)root);
}
}
public static int getPropertyTypeFromName(String name) {
return JcrUtils.getPropertyType((String)name);
}
}