Java tutorial
/* j8583 A Java implementation of the ISO8583 protocol Copyright (C) 2007 Enrique Zamudio Lopez This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ package com.moadbus.banking.iso.core.data; import java.io.IOException; import java.io.InputStream; import java.io.FileInputStream; import java.net.URL; import java.util.HashMap; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import com.moadbus.banking.common.MessageType; import com.moadbus.banking.util.Config; /** * Class to Parse the config xml * * @author Venu Sathu */ public class DataSetConfigParser { private static final Log log = LogFactory.getLog(DataSetConfigParser.class); private static DataSetFactory dsFactory = null; public static DataSetFactory createDefault() throws IOException { if (dsFactory == null) { dsFactory = createFromClasspathConfig("mbsMessageDataSet.xml"); } return dsFactory; } public static DataSetFactory createFromClasspathConfig(String path) throws IOException { if (dsFactory == null) { InputStream ins = DataSetFactory.class.getClassLoader().getResourceAsStream(path); if (ins == null) { try { System.err.println("TRYING:" + Config.getInstance().get("ISO_XML_PATH") + path); ins = new FileInputStream(Config.getInstance().get("ISO_XML_PATH") + path); } catch (Exception e) { return new DataSetFactory(); } if (ins == null) { log.error("createFromClasspathConfig: File not found in classpath: " + path); return new DataSetFactory(); } } dsFactory = new DataSetFactory(); if (ins != null) { if (log.isDebugEnabled()) { log.debug("Parsing config from classpath file " + path); } try { parse(dsFactory, ins); } finally { ins.close(); } } else { log.error("createFromClasspathConfig: File not found in classpath: " + path); } } return dsFactory; } /** Creates a dataset factory from the file located at the specified URL. */ public static DataSetFactory createFromUrl(URL url) throws IOException { if (dsFactory == null) { dsFactory = new DataSetFactory(); InputStream stream = url.openStream(); try { parse(dsFactory, stream); } finally { stream.close(); } } return dsFactory; } /** * Reads the XML from the stream and configures the dataset factory with its * values. * * @param mfact * The dataset factory to be configured with the values read from * the XML. * @param stream * The InputStream containing the XML configuration. */ protected static void parse(DataSetFactory mfact, InputStream stream) throws IOException { final DocumentBuilderFactory docfact = DocumentBuilderFactory.newInstance(); DocumentBuilder docb = null; Document doc = null; try { docb = docfact.newDocumentBuilder(); doc = docb.parse(stream); } catch (ParserConfigurationException ex) { log.error("parse: Cannot parse XML configuration", ex); return; } catch (SAXException ex) { log.error("parse: Parsing XML configuration", ex); return; } final Element root = doc.getDocumentElement(); // Read the ISO headers NodeList nodes = null; // Read the parsing guides nodes = root.getElementsByTagName("parse"); for (int i = 0; i < nodes.getLength(); i++) { Element elem = (Element) nodes.item(i); String msgtype = elem.getAttribute("msgtype"); MessageType messageType = MessageType.valueOf(msgtype); if (messageType == null) { throw new IOException("Invalid type for parse guide: " + elem.getAttribute("msgtype")); } NodeList fields = elem.getElementsByTagName("field"); /* * HashMap<Integer, DataElementParseInfo> parseMap = new * HashMap<Integer, DataElementParseInfo>(); for (int j = 0; j < * fields.getLength(); j++) { Element f = (Element) fields.item(j); * int num = Integer.parseInt(f.getAttribute("num")); * DataElementType datatype = * DataElementType.valueOf(f.getAttribute("datatype")); int length = * 0, decimallength = 0; if (f.getAttribute("length").length() > 0) * { length = Integer.parseInt(f.getAttribute("length")); } if * (f.getAttribute("decimallength").length() > 0) { decimallength = * Integer.parseInt(f.getAttribute("decimallength")); } String * description = elem.getAttribute("description"); parseMap.put(num, * new DataElementParseInfo(datatype, length, description, * decimallength)); } mfact.setParseMap(messageType, parseMap); */ HashMap<Integer, DataElementParseInfoList> parseMap = new HashMap<Integer, DataElementParseInfoList>(); for (int j = 0; j < fields.getLength(); j++) { Element f = (Element) fields.item(j); DataElementParseInfoList fieldList = new DataElementParseInfoList(); int num = 0; if (f.getAttribute("num").length() > 0) { num = Integer.parseInt(f.getAttribute("num")); } DataElementType datatype = DataElementType.valueOf(f.getAttribute("datatype")); int length = 0, decimallength = 0; if (f.getAttribute("length").length() > 0) { length = Integer.parseInt(f.getAttribute("length")); } if (f.getAttribute("decimallength").length() > 0) { decimallength = Integer.parseInt(f.getAttribute("decimallength")); } String description = f.getAttribute("description"); String repeatsStr = f.getAttribute("repeats"); if (repeatsStr != null && repeatsStr.length() > 0) { int repeats = -1; try { repeats = Integer.parseInt(repeatsStr); } catch (Exception e) { log.error("parse: " + e.getMessage(), e); } fieldList.setRepeats(repeats); } NodeList subfields = f.getElementsByTagName("subfield"); if (subfields != null && subfields.getLength() == 0) { // System.out.println(f+" have subfields "+subfields); fieldList.add(new DataElementParseInfo(datatype, length, description, decimallength)); } else { for (int k = 0; k < subfields.getLength(); k++) { Element subfield = (Element) subfields.item(k); /*int snum = 0; if(subfield.getAttribute("num").length()>0) { snum = Integer.parseInt(subfield .getAttribute("num")); }*/ DataElementType sdatatype = DataElementType.valueOf(subfield.getAttribute("datatype")); int slength = 0, sdecimallength = 0; if (subfield.getAttribute("length").length() > 0) { slength = Integer.parseInt(subfield.getAttribute("length")); } if (subfield.getAttribute("decimallength").length() > 0) { sdecimallength = Integer.parseInt(subfield.getAttribute("decimallength")); } String sdescription = subfield.getAttribute("description"); fieldList.add(new DataElementParseInfo(sdatatype, slength, sdescription, sdecimallength)); } } parseMap.put(num, fieldList); } mfact.setParseMap(messageType, parseMap); } } /** * Parses a message type expressed as a hex string and returns the integer * number. For example, "0200" or "200" return the number 512 (0x200) */ /* private static int parseType(String type) throws IOException { if (type.length() % 2 == 1) { type = "0" + type; } if (type.length() != 4) { return -1; } return ((type.charAt(0) - 48) << 12) | ((type.charAt(1) - 48) << 8) | ((type.charAt(2) - 48) << 4) | (type.charAt(3) - 48); } */ }