package com.njcn.gather.icd.mapping.utils;

import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Component
public class XmlTemplateParser {

    /**
     * 读取XML模板为原始字符串（100%保留所有换行、空格、注释、空属性）
     */
    public ParseResult parse(InputStream inputStream) throws Exception {
        StringBuilder template = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
            String line;
            while ((line = br.readLine()) != null) {
                template.append(line).append("\n"); // 保留原始换行
            }
        }
        Map<String, String> baseParams = new HashMap<>();
        return new ParseResult(template.toString(), baseParams);
    }

    public static class ParseResult {
        private final String templateContent;
        private final Map<String, String> baseParams;

        public ParseResult(String templateContent, Map<String, String> baseParams) {
            this.templateContent = templateContent;
            this.baseParams = baseParams;
        }

        public String getTemplateContent() { return templateContent; }
        public Map<String, String> getBaseParams() { return baseParams; }
    }
}