This is the mavenized version of the project, and it's the one I'll be working with from now on.

git-svn-id: https://j8583.svn.sourceforge.net/svnroot/j8583/j8583@52 db60a039-f72e-0410-b1d2-baa000846441
This commit is contained in:
chochos
2010-11-26 00:30:40 +00:00
commit 1b07cf9a32
21 changed files with 2075 additions and 0 deletions
@@ -0,0 +1,224 @@
/*
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.solab.iso8583.parse;
import java.io.IOException;
import java.io.InputStream;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
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.
*
* @author Enrique Zamudio
*/
public class ConfigParser {
private final static Logger log = LoggerFactory.getLogger(ConfigParser.class);
/** Creates a message factory configured from the default file, which is j8583.xml
* located in the root of the classpath. */
public static MessageFactory createDefault() throws IOException {
if (MessageFactory.class.getClassLoader().getResource("j8583.xml") == null) {
log.warn("ISO8583 ConfigParser cannot find j8583.xml, returning empty message factory");
return new MessageFactory();
} else {
return createFromClasspathConfig("j8583.xml");
}
}
/** Creates a message factory from the specified path inside the classpath. */
public static MessageFactory createFromClasspathConfig(String path) throws IOException {
InputStream ins = MessageFactory.class.getClassLoader().getResourceAsStream(path);
MessageFactory mfact = new MessageFactory();
if (ins != null) {
if (log.isDebugEnabled()) {
log.debug("ISO8583 Parsing config from classpath file {}", path);
}
try {
parse(mfact, ins);
} finally {
ins.close();
}
} else {
log.warn("ISO8583 File not found in classpath: {}", path);
}
return mfact;
}
/** Creates a message factory from the file located at the specified URL. */
public static MessageFactory createFromUrl(URL url) throws IOException {
MessageFactory mfact = new MessageFactory();
InputStream stream = url.openStream();
try {
parse(mfact, stream);
} finally {
stream.close();
}
return mfact;
}
/** Reads the XML from the stream and configures the message factory with its values.
* @param mfact The message factory to be configured with the values read from the XML.
* @param stream The InputStream containing the XML configuration. */
protected static void parse(MessageFactory 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("ISO8583 Cannot parse XML configuration", ex);
return;
} catch (SAXException ex) {
log.error("ISO8583 Parsing XML configuration", ex);
return;
}
final Element root = doc.getDocumentElement();
//Read the ISO headers
NodeList nodes = root.getElementsByTagName("header");
Element elem = null;
for (int i = 0; i < nodes.getLength(); i++) {
elem = (Element)nodes.item(i);
int type = parseType(elem.getAttribute("type"));
if (type == -1) {
throw new IOException("Invalid type for ISO8583 header: " + elem.getAttribute("type"));
}
if (elem.getChildNodes() == null || elem.getChildNodes().getLength() == 0) {
throw new IOException("Invalid ISO8583 header element");
}
String header = elem.getChildNodes().item(0).getNodeValue();
if (log.isTraceEnabled()) {
log.trace("Adding ISO8583 header for type {}: {}", elem.getAttribute("type"), header);
}
mfact.setIsoHeader(type, header);
}
//Read the message templates
nodes = root.getElementsByTagName("template");
for (int i = 0; i < nodes.getLength(); i++) {
elem = (Element)nodes.item(i);
int type = parseType(elem.getAttribute("type"));
if (type == -1) {
throw new IOException("Invalid ISO8583 type for template: " + elem.getAttribute("type"));
}
NodeList fields = elem.getElementsByTagName("field");
IsoMessage m = new IsoMessage();
m.setType(type);
for (int j = 0; j < fields.getLength(); j++) {
Element f = (Element)fields.item(j);
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"));
}
String v = f.getChildNodes().item(0).getNodeValue();
CustomField<?> _cf = mfact.getCustomField(num);
m.setValue(num, _cf == null ? v : _cf.decodeField(v), _cf, itype, length);
}
mfact.addMessageTemplate(m);
}
//Read the parsing guides
nodes = root.getElementsByTagName("parse");
for (int i = 0; i < nodes.getLength(); i++) {
elem = (Element)nodes.item(i);
int type = parseType(elem.getAttribute("type"));
if (type == -1) {
throw new IOException("Invalid ISO8583 type for parse guide: " + elem.getAttribute("type"));
}
NodeList fields = elem.getElementsByTagName("field");
HashMap<Integer, FieldParseInfo> parseMap = new HashMap<Integer, FieldParseInfo>();
for (int j = 0; j < fields.getLength(); j++) {
Element f = (Element)fields.item(j);
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"));
}
parseMap.put(num, new FieldParseInfo(itype, length));
}
mfact.setParseMap(type, parseMap);
}
}
/** Configures a MessageFactory using the default configuration file j8583.xml. This is useful
* if you have a MessageFactory created using Spring for example. */
public static void configureFromDefault(MessageFactory mfact) throws IOException {
if (MessageFactory.class.getClassLoader().getResource("j8583.xml") == null) {
log.warn("ISO8583 config file j8583.xml not found!");
} else {
configureFromClasspathConfig(mfact, "j8583.xml");
}
}
/** Configures a MessageFactory using the configuration file at the path specified (will be searched
* within the classpath using the MessageFactory's ClassLoader). This is useful for configuring
* Spring-bound instances of MessageFactory for example. */
public static void configureFromClasspathConfig(MessageFactory mfact, String path) throws IOException {
InputStream ins = MessageFactory.class.getClassLoader().getResourceAsStream(path);
if (ins != null) {
if (log.isDebugEnabled()) {
log.debug("ISO8583 Parsing config from classpath file {}", path);
}
try {
parse(mfact, ins);
} finally {
ins.close();
}
} else {
log.warn("ISO8583 File not found in classpath: {}", path);
}
}
/** 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);
}
}
@@ -0,0 +1,318 @@
/*
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.solab.iso8583.parse;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import com.solab.iso8583.CustomField;
import com.solab.iso8583.IsoType;
import com.solab.iso8583.IsoValue;
/** This class contains the information needed to parse a field from a message buffer.
*
* @author Enrique Zamudio
*/
public class FieldParseInfo {
private IsoType type;
private int length;
/** Creates a new instance that parses a value of the specified type, with the specified length.
* The length is only useful for ALPHA and NUMERIC types.
* @param t The ISO type to be parsed.
* @param len The length of the data to be read (useful only for ALPHA and NUMERIC types). */
public FieldParseInfo(IsoType t, int len) {
if (t == null) {
throw new IllegalArgumentException("IsoType cannot be null");
}
type = t;
length = len;
}
/** Returns the specified length for the data to be parsed. */
public int getLength() {
return length;
}
/** Returns the data type for the data to be parsed. */
public IsoType getType() {
return type;
}
/** Parses the character data from the buffer and returns the
* IsoValue with the correct data type in it. */
public <T extends Object> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom) throws ParseException {
if (pos < 0) {
throw new ParseException(String.format("Invalid position %d", pos), pos);
}
if (type == IsoType.NUMERIC || type == IsoType.ALPHA) {
if (pos+length > buf.length) {
throw new ParseException(String.format("Insufficient data for ALPHA or NUMERIC field of length %d, pos %d",
length, pos), pos);
}
if (custom == null) {
return new IsoValue<String>(type, new String(buf, pos, length), length, null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(new String(buf, pos, length)), length, custom);
if (v.getValue() == null) {
return new IsoValue<String>(type, new String(buf, pos, length), length, null);
}
return v;
}
} else if (type == IsoType.LLVAR) {
length = ((buf[pos] - 48) * 10) + (buf[pos + 1] - 48);
if (length < 0) {
throw new ParseException(String.format("Invalid LLVAR length %d pos %d", length, pos), pos);
}
if (pos+2 > buf.length || length+pos+2 > buf.length) {
throw new ParseException(String.format("Insufficient data for LLVAR field, pos %d", pos), pos);
}
if (custom == null) {
return new IsoValue<String>(type, new String(buf, pos + 2, length), null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(new String(buf, pos + 2, length)), custom);
if (v.getValue() == null) {
return new IsoValue<String>(type, new String(buf, pos + 2, length), null);
}
return v;
}
} else if (type == IsoType.LLLVAR) {
length = ((buf[pos] - 48) * 100) + ((buf[pos + 1] - 48) * 10) + (buf[pos + 2] - 48);
if (length < 0) {
throw new ParseException(String.format("Invalid LLLVAR length %d pos %d", length, pos), pos);
}
if (pos+3 > buf.length || length+pos+3 > buf.length) {
throw new ParseException(String.format("Insufficient data for LLLVAR field, pos %d", pos), pos);
}
if (custom == null) {
return new IsoValue<String>(type, new String(buf, pos + 3, length), null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(new String(buf, pos + 3, length)), custom);
if (v.getValue() == null) {
//problems decoding? return the string
return new IsoValue<String>(type, new String(buf, pos + 3, length), null);
}
return v;
}
} else if (type == IsoType.AMOUNT) {
if (pos+12 > buf.length) {
throw new ParseException(String.format("Insufficient data for AMOUNT field of length %d, pos %d",
length, pos), pos);
}
byte[] c = new byte[13];
System.arraycopy(buf, pos, c, 0, 10);
System.arraycopy(buf, pos + 10, c, 11, 2);
c[10] = '.';
try {
return new IsoValue<BigDecimal>(type, new BigDecimal(new String(c)), null);
} catch (NumberFormatException ex) {
throw new ParseException(String.format("Cannot read amount '%s' pos %d", new String(c), pos), pos);
}
} else if (type == IsoType.DATE10) {
if (pos+10 > buf.length) {
throw new ParseException(String.format("Insufficient data for DATE10 field of length %d, pos %d",
length, pos), pos);
}
//A SimpleDateFormat in the case of dates won't help because of the missing data
//we have to use the current date for reference and change what comes in the buffer
Calendar cal = Calendar.getInstance();
//Set the month in the date
cal.set(Calendar.MONTH, ((buf[pos] - 48) * 10) + buf[pos + 1] - 49);
cal.set(Calendar.DATE, ((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48);
cal.set(Calendar.HOUR_OF_DAY, ((buf[pos + 4] - 48) * 10) + buf[pos + 5] - 48);
cal.set(Calendar.MINUTE, ((buf[pos + 6] - 48) * 10) + buf[pos + 7] - 48);
cal.set(Calendar.SECOND, ((buf[pos + 8] - 48) * 10) + buf[pos + 9] - 48);
if (cal.getTime().after(new Date())) {
cal.add(Calendar.YEAR, -1);
}
return new IsoValue<Date>(type, cal.getTime(), null);
} else if (type == IsoType.DATE4) {
if (pos+4 > buf.length) {
throw new ParseException(String.format("Insufficient data for DATE4 field of length %d, pos %d",
length, pos), pos);
}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
//Set the month in the date
cal.set(Calendar.MONTH, ((buf[pos] - 48) * 10) + buf[pos + 1] - 49);
cal.set(Calendar.DATE, ((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48);
if (cal.getTime().after(new Date())) {
cal.add(Calendar.YEAR, -1);
}
return new IsoValue<Date>(type, cal.getTime(), null);
} else if (type == IsoType.DATE_EXP) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.DATE, 1);
//Set the month in the date
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) - (cal.get(Calendar.YEAR) % 100)
+ ((buf[pos] - 48) * 10) + buf[pos + 1] - 48);
cal.set(Calendar.MONTH, ((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 49);
return new IsoValue<Date>(type, cal.getTime(), null);
} else if (type == IsoType.TIME) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, ((buf[pos] - 48) * 10) + buf[pos + 1] - 48);
cal.set(Calendar.MINUTE, ((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48);
cal.set(Calendar.SECOND, ((buf[pos + 4] - 48) * 10) + buf[pos + 5] - 48);
return new IsoValue<Date>(type, cal.getTime(), null);
}
return null;
}
/** Parses binary data from the buffer, creating and returning an IsoValue of the configured
* type and length. */
public <T extends Object> IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<T> custom) throws ParseException {
if (type == IsoType.ALPHA) {
if (custom == null) {
return new IsoValue<String>(type, new String(buf, pos, length), length, null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(new String(buf, pos, length)), length, custom);
if (v.getValue() == null) {
return new IsoValue<String>(type, new String(buf, pos, length), length, null);
}
return v;
}
} else if (type == IsoType.NUMERIC) {
//A long covers up to 18 digits
if (length < 19) {
long l = 0;
long power = 1L;
for (int i = pos + (length / 2) + (length % 2) - 1; i >= pos; i--) {
l += (buf[i] & 0x0f) * power;
power *= 10L;
l += ((buf[i] & 0xf0) >> 4) * power;
power *= 10L;
}
return new IsoValue<Number>(IsoType.NUMERIC, l, length, null);
} else {
//Use a BigInteger
char[] digits = new char[length];
int start = 0;
for (int i = pos; i < pos + (length / 2) + (length % 2); i++) {
digits[start++] = (char)(((buf[i] & 0xf0) >> 4) + 48);
digits[start++] = (char)((buf[i] & 0x0f) + 48);
}
return new IsoValue<Number>(IsoType.NUMERIC, new BigInteger(new String(digits)), length, null);
}
} else if (type == IsoType.LLVAR) {
length = (((buf[pos] & 0xf0) >> 4) * 10) + (buf[pos] & 0x0f);
if (custom == null) {
return new IsoValue<String>(type, new String(buf, pos + 1, length), null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(new String(buf, pos + 1, length)), custom);
if (v.getValue() == null) {
return new IsoValue<String>(type, new String(buf, pos + 1, length), null);
}
return v;
}
} else if (type == IsoType.LLLVAR) {
length = ((buf[pos] & 0x0f) * 100) + (((buf[pos + 1] & 0xf0) >> 4) * 10) + (buf[pos + 1] & 0x0f);
if (custom == null) {
return new IsoValue<String>(type, new String(buf, pos + 2, length), null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(new String(buf, pos + 2, length)), custom);
if (v.getValue() == null) {
return new IsoValue<String>(type, new String(buf, pos + 2, length), null);
}
return v;
}
} else if (type == IsoType.AMOUNT) {
char[] digits = new char[13];
digits[10] = '.';
int start = 0;
for (int i = pos; i < pos + 6; i++) {
digits[start++] = (char)(((buf[i] & 0xf0) >> 4) + 48);
digits[start++] = (char)((buf[i] & 0x0f) + 48);
if (start == 10) {
start++;
}
}
return new IsoValue<BigDecimal>(IsoType.AMOUNT, new BigDecimal(new String(digits)), null);
} else if (type == IsoType.DATE10 || type == IsoType.DATE4 || type == IsoType.DATE_EXP
|| type == IsoType.TIME) {
int[] tens = new int[(type.getLength() / 2) + (type.getLength() % 2)];
int start = 0;
for (int i = pos; i < pos + tens.length; i++) {
tens[start++] = (((buf[i] & 0xf0) >> 4) * 10) + (buf[i] & 0x0f);
}
Calendar cal = Calendar.getInstance();
if (type == IsoType.DATE10) {
//A SimpleDateFormat in the case of dates won't help because of the missing data
//we have to use the current date for reference and change what comes in the buffer
//Set the month in the date
cal.set(Calendar.MONTH, tens[0] - 1);
cal.set(Calendar.DATE, tens[1]);
cal.set(Calendar.HOUR_OF_DAY, tens[2]);
cal.set(Calendar.MINUTE, tens[3]);
cal.set(Calendar.SECOND, tens[4]);
if (cal.getTime().after(new Date())) {
cal.add(Calendar.YEAR, -1);
}
return new IsoValue<Date>(type, cal.getTime(), null);
} else if (type == IsoType.DATE4) {
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
//Set the month in the date
cal.set(Calendar.MONTH, tens[0] - 1);
cal.set(Calendar.DATE, tens[1]);
if (cal.getTime().after(new Date())) {
cal.add(Calendar.YEAR, -1);
}
return new IsoValue<Date>(type, cal.getTime(), null);
} else if (type == IsoType.DATE_EXP) {
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.DATE, 1);
//Set the month in the date
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR)
- (cal.get(Calendar.YEAR) % 100) + tens[0]);
cal.set(Calendar.MONTH, tens[1] - 1);
return new IsoValue<Date>(type, cal.getTime(), null);
} else if (type == IsoType.TIME) {
cal.set(Calendar.HOUR_OF_DAY, tens[0]);
cal.set(Calendar.MINUTE, tens[1]);
cal.set(Calendar.SECOND, tens[2]);
return new IsoValue<Date>(type, cal.getTime(), null);
}
return new IsoValue<Date>(type, cal.getTime(), null);
}
return null;
}
}
@@ -0,0 +1,15 @@
<!ELEMENT j8583-config ( header+, template+, parse+ ) >
<!ELEMENT header ( #PCDATA ) >
<!ATTLIST header type NMTOKEN #REQUIRED >
<!ELEMENT template ( field+ ) >
<!ATTLIST template type NMTOKEN #REQUIRED >
<!ELEMENT parse ( field+ ) >
<!ATTLIST parse type NMTOKEN #REQUIRED >
<!ELEMENT field ( #PCDATA ) >
<!ATTLIST field num NMTOKEN #REQUIRED >
<!ATTLIST field length NMTOKEN #IMPLIED >
<!ATTLIST field type ( ALPHA | NUMERIC | AMOUNT | DATE10 | DATE4 | DATE_EXP | TIME | LLVAR | LLLVAR ) #REQUIRED >
@@ -0,0 +1 @@
<body>This package contains the classes used for parsing the XML configuration files to set up a MessageFactory, and also for storing the parse information for each field depending on the type of message.</body>