-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathXmlMerge.java
More file actions
342 lines (316 loc) · 12.1 KB
/
XmlMerge.java
File metadata and controls
342 lines (316 loc) · 12.1 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright 2017-2019 FIX Protocol Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*/
package io.fixprotocol.xml;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import io.fixprotocol.orchestra.event.EventListener;
import io.fixprotocol.orchestra.event.EventListenerFactory;
import io.fixprotocol.orchestra.event.TeeEventListener;
import static org.w3c.dom.Node.ATTRIBUTE_NODE;
/**
* Merges a difference file created by {@link XmlDiff} into a baseline XML file to create a new XML
* file
*
* Reads XML diffs as patch operations specified by IETF RFC 5261
*
* @author Don Mendelson
*
* @see <a href="https://tools.ietf.org/html/rfc5261">An Extensible Markup Language (XML) Patch
* Operations Framework Utilizing XML Path Language (XPath) Selectors</a>
*
*/
public class XmlMerge {
private static final Logger logger = LogManager.getLogger();
private int errors = 0;
/**
* Merges a baseline XML file with a differences file to produce a second XML file
*
* @param args three file names: baseline XML file, diff file, name of second XML to produce
* Optionally, argument '-e <event-filename>' can direct errors to a JSON file suitable for UI rendering.
*
*/
public static void main(String[] args) {
if (args.length < 3) {
usage();
System.exit(1);
} else {
try {
String eventFilename = null;
final XmlMerge tool = new XmlMerge();
for (int i = 3; i < args.length; i++) {
switch (args[i]) {
case "-e":
eventFilename = args[i + 1];
i++;
break;
}
}
tool.addEventLogger(eventFilename != null ? new FileOutputStream(eventFilename) : null);
boolean successful = tool.merge(new FileInputStream(args[0]), new FileInputStream(args[1]),
new FileOutputStream(args[2]));
System.exit(successful ? 0 : 1);
} catch (final Exception e) {
logger.fatal("XmlMerge failed", e);
System.exit(1);
}
}
}
/**
* Prints application usage
*/
public static void usage() {
System.out.println("Usage: XmlMerge <xml-file1> <diff-file> <xml-file2>");
}
private final TeeEventListener eventLogger;
private final EventListenerFactory factory;
public XmlMerge() {
eventLogger = new TeeEventListener();
factory = new EventListenerFactory();
final EventListener logEventLogger = factory.getInstance("LOG4J");
try {
logEventLogger.setResource(logger);
eventLogger.addEventListener(logEventLogger);
} catch (final Exception e) {
e.printStackTrace();
}
}
/**
* Merges differences into an XML file to produce a new XML file
*
* @param baseline XML input stream
* @param diff reads difference stream produced by {@link XmlDiff}
* @param out XML output
* @return returns {@code true} if merge completed successfully without errors
* @throws Exception if an IO or parser error occurs
*/
public boolean merge(InputStream baseline, InputStream diff, OutputStream out) throws Exception {
Objects.requireNonNull(baseline, "Baseline stream cannot be null");
Objects.requireNonNull(diff, "Difference stream cannot be null");
Objects.requireNonNull(out, "Output stream cannot be null");
final Document baselineDoc = parse(baseline);
// XPath implementation supplied with Java 8 fails so using Saxon
final XPathFactory factory = new net.sf.saxon.xpath.XPathFactoryImpl();
final XPath xpathEvaluator = factory.newXPath();
final CustomNamespaceContext nsContext = new CustomNamespaceContext();
nsContext.populate(baselineDoc);
xpathEvaluator.setNamespaceContext(nsContext);
final Document diffDoc = parse(diff);
final Element diffRoot = diffDoc.getDocumentElement();
final NodeList children = diffRoot.getChildNodes();
for (int index = 0; index < children.getLength(); index++) {
final Node child = children.item(index);
final short type = child.getNodeType();
if (type != Node.ELEMENT_NODE) {
continue;
}
final Element patchOpElement = (Element) child;
final String tag = patchOpElement.getNodeName();
switch (tag) {
case "add":
add(baselineDoc, xpathEvaluator, patchOpElement);
break;
case "remove":
remove(baselineDoc, xpathEvaluator, patchOpElement);
break;
case "replace":
replace(baselineDoc, xpathEvaluator, patchOpElement);
break;
default:
errors++;
throw new IllegalArgumentException(String.format("Invalid merge operation %s", tag));
}
}
write(baselineDoc, out);
eventLogger.info("XmlMerge completed with {0} errors", getErrors());
return getErrors() == 0;
}
private void add(Document doc, XPath xpathEvaluator, Element patchOpElement) {
final String xpathExpression = patchOpElement.getAttribute("sel");
final String attribute = patchOpElement.getAttribute("type");
final String pos = patchOpElement.getAttribute("pos");
XPathExpression compiled;
try {
compiled = xpathEvaluator.compile(xpathExpression);
final Node siteNode = (Node) compiled.evaluate(doc, XPathConstants.NODE);
if (siteNode == null) {
errors++;
eventLogger.error("Target not found for add; {0}", xpathExpression);
} else {
if (attribute.length() > 0) {
String value = null;
final NodeList children = patchOpElement.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
final Node child = children.item(i);
if (Node.TEXT_NODE == child.getNodeType()) {
value = patchOpElement.getTextContent();
break;
}
}
((Element) siteNode).setAttribute(attribute.substring(1), value);
} else {
final Node parent;
Node before = null;
switch (pos) {
case "prepend":
// siteNode is parent - make first child
parent = siteNode;
before = siteNode.getFirstChild();
break;
case "before":
// insert as sibling before siteNode
parent = siteNode.getParentNode();
before = siteNode;
break;
case "after":
// insert as sibling after siteNode
parent = siteNode.getParentNode();
final Node nextSibling = siteNode.getNextSibling();
if (nextSibling != null) {
before = nextSibling;
}
break;
default:
// siteNode is parent - make last child
parent = siteNode;
}
final NodeList children = patchOpElement.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
final Node child = children.item(i);
final Node imported = doc.importNode(child, true);
if (before != null) {
parent.insertBefore(imported, before);
} else {
parent.appendChild(imported);
}
}
}
}
} catch (final XPathExpressionException e) {
errors++;
eventLogger.error("Invalid XPath expression for add; {0}", xpathExpression);
}
}
public void addEventLogger(OutputStream jsonOutputStream) throws Exception {
if (jsonOutputStream != null) {
final EventListener jsonEventLogger = factory.getInstance("JSON");
jsonEventLogger.setResource(jsonOutputStream);
eventLogger.addEventListener(jsonEventLogger);
}
}
private Document parse(InputStream is)
throws ParserConfigurationException, SAXException, IOException {
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
final DocumentBuilder db = dbf.newDocumentBuilder();
return db.parse(is);
}
private void remove(final Document doc, XPath xpathEvaluator, Element patchOpElement) {
final String xpathExpression = patchOpElement.getAttribute("sel");
try {
final Node node =
(Node) xpathEvaluator.compile(xpathExpression).evaluate(doc, XPathConstants.NODE);
if (node != null) {
if (ATTRIBUTE_NODE == node.getNodeType()) {
Attr attr = (Attr) node;
Element parent = attr.getOwnerElement();
parent.removeAttributeNode(attr);
} else {
Node parent = node.getParentNode();
if (parent != null) {
final NodeList children = parent.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node item = children.item(i);
if (item == node) {
parent.removeChild(node);
break;
}
}
}
}
} else {
eventLogger.warn("XPath expression to remove not found; {0}", xpathExpression);
}
} catch (final XPathExpressionException e) {
errors++;
eventLogger.error("Invalid XPath expression for remove; {0}", xpathExpression);
}
}
private void replace(final Document doc, XPath xpathEvaluator, Element patchOpElement) {
final String xpathExpression = patchOpElement.getAttribute("sel");
try {
final String value = patchOpElement.getFirstChild().getNodeValue();
final Node siteNode =
(Node) xpathEvaluator.compile(xpathExpression).evaluate(doc, XPathConstants.NODE);
if (siteNode == null) {
errors++;
eventLogger.error("Target not found for replace; {0}", xpathExpression);
} else {
switch (siteNode.getNodeType()) {
case Node.ELEMENT_NODE:
final NodeList children = siteNode.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
if (children.item(i).getNodeType() == Node.TEXT_NODE) {
children.item(i).setNodeValue(value);
return;
}
}
final Text text = doc.createTextNode(value);
text.setNodeValue(value);
siteNode.appendChild(text);
break;
case ATTRIBUTE_NODE:
case Node.TEXT_NODE:
siteNode.setNodeValue(value);
break;
}
}
} catch (final XPathExpressionException e) {
errors++;
eventLogger.error("Invalid XPath expression for remove; {0}", xpathExpression);
}
}
public int getErrors() {
return errors;
}
private void write(Document document, OutputStream outputStream) throws TransformerException {
final DOMSource source = new DOMSource(document);
final StreamResult result = new StreamResult(outputStream);
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
final Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
transformer.transform(source, result);
}
}