All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.neko233.skilltree.commons.parser.protobuf.ProtobufMessageV2Parser233 Maven / Gradle / Ivy

package com.neko233.skilltree.commons.parser.protobuf;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 消息解析器
 */
public class ProtobufMessageV2Parser233 {

    public static List parse(String protobufText) {
        List messageClasses = new ArrayList<>();
        String[] lines = protobufText.split("\n");

        String messageName = null;
        String comment = null;

        for (String line : lines) {
            line = line.trim();

            // 嵌套式写法解析暂不在此处支持
            if (line.startsWith("message")) {
                messageName = line.substring(line.indexOf("message") + 8, line.indexOf("{")).trim();

            } else if (line.startsWith("//")) {
                // comment
                comment = getCommentContentNotWithSeperator(line);

            } else if (line.contains("=")) {
                if (StringUtils.isBlank(messageName)) {
                    throw new IllegalArgumentException("messageName 为空 = " + messageName);
                }

                String[] tokens = line.split("=");

                String leftKey = tokens[0];
                List tokenList = Arrays.stream(leftKey.split(" "))
                        .filter(StringUtils::isNotBlank)
                        .collect(Collectors.toList());
                String descriptor = tokenList.get(0).trim();
                String type = tokenList.get(1).trim();
                String key = tokenList.get(2).trim();

                String rightKey = tokens[1];
                String[] split = rightKey.split(";");
                Integer order = Integer.parseInt(split[0].trim());
                String suffixCommentPart = ArrayUtils.get(split, 1, "");
                String suffixComment = suffixCommentPart.trim();
                if (suffixComment.startsWith("//")) {
                    List commentList = new ArrayList<>(3);
                    commentList.add(comment);
                    commentList.add(getCommentContentNotWithSeperator(suffixComment));
                    List out = commentList.stream()
                            .filter(StringUtils::isNotBlank)
                            .collect(Collectors.toList());
                    comment = String.join(" | ", out).trim();
                }

                ProtobufMessageMetadata messageDto = ProtobufMessageMetadata.builder()
                        .messageName(messageName)
                        .descriptor(descriptor)
                        .type(type)
                        .fieldName(key)
                        .order(order)
                        .comment(comment)
                        .build();

                comment = null;
                messageClasses.add(messageDto);
            }
        }
        return messageClasses;
    }

    @NotNull
    private static String getCommentContentNotWithSeperator(String line) {
        return line.substring(line.lastIndexOf("/") + 1);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy