Commit 599cf024 by WeiCong

优化性能减少不必要的判断;修复循环域之后的域的值填充

parent d8ac5a73
......@@ -5,63 +5,72 @@ import java.util.List;
public abstract class AbstractMessageArea implements MessageArea {
private List<MessageArea> fields = new ArrayList<MessageArea>();
private String name;
private String anlyReg;
private String desp;
public String getDesp() {
return desp;
}
public void setDesp(String desp) {
this.desp = desp;
}
public void toSwfMessage(StringBuilder sb) {
for(MessageArea field : fields)
field.toSwfMessage(sb);
}
public String getName() {
// TODO Auto-generated method stub
return name;
}
public void setName(String name)
{
this.name = name;
}
public List<MessageArea> getFields()
{
return fields;
}
public void addSubArea(MessageArea sub) {
// TODO Auto-generated method stub
fields.add(sub);
}
public MessageArea clone()
{
return null;
}
public boolean isEnd()
{
return false;
}
private MessageArea pattern;
public void setPattern(MessageArea pattern)
{
this.pattern = pattern;
}
public MessageArea getPattern()
{
return this.pattern;
}
public String getAnlyReg() {
return anlyReg;
}
public void setAnlyReg(String anlyReg) {
this.anlyReg = anlyReg;
}
private List<MessageArea> fields = new ArrayList<MessageArea>();
private String name;
private String anlyReg;
private String desp;
private MessageArea pattern;
public int begNo=Integer.MAX_VALUE;
public int endNo=Integer.MIN_VALUE;
public boolean withIn(int index){
AbstractMessageArea ama=((AbstractMessageArea)this.fields.get(0));
return index>=ama.begNo && index<=ama.endNo;
}
public String getDesp() {
return desp;
}
public void setDesp(String desp) {
this.desp = desp;
}
public void toSwfMessage(StringBuilder sb) {
for (MessageArea field : fields)
field.toSwfMessage(sb);
}
public String getName() {
// TODO Auto-generated method stub
return name;
}
public void setName(String name) {
this.name = name;
}
public List<MessageArea> getFields() {
return fields;
}
public void addSubArea(MessageArea sub) {
// TODO Auto-generated method stub
fields.add(sub);
}
public MessageArea clone() {
return null;
}
public boolean isEnd() {
return false;
}
public MessageArea getPattern() {
return this.pattern;
}
public void setPattern(MessageArea pattern) {
this.pattern = pattern;
}
public String getAnlyReg() {
return anlyReg;
}
public void setAnlyReg(String anlyReg) {
this.anlyReg = anlyReg;
}
}
......@@ -3,22 +3,23 @@ package com.brilliace.swifteditor.tag.message;
import java.util.List;
public class CYC extends AbstractMessageArea {
private final int type = CYC;
private final int type = CYC;
public MessageArea clone() {
CYC cp =new CYC();
cp.setName(this.getName());
List<MessageArea> fields = this.getFields();
for(MessageArea field : fields)
cp.addSubArea(field.clone());
cp.setAnlyReg(this.getAnlyReg());
return cp;
}
public MessageArea clone() {
CYC cp = new CYC();
cp.setName(this.getName());
List<MessageArea> fields = this.getFields();
for (MessageArea field : fields)
cp.addSubArea(field.clone());
cp.setAnlyReg(this.getAnlyReg());
cp.begNo = this.begNo;
cp.endNo = this.endNo;
return cp;
}
public int getType() {
return type;
}
public int getType() {
return type;
}
}
......@@ -14,242 +14,230 @@ import java.util.*;
public class MessageFormat {
private static final String MT_PATTERN = "swift_pattern_2018.xlsx";
public static Map<String,SWFMessage> mtCache = new HashMap<String,SWFMessage>();
static{
loadSWFMessage();
}
public static SWFMessage getSWFMessage(String mtType){
SWFMessage swfmsg = mtCache.get(mtType);
if(swfmsg == null && mtType.charAt(1)=='9')
swfmsg = mtCache.get('n'+mtType.substring(1));
if(swfmsg != null)
{
swfmsg.setMtType(mtType);
return swfmsg.clone();
}
return null;
}
public static Gson gson = new Gson();
public static void loadSWFMessage()
{
InputStream ins = MessageFormat.class.getClassLoader().getResourceAsStream(MT_PATTERN);
try {
@SuppressWarnings("resource")
XSSFWorkbook workBook = new XSSFWorkbook(ins);
int sheetCount = workBook.getNumberOfSheets();
for(int i=0;i<sheetCount;i++)
{
Sheet sheet = workBook.getSheetAt(i);
loadSingleSWFSheet(sheet);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void loadSingleSWFSheet(Sheet sheet)
{
System.out.println(sheet.getSheetName());
//获得当前sheet的开始行
int firstRowNum = sheet.getFirstRowNum();
//获得当前sheet的结束行
int lastRowNum = sheet.getLastRowNum();
Stack<MessageArea> stack = new Stack<MessageArea>();
SWFMessage message = new SWFMessage();
message.setMtType(sheet.getSheetName());
mtCache.put(sheet.getSheetName(), message);
stack.add(message);
int lastTno = 0;
rowBegin:
for(int i= firstRowNum;i<=lastRowNum ;i++)
{
Row row = sheet.getRow(i);
if(row == null)
continue;
//获得当前行的开始列
int firstCellNum = row.getFirstCellNum();
//获得当前行的列数
int lastCellNum = row.getPhysicalNumberOfCells();
for(int j=0;j<= lastCellNum - firstCellNum;j++)
{
Cell cell = row.getCell(j+firstCellNum);
String cellValue = getCellValue(cell).trim();
if(j==0)
{
if(cellValue.length() == 0) //空行
continue rowBegin;
if(cellValue.matches("O|M"))
{
String tagName = getCellValue(row.getCell(j+firstCellNum+1));
String desp = getCellValue(row.getCell(j+firstCellNum+2));
String tno = getCellValue(row.getCell(j+firstCellNum+4)).replaceAll("\\D", "");
String pattern = getCellValue(row.getCell(j+firstCellNum+3));
TagLine tagLine = TagFormat.getTagLine(tagName); //一行tag
tagLine.setStatus(cellValue);
tagLine.setDesp(desp);
try{
tagLine.setTno(Integer.parseInt(tno));
lastTno = tagLine.getTno();
}catch(Exception e)
{
System.out.println(Integer.valueOf(tno.charAt(0)));
}
tagLine.setName(tagName);
if(tagName.endsWith("a") && pattern.contains(" or "))
{
tagName = tagName.substring(0,tagName.length() - 1);
String[] sube = pattern.split(",|or");
//String[] tagSuffix = new String[sube.length];
List<String> tagSuffix = new ArrayList<String>();
boolean hasNoLetter = false;
for(int m=0; m<sube.length; m++)
{
if(sube[m].trim().length() == 0)
continue;
if(!hasNoLetter && sube[m].contains("No") && sube[m].contains("letter") && sube[m].contains("option"))
{
tagSuffix.add(tagName);
hasNoLetter = true;
continue;
}
tagSuffix.add(tagName + sube[m].trim());
}
tagLine.setSuffix(tagSuffix);
}
stack.peek().addSubArea(tagLine);
continue rowBegin;
}
else if(cellValue.matches("[\\-]{1,}\\>")) //循环开发
{
//循环开始
CYCList cycLst = new CYCList();
cycLst.setName((lastTno+1)+"");
CYC cyc = new CYC();
cycLst.addSubArea(cyc);
cycLst.setPattern(cyc);
stack.peek().addSubArea(cycLst);
stack.push(cyc);
continue rowBegin;
}
else if(cellValue.matches("[\\-]{1,}\\|")) //循环结束
{
//循环结束
stack.pop();
continue rowBegin;
}
else if(cellValue.indexOf("End of") >= 0) //序列接收
{
//循环结束
stack.pop();
continue rowBegin;
}
int idx0 = cellValue.indexOf("Sequence");
int idx1 = cellValue.indexOf("Subsequence");
if(idx0 >=0 || idx1>= 0 ) //序列开始
{
SEQList seqlst = new SEQList();
//判断是否有循环
if(cellValue.indexOf("Repetitive") >= 0)
seqlst.setCyc(1);
else
seqlst.setCyc(0);
//判断是否必填
if(cellValue.indexOf("Mandatory") >= 0)
seqlst.setOm("M");
else
seqlst.setOm("O");
//取出名字
int idx = 0;
if(idx0>=0)
idx = idx0+ "Sequence".length();
if(idx1>=0)
idx = idx1+ "Subsequence".length();
String name = "";
while(idx < cellValue.length())
{
char c = cellValue.charAt(idx);
if(c>='a' && c<='z' || (c>='0' && c<= '9') || (c>='A' && c<='Z'))
{
name= name +c;
}
else
{
if(name.length() !=0)
break;
}
idx++;
}
seqlst.setName(name);
SEQ seq = new SEQ();
seqlst.addSubArea(seq);
seqlst.setPattern(seq);
seqlst.setDesp(cellValue);
stack.peek().addSubArea(seqlst);
stack.push(seq);
continue rowBegin;
}
else //标题
{
if(message.getName()==null)
message.setName(cellValue);
}
}
System.out.print(cellValue);
}
System.out.print("\r\n");
}
message.setAnlyReg(MessageAnalyzer.generReg(message));
}
private static String getCellValue(Cell cell){
String cellValue = "";
if(cell == null){
return cellValue;
}
//把数字当成String来读,避免出现1读成1.0的情况
if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
cell.setCellType(Cell.CELL_TYPE_STRING);
}
//判断数据的类型
switch (cell.getCellType()){
case Cell.CELL_TYPE_NUMERIC: //数字
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING: //字符串
cellValue = String.valueOf(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN: //Boolean
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA: //公式
cellValue = String.valueOf(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK: //空值
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR: //故障
cellValue = "非法字符";
break;
default:
cellValue = "未知类型";
break;
private static final String MT_PATTERN = "swift_pattern_2018.xlsx";
public static Map<String, SWFMessage> mtCache = new HashMap<String, SWFMessage>();
public static Gson gson = new Gson();
static {
loadSWFMessage();
}
public static SWFMessage getSWFMessage(String mtType) {
SWFMessage swfmsg = mtCache.get(mtType);
if (swfmsg == null && mtType.charAt(1) == '9')
swfmsg = mtCache.get('n' + mtType.substring(1));
if (swfmsg != null) {
swfmsg.setMtType(mtType);
return swfmsg.clone();
}
return null;
}
public static void loadSWFMessage() {
InputStream ins = MessageFormat.class.getClassLoader().getResourceAsStream(MT_PATTERN);
try {
@SuppressWarnings("resource")
XSSFWorkbook workBook = new XSSFWorkbook(ins);
int sheetCount = workBook.getNumberOfSheets();
for (int i = 0; i < sheetCount; i++) {
Sheet sheet = workBook.getSheetAt(i);
loadSingleSWFSheet(sheet);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void loadSingleSWFSheet(Sheet sheet) {
System.out.println(sheet.getSheetName());
//获得当前sheet的开始行
int firstRowNum = sheet.getFirstRowNum();
//获得当前sheet的结束行
int lastRowNum = sheet.getLastRowNum();
Stack<MessageArea> stack = new Stack<MessageArea>();
SWFMessage message = new SWFMessage();
message.setMtType(sheet.getSheetName());
mtCache.put(sheet.getSheetName(), message);
stack.add(message);
int lastTno = 0;
rowBegin:
for (int i = firstRowNum; i <= lastRowNum; i++) {
Row row = sheet.getRow(i);
if (row == null)
continue;
//获得当前行的开始列
int firstCellNum = row.getFirstCellNum();
//获得当前行的列数
int lastCellNum = row.getPhysicalNumberOfCells();
for (int j = 0; j <= lastCellNum - firstCellNum; j++) {
Cell cell = row.getCell(j + firstCellNum);
String cellValue = getCellValue(cell).trim();
if (j == 0) {
if (cellValue.length() == 0) //空行
continue rowBegin;
if (cellValue.matches("O|M")) {
String tagName = getCellValue(row.getCell(j + firstCellNum + 1));
String desp = getCellValue(row.getCell(j + firstCellNum + 2));
String tno = getCellValue(row.getCell(j + firstCellNum + 4)).replaceAll("\\D", "");
String pattern = getCellValue(row.getCell(j + firstCellNum + 3));
TagLine tagLine = TagFormat.getTagLine(tagName); //一行tag
tagLine.setStatus(cellValue);
tagLine.setDesp(desp);
try {
tagLine.setTno(Integer.parseInt(tno));
lastTno = tagLine.getTno();
} catch (Exception e) {
System.out.println(Integer.valueOf(tno.charAt(0)));
}
tagLine.setName(tagName);
if (tagName.endsWith("a") && pattern.contains(" or ")) {
tagName = tagName.substring(0, tagName.length() - 1);
String[] sube = pattern.split(",|or");
//String[] tagSuffix = new String[sube.length];
List<String> tagSuffix = new ArrayList<String>();
boolean hasNoLetter = false;
for (int m = 0; m < sube.length; m++) {
if (sube[m].trim().length() == 0)
continue;
if (!hasNoLetter && sube[m].contains("No") && sube[m].contains("letter") && sube[m].contains("option")) {
tagSuffix.add(tagName);
hasNoLetter = true;
continue;
}
tagSuffix.add(tagName + sube[m].trim());
}
tagLine.setSuffix(tagSuffix);
}
stack.peek().addSubArea(tagLine);
if (stack.peek().getType() == MessageArea.CYC || stack.peek().getType() == MessageArea.SEQ) {
if (((AbstractMessageArea) stack.peek()).begNo > tagLine.getTno()) {
((AbstractMessageArea) stack.peek()).begNo = tagLine.getTno();
}
if (((AbstractMessageArea) stack.peek()).endNo < tagLine.getTno()) {
((AbstractMessageArea) stack.peek()).endNo = tagLine.getTno();
}
}
continue rowBegin;
} else if (cellValue.matches("[\\-]{1,}\\>")) //循环开发
{
//循环开始
CYCList cycLst = new CYCList();
cycLst.setName((lastTno + 1) + "");
CYC cyc = new CYC();
cycLst.addSubArea(cyc);
cycLst.setPattern(cyc);
stack.peek().addSubArea(cycLst);
stack.push(cyc);
continue rowBegin;
} else if (cellValue.matches("[\\-]{1,}\\|")) //循环结束
{
//循环结束
stack.pop();
continue rowBegin;
} else if (cellValue.indexOf("End of") >= 0) //序列接收
{
//循环结束
stack.pop();
continue rowBegin;
}
int idx0 = cellValue.indexOf("Sequence");
int idx1 = cellValue.indexOf("Subsequence");
if (idx0 >= 0 || idx1 >= 0) //序列开始
{
SEQList seqlst = new SEQList();
//判断是否有循环
if (cellValue.indexOf("Repetitive") >= 0)
seqlst.setCyc(1);
else
seqlst.setCyc(0);
//判断是否必填
if (cellValue.indexOf("Mandatory") >= 0)
seqlst.setOm("M");
else
seqlst.setOm("O");
//取出名字
int idx = 0;
if (idx0 >= 0)
idx = idx0 + "Sequence".length();
if (idx1 >= 0)
idx = idx1 + "Subsequence".length();
String name = "";
while (idx < cellValue.length()) {
char c = cellValue.charAt(idx);
if (c >= 'a' && c <= 'z' || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z')) {
name = name + c;
} else {
if (name.length() != 0)
break;
}
idx++;
}
seqlst.setName(name);
SEQ seq = new SEQ();
seqlst.addSubArea(seq);
seqlst.setPattern(seq);
seqlst.setDesp(cellValue);
stack.peek().addSubArea(seqlst);
stack.push(seq);
continue rowBegin;
} else //标题
{
if (message.getName() == null)
message.setName(cellValue);
}
}
System.out.print(cellValue);
}
System.out.print("\r\n");
}
message.setAnlyReg(MessageAnalyzer.generReg(message));
}
private static String getCellValue(Cell cell) {
String cellValue = "";
if (cell == null) {
return cellValue;
}
//把数字当成String来读,避免出现1读成1.0的情况
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
cell.setCellType(Cell.CELL_TYPE_STRING);
}
//判断数据的类型
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: //数字
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING: //字符串
cellValue = String.valueOf(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN: //Boolean
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA: //公式
cellValue = String.valueOf(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK: //空值
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR: //故障
cellValue = "非法字符";
break;
default:
cellValue = "未知类型";
break;
}
return cellValue.replace((char) 12288, ' ').trim();
}
return cellValue.replace((char) 12288, ' ').trim();
}
}
......@@ -3,21 +3,23 @@ package com.brilliace.swifteditor.tag.message;
import java.util.List;
public class SEQ extends AbstractMessageArea{
private final int type = SEQ;
public class SEQ extends AbstractMessageArea {
public MessageArea clone() {
SEQ cp =new SEQ();
cp.setName(this.getName());
List<MessageArea> fields = this.getFields();
for(MessageArea field : fields)
cp.addSubArea(field.clone());
cp.setAnlyReg(this.getAnlyReg());
return cp;
}
private final int type = SEQ;
public int getType() {
return type;
}
public MessageArea clone() {
SEQ cp = new SEQ();
cp.setName(this.getName());
List<MessageArea> fields = this.getFields();
for (MessageArea field : fields)
cp.addSubArea(field.clone());
cp.setAnlyReg(this.getAnlyReg());
cp.begNo = this.begNo;
cp.endNo = this.endNo;
return cp;
}
public int getType() {
return type;
}
}
......@@ -289,6 +289,9 @@ public class SWFMessage extends AbstractMessageArea {
return true;
}
} else {
if ((item.getType() == MessageArea.CYCLIST || item.getType() == MessageArea.SEQLIST) && !((AbstractMessageArea) item).withIn(ano)) {
continue;
}
if (parent.getType() == MessageArea.SEQ ?
setGenericRepetitive((AbstractMessageArea) item, index, ano, (List) rs.get(cnt), 0)
: setGenericRepetitive((AbstractMessageArea) item, index, ano, rs, cnt)) {
......@@ -296,35 +299,22 @@ public class SWFMessage extends AbstractMessageArea {
}
}
}
} else if (parent.getType() == MessageArea.CYCLIST) {
MessageArea cp = ((AbstractMessageArea) parent).getFields().get(0);//默认有一个组项
do {
setGenericRepetitive((AbstractMessageArea) cp, index, ano, rs, cnt++);
if (rs.size() <= cnt)
return true;
try {
cp = ((AbstractMessageArea) parent).getFields().get(cnt);
} catch (Exception e) {
cp = ((AbstractMessageArea) parent).getPattern().clone();
parent.addSubArea(cp);
}
}
while (true);
} else if (parent.getType() == MessageArea.SEQLIST) {
MessageArea cp = ((AbstractMessageArea) parent).getFields().get(0);//默认有一个组项
do {
SEQList tmp = (SEQList) parent;
setGenericRepetitive((AbstractMessageArea) cp, index, ano, rs, cnt);
if (rs.size() <= ++cnt)
return true;
try {
cp = ((AbstractMessageArea) parent).getFields().get(cnt);
} catch (Exception e) {
cp = ((AbstractMessageArea) parent).getPattern().clone();
parent.addSubArea(cp);
} else if (parent.getType() == MessageArea.CYCLIST || parent.getType() == MessageArea.SEQLIST) {
if (((AbstractMessageArea) parent).withIn(ano)) {
MessageArea cp = ((AbstractMessageArea) parent).getFields().get(0);//默认有一个组项
do {
setGenericRepetitive((AbstractMessageArea) cp, index, ano, rs, cnt++);
if (rs.size() <= cnt)
return true;
try {
cp = ((AbstractMessageArea) parent).getFields().get(cnt);
} catch (Exception e) {
cp = ((AbstractMessageArea) parent).getPattern().clone();
parent.addSubArea(cp);
}
}
while (true);
}
while (true);
}
return false;
}
......@@ -452,7 +442,10 @@ public class SWFMessage extends AbstractMessageArea {
} else if (((TagLine) item).getTno() > ano) {
break;
}
} else if (item.getType() == MessageArea.CYCLIST) {
} else if (item.getType() == MessageArea.CYCLIST || item.getType() == MessageArea.SEQLIST) {
if (!((AbstractMessageArea) item).withIn(ano)) {
continue;
}
if (parent.getType() == MessageArea.SEQ) {
List<Object> sec = new LinkedList<>();
rs.add(sec);
......
......@@ -37,6 +37,7 @@ public class ParseSwiftTest extends TestCase {
System.out.println("获取3域第二部分(不可循环的Sequence A的28D域)=" + swf.getGenericByNo("3.1"));
System.out.println("获取10域(循环的Sequence B的21域)=" + swf.getGenericByNo("10"));
System.out.println("获取12域(循环的Sequence B下的循环部分的23E域)=" + swf.getGenericByNo("12"));
System.out.println("获取13域(循环的Sequence B的32B域)=" + swf.getGenericByNo("13"));
}
public void testGetGenericByTag() {
......@@ -54,6 +55,7 @@ public class ParseSwiftTest extends TestCase {
assertEquals(swf.getGenericByTag("21"), swf.getGenericByNo("10"));
assertEquals(swf.getGenericByTag("23E"), swf.getGenericByNo("12"));
assertEquals(swf.getGenericByTag("59"), swf.getGenericByNo("19"));
assertEquals(swf.getGenericByTag("32B"), swf.getGenericByNo("13"));
}
public void testGetGenericByNoWithIndexs() {
......@@ -68,6 +70,8 @@ public class ParseSwiftTest extends TestCase {
System.out.println("获取10域(循环的Sequence B的21域)的第一个Sequence部分=" + swf.getGenericByNo("10", 0));
System.out.println("获取12域(循环的Sequence B下的循环部分的23E域)的第二个sequence的第三个成员="
+ swf.getGenericByNo("12", 1, 2));
System.out.println("获取Sequence B下的循环部分的32B域的第一个sequence成员的第一个币种部分="
+ swf.getGenericByTag("32B.0", 0));
}
public void testGetGenericByTagWithIndexs() {
......@@ -138,6 +142,11 @@ public class ParseSwiftTest extends TestCase {
comList.add(one);
swf.setGenericByNo("12", comList);
assertEquals(swf.getGenericByNo("12"), comList);
comList = new LinkedList<>();
comList.add("USD80000,");
comList.add("USD12322,11");
swf.setGenericByNo("13", comList);
assertEquals(swf.getGenericByNo("13"), comList);
}
public void testSetGenericByTag() {
......@@ -196,5 +205,10 @@ public class ParseSwiftTest extends TestCase {
comList.add(one);
swf.setGenericByTag("23E", comList);
assertEquals(swf.getGenericByTag("23E"), comList);
comList = new LinkedList<>();
comList.add("USD80000,");
comList.add("USD12322,11");
swf.setGenericByTag("32B", comList);
assertEquals(swf.getGenericByTag("32B"), comList);
}
}
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