9
9
import org .w3c .dom .Element ;
10
10
import org .w3c .dom .NodeList ;
11
11
12
+ import com .devonfw .tools .ide .context .IdeContext ;
12
13
import com .devonfw .tools .ide .merge .xmlmerger .XmlMergeSupport ;
13
14
14
15
/**
@@ -19,17 +20,23 @@ public class IdComputer {
19
20
/** The value of merge:id that is used to evaluate the xpath expression. */
20
21
private final String id ;
21
22
23
+ private final IdeContext context ;
24
+
22
25
private static final XPathFactory xPathFactory = XPathFactory .newInstance ();
23
26
27
+ private final boolean throwExceptionOnMultipleMatches = Boolean .parseBoolean (System .getProperty ("throwExceptionOnMultipleMatches" , "false" ));
28
+
29
+
24
30
/**
25
31
* The constructor.
26
32
*
27
33
* @param id the {@link #getId() merge ID}.
28
34
*/
29
- public IdComputer (String id ) {
35
+ public IdComputer (String id , IdeContext context ) {
30
36
31
37
super ();
32
38
this .id = id ;
39
+ this .context = context ;
33
40
}
34
41
35
42
/**
@@ -61,8 +68,14 @@ public Element evaluateExpression(Element templateElement, Element workspaceElem
61
68
} else if (length == 0 ) {
62
69
return null ;
63
70
} 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 );
66
79
}
67
80
} catch (XPathExpressionException e ) {
68
81
throw new IllegalStateException ("Failed to compile XPath expression " + xpath , e );
0 commit comments