Some useful hints for this Helper
****************************************************************************
By default CalcDiff() function works in "NonOrderlySafe" mode. It means, that nodes in Lists will be compared without
checking order of nodes.
You can change that by setting "orderlySafeMode" == True.
For example:
XmlList1:
<Nodes>
<Node1/>
<Node2/>
<Node3/>
</Nodes>
Xml2List2:<Nodes>
<Node1/>
<Node3/>
<Node2/>
</Nodes>
1) orderlySafeMode = false; // Default
orderlySafeMode = true;
<Dealers>
<Dealer id="1">
<Prop1>
<Prop2>
<Prop3>
</Dealer>
<Dealer id="2">
<Prop1>
<Prop2>
<Prop3>
</Dealer>
</Dealers>
<Dealers><br>
<Dealer id="1"><br>
<Prop1><br>
<Prop3><br>
<Prop2><br>
</Dealer><br>
<Dealer id="2"><br>
<Prop1><br>
<Prop2><br>
<Prop3><br>
</Dealer><br>
</Dealers><br>
orderlySafeCompareChildrenMode = false;
orderlySafeCompareChildrenMode = true; //Default
#ignoreNodes
<dealer count="235">
<SpecialProperties count="0"/>
</dealer>
xdh.ignoreAttrs = ["SpecialProperties.@count"]; // "count" Attribute from "SpecialProperties" only will be ignored
<dealer>
<property1/>
<SpecialProperties>
<property1>
<property2>
</SpecialProperties>
</dealer>
xdh.ignoreAttrs = ["SpecialProperties.property1"]; // "property1" Node from "SpecialProperties" only will be ignored
ignoreNodesWValues
, whereKey
- xmlPath to Node(s) that you want to ignore during comparing,Value
- value that specified Node must have to be ignored.Value
. In this case last element in Path must be marked with '@' symbol.Value
.<dealer><br>
<SpecialProperties><br>
<property>NVD</property><br>
</SpecialProperties><br>
</dealer><br>
<dealer><br>
<SpecialProperties><br>
<property>Special</property><br>
</SpecialProperties><br>
</dealer><br>
xdh.ignoreNodesWValues = ["dealer.SpecialProperties.property" = "Special"]; // Only Dealer2 will be ignored.
#ignoreCommand
.<dealer count="235">
<SpecialProperties count="0"/>
</dealer>
xdh.ignoreCommand = {NodeChild XML ->
return XML.@count != "" && XML.parent().name() == "SpecialProperties";
;
}
"count" Attribute from "SpecialProperties" only will be ignoredignoreCommand
.<dealer>
<SpecialProperties>
<property>NVD</property>
<property>Special</property>
</SpecialProperties>
</dealer>
xdh.ignoreCommand = {NodeChild XML ->
return XML.name() == "property" && XML.localText()[0] == "NVD";
;
}
"property" Node with text "NVD" only will be ignored. "property" Node with text "Special" will be compared.listPath
- string of NodeNames separated with "." (dots)xml
- original XMLignoreFilter
public Tests(){
NodeChild qaXML = new XmlSlurper().parse(new File(props.outputDir+"/example/qa_diff-getDealers"));
NodeChild prodXML = new XmlSlurper().parse(new File(props.outputDir+"/example/prod_diff-getDealers"));
XmlDiffHelper xdh = new XmlDiffHelper(XmlDiffHelper.walkXmlByPath(listPath, qaXML), XmlDiffHelper.walkXmlByPath(listPath, prodXML));
xdh.setIgnoreAttrs(["SpecialProperties.@count"]);
xdh.ignoreCommand = this.&filter;
xdh.calcDiff();
public filter(){
return XML.name() == "property" && XML.localText()[0] == "NCV";
}
}
****************************************************Modifiers | Name | Description |
---|---|---|
java.util.List<java.lang.String> |
ignoreAttrs |
Put java.util.List<java.lang.String> to ignoreAttrs with xmlPath to Attribute(s) that you want to ignore during comparing. |
groovy.lang.Closure<groovy.util.slurpersupport.NodeChild> |
ignoreCommand |
Assign groovy.lang.Closure that accepts 1 groovy.util.slurpersupport.NodeChild parameter to ignoreCommand . |
java.util.List<java.lang.String> |
ignoreNodes |
Put java.util.List<java.lang.String> to ignoreNodes with xmlPath to Node(s) that you want to ignore during comparing. |
java.util.HashMap<java.lang.String, java.lang.String> |
ignoreNodesWValues |
Put java.util.Map<java.lang.String, java.lang.String> to ignoreNodesWValues , whereKey - xmlPath to Node(s) that you want to ignore during comparing,Value - value that specified Node must have to be ignored. |
java.util.List<java.lang.String> |
needleHelper |
|
java.lang.Boolean |
orderlySafeChildrenMode |
By default compareNodes() works in "OrderlySafe" mode. |
java.lang.Boolean |
orderlySafeMode |
By default CalcDiff() function works in "NonOrderlySafe" mode. |
private java.util.List<groovy.util.slurpersupport.NodeChild> |
source1 |
|
private java.util.List<groovy.util.slurpersupport.NodeChild> |
source2 |
Constructor and description |
---|
XmlDiffHelper
(java.util.List<groovy.util.slurpersupport.NodeChild> xml1, java.util.List<groovy.util.slurpersupport.NodeChild> xml2) |
Type | Name and description |
---|---|
private java.lang.Boolean |
_compareAttrs(java.util.Map<java.lang.String, java.lang.String> attrs1, java.util.Map<java.lang.String, java.lang.String> attrs2, java.util.Map<java.lang.String, java.util.List<java.lang.String>> debug) |
private java.lang.Boolean |
_compareChildren(java.util.List<groovy.util.slurpersupport.NodeChild> nodeChildren1, java.util.List<groovy.util.slurpersupport.NodeChild> nodeChildren2) |
private java.lang.Object |
_deleteIgnoredAttrs(groovy.util.slurpersupport.NodeChild node, java.util.List<java.lang.String> _ref = null) |
protected groovy.util.slurpersupport.NodeChild |
_deleteIgnoredElements(groovy.util.slurpersupport.NodeChild node) |
private java.util.List<groovy.util.slurpersupport.NodeChild> |
_deleteIgnoredNodes(java.util.List<groovy.util.slurpersupport.NodeChild> Xml, java.util.List<groovy.util.slurpersupport.NodeChild> _ref = null) |
private java.lang.Boolean |
_isAttrIgnorable(groovy.util.slurpersupport.NodeChild node) |
private java.lang.Boolean |
_isNodeIgnorable(groovy.util.slurpersupport.NodeChild node) |
void |
calcDiff() Performs calculation of differences between consumed java.util.List of groovy.util.slurpersupport.NodeChild. |
java.lang.Boolean |
compareNodes(groovy.util.slurpersupport.NodeChild node1, groovy.util.slurpersupport.NodeChild node2) Performs comparison between specified groovy.util.slurpersupport.NodeChild. |
java.lang.String |
getDiffString(java.lang.Boolean source) Returns java.lang.String with XML that shows diff found in specified element |
java.util.List<groovy.util.slurpersupport.NodeChild> |
retainNodes(java.lang.Boolean source) Performing Diff calculation between consumed java.util.List of groovy.util.slurpersupport.NodeChild and deletion from source elements that match with second list. |
void |
setupFromConfig(java.util.Map params) Automatically scans params for elements that match with names of public parameters of XmlDiffHelper
and assigns values to them. |
Put java.util.List<java.lang.String> to ignoreAttrs with xmlPath to Attribute(s) that you want to ignore during comparing.
xmlPath in this case in list of NodeNames, separated with dot "." to your Attribute.
You don't have to put whole path to Attribute.
If you aware, that AttributeName is unique in XML, you can put it to the map.
If you want to exclude Attribute from specific ParentNode only, then you have to put ParentNodeName into the xmlPath.
Note! That last element in tree must be Attribute name and it must be marked with "@". Mostly, it's done for making distinction with #ignoreNodes
For example:
<dealer count="235">
<SpecialProperties count="0"/>
</dealer>
xdh.ignoreAttrs = ["SpecialProperties.@count"]; // "count" Attribute from "SpecialProperties" only will be ignored
Assign groovy.lang.Closure that accepts 1 groovy.util.slurpersupport.NodeChild parameter to ignoreCommand
.
groovy.lang.Closure must return Boolean value only.
true -> Node will be ignored
false -> Node will be used for comparing
For example:
<dealer>
<SpecialProperties>
<property>NVD</property>
<property>Special</property>
</SpecialProperties>
</dealer>
xdh.ignoreCommand = {NodeChild XML ->
return XML.name() == "property" && XML.localText()[0] == "NVD";
;
}
"property" Node with text "NVD" only will be ignored. "property" Node with text "Special" will be compared. Put java.util.List<java.lang.String> to ignoreNodes with xmlPath to Node(s) that you want to ignore during comparing.
xmlPath in this case in list of NodeNames, separated with dot "." to your Node.
You don't have to put whole path to Node.
If you aware, that NodeName is unique in XML, you can put it to the map.
If you want to exclude Node from specific ParentNode only, then you have to put ParentNodeName into the xmlPath.
Also you can delete Node that has groovy.util.slurpersupport.Attribute. In this case last element in Path must be marked with '@' symbol.
Also you can delete Node that has another groovy.util.slurpersupport.NodeChild or groovy.util.slurpersupport.Attribute.
For doing that, separate your path with '>' symbol. Everything before '>' is related to current groovy.util.slurpersupport.NodeChild itself,
everything after is related to groovy.util.slurpersupport.NodeChildren. In this case you have to put whole path to children element.
For example:
<dealer>
<property1/>
<SpecialProperties>
<property1>
<property2>
</SpecialProperties>
</dealer>
xdh.ignoreAttrs = ["SpecialProperties.property1"]; // "property1" Node from "SpecialProperties" only will be ignored
Put java.util.Map<java.lang.String, java.lang.String> to ignoreNodesWValues
, where
Key
- xmlPath to Node(s) that you want to ignore during comparing,
Value
- value that specified Node must have to be ignored. RegExp
expression are also accepted and will be tested.
You don't have to put whole path to Node.
If you aware, that NodeName is unique in XML, you can put it to the map.
If you want to exclude Node from specific ParentNode only, then you have to put ParentNodeName into the xmlPath.
Also you can delete Node that has groovy.util.slurpersupport.Attribute equal to specified Value
. In this case last element in Path must be marked with '@' symbol.
Also you can delete Node that has another groovy.util.slurpersupport.NodeChild or groovy.util.slurpersupport.Attribute that is equal to specified Value
.
For doing that, separate your path with '>' symbol. Everything before '>' is related to current groovy.util.slurpersupport.NodeChild itself,
everything after is related to groovy.util.slurpersupport.NodeChildren. In this case you have to put whole path to children element.
For example:
<dealer><br>
<SpecialProperties><br>
<property>NVD</property><br>
</SpecialProperties><br>
</dealer><br>
<dealer><br>
<SpecialProperties><br>
<property>Special</property><br>
</SpecialProperties><br>
</dealer><br>
xdh.ignoreNodesWValues = ["dealer.SpecialProperties.property" = "Special"]; // Only Dealer2 will be ignored.
By default compareNodes() works in "OrderlySafe" mode. It means, that ChildrenNodes will be compared in order as they comes
in original XML feed.
You can change that by setting "orderlySafeCompareChildrenMode" == False.
NOTE! All children will be compared according to selected Mode. Even children included in child included in child etc.
For example:
<Dealers>
<Dealer id="1">
<Prop1>
<Prop2>
<Prop3>
</Dealer>
<Dealer id="2">
<Prop1>
<Prop2>
<Prop3>
</Dealer>
</Dealers>
<Dealers><br>
<Dealer id="1"><br>
<Prop1><br>
<Prop3><br>
<Prop2><br>
</Dealer><br>
<Dealer id="2"><br>
<Prop1><br>
<Prop2><br>
<Prop3><br>
</Dealer><br>
</Dealers><br>
orderlySafeCompareChildrenMode = false;
orderlySafeCompareChildrenMode = true; //Default
By default CalcDiff() function works in "NonOrderlySafe" mode. It means, that nodes in Lists will be compared without
checking order of nodes.
You can change that by setting "orderlySafeMode" == True.
For example:
XmlList1:
<Nodes>
<Node1/>
<Node2/>
<Node3/>
</Nodes>
Xml2List2:<Nodes>
<Node1/>
<Node3/>
<Node2/>
</Nodes>
1) orderlySafeMode = false; // Default
orderlySafeMode = true;
Performs calculation of differences between consumed java.util.List of groovy.util.slurpersupport.NodeChild. Result of Diff calculation depends on parameters specified before calling this method. Every time when called, overrides Diff and output Xml.
Performs comparison between specified groovy.util.slurpersupport.NodeChild.
Result of comparison depends on parameters specified before calling this method.
If notifications are enabled, may create Console messages with additional info if Diff found.
Console messages may appear even if result is true. It depends on orderlySafeChildrenMode
.
If orderlySafeChildrenMode = false
, Diff could be found between 2 child nodes, but match may happen
with other child node in future as system will continue comparison with all children on the same level.
Returns java.lang.String with XML that shows diff found in specified element
source
- true to use #source1
, false to use #source2
Performing Diff calculation between consumed java.util.List of groovy.util.slurpersupport.NodeChild
and deletion from source
elements that match with second list.
source
- true to use #source1
, false to use #source2
Automatically scans params
for elements that match with names of public
parameters of XmlDiffHelper
and assigns values to them.
params
- java.util.Map of parameters that will be automatically mapped to public
parameters of XmlDiffHelper