From 86d4cdb3eebe76ff8f9c3d9c85f0141375237f94 Mon Sep 17 00:00:00 2001 From: Enrique Zamudio Date: Sun, 3 May 2015 11:07:14 -0500 Subject: [PATCH] fix #41 --- .../com/solab/iso8583/parse/ConfigParser.java | 79 +++++++++++++------ 1 file changed, 54 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/solab/iso8583/parse/ConfigParser.java b/src/main/java/com/solab/iso8583/parse/ConfigParser.java index 7a1d9e9..d72caac 100644 --- a/src/main/java/com/solab/iso8583/parse/ConfigParser.java +++ b/src/main/java/com/solab/iso8583/parse/ConfigParser.java @@ -28,6 +28,7 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import com.solab.iso8583.*; import com.solab.iso8583.codecs.CompositeField; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,11 +39,6 @@ import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import com.solab.iso8583.CustomField; -import com.solab.iso8583.IsoMessage; -import com.solab.iso8583.IsoType; -import com.solab.iso8583.MessageFactory; - /** This class is used to parse a XML configuration file and configure * a MessageFactory with the values from it. * @@ -179,15 +175,12 @@ public class ConfigParser { for (int j = 0; j < fields.getLength(); j++) { Element f = (Element)fields.item(j); if (f.getParentNode()==elem) { - int num = Integer.parseInt(f.getAttribute("num")); - IsoType itype = IsoType.valueOf(f.getAttribute("type")); - int length = 0; - if (f.getAttribute("length").length() > 0) { - length = Integer.parseInt(f.getAttribute("length")); + final int num = Integer.parseInt(f.getAttribute("num")); + IsoValue v = getTemplateField(f, mfact, true); + if (v != null) { + v.setCharacterEncoding(mfact.getCharacterEncoding()); } - String v = f.getChildNodes().item(0).getNodeValue(); - CustomField _cf = mfact.getCustomField(num); - m.setValue(num, _cf == null ? v : _cf.decodeField(v), _cf, itype, length); + m.setField(num, v); } } mfact.addMessageTemplate(m); @@ -201,7 +194,7 @@ public class ConfigParser { + elem.getAttribute("type") + " extends invalid template " + elem.getAttribute("extends")); } - T tref = mfact.getMessageTemplate(ref); + IsoMessage tref = mfact.getMessageTemplate(ref); if (tref == null) { throw new IllegalArgumentException("Message template " + elem.getAttribute("type") + " extends nonexistent template " @@ -220,18 +213,12 @@ public class ConfigParser { for (int j = 0; j < fields.getLength(); j++) { Element f = (Element)fields.item(j); int num = Integer.parseInt(f.getAttribute("num")); - final String typedef = f.getAttribute("type"); - if ("exclude".equals(typedef)) { - m.setField(num, null); - } else { - IsoType itype = IsoType.valueOf(typedef); - int length = 0; - if (f.getAttribute("length").length() > 0) { - length = Integer.parseInt(f.getAttribute("length")); + if (f.getParentNode()==elem) { + IsoValue v = getTemplateField(f, mfact, true); + if (v != null) { + v.setCharacterEncoding(mfact.getCharacterEncoding()); } - String v = f.getChildNodes().item(0).getNodeValue(); - CustomField _cf = mfact.getCustomField(num); - m.setValue(num, _cf == null ? v : _cf.decodeField(v), _cf, itype, length); + m.setField(num, v); } } mfact.addMessageTemplate(m); @@ -239,6 +226,48 @@ public class ConfigParser { } } + /** Creates an IsoValue from the XML definition in a message template. + * If it's for a toplevel field and the message factory has a codec for this field, + * that codec is assigned to that field. For nested fields, a CompositeField is + * created and populated. */ + protected static IsoValue getTemplateField( + Element f, MessageFactory mfact, boolean toplevel) { + final int num = Integer.parseInt(f.getAttribute("num")); + final String typedef = f.getAttribute("type"); + if ("exclude".equals(typedef)) { + return null; + } + int length = 0; + if (f.getAttribute("length").length() > 0) { + length = Integer.parseInt(f.getAttribute("length")); + } + final IsoType itype = IsoType.valueOf(typedef); + final NodeList subs = f.getElementsByTagName("field"); + if (subs != null && subs.getLength() > 0) { + //Composite field + final CompositeField cf = new CompositeField(); + for (int j = 0; j < subs.getLength(); j++) { + Element sub = (Element)subs.item(j); + if (sub.getParentNode()==f) { + IsoValue sv = getTemplateField(sub, mfact, false); + if (sv != null) { + sv.setCharacterEncoding(mfact.getCharacterEncoding()); + cf.addValue(sv); + } + } + } + return itype.needsLength() ? new IsoValue<>(itype, cf, length, cf) : + new IsoValue<>(itype, cf, cf); + } + final String v = f.getChildNodes().item(0).getNodeValue(); + final CustomField cf = toplevel ? mfact.getCustomField(num) : null; + if (cf == null) { + return itype.needsLength() ? new IsoValue<>(itype, v, length) : new IsoValue<>(itype, v); + } + return itype.needsLength() ? new IsoValue<>(itype, cf.decodeField(v), length, cf) : + new IsoValue<>(itype, cf.decodeField(v), cf); + } + protected static FieldParseInfo getParser( Element f, MessageFactory mfact) throws IOException { IsoType itype = IsoType.valueOf(f.getAttribute("type"));