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

com.feingto.cloud.data.jdbc.DataTransform Maven / Gradle / Ivy

package com.feingto.cloud.data.jdbc;

import com.feingto.cloud.kit.json.JSON;
import com.google.common.collect.Maps;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 数据转换
 *
 * @author longfei
 */
public class DataTransform {
    public static String mergeSql(String sql, String[] field, String[] value) {
        Map paramsMap = Maps.newHashMap();
        if (field != null && field.length > 0 && value != null && value.length > 0) {
            for (int i = 0; i < field.length; i++) {
                paramsMap.put(field[i], value[i]);
            }
        }
        return mergeSql(sql, paramsMap);
    }

    public static String mergeSql(String sql, Map paramsMap) {
        String regex = "\\{#(\\S+)#\\}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(sql);
        while (matcher.find()) {
            String name = matcher.group(1);
            if (StringUtils.hasText(paramsMap.get(name))) {
                sql = sql.replace("{#" + name + "#}", paramsMap.get(name));
            }
        }
        return sql;
    }

    public static String toJSON(List> data) {
        return JSON.obj2json(data);
    }

    public static String toXML(List> data) {
        if (data.size() == 0) return "";
        StringBuilder xml = new StringBuilder();
        xml.append("");
        for (Map map : data) {
            xml.append("");
            for (String key : map.keySet()) {
                xml.append("");
            }
            xml.append("");
        }
        xml.append("");
        return xml.toString();
    }

    public static String toHTML(List> data) {
        if (data.size() == 0) return "";
        StringBuilder html = new StringBuilder("");
        StringBuilder head = new StringBuilder("");
        StringBuilder body = new StringBuilder("");
        for (int i = 0; i < data.size(); i++) {
            Map map = data.get(i);
            for (String key : map.keySet()) {
                if (i == 0) {
                    head.append("");
                }
                body.append("");
            }
            body.append("");
        }
        head.append("");
        body.append("");
        html.append(head).append(body).append("
").append(key).append("").append(map.get(key)).append("
"); return html.toString(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy