📄 messageparser.java
字号:
package com.jpmorgan.neo.neoclient.mds.slcv5.parser;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.jpmorgan.neo.neoclient.mds.slcv5.message.DictionaryReply;
import com.jpmorgan.neo.neoclient.mds.test.TestDictionary;
/**
* The XMLParser class use mapping file Message.xml to <br>
* parser bytes to message,and message to bytes. <br>
* it's a singtlon model.
*
* @see Message
*
*/
public class MessageParser {
/** Log use for debug and out print. */
private static final Logger log = Logger.getLogger(MessageParser.class);
/** Singtlon instance. */
private static MessageParser parser;
/** Mapping file. */
private static String mappingfile = "classes/Message.xml";
/** Contain all of mapping rule in mapping file. */
protected Hashtable table;
protected String requestNumber;
/**
* Constructor.
*/
private MessageParser() {
init();
}
/**
* Initializes some fields <br>
* it can be override.
*/
protected void init() {
table = new Hashtable();
parse(new File(mappingfile));
}
/**
* provide a XMLParser instance
* @return a singtlon instacnce of XMLParser
*/
public static MessageParser getInstance() {
if (parser == null)
parser = new MessageParser();
return parser;
}
/**
* parse Message.xml.
* @param input a <code>File</code>.
*/
public void parse(File input) {
SAXReader saxReader = new SAXReader();
Document document = null;
try {
document = saxReader.read(input);
Element root = document.getRootElement();
List messages = root.elements("Message");
Iterator iter = messages.listIterator();
while (iter.hasNext()) {
Element message = (Element) iter.next();
table.put(message.attribute("id").getValue(), message);
}
log.debug(table);
} catch (DocumentException e) {
// TODO Auto-generated catch block
log.error("parse error " + e.getMessage());
}
}
/**
* Parse byte array to Message's field.
* @param msg a <code>Message</code>.
* @param requestNumber Message's request Number.
* @param ascii received byte array.
*
* @see Message#getRequestNumber()
*/
public void parseBytesToField(Message msg, byte[] ascii) {
log.debug(new String(ascii));
//get a Message rule
Element element = (Element) table.get(msg.getReplyNumber());
List fields = element.elements("Field");
Iterator iter = fields.iterator();
Class classname = null;
ArrayList repeat = new ArrayList();
// current field begin and end
int offset = 0, length = 0;
try {
classname = Class.forName(element.attribute("class").getValue());
Object obj = msg;
while (iter.hasNext()) {
//get a field rule
final Element a = (Element) iter.next();
final String datatype = a.attributeValue("DataType");
final String codage = a.attributeValue("Codage");
int maxlen = 0;
if (null != a.attributeValue("MaxLength")) {
maxlen = Integer.parseInt(a.attributeValue("MaxLength"));
}
if ("TRUE".equalsIgnoreCase(a.attributeValue("Repeat"))){
repeat.add(a);
} else if ("ASCII".equalsIgnoreCase(codage)) {
length = offset + maxlen;
if ("INT".equalsIgnoreCase(datatype)) {
Class[] paramtype = new Class[] { Integer.TYPE };
final Method method = classname.getMethod("set"
+ a.attributeValue("Name"), paramtype);
method.invoke(obj, new Object[] { Integer
.parseInt(ByteUtil.bytesToString(ascii, offset,
length).trim()) });
log.debug(ByteUtil.bytesToString(ascii, offset,
length).trim());
}
if ("CHAR".equalsIgnoreCase(datatype)) {
Class[] paramtype = new Class[] { String.class };
final Method method = classname.getMethod("set"
+ a.attributeValue("Name"), paramtype);
method.invoke(obj, new Object[] { ByteUtil
.bytesToString(ascii, offset, length) });
log.debug(ByteUtil
.bytesToString(ascii, offset, length));
}
offset = length;
} else if ("GL".equalsIgnoreCase(codage)) {
int len = ByteUtil.subBytes(ascii, offset, offset + 1)[0] - 32;
length = offset + len + 1;
if ("INT".equalsIgnoreCase(datatype)) {
Class[] paramtype = new Class[] { Integer.TYPE };
final Method method = classname.getMethod("set"
+ a.attributeValue("Name"), paramtype);
String value = ByteUtil.bytesToString(ascii, offset + 1, length).trim();
if ("".equals(value) || null == value){
value = "0";
}
method.invoke(obj, new Object[] { Integer.parseInt(value) });
log.debug(value);
}
if ("CHAR".equalsIgnoreCase(datatype)) {
Class[] paramtype = new Class[] { String.class };
final Method method = classname.getMethod("set"
+ a.attributeValue("Name"), paramtype);
method.invoke(obj, new Object[] {
ByteUtil.bytesToString(ascii, offset + 1, length)});
log.debug(ByteUtil.bytesToString(ascii, offset + 1, length));
}
if ("NUM".equalsIgnoreCase(datatype)) {
Class[] paramtype = new Class[] { Double.TYPE };
final Method method = classname.getMethod("set"
+ a.attributeValue("Name"), paramtype);
String value = ByteUtil.bytesToString(ascii, offset + 1, length).trim();
if ("".equals(value) || null == value){
value = "0";
}
method.invoke(obj, new Object[] { Double.parseDouble(value)});
log.debug(value);
}
offset = length;
} else if ("GL_C".equalsIgnoreCase(codage)) {
int value = ByteUtil.subBytes(ascii, offset, offset + 1)[0] - 32;
length = offset + 1;
if ("INT".equalsIgnoreCase(datatype)) {
Class[] paramtype = new Class[] { Integer.TYPE };
final Method method = classname.getMethod("set"
+ a.attributeValue("Name"), paramtype);
method.invoke(obj, new Object[] { value });
log.debug(value);
}
if ("CHAR".equalsIgnoreCase(datatype)) {
Class[] paramtype = new Class[] { String.class };
final Method method = classname.getMethod("set"
+ a.attributeValue("Name"), paramtype);
method.invoke(obj,
new Object[] { String.valueOf(value) });
log.debug(value);
}
offset = length;
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (repeat.size() > 0) {
parseRepeatBytes2Fields(ByteUtil.subBytes(ascii,offset),repeat,classname,msg);
}
}
/**
* parse repeat data.
* @param ascii
* @param repeat
* @param classname
* @param msg
*/
private void parseRepeatBytes2Fields(byte[] ascii, ArrayList repeat, Class classname, Message msg) {
//offset is next field start postion,length is current field length.
int offset = 0,length = 0;
//if not end of bytes.
while(offset < ascii.length){
for(int i = 0; i < repeat.size(); i++){
Element ele = (Element)repeat.get(i);
final String datatype = ele.attributeValue("DataType");
final String codage = ele.attributeValue("Codage");
int maxlen = 0;
if (null != ele.attributeValue("MaxLength")) {
maxlen = Integer.parseInt(ele.attributeValue("MaxLength"));
}
Class[] paramtype = new Class[] { ArrayList.class };
try {
final Method getMethod = classname.getMethod("get" + ele.attributeValue("Name"),null);
final Method setMethod = classname.getMethod("set" + ele.attributeValue("Name"), paramtype);
ArrayList list = (ArrayList)getMethod.invoke(msg,null);
if (list == null)
list = new ArrayList();
if ("GL".equalsIgnoreCase(codage)) {
int len = ByteUtil.subBytes(ascii, offset, offset + 1)[0] - 32;
length = offset + len + 1;
if ("INT".equalsIgnoreCase(datatype)) {
log.debug(ele.attributeValue("Name") + " : " + ByteUtil.bytesToString(ascii, offset, length).trim());
list.add(ByteUtil.bytesToString(ascii, offset + 1, length).trim());
setMethod.invoke(msg,list);
}
if ("CHAR".equalsIgnoreCase(datatype)) {
log.debug(ele.attributeValue("Name") + " : " + ByteUtil.bytesToString(ascii, offset, length).trim());
list.add(ByteUtil.bytesToString(ascii, offset + 1, length).trim());
setMethod.invoke(msg, list);
}
offset = length;
}
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NoSuchMethodException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* parse fields to bytes
* @param msg
* @param requestNumber
* @return byte arry
*/
public byte[] parseFieldsToBytes(Message msg) {
Element element = (Element) table.get(msg.getRequestNumber());
List fields = element.elements("Field");
Iterator iter = fields.iterator();
Class classname = null;
ArrayList repeat = new ArrayList();
StringBuffer buffer = new StringBuffer();
try {
classname = Class.forName(element.attribute("class").getValue());
Object obj = msg;
while (iter.hasNext()) {
final Element a = (Element) iter.next();
final String datatype = a.attributeValue("DataType");
final String codage = a.attributeValue("Codage");
int maxlen = 0;
if (null != a.attributeValue("MaxLength")) {
maxlen = Integer.parseInt(a.attributeValue("MaxLength"));
}
Method method = null;
if (!"FILLER".equalsIgnoreCase(datatype)){
method = classname.getMethod("get"
+ a.attributeValue("Name"), null);
}
log.debug(method);
if ("ASCII".equalsIgnoreCase(codage)) {
if ("INT".equalsIgnoreCase(datatype)) {
Integer i = (Integer) method.invoke(obj, null);
log.debug(i);
buffer.append(ByteUtil.fillBlank(maxlen, i));
}
if ("CHAR".equalsIgnoreCase(datatype)) {
String str = (String) method.invoke(obj, null);
log.debug(str);
buffer.append(ByteUtil.fillBlank(maxlen, str));
}
if ("NUM".equalsIgnoreCase(datatype)) {
Double d = (Double) method.invoke(obj, null);
log.debug(d);
buffer.append(ByteUtil.fillBlank(maxlen, d));
}
if ("Filler".equalsIgnoreCase(datatype)){
log.debug("Filler length " + maxlen);
buffer.append(ByteUtil.fillBlank(maxlen, ""));
}
} else if ("GL".equalsIgnoreCase(codage)) {
final Class t = method.getReturnType();
String value = "";
if ("java.lang.String".equals(t.getName())) {
String str = (String) method.invoke(obj, null);
value = ByteUtil.computerToGL(str);
} else if ("int".equals(t.getName())) {
int i = Integer.parseInt((String) method.invoke(obj,
null));
value = ByteUtil.computerToGL(i);
} else if ("double".equals(t.getName())) {
double d = ((Double) method.invoke(obj, null)).doubleValue();
value = ByteUtil.computerToGL(d);
}
log.debug(value);
buffer.append(value);
} else if ("GL_C".equalsIgnoreCase(codage)) {
final Class t = method.getReturnType();
char value = 0;
if ("int".equals(t.getName())) {
int i = ((Integer) method.invoke(obj, null)).intValue();
value = (char) (i + 32);
}
log.debug(value);
buffer.append(String.valueOf(value));
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return buffer.toString().getBytes();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -