|
4 | 4 | import java.io.InputStream;
|
5 | 5 | import java.nio.file.Files;
|
6 | 6 | import java.nio.file.Path;
|
7 |
| -import javax.xml.namespace.QName; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.Set; |
8 | 9 | import javax.xml.parsers.DocumentBuilder;
|
9 | 10 | import javax.xml.parsers.DocumentBuilderFactory;
|
10 | 11 | import javax.xml.transform.OutputKeys;
|
@@ -111,20 +112,34 @@ public Document merge(XmlMergeDocument templateDocument, XmlMergeDocument worksp
|
111 | 112 | Path template = workspaceDocument.getPath();
|
112 | 113 | this.context.debug("Merging {} into {} ...", template, source);
|
113 | 114 | Element templateRoot = templateDocument.getRoot();
|
114 |
| - QName templateQName = XmlMergeSupport.getQualifiedName(templateRoot); |
115 | 115 | Element workspaceRoot = workspaceDocument.getRoot();
|
116 |
| - QName workspaceQName = XmlMergeSupport.getQualifiedName(workspaceRoot); |
117 |
| - if (templateQName.equals(workspaceQName)) { |
| 116 | + |
| 117 | + if (templateRoot.getNodeName().equals(workspaceRoot.getNodeName())) { |
118 | 118 | XmlMergeStrategy strategy = XmlMergeSupport.getMergeStrategy(templateRoot);
|
119 | 119 | if (strategy == null) {
|
120 | 120 | strategy = XmlMergeStrategy.COMBINE; // default strategy used as fallback
|
121 | 121 | }
|
122 |
| - ElementMatcher elementMatcher = new ElementMatcher(this.context); |
123 |
| - strategy.merge(templateRoot, workspaceRoot, elementMatcher); |
| 122 | + |
| 123 | + Set<String> seenElements = new HashSet<>(); |
| 124 | + |
| 125 | + NodeList templateChildren = templateRoot.getChildNodes(); |
| 126 | + for (int i = 0; i < templateChildren.getLength(); i++) { |
| 127 | + Node templateNode = templateChildren.item(i); |
| 128 | + if (templateNode.getNodeType() == Node.ELEMENT_NODE) { |
| 129 | + Element templateElement = (Element) templateNode; |
| 130 | + String elementQName = templateElement.getNodeName(); |
| 131 | + |
| 132 | + if (!seenElements.contains(elementQName)) { |
| 133 | + strategy.merge(templateElement, workspaceRoot, new ElementMatcher(this.context)); |
| 134 | + seenElements.add(elementQName); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
124 | 139 | resultDocument = workspaceDocument.getDocument();
|
125 | 140 | } else {
|
126 |
| - this.context.error("Cannot merge XML template {} with root {} into XML file {} with root {} as roots do not match.", templateDocument.getPath(), |
127 |
| - templateQName, workspaceDocument.getPath(), workspaceQName); |
| 141 | + this.context.warning("Cannot merge XML template {} with root {} into XML file {} with root {} as roots do not match.", |
| 142 | + templateDocument.getPath(), templateRoot.getNodeName(), workspaceDocument.getPath(), workspaceRoot.getNodeName()); |
128 | 143 | return null;
|
129 | 144 | }
|
130 | 145 | return resultDocument;
|
|
0 commit comments