自定义配置化解析定长报文(JAVA)

本文介绍了一种基于配置化的Java方法来解析定长报文,该方法参考了之前的一篇博客并进行了改进。使用了Hutool、Fastjson等工具包,包括实体类、配置类和测试类的详细说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过固定实体类解析的参考我上一篇转载文章,在此基础上我修改为配置化解析报文。

原作者:

https://blog.youkuaiyun.com/weixin_33851604/article/details/88241284

 

关于jar包:hutool,fastjson,其他工具类报错可以自行解决。

实体类

MsgConfig :

package com.ins.common.msg.entity;

import java.util.LinkedList;

/**
 * MsgConfig.java 报文配置实体类
 * 
 * @author skysource
 * @date 2019-06-03 11:56:07
 * @since 1.0
 */
public class MsgConfig {
    /**
     * 消息类型
     */
    String msgType;
    /**
     * 消息规则配置,可改为抽象方法来实现多个不同的规则
     */
    LinkedList<MsgConfigInfo> msgConfigInfos;

    /**
     * @return the msgType
     */
    public String getMsgType() {
        return msgType;
    }

    /**
     * @param msgType
     *            the msgType to set
     */
    public void setMsgType(String msgType) {
        this.msgType = msgType;
    }

    /**
     * @return the msgConfigInfos
     */
    public LinkedList<MsgConfigInfo> getMsgConfigInfos() {
        return msgConfigInfos;
    }

    /**
     * @param msgConfigInfos
     *            the msgConfigInfos to set
     */
    public void setMsgConfigInfos(LinkedList<MsgConfigInfo> msgConfigInfos) {
        this.msgConfigInfos = msgConfigInfos;
    }

}

MsgConfigInfo:

package com.ins.common.msg.entity;

import java.util.LinkedList;

/**
 * MsgConfigInfo.java 报文规则配置
 * 
 * @author skysource
 * @date 2019-06-03 11:57:44
 * @since 1.0
 */
public class MsgConfigInfo {
    /**
     * 定长消息主体名称,如Head、Body等
     */
    private String msgName;
    
    /**
     * msgName值对应的字段,如key:value
     */
    private String msgField;

    /**
     * 循环字段,如值为"occurrenceCount",则表示需要根据报文字段occurrenceCount的值,循环当前报文体下面所有的值。如果occurrenceCount的值为5,则表示有5次循环
     */
    private String cycleField;

    /**
     * 固定循环次数,如,50 表示循环50次,不足50次使用占位符表示
     */
    private int fixedCycleCount;

    /**
     * 字段类型,0字符串,1Map对象,2List集合,4整数,5浮点数字
     */
    private String fieldType;
    
    /**
     * 定长消息规则集合
     */
    private LinkedList<MsgField> msgConfigs;

    /**
     * @return the msgName
     */
    public String getMsgName() {
        return msgName;
    }

    /**
     * @param msgName
     *            the msgName to set
     */
    public void setMsgName(String msgName) {
        this.msgName = msgName;
    }
    
    /**
     * @return the msgField
     */
    public String getMsgField() {
        return msgField;
    }

    /**
     * @param msgField the msgField to set
     */
    public void setMsgField(String msgField) {
        this.msgField = msgField;
    }

    /**
     * @return the msgConfigs
     */
    public LinkedList<MsgField> getMsgConfigs() {
        return msgConfigs;
    }

    /**
     * @return the cycleField
     */
    public String getCycleField() {
        return cycleField;
    }


    /**
     * @return the fixedCycleCount
     */
    public int getFixedCycleCount() {
        return fixedCycleCount;
    }

    /**
     * @param fixedCycleCount
     *            the fixedCycleCount to set
     */
    public void setFixedCycleCount(int fixedCycleCount) {
        this.fixedCycleCount = fixedCycleCount;
    }

    /**
     * @return the fieldType
     */
    public String getFieldType() {
        return fieldType;
    }

    /**
     * @param fieldType
     *            the fieldType to set
     */
    public void setFieldType(String fieldType) {
        this.fieldType = fieldType;
    }

    /**
     * @param cycleField
     *            the cycleField to set
     */
    public void setCycleField(String cycleField) {
        this.cycleField = cycleField;
    }

    /**
     * @param msgConfigs
     *            the msgConfigs to set
     */
    public void setMsgConfigs(LinkedList<MsgField> msgConfigs) {
        this.msgConfigs = msgConfigs;
    }
}

MsgField:

/**
 * package-info.java
 * 
 * @author skysource
 * @date 2019-05-30 14:24:14
 * @since 1.0
 */
package com.ins.common.msg.entity;

/**
 * 消息域 MsgField.java
 * 
 * @author skysource
 * @date 2019-06-04 11:12:49
 * @since 1.0
 */
public class MsgField {

    public MsgField(String name, int length, char fillChar, FillSide fillSide) {
        this.name = name;
        this.length = length;
        this.fillChar = fillChar;
        this.fillSide = fillSide;
    }

    public MsgField(String name, int length, char fillChar, FillSide fillSide,String fieldType) {
        this.name = name;
        this.length = length;
        this.fillChar = fillChar;
        this.fillSide = fillSide;
        this.fieldType = fieldType;
    }

    /**
     * 填充位置
     * 
     * @author Zhenwei.Zhang (2013-9-25)
     */
    public enum FillSide {
        LEFT, RIGHT
    }
     
    /** 
     * 字段名称
     */
    private String name;
    
    /** 
     * 长度 
     */
    private int length;
    
    /**
     * 填充字符
     */
    private char fillChar;
    
    /**
     * 填充位置
     */
    private FillSide fillSide;
    
    /**
     * 字段类型,0字符串,1Map对象,2List集合,4整数,5浮点数字
     */
    private String fieldType;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public char getFillChar() {
        return fillChar;
    }

    public void setFillChar(char fillChar) {
        this.fillChar = fillChar;
    }

    public FillSide getFillSide() {
        return fillSide;
    }

    public void setFillSide(FillSide fillSide) {
        this.fillSide = fillSide;
    }

    public String getFieldType() {
        return fieldType;
    }

    public void setFieldType(String fieldType) {
        this.fieldType = fieldType;
    }
}

主程序类

MsgPackage:

package com.ins.common.msg.mainPackage;

/**
 * MsgPackage.java
 * 
 * @author skysource
 * @date 2019-05-30 14:26:44
 * @since 1.0
 */
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;

import com.ins.common.msg.entity.MsgConfig;
import com.ins.common.msg.entity.MsgConfigInfo;
import com.ins.common.msg.entity.MsgField;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONUtil;

/**
 * 消息包,由多个消息片组成
 * 
 */
public abstract class MsgPackage {

    /** 消息包的消息片数组 */
    String[] pieces = null;
    
    LinkedHashMap<String, MsgConfigInfo> msgConfigMap = new LinkedHashMap<>();

    public MsgPackage(String... pieName) {
        pieces = pieName;
    }

    public MsgPackage(LinkedList<MsgConfigInfo> msgConfigInfoList) {
        msgConfigInfoList.stream().forEach(t -> msgConfigMap.put(t.getMsgName(), t));
    }


    /**
     * 组包
     * 
     * @param charsetName
     * @return
     * @throws Exception
     */
    public byte[] pack(String charsetName) throws Exception {
        int index = 0;
        byte[] result = new byte[this.getLength()];
        for (String p : pieces) {
            PropertyDescriptor pd = new PropertyDescriptor(p, this.getClass());
            Method getterMethod = pd.getReadMethod();
            MsgPiece piece = (MsgPiece) getterMethod.invoke(this);
            if (piece == null) { // 属性为空则该属性组空串
                piece = (MsgPiece) pd.getPropertyType().newInstance();
            }
            byte[] t = piece.pack(charsetName);
            System.arraycopy(t, 0, result, index, t.length);
            index += t.length;
        }
        return result;
    }

    /**
     * 解包
     * 
     * @param msg
     * @param charsetName
     * @throws Exception
     */
    public void unPack(byte[] msg, String charsetName) throws Exception {
        if (msg.length != this.getLength()) {
            throw new Exception("解消息出错,消息包长度不合法!");
        }

        int index = 0;
        for (String p : pieces) { // 创建一个新的属性对象,解包,赋值
            PropertyDescriptor pd = new PropertyDescriptor(p, this.getClass());
            Method writeMethod = pd.getWriteMethod();
            MsgPiece piece = (MsgPiece) pd.getPropertyType().newInstance();
            byte[] t = new byte[piece.getLength()];
            System.arraycopy(msg, index, t, 0, t.length);
            piece.unPack(t, charsetName);
            writeMethod.invoke(this, piece);
            index += t.length;
        }
    }

    /**
     * 解包
### 定长报文转义字符处理的方法与实现 在定长报文中,为了防止某些特殊字符干扰数据解析或传输,通常需要对其进行转义和反向解码。这种技术的核心在于定义一套清晰的规则集,使得发送方能够在特定条件下对敏感字符进行编码,而接收方则能准确识别并还原这些字符。 #### 1. 自定义转义规则的设计原则 针对定长报文的特点,设计合理的转义方案尤为重要。一般情况下,会选取一些不会自然出现在正常业务数据中的符号作为标志位[^1]。例如,“ESC”(ASCII值27)常被用来表示接下来的内容是一个特殊的控制序列而非普通的文本信息。 具体来说,可以遵循以下几个方面制定策略: - **唯一性**:确保所选标记在整个系统范围内都是独一无二的; - **简洁性**:尽量保持规则简单明了以便于维护; - **兼容性**:考虑到未来扩展的可能性,预留足够的空间适应新增需求; #### 2. 编程实践——基于Java语言的例子说明 下面给出一段具体的代码示例展示如何手动构建这样一个简易版的转义处理器: ```java public class FixedLengthMessageEncoderDecoder { private final char ESCAPE_CHAR = '\u001B'; // Escape character /** * Encodes the given message by escaping reserved characters. * * @param rawMessage The unencoded message string. * @return A new encoded version of the input message. */ public String encode(String rawMessage){ StringBuilder sbuilder=new StringBuilder(); for(int i=0;i<rawMessage.length();i++){ char currentChar=rawMessage.charAt(i); if(isReservedCharacter(currentChar)){ sbuilder.append(ESCAPE_CHAR).append((char)(currentChar+1)); }else{ sbuilder.append(currentChar); } } return sbuilder.toString(); } /** * Decodes an already received fixed-length message back into its original form. * * @param encodedMessage An incoming, possibly escaped, message from another system. * @return Original decoded message without any escapes applied. */ public String decode(String encodedMessage){ StringBuilder resultBuilder=new StringBuilder(); boolean previousWasEscape=false; for(int index=0;index<encodedMessage.length();index++){ char currCh=encodedMessage.charAt(index); if(previousWasEscape){ resultBuilder.append((char)((currCh)-1)); previousWasEscape=false; }else if(currCh==ESCAPE_CHAR){ previousWasEscape=true; }else{ resultBuilder.append(currCh); } } return resultBuilder.toString(); } /** * Determines whether this particular character needs special treatment during encoding process. * * @param testChar Single character under examination. * @return True only when passed argument represents one among predefined set requiring attention. */ protected boolean isReservedCharacter(char testChar){ switch(testChar){ case '\r':case'\n':case',':case'.':case';': return true; default: return false; } } } ``` 上述代码片段实现了基本的功能模块,包括但不限于以下几点[^1]: - 提供了一个通用方法用于检测哪些字符属于保留类别从而决定它们是否应该受到额外的关注。 - 创建两个主要的操作函数分别负责正向转换(`encode`)以及逆向重构(`decode`)流程。 - 利用了状态机的思想跟踪遇到转义符之后的状态变化情况。 #### 3. 结合Spring框架优化配置方式 除了纯手工编写之外,在现代开发环境中还可以借助成熟的开源工具简化工作量。比如在Spring集成项目里,可以通过XML声明的方式预先设定好各个字段的位置及其属性特征[^3]: ```xml <int:header-enricher> <int:header name="Content-Type" value="text/plain;charset=UTF-8"/> </int:header-enricher> <int:transformer expression="@customTranslator.translate(payload)"/> <int-jms:message-driven-channel-adapter id="jmsIn" destination-name="fixedMessagesQueue" channel="inputChannel"/> <int-file:inbound-channel-adapter directory="/path/to/files/" filename-pattern="*.txt" prevent-duplicates="true"> <int:poller max-messages-per-poll="-1" fixed-delay="5000"/> </int-file:inbound-channel-adapter> ``` 在这里引入了一个假想的服务组件`@customTranslator`专门承担起整个项目的个性化翻译职责[^3]。这样做的好处是可以把复杂的逻辑封装起来集中管理,同时也便于后期测试验证各个环节的行为表现。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值