XPathUtils.java
7.61 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemfd.docmanager.Document
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.ValueFactory
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.ResourceResolver
*/
package com.adobe.fd.workflow.api;
import com.adobe.aemfd.docmanager.Document;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.jcr.Binary;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathException;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ResourceResolver;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
@Component(immediate=1)
@Service(value={XPathUtils.class})
public class XPathUtils {
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public Map<String, Object> query(Document xmlDoc, Map<String, QName> queries) throws ParserConfigurationException, IOException, SAXException, XPathException {
HashMap<String, Object> res;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream is = xmlDoc.getInputStream();
res = new HashMap<String, Object>(queries.size());
try {
org.w3c.dom.Document doc = builder.parse(is);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
for (Map.Entry<String, QName> me : queries.entrySet()) {
String xpathQuery = me.getKey();
QName type = me.getValue();
XPathExpression expr = xpath.compile(xpathQuery);
String val = type == null ? expr.evaluate(doc) : expr.evaluate(doc, type);
res.put(xpathQuery, val);
}
}
finally {
is.close();
}
return res;
}
public Object query(Document xmlDoc, String xpathQuery, QName type) throws ParserConfigurationException, IOException, SAXException, XPathException {
Map<String, Object> res = this.query(xmlDoc, Collections.singletonMap(xpathQuery, type));
return res.values().iterator().next();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public Map<String, Object> query(String xmlJcrPath, ResourceResolver rr, Map<String, QName> queries) throws ParserConfigurationException, IOException, SAXException, XPathException {
Document xmlDoc = new Document(xmlJcrPath, rr);
try {
Map<String, Object> map = this.query(xmlDoc, queries);
return map;
}
finally {
xmlDoc.dispose();
}
}
public Object query(String xmlJcrPath, ResourceResolver rr, String xpathQuery, QName type) throws ParserConfigurationException, IOException, SAXException, XPathException {
Map<String, Object> res = this.query(xmlJcrPath, rr, Collections.singletonMap(xpathQuery, type));
return res.values().iterator().next();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public Document update(Document xmlDoc, Map<String, String> values) throws ParserConfigurationException, IOException, SAXException, XPathException, TransformerException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream is = xmlDoc.getInputStream();
try {
Node node;
org.w3c.dom.Document doc = builder.parse(is);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
for (Map.Entry<String, String> me : values.entrySet()) {
String xpathQuery = me.getKey();
XPathExpression expr = xpath.compile(xpathQuery);
Object n = expr.evaluate(doc, XPathConstants.NODE);
if (n == null || !(n instanceof Node)) {
throw new XPathException("Value " + n + " at query path " + xpathQuery + " does not resolve to a Node object!");
}
node = (Node)n;
node.setTextContent(me.getValue());
}
DOMSource domSource = new DOMSource(doc);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
StreamResult result = new StreamResult(baos);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
node = new Document(baos.toByteArray());
return node;
}
finally {
is.close();
}
}
public Document update(Document xmlDoc, String xpathQuery, String value) throws ParserConfigurationException, IOException, SAXException, XPathException, TransformerException {
return this.update(xmlDoc, Collections.singletonMap(xpathQuery, value));
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void update(String xmlJcrPath, ResourceResolver rr, Map<String, String> values, boolean save) throws ParserConfigurationException, IOException, SAXException, XPathException, TransformerException, RepositoryException {
Document origDoc = new Document(xmlJcrPath, rr);
try {
Document newDoc = this.update(origDoc, values);
try {
Session sess = (Session)rr.adaptTo(Session.class);
javax.jcr.Node xmlNode = sess.getNode(xmlJcrPath);
javax.jcr.Node resNode = xmlNode.getNode("jcr:content");
InputStream is = newDoc.getInputStream();
try {
Binary bin = sess.getValueFactory().createBinary(is);
resNode.setProperty("jcr:data", bin);
if (save) {
sess.save();
}
}
finally {
is.close();
}
}
finally {
newDoc.dispose();
}
}
finally {
origDoc.dispose();
}
}
public void update(String xmlJcrPath, ResourceResolver rr, String xpathQuery, String value, boolean save) throws ParserConfigurationException, IOException, SAXException, XPathException, TransformerException, RepositoryException {
this.update(xmlJcrPath, rr, Collections.singletonMap(xpathQuery, value), save);
}
}