Skip to content

Commit 10254a8

Browse files
leonrohne27KianRolfhohwille
authored
#1130: improve behaviour on ambigous xpath match (#1138)
Co-authored-by: KianRolf <[email protected]> Co-authored-by: Jörg Hohwiller <[email protected]>
1 parent 64fd952 commit 10254a8

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

CHANGELOG.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Release with new features and bugfixes:
1010
* https://github.com/devonfw/IDEasy/issues/1006[#1006]: Eclipse automation opens UI that blocks further processing until closed
1111
* https://github.com/devonfw/IDEasy/issues/1110[#1110]: ide status fails with IllegalStateException when offline
1212
* https://github.com/devonfw/IDEasy/issues/1039[#1039]: Update Jasypt commandlet implementation to run Java version from dependencies.json
13+
* https://github.com/devonfw/IDEasy/issues/1138[#1138]: Duplicated variables in configuration logs a clear warning and do not prevent merge anymore
1314

1415
The full list of changes for this release can be found in https://github.com/devonfw/IDEasy/milestone/24?closed=1[milestone 2025.03.002].
1516

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

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ protected void doMerge(Element templateElement, Element resultElement, ElementMa
6060
* @param matcher the {@link ElementMatcher}.
6161
*/
6262
public void merge(Element templateElement, Element resultElement, ElementMatcher matcher) {
63-
6463
try {
6564
doMerge(templateElement, resultElement, matcher);
6665
} catch (XmlMergeException e) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private IdComputer getIdComputer(Element element) {
4545
} else {
4646
this.qName2IdMap.putIfAbsent(qName, id);
4747
}
48-
return this.id2ComputerMap.computeIfAbsent(id, IdComputer::new);
48+
return this.id2ComputerMap.computeIfAbsent(id, i -> new IdComputer(i, context));
4949
}
5050

5151
/**

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.w3c.dom.Element;
1010
import org.w3c.dom.NodeList;
1111

12+
import com.devonfw.tools.ide.context.IdeContext;
1213
import com.devonfw.tools.ide.merge.xmlmerger.XmlMergeSupport;
1314

1415
/**
@@ -19,17 +20,23 @@ public class IdComputer {
1920
/** The value of merge:id that is used to evaluate the xpath expression. */
2021
private final String id;
2122

23+
private final IdeContext context;
24+
2225
private static final XPathFactory xPathFactory = XPathFactory.newInstance();
2326

27+
private final boolean throwExceptionOnMultipleMatches = Boolean.parseBoolean(System.getProperty("throwExceptionOnMultipleMatches", "false"));
28+
29+
2430
/**
2531
* The constructor.
2632
*
2733
* @param id the {@link #getId() merge ID}.
2834
*/
29-
public IdComputer(String id) {
35+
public IdComputer(String id, IdeContext context) {
3036

3137
super();
3238
this.id = id;
39+
this.context = context;
3340
}
3441

3542
/**
@@ -61,8 +68,14 @@ public Element evaluateExpression(Element templateElement, Element workspaceElem
6168
} else if (length == 0) {
6269
return null;
6370
} else {
64-
throw new IllegalStateException(
65-
length + " matches found for XPath " + xpathExpr + " in workspace XML at " + XmlMergeSupport.getXPath(workspaceElement, true));
71+
if (throwExceptionOnMultipleMatches) {
72+
throw new IllegalStateException(
73+
length + " matches found for XPath " + xpathExpr + " in workspace XML at " + XmlMergeSupport.getXPath(workspaceElement, true));
74+
} else {
75+
this.context.warning("Matches found: {} matches for XPath {} in workspace XML at {}",
76+
length, xpathExpr, XmlMergeSupport.getXPath(workspaceElement, true));
77+
}
78+
return (Element) nodeList.item(0);
6679
}
6780
} catch (XPathExpressionException e) {
6881
throw new IllegalStateException("Failed to compile XPath expression " + xpath, e);

0 commit comments

Comments
 (0)