package com.eactive.eai.message.parser; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.LinkedHashMap; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.eactive.eai.message.EncodingVar; import com.eactive.eai.message.StandardDataType; import com.eactive.eai.message.StandardItem; import com.eactive.eai.message.StandardMessage; import com.eactive.eai.message.StandardType; public class FlatReader implements StandardReader { static Logger logger = LoggerFactory.getLogger(FlatReader.class); private char FIELD_SEPARATOR = '.'; private boolean ZERO_BASE_INDEX = true; public FlatReader() { } public StandardMessage parse(StandardMessage message, Object flatObject) throws Exception { ArrayList list = message.toList(); for(int i=0; i itemMap = new LinkedHashMap(); message.setReadPosition(0); traverse(message, message, srcbytes, null, itemMap); int remain = srcbytes.length - message.getReadPosition(); if(remain > 0) { throw new Exception("flat message parsing error(overflow) remain size="+remain); } message.setBizDataCharset(encoding); logger.debug("setBizDataCharset : {}", encoding); // for(String key:itemMap.keySet()) { // logger.debug("~ itemMap : {} => {}", key, itemMap.get(key) ); // } return message; } private void traverse(StandardMessage message, StandardItem currentItem, byte[] srcbytes, String parentName , LinkedHashMap itemMap){ String fieldName = null; ArrayList childList = null; StandardItem item = null; int type = currentItem.getType(); int start = message.getReadPosition(); fieldName = genPath(parentName, currentItem.getName()); logger.debug("<-> CALL path = " + fieldName); switch(type) { case StandardType.MESSAGE: childList = currentItem.getChildsList(); for(int i=0; i map = currentItem.getArrayChilds(ai, true); if(map == null) { logger.warn("Array create failed item path = {}", fieldName +"[" + ai +"]"); continue; } childList = new ArrayList(map.values()); for(int i=0; i childList = currentItem.getChildsList(); String refItemValue =""; for(StandardItem item : childList) { if (currentFieldName.indexOf(".") < 0) { byte[] tempValue = null; if (orgRef.equals(fieldName + "." + item.getName())) { tempValue = cut(fieldName, srcbytes, start, item.getLength()); return new String(tempValue); }else{ start = start + item.getLength(); } } else { return getRefItemValue(orgRef, fieldName+"."+item.getName(), srcbytes, item, start); } } return refItemValue; } // public static void main(String[] args) { // } }