Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1130: improve behaviour on ambigous xpath match #1138

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Release with new features and bugfixes:
* https://github.com/devonfw/IDEasy/issues/1006[#1006]: Eclipse automation opens UI that blocks further processing until closed
* https://github.com/devonfw/IDEasy/issues/1110[#1110]: ide status fails with IllegalStateException when offline
* https://github.com/devonfw/IDEasy/issues/1039[#1039]: Update Jasypt commandlet implementation to run Java version from dependencies.json
* https://github.com/devonfw/IDEasy/issues/1138[#1138]: Duplicated variables in configuration logs a clear warning and do not prevent merge anymore

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].

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ protected void doMerge(Element templateElement, Element resultElement, ElementMa
* @param matcher the {@link ElementMatcher}.
*/
public void merge(Element templateElement, Element resultElement, ElementMatcher matcher) {

try {
doMerge(templateElement, resultElement, matcher);
} catch (XmlMergeException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private IdComputer getIdComputer(Element element) {
} else {
this.qName2IdMap.putIfAbsent(qName, id);
}
return this.id2ComputerMap.computeIfAbsent(id, IdComputer::new);
return this.id2ComputerMap.computeIfAbsent(id, i -> new IdComputer(i, context));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.merge.xmlmerger.XmlMergeSupport;

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

private final IdeContext context;

private static final XPathFactory xPathFactory = XPathFactory.newInstance();

private final boolean throwExceptionOnMultipleMatches = Boolean.parseBoolean(System.getProperty("throwExceptionOnMultipleMatches", "false"));


/**
* The constructor.
*
* @param id the {@link #getId() merge ID}.
*/
public IdComputer(String id) {
public IdComputer(String id, IdeContext context) {

super();
this.id = id;
this.context = context;
}

/**
Expand Down Expand Up @@ -61,8 +68,14 @@ public Element evaluateExpression(Element templateElement, Element workspaceElem
} else if (length == 0) {
return null;
} else {
throw new IllegalStateException(
length + " matches found for XPath " + xpathExpr + " in workspace XML at " + XmlMergeSupport.getXPath(workspaceElement, true));
if (throwExceptionOnMultipleMatches) {
throw new IllegalStateException(
length + " matches found for XPath " + xpathExpr + " in workspace XML at " + XmlMergeSupport.getXPath(workspaceElement, true));
} else {
this.context.warning("Matches found: {} matches for XPath {} in workspace XML at {}",
length, xpathExpr, XmlMergeSupport.getXPath(workspaceElement, true));
}
return (Element) nodeList.item(0);
}
} catch (XPathExpressionException e) {
throw new IllegalStateException("Failed to compile XPath expression " + xpath, e);
Expand Down