Skip to content

Commit bfd4cb2

Browse files
committed
devonfw#1130: improve behaviour on ambigous XPath match
1 parent e890ede commit bfd4cb2

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

cli/src/main/java/com/devonfw/tools/ide/merge/FileMerger.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public final int merge(Path setup, Path update, EnvironmentVariables variables,
4949
try {
5050
doMerge(setup, update, variables, workspace);
5151
} catch (Exception e) {
52-
this.context.error(e, "Failed to merge workspace file {} with update template {} and setup file {}!", workspace, update, setup);
53-
return 1;
52+
this.context.warning("Failed to merge workspace file {} with update template {} and setup file {}!", workspace, update, setup);
5453
}
5554
return 0;
5655
}

cli/src/main/java/com/devonfw/tools/ide/merge/xmlmerger/XmlMerger.java

+23-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import java.io.InputStream;
55
import java.nio.file.Files;
66
import java.nio.file.Path;
7-
import javax.xml.namespace.QName;
7+
import java.util.HashSet;
8+
import java.util.Set;
89
import javax.xml.parsers.DocumentBuilder;
910
import javax.xml.parsers.DocumentBuilderFactory;
1011
import javax.xml.transform.OutputKeys;
@@ -111,20 +112,34 @@ public Document merge(XmlMergeDocument templateDocument, XmlMergeDocument worksp
111112
Path template = workspaceDocument.getPath();
112113
this.context.debug("Merging {} into {} ...", template, source);
113114
Element templateRoot = templateDocument.getRoot();
114-
QName templateQName = XmlMergeSupport.getQualifiedName(templateRoot);
115115
Element workspaceRoot = workspaceDocument.getRoot();
116-
QName workspaceQName = XmlMergeSupport.getQualifiedName(workspaceRoot);
117-
if (templateQName.equals(workspaceQName)) {
116+
117+
if (templateRoot.getNodeName().equals(workspaceRoot.getNodeName())) {
118118
XmlMergeStrategy strategy = XmlMergeSupport.getMergeStrategy(templateRoot);
119119
if (strategy == null) {
120120
strategy = XmlMergeStrategy.COMBINE; // default strategy used as fallback
121121
}
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+
124139
resultDocument = workspaceDocument.getDocument();
125140
} 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());
128143
return null;
129144
}
130145
return resultDocument;

0 commit comments

Comments
 (0)