Skip to content

Commit 50d0ffd

Browse files
committed
Unexpected diff #11
1 parent 95f218f commit 50d0ffd

File tree

6 files changed

+83
-14
lines changed

6 files changed

+83
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Optionally, argument `-u` treats XML nodes as unordered so moves among children
3131

3232
## Merge
3333

34-
The XmlMerge utility takes a base XML file and a difference file and merges the two to produce an new XML file.
34+
The XmlMerge utility takes a base XML file and a difference file and merges the two to produce a new XML file.
3535

3636
To run the merge utility, run this command line:
3737

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
<properties>
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
<java.version>11</java.version>
18-
<junit.version>5.7.1</junit.version>
18+
<junit.version>5.8.2</junit.version>
1919
<log4j.version>2.16.0</log4j.version>
20-
<orchestra.version>1.6.10</orchestra.version>
21-
<saxon.version>10.3</saxon.version>
20+
<orchestra.version>1.7.3</orchestra.version>
21+
<saxon.version>10.6</saxon.version>
2222
<ignoreSigningInformation>true</ignoreSigningInformation>
2323
</properties>
2424

src/main/java/io/fixprotocol/xml/XmlDiff.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,33 @@ public int compare(Element n1, Element n2) {
7979
retv = name1.compareTo(name2);
8080
}
8181
}
82+
if (retv == 0) {
83+
retv = new ElementTextComparator().compare(n1, n2);
84+
}
8285
}
8386
return retv;
8487
}
8588
}
8689

90+
private static final class ElementTextComparator implements Comparator<Element> {
91+
92+
@Override
93+
public int compare(Element o1, Element o2) {
94+
95+
final Node child1 = o1.getFirstChild();
96+
final Node child2 = o2.getFirstChild();
97+
98+
String text1 =
99+
(child1 != null && Node.TEXT_NODE == child1.getNodeType()) ? child1.getNodeValue().trim()
100+
: "";
101+
String text2 =
102+
(child2 != null && Node.TEXT_NODE == child2.getNodeType()) ? child2.getNodeValue().trim()
103+
: "";
104+
105+
return text1.compareTo(text2);
106+
}
107+
}
108+
87109
private static final Logger logger = LogManager.getLogger();
88110

89111
/**
@@ -418,8 +440,8 @@ private boolean diffText(Element element1, Element element2) {
418440
if (child2 == null || Node.TEXT_NODE != child2.getNodeType()) {
419441
listener.accept(Event.remove(XpathUtil.getFullXPath(child1)));
420442
} else {
421-
final int valueCompare = stripWhitespace(child1.getNodeValue())
422-
.compareTo(stripWhitespace(child2.getNodeValue()));
443+
final int valueCompare =
444+
child1.getNodeValue().trim().compareTo(child2.getNodeValue().trim());
423445

424446
if (valueCompare != 0) {
425447
listener.accept(Event.replace(XpathUtil.getFullXPath(child1), child2, child1));
@@ -428,7 +450,7 @@ private boolean diffText(Element element1, Element element2) {
428450
}
429451
}
430452
} else if (child2 != null && Node.TEXT_NODE == child2.getNodeType()
431-
&& stripWhitespace(child2.getNodeValue()).length() > 0) {
453+
&& child2.getNodeValue().trim().length() > 0) {
432454
listener.accept(Event.add(XpathUtil.getFullXPath(child2), child2, append));
433455
}
434456
return false;
@@ -472,8 +494,4 @@ private ArrayList<Attr> sortAttributes(NamedNodeMap attributes, ArrayList<Attr>
472494
return nodeArray;
473495
}
474496

475-
private String stripWhitespace(String str) {
476-
return str.replaceAll("\\s", "");
477-
}
478-
479497
}

src/test/java/io/fixprotocol/xml/XmlDiffTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ public void unordered() throws Exception {
108108
// remove <fff id="1"/>
109109
// add <fff id="3"/>
110110
//
111-
assertEquals(2, doc.getElementsByTagName("add").getLength());
112-
assertEquals(2, doc.getElementsByTagName("replace").getLength());
113-
assertEquals(2, doc.getElementsByTagName("remove").getLength());
111+
assertEquals(3, doc.getElementsByTagName("add").getLength());
112+
assertEquals(1, doc.getElementsByTagName("replace").getLength());
113+
assertEquals(3, doc.getElementsByTagName("remove").getLength());
114114

115115
try (
116116
final FileInputStream is1Baseline = new FileInputStream(
@@ -160,4 +160,11 @@ public void epDiff() throws Exception {
160160
}
161161
}
162162

163+
164+
@Test
165+
public void xsdDiff() throws Exception {
166+
final String diffFilename = "target/test/xsddiff.xml";
167+
XmlDiff.main(new String[] {"src/test/resources/Enums-new.xsd", "src/test/resources/Enums-old.xsd", diffFilename, "-u"});
168+
}
169+
163170
}

src/test/resources/Enums-new.xsd

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3+
xmlns:fm="http://www.fixprotocol.org/FIXML-Latest/METADATA"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<xs:simpleType name="SecurityType_enum_t">
6+
<xs:annotation>
7+
<xs:documentation>Indicates type of security. Security type enumerations are grouped by Product(460) field value. NOTE: Additional values may be used by mutual agreement of the counterparties.</xs:documentation>
8+
<xs:appinfo>
9+
<fm:Xref Protocol="FIX" name="SecurityType" ComponentType="Field" Tag="167"
10+
Type="String"
11+
AbbrName="SecTyp"/>
12+
</xs:appinfo>
13+
<xs:appinfo>
14+
<fm:EnumDoc value="EUSUPRA">Euro Supranational Coupons *</fm:EnumDoc>
15+
<fm:EnumDoc value="FOR">Foreign Exchange Contract</fm:EnumDoc>
16+
<fm:EnumDoc value="CS">Common Stock</fm:EnumDoc>
17+
<fm:EnumDoc value="REPO">Repurchase</fm:EnumDoc>
18+
<fm:EnumDoc value="CORP">Corporate Bond</fm:EnumDoc>
19+
</xs:appinfo>
20+
</xs:annotation>
21+
</xs:simpleType>
22+
</xs:schema>

src/test/resources/Enums-old.xsd

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3+
xmlns:fm="http://www.fixprotocol.org/FIXML-Latest/METADATA"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<xs:simpleType name="SecurityType_enum_t">
6+
<xs:annotation>
7+
<xs:documentation>Indicates type of security. Security type enumerations are grouped by Product(460) field value. NOTE: Additional values may be used by mutual agreement of the counterparties.</xs:documentation>
8+
<xs:appinfo>
9+
<fm:Xref Protocol="FIX" name="SecurityType" ComponentType="Field" Tag="167"
10+
Type="String"
11+
AbbrName="SecTyp"/>
12+
</xs:appinfo>
13+
<xs:appinfo>
14+
<fm:EnumDoc value="EUSUPRA">Euro Supranational Coupons *</fm:EnumDoc>
15+
<fm:EnumDoc value="CORP">Corporate Bond</fm:EnumDoc>
16+
<fm:EnumDoc value="FOR">Foreign Exchange Contract</fm:EnumDoc>
17+
<fm:EnumDoc value="CS">Common Stock</fm:EnumDoc>
18+
<fm:EnumDoc value="REPO">Repurchase</fm:EnumDoc>
19+
</xs:appinfo>
20+
</xs:annotation>
21+
</xs:simpleType>
22+
</xs:schema>

0 commit comments

Comments
 (0)