Commit f9e08bdc by WeiCong

完成swift报文的组报功能

parent 599cf024
......@@ -12,219 +12,203 @@ public class TagCell {
public String content;
public int line;
public String value;
public TagCell clone()
{
public TagCell clone() {
TagCell cell = new TagCell();
cell.fixed = fixed;
cell.length = length;
cell.type = type;
cell.cnt = cnt;
cell.content = content;
cell.line=line;
cell.line = line;
cell.value = value;
return cell;
}
public void setValue(String amt)
{
if("d".equals(type) || ("AMOUNT".equals(content)&&"FUNC".equals(type)))
{
if(amt.endsWith(","))
this.value = amt.substring(0,amt.length() - 1);
public void setValue(String amt) {
if ("d".equals(type) || ("AMOUNT".equals(content) && "FUNC".equals(type))) {
if (amt.endsWith(","))
this.value = amt.substring(0, amt.length() - 1);
else
this.value = amt.replaceAll(",", ".");
return ;
return;
}
this.value = amt;
}
public String toReg()
{
public String toReg() {
return toReg(true);
}
public String toReg(boolean useRegName)
{
String cnt = "S"+this.cnt;
if(CONST.equals(type))
{
if("CRLF".equals(content))
public String toReg(boolean useRegName) {
String cnt = "S" + this.cnt;
if (CONST.equals(type)) {
if ("CRLF".equals(content))
return "[\\r\\n]{1,2}";
return content;
}
else if(FUNC.equals(type))
{
if("AMOUNT".equals(content))
{
if(useRegName)
return "(?<"+cnt+">[\\d,]{1,"+this.length+"})";
} else if (FUNC.equals(type)) {
if ("AMOUNT".equals(content)) {
if (useRegName)
return "(?<" + cnt + ">[\\d,.]{1," + this.length + "})";
else
return "[\\d,]{1,"+this.length+"}";
}
else if("NUMBER".equals(content))
{
if(useRegName)
return "(?<"+cnt+">[\\d,]{1,"+this.length+"})";
return "[\\d,.]{1," + this.length + "}";
} else if ("NUMBER".equals(content)) {
if (useRegName)
return "(?<" + cnt + ">[\\d,]{1," + this.length + "})";
else
return "[\\d,]{1,"+this.length+"}";
}
else if("HHMM".equals(content)) //hhmm
return "[\\d,]{1," + this.length + "}";
} else if ("HHMM".equals(content)) //hhmm
{
if(useRegName)
return "(?<"+cnt+">\\d{4})";
if (useRegName)
return "(?<" + cnt + ">\\d{4})";
else
return "\\d{4}";
}
else if("TIME2".equals(content))
{
if(useRegName)
return "(?<"+cnt+">\\d{6})";
} else if ("TIME2".equals(content)) {
if (useRegName)
return "(?<" + cnt + ">\\d{6})";
else
return "\\d{6}";
}
else if("TIME3".equals(content))
{
if(useRegName)
return "(?<"+cnt+">\\d{2}\\d{2}?)";
} else if ("TIME3".equals(content)) {
if (useRegName)
return "(?<" + cnt + ">\\d{2}\\d{2}?)";
else
return "\\d{2}\\d{2}?";
}
else if("OFFSET".equals(content))//hhmm
} else if ("OFFSET".equals(content))//hhmm
{
if(useRegName)
return "(?<"+cnt+">\\d{4})";
if (useRegName)
return "(?<" + cnt + ">\\d{4})";
else
return "\\d{4}";
}
else if("SIGN".equals(content)) // +/-
} else if ("SIGN".equals(content)) // +/-
{
if(useRegName)
return "(?<"+cnt+">\\+|\\-)";
if (useRegName)
return "(?<" + cnt + ">\\+|\\-)";
else
return "\\+|\\-";
}
else if("DATE1".equals(content)) //yyMMdd
} else if ("DATE1".equals(content)) //yyMMdd
{
if(useRegName)
return "(?<"+cnt+">\\d{4})";
if (useRegName)
return "(?<" + cnt + ">\\d{4})";
else
return "\\d{4}";
}
else if("DATE2".equals(content)) //yyMMdd
} else if ("DATE2".equals(content)) //yyMMdd
{
if(useRegName)
return "(?<"+cnt+">\\d{6})";
if (useRegName)
return "(?<" + cnt + ">\\d{6})";
else
return "\\d{6}";
}
else if("YEAR".equals(content))
{
if(useRegName)
return "(?<"+cnt+">\\d{4})";
} else if ("YEAR".equals(content)) {
if (useRegName)
return "(?<" + cnt + ">\\d{4})";
else
return "\\d{4}";
}
else if("DATE4".equals(content)) //yyyyMMdd
} else if ("DATE4".equals(content)) //yyyyMMdd
{
if(useRegName)
return "(?<"+cnt+">\\d{8})";
if (useRegName)
return "(?<" + cnt + ">\\d{8})";
else
return "\\d{8}";
}
else if("CUR".equals(content)) //yyyyMMdd
} else if ("CUR".equals(content)) //yyyyMMdd
{
if(useRegName)
return "(?<"+cnt+">[A-Z]{3})";
if (useRegName)
return "(?<" + cnt + ">[A-Z]{3})";
else
return "[A-Z]{3}";
}
else if("DC".equals(content)) //yyyyMMdd
} else if ("DC".equals(content)) //yyyyMMdd
{
if(useRegName)
return "(?<"+cnt+">C|D|RC|RD)";
if (useRegName)
return "(?<" + cnt + ">C|D|RC|RD)";
else
return "C|D|RC|RD";
}
else if("SWIFTBIC".equals(content)) //4!a2!a2!c3!c
} else if ("SWIFTBIC".equals(content)) //4!a2!a2!c3!c
{
if(useRegName)
return "(?<"+cnt+">[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?)";
if (useRegName)
return "(?<" + cnt + ">[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?)";
else
return "[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?";
}
else if("MT".equals(content))
{
if(useRegName)
return "(?<"+cnt+">[1-9]\\d{2})";
} else if ("MT".equals(content)) {
if (useRegName)
return "(?<" + cnt + ">[1-9]\\d{2})";
else
return "[1-9]\\d{2}";
}
else if("SB-LC".equals(content))
{
if(useRegName)
return "(?<"+cnt+">[A-Z]{4}[A-Z0-9]{2}\\d{4}[A-Z]{4}[A-Z0-9]{2})";
} else if ("SB-LC".equals(content)) {
if (useRegName)
return "(?<" + cnt + ">[A-Z]{4}[A-Z0-9]{2}\\d{4}[A-Z]{4}[A-Z0-9]{2})";
else
return "[A-Z]{4}[A-Z0-9]{2}\\d{4}[A-Z]{4}[A-Z0-9]{2}";
}
else if("DM".equals(content))
{
if(useRegName)
return "(?<"+cnt+">D|M)";
} else if ("DM".equals(content)) {
if (useRegName)
return "(?<" + cnt + ">D|M)";
else
return "D|M";
}
else if("CC".equals(content))
{
if(useRegName)
return "(?<"+cnt+">[A-Z]{2})";
} else if ("CC".equals(content)) {
if (useRegName)
return "(?<" + cnt + ">[A-Z]{2})";
else
return "[A-Z]{2}";
} else {
System.err.println("unknow function:" + content);
}
else{
System.err.println("unknow function:"+content);
}
}
else
{
} else {
String type = "";
char c = this.type.charAt(0);
switch(c)
{
case 'n': type = "\\d";break;
case 'a': type = "[A-Z]";break;
case 'd': type = "[\\d,]";break;
case 'x': type = "[\\w/\\-\\?:\\(\\)\\.,'+\\{\\} ]";break;
case 'y': type = "[0-9A-Z\\.,\\-\\(\\)/='\\+:\\?\\!\"%&\\*\\<\\>;\\s]";break;
case 'z': type = "[\\w\\.,\\-\\(\\)/='\\+:\\?!\"%&\\*\\<\\>;\\{\\s@#]";break;
case 'c': type = "[A-Z0-9]";break;
case 'h': type = "[A-F0-9]";break;
case 'e': type = "\\s";break;
case 'A': type = "[A-Za-z]";break;
case 'B': type = "[A-Za-z0-9]";break;
default:System.err.println("unknow type:"+type);
switch (c) {
case 'n':
type = "\\d";
break;
case 'a':
type = "[A-Z]";
break;
case 'd':
type = "[\\d,.]";
break;
case 'x':
type = "[\\w/\\-\\?:\\(\\)\\.,'+\\{\\} ]";
break;
case 'y':
type = "[0-9A-Z\\.,\\-\\(\\)/='\\+:\\?\\!\"%&\\*\\<\\>;\\s]";
break;
case 'z':
type = "[\\w\\.,\\-\\(\\)/='\\+:\\?!\"%&\\*\\<\\>;\\{\\s@#]";
break;
case 'c':
type = "[A-Z0-9]";
break;
case 'h':
type = "[A-F0-9]";
break;
case 'e':
type = "\\s";
break;
case 'A':
type = "[A-Za-z]";
break;
case 'B':
type = "[A-Za-z0-9]";
break;
default:
System.err.println("unknow type:" + type);
}
//String length = "";
String nolines = "";
if(fixed || minLen==length)
{
nolines = type+"{"+length+"}";
}else
nolines = type+"{"+minLen+","+length+"}";
if(line > 1)
{
if(useRegName)
nolines = nolines+"([\\r\\n]{1,2}"+nolines +"){0,"+(line-1)+"}";
if (fixed || minLen == length) {
nolines = type + "{" + length + "}";
} else
nolines = type + "{" + minLen + "," + length + "}";
if (line > 1) {
if (useRegName)
nolines = nolines + "([\\r\\n]{1,2}" + nolines + "){0," + (line - 1) + "}";
else //校验所用的,只转换单行
{
if(fixed || minLen==length)
{
nolines = type+"{"+length+"}";
}else
nolines = type+"{"+0+","+length+"}";
if (fixed || minLen == length) {
nolines = type + "{" + length + "}";
} else
nolines = type + "{" + 0 + "," + length + "}";
}
}
if(useRegName)
return "(?<"+cnt+">"+nolines+")";
if (useRegName)
return "(?<" + cnt + ">" + nolines + ")";
else
return nolines;
}
......
......@@ -5,123 +5,130 @@ import com.brilliace.swifteditor.tag.message.MessageArea;
import java.util.*;
public class TagLine implements MessageArea {
private final int type = TAG;
public List<TagCell> cellList = new ArrayList<TagCell>();
public List<int[]> options = new ArrayList<int[]>();
public MessageArea parent;
public boolean onlyOne;
String analyzeReg = null;
private String pattern;
private String value;
private String tagName;
private String status;
public MessageArea parent;
public boolean onlyOne;
private int maxCnt = 0;
private int tno = 0;
private String desp = "";
private List<String> suffix = new ArrayList<String>();
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
private int maxCnt = 0;
private int tno = 0;
private String desp = "";
private final int type = TAG;
private List<String> suffix = new ArrayList<String>();
public void add(TagCell cell) {
cellList.add(cell);
}
public void addOptions(int[] points)
{
public void addOptions(int[] points) {
options.add(points);
}
public void fillOption(int index,int point)
{
if(options.size() ==0)
{
System.err.println("error pattern:"+this.getName()+"----"+this.getPattern());
return ;
public void fillOption(int index, int point) {
if (options.size() == 0) {
System.err.println("error pattern:" + this.getName() + "----" + this.getPattern());
return;
}
options.get(index)[1] = point;
}
public void fillLastOption(int point)
{
if(options.size() ==0)
{
System.err.println("error pattern:"+this.getName()+"----"+this.getPattern());
return ;
public void fillLastOption(int point) {
if (options.size() == 0) {
System.err.println("error pattern:" + this.getName() + "----" + this.getPattern());
return;
}
options.get(options.size() - 1)[1] = point;
}
options.get(options.size() -1)[1] = point;
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
public String getPattern()
{
return pattern;
public String getSourceValue() {
return this.value;
}
public void setValue(String value)
{
if(value == null)
{
/***
* 返回组装后的swift域值
* @return
*/
public String getValue() {
StringBuilder sb = new StringBuilder();
this.toSwfMessage(sb);
String val = sb.toString();
return val;
}
public void setValue(String value) {
if (value == null) {
this.value = null;
return ;
return;
}
if(this.value!=null && this.value.equals(value))
if (this.value != null && this.value.equals(value))
return;
this.value = value;
this.fillCellValue();
}
public String getSourceValue()
{
return this.value;
}
/***
* 返回组装后的
* 返回组装后人类查看的域
* @return
*/
public String getValue()
{
public String getValueForHuman() {
StringBuilder sb = new StringBuilder();
this.toSwfMessage(sb);
return sb.toString();
this.toHumanMessage(sb);
String val = sb.toString();
return val;
}
public String getCellValue(int position)
{
for(TagCell cell : this.cellList)
{
if(cell.cnt == position)
public String getCellValue(int position) {
for (TagCell cell : this.cellList) {
if (cell.cnt == position)
return cell.value;
}
return null;
}
/***
* 将栏位值填充到指定的栏位中去
*/
private void fillCellValue()
{
Map<Integer,String> subValues = TagAnalyzer.analyze(this, value);
if(subValues == null)
return ;
for(Map.Entry<Integer, String> entry : subValues.entrySet())
{
private void fillCellValue() {
Map<Integer, String> subValues = TagAnalyzer.analyze(this, value);
if (subValues == null)
return;
for (Map.Entry<Integer, String> entry : subValues.entrySet()) {
int cnt = entry.getKey();
for(TagCell cell : cellList)
{
if(cell.cnt == cnt)
{
cell.setValue( entry.getValue());
for (TagCell cell : cellList) {
if (cell.cnt == cnt) {
cell.setValue(entry.getValue());
break;
}
}
}
}
public TagLine clone()
{
public TagLine clone() {
TagLine cp = new TagLine();
for(TagCell cell: cellList)
{
for (TagCell cell : cellList) {
cp.add(cell.clone());
}
for(int[] option : options)
{
for (int[] option : options) {
int[] cpopt = Arrays.copyOf(option, 2);
cp.addOptions(cpopt);
}
......@@ -136,75 +143,118 @@ public class TagLine implements MessageArea {
cp.analyzeReg = this.analyzeReg;
return cp;
}
public void toSwfMessage(StringBuilder sb) {
for(TagCell cell : this.cellList)
if("CONST".equals(cell.type))
boolean isEmpty = true;
TagCell lastCell = null;
for (TagCell cell : this.cellList) {
if ("CONST".equals(cell.type)) {
lastCell = cell;
if ("CRLF".equals(cell.content)) {
sb.append("\r\n");
} else {
sb.append(cell.content);
else if(cell.value != null)
}
} else if (cell.value != null) {
isEmpty = false;
if ("d".equals(cell.type) || ("AMOUNT".equals(cell.content) && "FUNC".equals(cell.type))) {
if (cell.value.indexOf(".") > 0) {
sb.append(cell.value.replace(".", ","));
} else {
sb.append(cell.value + ",");
}
} else {
if (lastCell != null && cell.value.startsWith(lastCell.content)) {
sb.append(cell.value.substring(lastCell.content.length()));
} else {
sb.append(cell.value);
}
}
lastCell = null;
}
}
if (isEmpty) {
sb.delete(0, sb.length());
}
}
public void toHumanMessage(StringBuilder sb) {
boolean isEmpty = true;
for (TagCell cell : this.cellList) {
if ("CONST".equals(cell.type)) {
if ("CRLF".equals(cell.content)) {
sb.append("\r\n");
} else {
sb.append(cell.content);
}
} else if (cell.value != null) {
isEmpty = false;
sb.append(cell.value);
}
}
if (isEmpty) {
sb.delete(0, sb.length());
}
}
public String getName() {
return tagName;
}
public void setName(String name) {
this.tagName = name;
}
public void addSubArea(MessageArea sub) {
}
public boolean isEnd()
{
public boolean isEnd() {
return true;
}
public int getType() {
return type;
}
public void setName(String name) {
this.tagName = name;
}
/***
* 返回解析字符串
* @return
*/
public String toAnalyzeReg()
{
public String toAnalyzeReg() {
StringBuilder sb = new StringBuilder();
int cnt = 0;
boolean hasOpen = false;
for(TagCell cell : cellList)
{
for(int[] opt : options)
{
if(cnt == opt[0])
{
for (TagCell cell : cellList) {
for (int[] opt : options) {
if (cnt == opt[0]) {
sb.append('(');
hasOpen=true;
hasOpen = true;
// break;
}
else if(cnt == opt[1])
{
} else if (cnt == opt[1]) {
sb.append(")?");
hasOpen=false;
hasOpen = false;
// break;
}
}
cnt++;
sb.append(cell.toReg());
}
if(hasOpen)
if (hasOpen)
sb.append(")?");
return sb.toString();
}
//分段校验信息
public Map<String,Object> toSegRegMap()
{
Map<String,Object> set = new HashMap<String,Object>();
public Map<String, Object> toSegRegMap() {
Map<String, Object> set = new HashMap<String, Object>();
// int cnt = 0;
for(int cnt =0;cnt<this.cellList.size();cnt++)
{
for (int cnt = 0; cnt < this.cellList.size(); cnt++) {
TagCell cell = this.cellList.get(cnt);
if(cell.cnt < 0)
if (cell.cnt < 0)
continue;
String key = "s"+cell.cnt;
HashMap<String,Object> info = new HashMap<String,Object>();
String key = "s" + cell.cnt;
HashMap<String, Object> info = new HashMap<String, Object>();
set.put(key, info);
info.put("reg", cell.toReg(false));
info.put("type", cell.type);
......@@ -214,10 +264,8 @@ public class TagLine implements MessageArea {
info.put("length", cell.length);
info.put("line", cell.line);
for(int[] opt : options)
{
if(cnt >= opt[0] && cnt<opt[1])
{
for (int[] opt : options) {
if (cnt >= opt[0] && cnt < opt[1]) {
info.put("option", "O");
}
}
......@@ -225,49 +273,54 @@ public class TagLine implements MessageArea {
return set;
}
String analyzeReg = null;
public void setAnalyzeReg(String analyzeReg){
this.analyzeReg=analyzeReg;
}
public String getAnalyzeReg()
{
if(analyzeReg==null)
public String getAnalyzeReg() {
if (analyzeReg == null)
analyzeReg = this.toAnalyzeReg();
return analyzeReg;
}
public void setAnalyzeReg(String analyzeReg) {
this.analyzeReg = analyzeReg;
}
public int getMaxCnt() {
return maxCnt;
}
public void setMaxCnt(int maxCnt) {
this.maxCnt = maxCnt;
}
public int getTno() {
return tno;
}
public void setTno(int tno) {
this.tno = tno;
}
public String getDesp() {
return desp;
}
public void setDesp(String desp) {
this.desp = desp;
}
public List<String> getSuffix() {
return this.suffix;
}
public void setSuffix(List<String> tagSuffix) {
this.suffix.clear();
this.suffix.addAll(tagSuffix);
}
public List<String> getSuffix()
{
return this.suffix;
}
public Map<String,String> getIndexMap() {
Map<String,String> indexMap = new HashMap<String,String>();
for(TagCell cell : cellList)
{
if(cell.cnt > 0 && cell.value!=null)
indexMap.put("s"+cell.cnt, cell.value);
public Map<String, String> getIndexMap() {
Map<String, String> indexMap = new HashMap<String, String>();
for (TagCell cell : cellList) {
if (cell.cnt > 0 && cell.value != null)
indexMap.put("s" + cell.cnt, cell.value);
}
return indexMap;
}
......
......@@ -27,6 +27,10 @@ public class MessageFormat {
if (swfmsg == null && mtType.charAt(1) == '9')
swfmsg = mtCache.get('n' + mtType.substring(1));
if (swfmsg == null) {
mtType = mtType.toUpperCase();
swfmsg = mtCache.get(mtType);
}
if (swfmsg != null) {
swfmsg.setMtType(mtType);
return swfmsg.clone();
......
......@@ -6,6 +6,10 @@ import com.brilliace.swifteditor.tag.TagLine;
import com.brilliace.swifteditor.util.Assert;
import com.google.gson.Gson;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.*;
import java.util.regex.Pattern;
......@@ -17,6 +21,9 @@ public class SWFMessage extends AbstractMessageArea {
private Map<Integer, TagLine> flatModel = new HashMap<>();
private Map<String, Integer> tag2No = new HashMap<>();
private Map<String, String> bscInfo;
private volatile String body;
private String sndbic;
private String rcvbic;
protected SWFMessage() {
}
......@@ -81,6 +88,14 @@ public class SWFMessage extends AbstractMessageArea {
sb.append("]");
}
public void setSndbic(String sndbic) {
this.sndbic = sndbic;
}
public void setRcvbic(String rcvbic) {
this.rcvbic = rcvbic;
}
public Map<Integer, String> getHeaders() {
return headers;
}
......@@ -231,7 +246,7 @@ public class SWFMessage extends AbstractMessageArea {
throw new IllegalArgumentException("域" + tagLine.getName() + "需要填充List集合类型的数据");
}
}
setGenericRepetitive((AbstractMessageArea) tagLine.parent, index, tagLine.getTno(), (List<Object>) val, 0);
setGenericRepetitive((AbstractMessageArea) tagLine.parent, index, tagLine.getTno(), (List<Object>) val, 0, tagName);
} else {
tagLine.setValue((String) val);
}
......@@ -241,7 +256,14 @@ public class SWFMessage extends AbstractMessageArea {
int part = Integer.parseInt(path.substring(index + 1)) + 1;
Assert.state(part > 0, "索引[" + path + "]指定的域块不存在");
if (tagLine.parent != null) {
setGenericRepetitive((AbstractMessageArea) tagLine.parent, part, tagLine.getTno(), (List<Object>) val, 0);
if (val instanceof String && tagLine.onlyOne) {
if (tagLine.onlyOne) {
val = Collections.singletonList(val);
} else {
throw new IllegalArgumentException("域" + tagLine.getName() + "需要填充List集合类型的数据");
}
}
setGenericRepetitive((AbstractMessageArea) tagLine.parent, part, tagLine.getTno(), (List<Object>) val, 0, tagName);
} else {
for (TagCell cell : tagLine.cellList) {
if (cell.cnt == part) {
......@@ -270,12 +292,13 @@ public class SWFMessage extends AbstractMessageArea {
}
private boolean setGenericRepetitive(AbstractMessageArea parent, int index, int ano, List<Object> rs, int cnt) {
private boolean setGenericRepetitive(AbstractMessageArea parent, int index, int ano, List<Object> rs, int cnt, String tagName) {
if (parent.getType() == MessageArea.CYC || parent.getType() == MessageArea.SEQ) {
for (int i = 0; i < parent.getFields().size(); i++) {
MessageArea item = parent.getFields().get(i);
if (item.getType() == MessageArea.TAG) {
if (((TagLine) item).getTno() == ano) {
checkTagLine((TagLine) item, tagName);
if (index == -1) {
((TagLine) item).setValue((String) rs.get(cnt));
} else {
......@@ -293,8 +316,8 @@ public class SWFMessage extends AbstractMessageArea {
continue;
}
if (parent.getType() == MessageArea.SEQ ?
setGenericRepetitive((AbstractMessageArea) item, index, ano, (List) rs.get(cnt), 0)
: setGenericRepetitive((AbstractMessageArea) item, index, ano, rs, cnt)) {
setGenericRepetitive((AbstractMessageArea) item, index, ano, (List) rs.get(cnt), 0, tagName)
: setGenericRepetitive((AbstractMessageArea) item, index, ano, rs, cnt, tagName)) {
return true;
}
}
......@@ -303,7 +326,7 @@ public class SWFMessage extends AbstractMessageArea {
if (((AbstractMessageArea) parent).withIn(ano)) {
MessageArea cp = ((AbstractMessageArea) parent).getFields().get(0);//默认有一个组项
do {
setGenericRepetitive((AbstractMessageArea) cp, index, ano, rs, cnt++);
setGenericRepetitive((AbstractMessageArea) cp, index, ano, rs, cnt++, tagName);
if (rs.size() <= cnt)
return true;
try {
......@@ -380,7 +403,7 @@ public class SWFMessage extends AbstractMessageArea {
rs = new LinkedList<Object>();
getGenericRepetitive((AbstractMessageArea) tagLine.parent, index, tagLine.getTno(), (List<Object>) rs);
} else {
rs = tagLine.getSourceValue();
rs = tagLine.getValueForHuman();
}
} else {
tagLine = this.flatModel.get(Integer.valueOf(path.substring(0, index)));
......@@ -429,7 +452,7 @@ public class SWFMessage extends AbstractMessageArea {
if (item.getType() == MessageArea.TAG) {
if (((TagLine) item).getTno() == ano) {
if (index == -1) {
rs.add(((TagLine) item).getSourceValue());
rs.add(((TagLine) item).getValueForHuman());
} else {
for (TagCell cell : ((TagLine) item).cellList) {
if (cell.cnt == index) {
......@@ -509,4 +532,110 @@ public class SWFMessage extends AbstractMessageArea {
}
}
/**
* 获取swift报文体
*/
public String getMsg() {
if (body != null) {
return body;
}
StringWriter sw = null;
SwtBufferedWriter sb = null;
try {
Assert.notNull(sndbic, "发报行不能为空");
Assert.notNull(rcvbic, "收报行不能为空");
sw = new StringWriter();
sb = this.new SwtBufferedWriter(sw);
sb.write("{1:F01");
sb.write(getLogicalTerminalAddress(sndbic, "1") + "0000000000");
sb.write("}");
sb.write("{2:I" + mtType.substring(0, 3));
sb.write(getLogicalTerminalAddress(rcvbic, "2"));
sb.write("N");
sb.write("}");
if(mtType.endsWith("COV")){
sb.write("{3:{119:COV}}");
}
sb.write("{4:");
sb.newLine();
printMsgBody(this, sb);
sb.write("-}");
sb.flush();
body = sw.toString();
} catch (Exception e) {
return "获取报文体异常:" + e.getMessage();
} finally {
try {
if (sb != null)
sb.close();
} catch (IOException ioe) {
}
}
return body;
}
private String getLogicalTerminalAddress(String bic, String field) {
if (bic == null) {
return null;
}
if ((bic.length() == 8) || (bic.length() == 11) || (bic.length() == 12)) {
if (bic.length() == 11) {
if ("1".equals(field)) {
return bic.substring(0, 8) + "A" + bic.substring(8, 11);
}
return bic.substring(0, 8) + "X" + bic.substring(8, 11);
}
if (bic.length() == 8) {
if ("1".equals(field)) {
return bic.substring(0, 8) + "AXXX";
}
return bic.substring(0, 8) + "XXXX";
}
return bic;
}
throw new IllegalStateException("第"+field+"部分的bic码["+bic+"]不符合规范,需满足8、11或12位长度");
}
private void printMsgBody(AbstractMessageArea ma, SwtBufferedWriter sb) throws IOException {
for (MessageArea sma : ma.getFields()) {
if (sma.getType() == TAG) {
sb.write((TagLine) sma);
} else {
for (MessageArea ssma : ((AbstractMessageArea) sma).getFields()) {
AbstractMessageArea temp = (AbstractMessageArea) ssma;
printMsgBody(temp, sb);
}
}
}
}
class SwtBufferedWriter extends BufferedWriter {
public static final String NEW_LINE = "\r\n";
public SwtBufferedWriter(Writer arg0) {
super(arg0);
}
public void newLine() throws IOException {
write(NEW_LINE);
}
public void write(TagLine tagLine) throws IOException {
checkNullForNeedField(tagLine);
String val = tagLine.getValue();
if (!val.equals("")) {
write(":" + tagLine.getName() + ":");
write(val);
write(NEW_LINE);
}
}
private void checkNullForNeedField(TagLine tagLine) {
boolean flag = "M".equals(tagLine.getStatus()) && "".equals(tagLine.getValue()) ? true : false;
Assert.state(!flag, "域[" + tagLine.getName() + "]是必输项,不能为空");
}
}
}
......@@ -9,18 +9,22 @@ import java.util.LinkedList;
import java.util.List;
public class ParseSwiftTest extends TestCase {
public void testLoadSwiftMessage() {
String path = "F:\\brilliance\\jkpt-pj\\task\\南洋商业银行\\接口配置\\swift标准报文\\950.sf2";
String path = ParseSwiftTest.class.getClassLoader().getResource("103_SEND.sf2").getPath();
String msg = MessageAnalyzer.readFull(path);
SWFMessage swf = MessageAnalyzer.loadSwiftMessage(msg);
System.out.println(swf.toJSON());
}
public void testGetFlatModel() {
SWFMessage obj = MessageFormat.getSWFMessage("101");
SWFMessage obj = MessageFormat.getSWFMessage("202COV");
System.out.println(obj.getFlatModel());
}
/**
* 利用序号获取报文指定域和域块的值
*/
public void testGetGenericByNo() {
String path = ParseSwiftTest.class.getClassLoader().getResource("950.sf2").getPath();
String msg = MessageAnalyzer.readFull(path);
......@@ -40,6 +44,9 @@ public class ParseSwiftTest extends TestCase {
System.out.println("获取13域(循环的Sequence B的32B域)=" + swf.getGenericByNo("13"));
}
/**
* 利用域名获取报文指定域和域块的值
*/
public void testGetGenericByTag() {
String path = ParseSwiftTest.class.getClassLoader().getResource("950.sf2").getPath();
String msg = MessageAnalyzer.readFull(path);
......@@ -58,6 +65,9 @@ public class ParseSwiftTest extends TestCase {
assertEquals(swf.getGenericByTag("32B"), swf.getGenericByNo("13"));
}
/**
* 利用序号获取报文指定域和域块值得具体部分
*/
public void testGetGenericByNoWithIndexs() {
String path = ParseSwiftTest.class.getClassLoader().getResource("950.sf2").getPath();
String msg = MessageAnalyzer.readFull(path);
......@@ -74,6 +84,9 @@ public class ParseSwiftTest extends TestCase {
+ swf.getGenericByTag("32B.0", 0));
}
/**
* 利用域名获取报文指定域和域块值得具体部分
*/
public void testGetGenericByTagWithIndexs() {
String path = ParseSwiftTest.class.getClassLoader().getResource("950.sf2").getPath();
String msg = MessageAnalyzer.readFull(path);
......@@ -86,38 +99,37 @@ public class ParseSwiftTest extends TestCase {
assertEquals(swf.getGenericByTag("23E",1,2), swf.getGenericByNo("12",1,2));
}
/**
* 利用序号设置域或域模块的值
*/
public void testSetGenericByNo() {
SWFMessage swf = MessageFormat.getSWFMessage("950");
//填充950的20域
swf.setGenericByNo("1", "STMMDL129058636E");
assertEquals(swf.getGenericByNo("1"), "STMMDL129058636E");
//填充950的60a域
swf.setGenericByNo("4", "C120330GBP94854,84", "60F");
assertEquals(swf.getGenericByNo("4"), "C120330GBP94854,84");
swf.setGenericByNo("4", "C120330GBP94854.84", "60F");
assertEquals(swf.getGenericByNo("4"), "C120330GBP94854.84");
//填充950的60a域的第二部分
swf.setGenericByNo("4.1", "120330", "60F");
assertEquals(swf.getGenericByNo("4.1"), "120330");
//填充950的61循环域
List<String> sixtyone = new LinkedList<String>();
sixtyone.add("1204120402DP10000,25S103CP23712040100007//GBS020420N8IM0HT\n" +
sixtyone.add("1204120402DP10000.25S103CP23712040100007//GBS020420N8IM0HT\r\n" +
"FENG XING");
sixtyone.add("1304220402DP30000,55S103CP23712040100003//GBS02042H98IM0HS\n" +
sixtyone.add("1304220402DP30000.55S103CP23712040100003//GBS02042H98IM0HS\r\n" +
"FENG ZZWE");
sixtyone.add("1404320402DP20115,S103CP23712040200001//GBS020428Z8IVWJK\n" +
sixtyone.add("1404320402DP20115S103CP23712040200001//GBS020428Z8IVWJK\r\n" +
"FENG EFFE");
swf.setGenericByNo("5", sixtyone);
assertEquals(swf.getGenericByNo("5"), sixtyone);
//填充950的61域第5部分,Amount
swf = MessageFormat.getSWFMessage("950");
List<String> sixtyone_amount = new LinkedList<String>();
sixtyone_amount.add("10000,25");
sixtyone_amount.add("30000,55");
sixtyone_amount.add("20115,");
swf.setGenericByNo("5.4", sixtyone_amount);
sixtyone_amount = new LinkedList<String>();
sixtyone_amount.add("10000.25");
sixtyone_amount.add("30000.55");
sixtyone_amount.add("20115");
swf.setGenericByNo("5.4", sixtyone_amount);
assertEquals(swf.getGenericByNo("5.4"), sixtyone_amount);
//填充101的不可循环的Sequence A的20域
swf = MessageFormat.getSWFMessage("101");
......@@ -143,44 +155,50 @@ public class ParseSwiftTest extends TestCase {
swf.setGenericByNo("12", comList);
assertEquals(swf.getGenericByNo("12"), comList);
comList = new LinkedList<>();
comList.add("USD80000,");
comList.add("USD12322,11");
comList.add("USD80000");
comList.add("USD12322.11");
swf.setGenericByNo("13", comList);
assertEquals(swf.getGenericByNo("13"), comList);
}
/**
* 利用域名设置域或域模块的值
*/
public void testSetGenericByTag() {
SWFMessage swf = MessageFormat.getSWFMessage("950");
//填充950的20域
swf.setGenericByTag("20", "STMMDL129058636E");
assertEquals(swf.getGenericByTag("20"), "STMMDL129058636E");
//填充950的60a域
swf.setGenericByTag("60F", "C120330GBP94854,84");
assertEquals(swf.getGenericByTag("60F"), "C120330GBP94854,84");
swf.setGenericByTag("60F", "C120330GBP94854.84");
assertEquals(swf.getGenericByTag("60F"), "C120330GBP94854.84");
//填充950的60a域的第二部分
swf.setGenericByTag("60F.1", "120330");
assertEquals(swf.getGenericByTag("60F.1"), "120330");
//填充950的61循环域
List<String> sixtyone = new LinkedList<String>();
sixtyone.add("1204120402DP10000,25S103CP23712040100007//GBS020420N8IM0HT\n" +
sixtyone.add("1204120402DP10000,25S103CP23712040100007//GBS020420N8IM0HT\r\n" +
"FENG XING");
sixtyone.add("1304220402DP30000,55S103CP23712040100003//GBS02042H98IM0HS\n" +
sixtyone.add("1304220402DP30000,55S103CP23712040100003//GBS02042H98IM0HS\n\n" +
"FENG ZZWE");
sixtyone.add("1404320402DP20115,S103CP23712040200001//GBS020428Z8IVWJK\n" +
sixtyone.add("1404320402DP20115,S103CP23712040200001//GBS020428Z8IVWJK\n\n" +
"FENG EFFE");
swf.setGenericByTag("61", sixtyone);
sixtyone = new LinkedList<String>();
sixtyone.add("1204120402DP10000.25S103CP23712040100007//GBS020420N8IM0HT\r\n" +
"FENG XING");
sixtyone.add("1304220402DP30000.55S103CP23712040100003//GBS02042H98IM0HS\r\n" +
"FENG ZZWE");
sixtyone.add("1404320402DP20115S103CP23712040200001//GBS020428Z8IVWJK\r\n" +
"FENG EFFE");
assertEquals(swf.getGenericByTag("61"), sixtyone);
//填充950的61域第5部分,Amount
swf = MessageFormat.getSWFMessage("950");
List<String> sixtyone_amount = new LinkedList<String>();
sixtyone_amount.add("10000,25");
sixtyone_amount.add("30000,55");
sixtyone_amount.add("20115,");
swf.setGenericByTag("61.4", sixtyone_amount);
sixtyone_amount = new LinkedList<String>();
sixtyone_amount.add("10000.25");
sixtyone_amount.add("30000.55");
sixtyone_amount.add("20115");
swf.setGenericByTag("61.4", sixtyone_amount);
assertEquals(swf.getGenericByTag("61.4"), sixtyone_amount);
//填充101的不可循环的Sequence A的20域
swf = MessageFormat.getSWFMessage("101");
......@@ -206,9 +224,96 @@ public class ParseSwiftTest extends TestCase {
swf.setGenericByTag("23E", comList);
assertEquals(swf.getGenericByTag("23E"), comList);
comList = new LinkedList<>();
comList.add("USD80000,");
comList.add("USD12322,11");
comList.add("USD80000");
comList.add("USD12322.11");
swf.setGenericByTag("32B", comList);
assertEquals(swf.getGenericByTag("32B"), comList);
}
/**
* 利用域名设置域或域模块的值,最后组成950 swift报文
*/
public void testGetMsgBodyFor950() {
SWFMessage swf = MessageFormat.getSWFMessage("950");
swf.setGenericByTag("20", "STMMDL129058636E");
swf.setGenericByTag("25", "68446375");
swf.setGenericByTag("28C.0", "00066");
swf.setGenericByTag("28C.1", "00001");
swf.setGenericByTag("60F.0", "C");
swf.setGenericByTag("60F.1", "120330");
swf.setGenericByTag("60F.2", "GBP");
swf.setGenericByTag("60F.3", "94854.84");
//填充950的61循环域
List<String> sixtyone = new LinkedList<String>();
sixtyone.add("1204120402DP10000.25S103CP23712040100007//GBS020420N8IM0HT\n" +
"FENG XING");
sixtyone.add("1304220402DP30000.55S103CP23712040100003//GBS02042H98IM0HS\n" +
"FENG ZZWE");
sixtyone.add("1404320402DP20115.S103CP23712040200001//GBS020428Z8IVWJK\n" +
"FENG EFFE");
swf.setGenericByTag("61", sixtyone);
swf.setGenericByTag("62F.0", "C");
swf.setGenericByTag("62F.1", "120402");
swf.setGenericByTag("62F.2", "GBP");
swf.setGenericByTag("62F.3", "14739.84");
swf.setGenericByTag("64", "C120402GBP14739.84");
System.out.println("开始生成950报文");
//设置收发报行
swf.setSndbic("NYCBCNSH");
swf.setRcvbic("ICBKGB2LAAAA");
System.out.println(swf.getMsg());
System.out.println("结束生成950报文");
}
/**
* 利用域名设置域或域模块的值,最后组成101 swift报文
*/
public void testGetMsgBodyFor101() {
SWFMessage swf = MessageFormat.getSWFMessage("101");
swf.setGenericByTag("20", "STMMDL129058636E");
swf.setGenericByTag("28D.0", "1");
swf.setGenericByTag("28D.1", "10");
swf.setGenericByTag("30", "191111");
List<Object> comList = new LinkedList<>();
comList.add("101651ATT000005W");
comList.add("321651ATT000005Z");
swf.setGenericByTag("21", comList);
comList = new LinkedList<>();
List<String> one = new LinkedList<>();
one.add("CHQB/HELLO");
one.add("CHQB/WORLD");
comList.add(one);
one = new LinkedList<>();
one.add("CHQB/MY");
one.add("CHQB/NAME");
one.add("CHQB/IS");
comList.add(one);
swf.setGenericByTag("23E", comList);
List<Object> b32 = new LinkedList<>();
b32.add("USD");
b32.add("USD");
swf.setGenericByTag("32B.0", b32);
b32 = new LinkedList<>();
b32.add("80000");
b32.add("12322.11");
swf.setGenericByTag("32B.1", b32);
List<Object> b59 = new LinkedList<>();
b59.add("/22222222222");
b59.add("/11111111111");
swf.setGenericByTag("59.0", b59);
b59 = new LinkedList<>();
b59.add("STRIX LIMITED\r\nFORREST HOUSE RONALDSWAY BALLASALLA\r\nISLE OF MAN UK");
b59.add("HOLLW LSHIUHIWA\r\nWZDESSS HOUSE WAWAZDR\r\nDEDFFF OF EE ZK");
swf.setGenericByTag("59.1", b59);
List<Object> b71 = new LinkedList<>();
b71.add("BEN");
b71.add("OUR");
swf.setGenericByTag("71A", b71);
//设置收发报行
swf.setSndbic("NYCBCNSH");
swf.setRcvbic("ICBKGB2LAAAA");
System.out.println("开始生成101报文");
System.out.println(swf.getMsg());
System.out.println("结束生成101报文");
}
}
{1:F01NYCBCNSHAXXX0000000000}{2:I103BKCHHKHHAXXXN}{4:
:20:NYCBCNSH1
:23B:CRED
:32A:180608USD80020,12
:33B:USD80070,12
:36:5,11
:50K:/110000000
FOSHAN HUAXI IRON PRINTED CAN CO.,L
TD
HEJIAO INDUSTRY AREA NANZHUANG CHAN
CHENG FOSHAN CHINA
:52A:NYCBCNSH2
:53D:STRIX LIMITED
:54A:WYCBCNSH
:55D:HU LU WA
:56A:/8655221555
MdlBnkBIC
:57A:/6659823266333
AcctBnkBIC
:59:/22000000
STRIX LIMITED
FORREST HOUSE RONALDSWAY BALLASALLA
ISLE OF MAN UK
:70:RmtInfo
:71A:OUR
:71G:USD50,
:72:PstcrptDscInfo
-}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment