Commit 0986f44f by WeiCong

1.修复嵌套可选项的解析;2.完成通用获取域的API

parent c6c688a7
......@@ -2,11 +2,8 @@ package com.brilliace.swifteditor.tag;
import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Properties;
public class TagFormat {
public static Properties TagProps = new Properties();
......@@ -60,6 +57,7 @@ public class TagFormat {
int minLen = 1;
boolean isConst = false;
TagCell cell = null;
Stack<Integer> bracket = new Stack<Integer>();
while(true)
{
ic=reader.read();
......@@ -163,11 +161,13 @@ public class TagFormat {
else if(c == '[')
{
line.addOptions(new int[]{line.cellList.size(),-1});
bracket.push(line.options.size()-1);
}
else if(c == ']')
{
//if("51A".equals(tag))
line.fillLastOption(line.cellList.size());
//line.fillLastOption(line.cellList.size());
line.fillOption(bracket.pop(),line.cellList.size());
}
}
line.setMaxCnt(cnt-1);
......
package com.brilliace.swifteditor.tag;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.brilliace.swifteditor.tag.message.MessageArea;
import java.util.*;
public class TagLine implements MessageArea {
public List<TagCell> cellList = new ArrayList<TagCell>();
public List<int[]> options = new ArrayList<int[]>();
......@@ -15,6 +11,8 @@ public class TagLine implements MessageArea {
private String value;
private String tagName;
private String status;
public MessageArea parent;
public String getStatus() {
return status;
}
......@@ -33,6 +31,15 @@ public class TagLine implements MessageArea {
{
options.add(points);
}
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)
......@@ -260,4 +267,5 @@ public class TagLine implements MessageArea {
}
return indexMap;
}
}
package com.brilliace.swifteditor.tag.message;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import com.brilliace.swifteditor.tag.TagFormat;
import com.brilliace.swifteditor.tag.TagLine;
import com.google.gson.Gson;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.brilliace.swifteditor.tag.TagFormat;
import com.brilliace.swifteditor.tag.TagLine;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class MessageFormat {
......
package com.brilliace.swifteditor.tag.message;
import java.util.HashMap;
import java.util.Map;
import com.brilliace.swifteditor.tag.TagLine;
import com.brilliace.swifteditor.util.Assert;
import com.google.gson.Gson;
public class SWFMessage extends AbstractMessageArea {
private String mtType;
private Map<Integer, String> headers;
public Map<Integer, String> getHeaders() {
return headers;
}
public void setHeaders(Map<Integer, String> headers) {
Map<Integer, String> clone = new HashMap<Integer, String>();
clone.putAll(headers);
clone.remove(4);
this.headers = clone;
}
public Map<String, String> getBscInfo() {
return bscInfo;
}
public void setBscInfo(Map<String, String> bscInfo) {
this.bscInfo = bscInfo;
}
private Map<String, String> bscInfo;
public String getMtType() {
return mtType;
}
public void setMtType(String mtType) {
this.mtType = mtType;
}
protected SWFMessage() {
}
public static SWFMessage loadMTTemp(String mt) {
return null;
}
/***
*
* @param sb
* 缓存4文首域名
*/
public void toSwfMessage(StringBuilder sb) {
sb.append("{4:\r\n");
for (MessageArea sub : this.getFields())
sub.toSwfMessage(sb);
sb.append("\r\n}");
}
public String toSwfMessage() {
StringBuilder sb = new StringBuilder();
toSwfMessage(sb);
return sb.toString();
}
public SWFMessage clone() {
SWFMessage msg = new SWFMessage();
msg.mtType = this.mtType;
for (MessageArea field : this.getFields()) {
msg.addSubArea(field.clone());
}
msg.setName(this.getName());
msg.setAnlyReg(this.getAnlyReg());
return msg;
}
public String toJSON() {
Map<String, Object> rs = new HashMap<String, Object>();
rs.put("title", this.getName());
rs.put("mty", this.mtType);
rs.put("snd_bic", bscInfo.get("sndbic"));
rs.put("rcv_bic", bscInfo.get("rcvbic"));
if(bscInfo.containsKey("t111"))
rs.put("t111", bscInfo.get("t111"));
if(bscInfo.containsKey("t121"))
rs.put("t121", bscInfo.get("t121"));
rs.put("tdheaders", bscInfo.get("tdmsgHead"));
rs.put("headers", headers);
rs.put("swift_message", toFormatJSON());
return new Gson().toJson(rs);
// return
// String.format("{\r\ntitle:\"%s\",\r\nmty:'%s',\r\nrcv_bic:'%s',\r\ntdheader:'%s',\r\nheaders:%s,\r\nswift_message:%s\r\n}",
// this.getName(),
// this.mtType,
// this.bscInfo.get("rcvbic"),
// this.bscInfo.get("tdmsgHead")==null?null:URLEncoder.encode(this.bscInfo.get("tdmsgHead")),
// new Gson().toJson( headers),
// this.toFormatJSON()
// );
}
public String toFormatJSON() {
StringBuilder sb = new StringBuilder();
// sb.append("const M='M' \r\n");
// sb.append("const O='O' \r\n");
// sb.append("const Tags=");
toSubJson(this, sb);
// sb.append("\r\nconst Title=\""+this.getName()+"\"");
// sb.append("\r\n\r\nexport default { Tags,Title}");
return sb.toString();
}
private static void toSubJson(AbstractMessageArea ma, StringBuilder sb) {
sb.append("[\r\n");
for (MessageArea sma : ma.getFields()) {
if (sma.getType() == TAG) {
TagLine tag = (TagLine) sma;
if (tag.getName().startsWith("50")) {
System.out.println(111);
}
sb.append(String.format("\t{\"tag\":\"%s\",\"status\":\"%s\",\"tno\":%d,\"letter\":%s,\"label\":\"%s\",\"tagValue\":%s},\r\n", tag
.getSuffix().size() > 0 && tag.getName().endsWith("a") ? tag.getSuffix().get(0) : tag.getName(), tag.getStatus(),
tag.getTno(), new Gson().toJson(tag.getSuffix()), tag.getDesp(),
new Gson().toJson(new Object[] { tag.getIndexMap(), tag.getSourceValue() })));
} else if (sma.getType() == CYCLIST) {
StringBuilder buff = new StringBuilder();
// int first = 9999;
for (MessageArea ssma : ((AbstractMessageArea) sma).getFields()) {
AbstractMessageArea temp = (AbstractMessageArea) ssma;
toSubJson(temp, buff);
buff.append(',');
}
sb.append(String.format("\t{\"cyclist\":\"%s\",\"cycs\":[", sma.getName()));
// String temp = buff.toString().trim();
sb.append(buff.subSequence(0, buff.length() - 1));
sb.append("],\r\n\t\"pattern\":");
buff = new StringBuilder();
toSubJson((AbstractMessageArea) ((AbstractMessageArea) sma).getPattern(), buff);
sb.append(buff);
sb.append("\t},\r\n");
} else if (sma.getType() == SEQLIST) {
StringBuilder buff = new StringBuilder();
// int first = 9999;
for (MessageArea ssma : ((AbstractMessageArea) sma).getFields()) {
AbstractMessageArea temp = (AbstractMessageArea) ssma;
toSubJson(temp, buff);
buff.append(',');
}
SEQList seqlst = (SEQList) sma;
sb.append(String.format("\t{\"seqlist\":\"%s\",\"cyccnt\":%d,\"status\":\"%s\",\"seqs\":[", sma.getName(), seqlst.getCyc(),
seqlst.getOm()));
// String temp = buff.toString().trim();
sb.append(buff.subSequence(0, buff.length() - 1));
sb.append("],\r\n\t\"pattern\":");
buff = new StringBuilder();
toSubJson((AbstractMessageArea) ((AbstractMessageArea) sma).getPattern(), buff);
sb.append(buff);
sb.append(String.format(",\r\n\t\"desp\":\"%s\"", seqlst.getDesp()));
sb.append("\t},\r\n");
}
}
if (sb.length() > 3 && sb.charAt(sb.length() - 3) == ',')
sb.setCharAt(sb.length() - 3, ' ');
sb.append("]");
}
@Override
public int getType() {
return MESSAGE;
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
public class SWFMessage extends AbstractMessageArea {
private String mtType;
private Map<Integer, String> headers;
private Map<Integer, TagLine> flatModel = new HashMap<>();
private Map<String, String> bscInfo;
protected SWFMessage() {
}
public static SWFMessage loadMTTemp(String mt) {
return null;
}
private static void toSubJson(AbstractMessageArea ma, StringBuilder sb) {
sb.append("[\r\n");
for (MessageArea sma : ma.getFields()) {
if (sma.getType() == TAG) {
TagLine tag = (TagLine) sma;
if (tag.getName().startsWith("50")) {
System.out.println(111);
}
sb.append(String.format("\t{\"tag\":\"%s\",\"status\":\"%s\",\"tno\":%d,\"letter\":%s,\"label\":\"%s\",\"tagValue\":%s},\r\n", tag
.getSuffix().size() > 0 && tag.getName().endsWith("a") ? tag.getSuffix().get(0) : tag.getName(), tag.getStatus(),
tag.getTno(), new Gson().toJson(tag.getSuffix()), tag.getDesp(),
new Gson().toJson(new Object[]{tag.getIndexMap(), tag.getSourceValue()})));
} else if (sma.getType() == CYCLIST) {
StringBuilder buff = new StringBuilder();
// int first = 9999;
for (MessageArea ssma : ((AbstractMessageArea) sma).getFields()) {
AbstractMessageArea temp = (AbstractMessageArea) ssma;
toSubJson(temp, buff);
buff.append(',');
}
sb.append(String.format("\t{\"cyclist\":\"%s\",\"cycs\":[", sma.getName()));
// String temp = buff.toString().trim();
sb.append(buff.subSequence(0, buff.length() - 1));
sb.append("],\r\n\t\"pattern\":");
buff = new StringBuilder();
toSubJson((AbstractMessageArea) ((AbstractMessageArea) sma).getPattern(), buff);
sb.append(buff);
sb.append("\t},\r\n");
} else if (sma.getType() == SEQLIST) {
StringBuilder buff = new StringBuilder();
// int first = 9999;
for (MessageArea ssma : ((AbstractMessageArea) sma).getFields()) {
AbstractMessageArea temp = (AbstractMessageArea) ssma;
toSubJson(temp, buff);
buff.append(',');
}
SEQList seqlst = (SEQList) sma;
sb.append(String.format("\t{\"seqlist\":\"%s\",\"cyccnt\":%d,\"status\":\"%s\",\"seqs\":[", sma.getName(), seqlst.getCyc(),
seqlst.getOm()));
// String temp = buff.toString().trim();
sb.append(buff.subSequence(0, buff.length() - 1));
sb.append("],\r\n\t\"pattern\":");
buff = new StringBuilder();
toSubJson((AbstractMessageArea) ((AbstractMessageArea) sma).getPattern(), buff);
sb.append(buff);
sb.append(String.format(",\r\n\t\"desp\":\"%s\"", seqlst.getDesp()));
sb.append("\t},\r\n");
}
}
if (sb.length() > 3 && sb.charAt(sb.length() - 3) == ',')
sb.setCharAt(sb.length() - 3, ' ');
sb.append("]");
}
public Map<Integer, String> getHeaders() {
return headers;
}
public void setHeaders(Map<Integer, String> headers) {
Map<Integer, String> clone = new HashMap<Integer, String>();
clone.putAll(headers);
clone.remove(4);
this.headers = clone;
}
public Map<Integer, TagLine> getFlatModel() {
return this.flatModel;
}
public Map<String, String> getBscInfo() {
return bscInfo;
}
public void setBscInfo(Map<String, String> bscInfo) {
this.bscInfo = bscInfo;
}
public String getMtType() {
return mtType;
}
public void setMtType(String mtType) {
this.mtType = mtType;
}
/***
*
* @param sb
* 缓存4文首域名
*/
public void toSwfMessage(StringBuilder sb) {
sb.append("{4:\r\n");
for (MessageArea sub : this.getFields())
sub.toSwfMessage(sb);
sb.append("\r\n}");
}
public String toSwfMessage() {
StringBuilder sb = new StringBuilder();
toSwfMessage(sb);
return sb.toString();
}
public SWFMessage clone() {
SWFMessage msg = new SWFMessage();
msg.mtType = this.mtType;
for (MessageArea field : this.getFields()) {
msg.addSubArea(field.clone());
}
msg.setName(this.getName());
msg.setAnlyReg(this.getAnlyReg());
generateFlatModel(msg,msg.flatModel,null);
return msg;
}
public String toJSON() {
Map<String, Object> rs = new HashMap<String, Object>();
rs.put("title", this.getName());
rs.put("mty", this.mtType);
rs.put("snd_bic", bscInfo.get("sndbic"));
rs.put("rcv_bic", bscInfo.get("rcvbic"));
if (bscInfo.containsKey("t111"))
rs.put("t111", bscInfo.get("t111"));
if (bscInfo.containsKey("t121"))
rs.put("t121", bscInfo.get("t121"));
rs.put("tdheaders", bscInfo.get("tdmsgHead"));
rs.put("headers", headers);
rs.put("swift_message", toFormatJSON());
return new Gson().toJson(rs);
// return
// String.format("{\r\ntitle:\"%s\",\r\nmty:'%s',\r\nrcv_bic:'%s',\r\ntdheader:'%s',\r\nheaders:%s,\r\nswift_message:%s\r\n}",
// this.getName(),
// this.mtType,
// this.bscInfo.get("rcvbic"),
// this.bscInfo.get("tdmsgHead")==null?null:URLEncoder.encode(this.bscInfo.get("tdmsgHead")),
// new Gson().toJson( headers),
// this.toFormatJSON()
// );
}
public String toFormatJSON() {
StringBuilder sb = new StringBuilder();
// sb.append("const M='M' \r\n");
// sb.append("const O='O' \r\n");
// sb.append("const Tags=");
toSubJson(this, sb);
// sb.append("\r\nconst Title=\""+this.getName()+"\"");
// sb.append("\r\n\r\nexport default { Tags,Title}");
return sb.toString();
}
@Override
public int getType() {
return MESSAGE;
}
final Pattern CHECKPATH=Pattern.compile("^\\d+(\\.\\d)?$");
public String getGeneric(String path){
Assert.state(path != null && path != "" && CHECKPATH.matcher(path).find(),"索引["+path+"]不符合规范");
int index=path.indexOf(".");
TagLine tagLine;
if(index==-1){
tagLine=this.flatModel.get(Integer.valueOf(path));
return tagLine.getValue();
}else{
tagLine=this.flatModel.get(Integer.valueOf(path.substring(0,index)));
int part=Integer.parseInt(path.substring(index+1));
Assert.state(part>=0 && part < tagLine.cellList.size(),"索引["+path+"]指定的域块不存在");
return tagLine.cellList.get(part).value;
}
}
private void generateFlatModel(MessageArea area, Map<Integer, TagLine> flatModel, MessageArea parent) {
List<MessageArea> fieldList = ((AbstractMessageArea) area).getFields();
for(int i=0; i< fieldList.size();i++) {
MessageArea item = fieldList.get(i);
if (item.getType() == MessageArea.TAG) {
TagLine tagLine = (TagLine) item;
tagLine.parent=parent;
flatModel.put(tagLine.getTno(), tagLine);
} else if (item.getType() == MessageArea.CYCLIST || item.getType() == MessageArea.SEQLIST) {
generateFlatModel(((AbstractMessageArea) item).getFields().get(0), flatModel, item);
}else{
System.out.println(item.getType()+" no match");
continue;
}
}
}
}
package com.brilliace.swifteditor.util;
public abstract class Assert {
protected Assert() {
}
public static void notNull(Object obj, String message) {
if (obj == null) {
throw new IllegalArgumentException(message);
}
}
public static void notNullByArray(Object[] obj, String message) {
if (obj == null || obj.length == 0) {
throw new IllegalArgumentException(message);
}
}
public static void state(boolean expression, String message) {
if (!expression) {
throw new IllegalStateException(message);
}
}
}
package com.brilliace.swifteditor;
import com.brilliace.swifteditor.tag.message.MessageAnalyzer;
import com.brilliace.swifteditor.tag.message.MessageFormat;
import com.brilliace.swifteditor.tag.message.SWFMessage;
import junit.framework.TestCase;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ParseSwiftTest extends TestCase {
public void testLoadSwiftMessage() {
String path = "F:\\brilliance\\jkpt-pj\\task\\南洋商业银行\\接口配置\\swift标准报文\\950.sf2";
String msg = MessageAnalyzer.readFull(path);
SWFMessage swf = MessageAnalyzer.loadSwiftMessage(msg);
System.out.println(swf.toJSON());
}
public void testGetSWFMessage() {
SWFMessage obj = MessageFormat.getSWFMessage("101");
System.out.println(obj.getFlatModel());
}
public void testRegWithName() {
String reg = "((?<S1>\\d{6})((?<S2>\\d{4}))?)?((?<S3>[A-Z]{7}))?";
String tagsCombo = "123456";
Matcher m = Pattern.compile(reg).matcher(tagsCombo);
if (m.find()) {
System.out.println(m.group("S1"));
} else {
System.out.println("no found");
}
reg="^\\d+(\\.\\d)?$";
m = Pattern.compile(reg).matcher("122.");
if(m.find()){
System.out.println("yes");
}else{
System.out.println("no");
}
}
public void testGetTagLineValue(){
String path = "F:\\brilliance\\jkpt-pj\\task\\南洋商业银行\\接口配置\\swift标准报文\\950.sf2";
String msg = MessageAnalyzer.readFull(path);
SWFMessage swf = MessageAnalyzer.loadSwiftMessage(msg);
//获取3域
System.out.println(swf.getGeneric("3"));
//获取3域第一部分
System.out.println(swf.getGeneric("3.0"));
}
}
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