Skip to content

Commit dd28132

Browse files
committed
Pad the X12 ISA segment back to fixed width on XML to EDI (#450)
The ISA segment is fixed width, but the XML can lose the padding: a field of spaces pretty-printed to an empty element, or a short value set in a transformer. fromXML then wrote an ISA of the wrong length. EDIXMLHandler now pads short ISA01-ISA15 elements back as it writes them (ISA13 with leading zeros, the rest with trailing spaces) and leaves longer values alone. Signed-off-by: Ezio Caffi <ezio.caffi@gmail.com>
1 parent a750221 commit dd28132

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

‎server/src/main/java/com/mirth/connect/plugins/datatypes/edi/EDIXMLHandler.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ private enum Location {
3232

3333
private StringBuilder output = new StringBuilder();
3434

35+
// The ISA segment is fixed width. Widths of ISA01 through ISA15; ISA16 is always one char.
36+
private static final int[] ISA_FIELD_WIDTHS = { 2, 10, 2, 10, 2, 15, 2, 15, 6, 4, 1, 5, 9, 1, 1 };
37+
private static final int ISA_CONTROL_NUMBER = 13;
38+
39+
private boolean inIsa = false;
40+
private int elementId = 0;
41+
private int elementStart = 0;
42+
3543
public EDIXMLHandler() {
3644
super();
3745
}
@@ -63,13 +71,15 @@ public void startElement(String uri, String name, String qName, Attributes atts)
6371
} else {
6472
if (currentLocation.equals(Location.DOCUMENT)) {
6573
output.append(name);
74+
inIsa = name.equals("ISA");
6675
currentLocation = Location.SEGMENT;
6776
lastInSubelement = false;
6877

6978
previousSegmentNameArray = null;
7079
} else if (currentLocation.equals(Location.SEGMENT)) {
7180
String[] currentNameArray = StringUtils.split(name, ".");
7281
int currentDelimeterCount = currentNameArray.length - 1;
82+
elementId = 0;
7383

7484
if (currentDelimeterCount == 1) {
7585
int previousId = 0;
@@ -82,12 +92,15 @@ public void startElement(String uri, String name, String qName, Attributes atts)
8292

8393
for (int i = 1; i < (currentId - previousId); i++) {
8494
output.append(elementDelimiter);
95+
padIsaElement(previousId + i, output.length());
8596
}
8697

8798
previousSegmentNameArray = currentNameArray;
99+
elementId = currentId;
88100
}
89101

90102
output.append(elementDelimiter);
103+
elementStart = output.length();
91104
currentLocation = Location.ELEMENT;
92105
lastInSubelement = false;
93106

@@ -127,12 +140,34 @@ public void endElement(String uri, String name, String qName) {
127140
output.append(segmentDelimiter);
128141
currentLocation = Location.DOCUMENT;
129142
} else if (currentLocation.equals(Location.ELEMENT)) {
143+
padIsaElement(elementId, elementStart);
130144
currentLocation = Location.SEGMENT;
131145
} else if (currentLocation.equals(Location.SUBELEMENT)) {
132146
currentLocation = Location.ELEMENT;
133147
}
134148
}
135149

150+
/*
151+
* The XML may have lost the ISA padding (e.g. a field of spaces pretty-printed to an empty
152+
* element, or a short value set in a transformer). Pad a short ISA element back so the ISA
153+
* stays valid: the control number with leading zeros, the rest with trailing spaces. Longer
154+
* values are left alone.
155+
*/
156+
private void padIsaElement(int id, int start) {
157+
if (!inIsa || id < 1 || id > ISA_FIELD_WIDTHS.length) {
158+
return;
159+
}
160+
161+
int missing = ISA_FIELD_WIDTHS[id - 1] - (output.length() - start);
162+
if (missing > 0) {
163+
if (id == ISA_CONTROL_NUMBER) {
164+
output.insert(start, StringUtils.repeat('0', missing));
165+
} else {
166+
output.append(StringUtils.repeat(' ', missing));
167+
}
168+
}
169+
}
170+
136171
public void characters(char ch[], int start, int length) {
137172
output.append(ch, start, length);
138173
}

‎server/src/test/java/com/mirth/connect/plugins/datatypes/edi/EDISerializerTest.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,44 @@ public void testFromXMLWithExternalDtd() {
4141
assertTrue(exceptionCaught);
4242
}
4343

44+
@Test
45+
public void testFromXMLKeepsIsaFixedWidth() throws Exception {
46+
String xml = serializer.toXML(validX12);
47+
assertEquals(validX12, serializer.fromXML(xml));
48+
}
49+
50+
@Test
51+
public void testFromXMLPadsBlankIsaFields() throws Exception {
52+
// A field of spaces can come back empty, e.g. after XmlUtil.prettyPrint
53+
String xml = serializer.toXML(validX12).replace("<ISA.02.1> </ISA.02.1>", "<ISA.02.1/>");
54+
assertEquals(validX12, serializer.fromXML(xml));
55+
}
56+
57+
@Test
58+
public void testFromXMLPadsShortIsaValues() throws Exception {
59+
String xml = serializer.toXML(validX12)
60+
.replace("<ISA.06.1>SUBMITTERID </ISA.06.1>", "<ISA.06.1>SUBMITTERID</ISA.06.1>")
61+
.replace("<ISA.13.1>000000001</ISA.13.1>", "<ISA.13.1>1</ISA.13.1>");
62+
assertEquals(validX12, serializer.fromXML(xml));
63+
}
64+
65+
@Test
66+
public void testFromXMLPadsIsaValueSplitAcrossCharacters() throws Exception {
67+
// SAX hands "S&amp;P" over in several characters() calls
68+
String xml = serializer.toXML(validX12).replace("<ISA.06.1>SUBMITTERID </ISA.06.1>", "<ISA.06.1>S&amp;P</ISA.06.1>");
69+
assertEquals(validX12.replace("SUBMITTERID ", "S&P "), serializer.fromXML(xml));
70+
}
71+
72+
@Test
73+
public void testFromXMLPadsEveryIsa() throws Exception {
74+
String twoInterchanges = validX12 + validX12;
75+
String xml = serializer.toXML(twoInterchanges).replace("<ISA.02.1> </ISA.02.1>", "<ISA.02.1/>");
76+
assertEquals(twoInterchanges, serializer.fromXML(xml));
77+
}
78+
79+
private static String validX12 = "ISA*00* *00* *ZZ*SUBMITTERID *ZZ*PAYERID *260330*1430*^*00501*000000001*0*T*:~"
80+
+ "GS*HC*SUBMITTERID*PAYERID*20260330*1430*1*X*005010X222A1~";
81+
4482
private static String validXml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n"
4583
+ "<foo><bar>bar</bar></foo>";
4684

0 commit comments

Comments
 (0)