Added a new property, characterEncoding, to MessageFactory, IsoMessage, FieldParseInfo and IsoValue. This helps in parsing messages from byte buffers containing extended characters in encodings other than the default for the JVM. Tested with UTF8 on a Mac (which has default Mac OS Roman) and worked fine.

Several methods now have an additional encoding parameter (just a String, not directly a Charset), but the default is always the JVM's default encoding (taken from the property 'file.encoding').


git-svn-id: https://j8583.svn.sourceforge.net/svnroot/j8583/j8583@70 db60a039-f72e-0410-b1d2-baa000846441
This commit is contained in:
chochos
2011-03-21 23:22:58 +00:00
parent c7d696acc7
commit 5c4259eba3
15 changed files with 153 additions and 56 deletions
@@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package com.solab.iso8583.parse;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import com.solab.iso8583.CustomField;
@@ -32,6 +33,7 @@ public abstract class FieldParseInfo {
protected IsoType type;
protected int length;
private String encoding = System.getProperty("file.encoding");
/** 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.
@@ -45,6 +47,13 @@ public abstract class FieldParseInfo {
length = len;
}
public void setCharacterEncoding(String value) {
encoding = value;
}
public String getCharacterEncoding() {
return encoding;
}
/** Returns the specified length for the data to be parsed. */
public int getLength() {
return length;
@@ -57,40 +66,47 @@ public abstract class FieldParseInfo {
/** Parses the character data from the buffer and returns the
* IsoValue with the correct data type in it. */
public abstract <T extends Object> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom) throws ParseException;
public abstract <T extends Object> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom)
throws ParseException, UnsupportedEncodingException;
/** Parses binary data from the buffer, creating and returning an IsoValue of the configured
* type and length. */
public abstract <T extends Object> IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<T> custom) throws ParseException;
public abstract <T extends Object> IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<T> custom)
throws ParseException, UnsupportedEncodingException;
/** Returns a new FieldParseInfo instance that can parse the specified type. */
public static FieldParseInfo getInstance(IsoType t, int len) {
public static FieldParseInfo getInstance(IsoType t, int len, String encoding) {
FieldParseInfo fpi = null;
if (t == IsoType.ALPHA) {
return new AlphaParseInfo(len);
fpi = new AlphaParseInfo(len);
} else if (t == IsoType.AMOUNT) {
return new AmountParseInfo();
fpi = new AmountParseInfo();
} else if (t == IsoType.BINARY) {
return new BinaryParseInfo(len);
fpi = new BinaryParseInfo(len);
} else if (t == IsoType.DATE10) {
return new Date10ParseInfo();
fpi = new Date10ParseInfo();
} else if (t == IsoType.DATE4) {
return new Date4ParseInfo();
fpi = new Date4ParseInfo();
} else if (t == IsoType.DATE_EXP) {
return new DateExpParseInfo();
fpi = new DateExpParseInfo();
} else if (t == IsoType.LLBIN) {
return new LlbinParseInfo();
fpi = new LlbinParseInfo();
} else if (t == IsoType.LLLBIN) {
return new LllbinParseInfo();
fpi = new LllbinParseInfo();
} else if (t == IsoType.LLLVAR) {
return new LllvarParseInfo();
fpi = new LllvarParseInfo();
} else if (t == IsoType.LLVAR) {
return new LlvarParseInfo();
fpi = new LlvarParseInfo();
} else if (t == IsoType.NUMERIC) {
return new NumericParseInfo(len);
fpi = new NumericParseInfo(len);
} else if (t == IsoType.TIME) {
return new TimeParseInfo();
fpi = new TimeParseInfo();
}
throw new IllegalArgumentException(String.format("Cannot parse type %s", t));
if (fpi == null) {
throw new IllegalArgumentException(String.format("Cannot parse type %s", t));
}
fpi.setCharacterEncoding(encoding);
return fpi;
}
}