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

xxl.mathematica.io.ImportString Maven / Gradle / Ivy

package xxl.mathematica.io;

import com.google.gson.reflect.TypeToken;
import io.vavr.control.Try;
import org.apache.commons.lang3.ClassUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import xxl.mathematica.map.AssociationMap;
import xxl.mathematica.map.KeySelect;
import xxl.mathematica.map.Keys;
import xxl.mathematica.single.GsonSingle;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.util.*;

/**
 * 导入字符串
 */
public class ImportString {
    /**
     * 导入字符串
     *
     * @param json
     * @param cls
     * @param 
     * @return
     */
    public static  T importJsonObject(String json, Class cls) {
        return GsonSingle.instance().fromJson(json, cls);
    }

    /**
     * 导入json字符串为Map对象
     *
     * @param json
     * @return
     */
    public static Map importJsonMapObject(String json) {
        return GsonSingle.instance().fromJson(json, new TypeToken>() {
        }.getType());
    }

    /**
     * 导入json字符串为Map字符串
     *
     * @param json
     * @return
     */
    public static Map importJsonMapString(String json) {
        Map res = importJsonMapObject(json);
        Map select = KeySelect.keySelect(res, s -> {
            if (res.get(s) != null) {
                Class cls = res.get(s).getClass();
                return cls.isPrimitive() || ClassUtils.isPrimitiveWrapper(cls) || cls.equals(String.class);
            } else {
                return false;
            }
        });
        return AssociationMap.associationMap(t -> String.valueOf(select.get(t)), Keys.keys(select));
    }

    /**
     * 导入xml
     *
     * @param xml
     * @param 
     * @return
     */
    public static  T importXml(String xml, Class cls) {
        try {
            JAXBContext context = JAXBContext.newInstance(cls);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            return (T) unmarshaller.unmarshal(new StringReader(xml));
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 检索数据
     *
     * @param element
     * @param parent
     */
    private static void retrieveElement(Element element, Map parent) {
        if (element.isTextOnly()) {
            parent.put(element.getName(), element.getData());
        } else {
            Iterator iterator = element.elementIterator();
            while (iterator.hasNext()) {
                Element e = iterator.next();
                if (e.isTextOnly()) {
                    parent.put(e.getName(), e.getData());
                } else {
                    if (parent.containsKey(e.getName())) {
                        Object object = parent.get(e.getName());
                        if (object instanceof Map) {
                            //取出原来的Map
                            Map one = (Map) object;
                            //Map替换为List
                            List> list = new ArrayList<>();
                            parent.put(e.getName(), list);
                            //加入原来的Map和现在的Map
                            list.add(one);
                            Map cur = new HashMap<>();
                            retrieveElement(e, cur);
                            list.add(cur);
                        } else {
                            List> list = (List>) object;
                            Map cur = new HashMap<>();
                            retrieveElement(e, cur);
                            list.add(cur);
                        }
                    } else {
                        Map child = new HashMap<>();
                        parent.put(e.getName(), child);
                        retrieveElement(e, child);
                    }
                }
            }
        }
    }

    /**
     * 导入为Map
     *
     * @param xml
     * @return
     */
    public static Map importXml(String xml) {
        return Try.ofCallable(() -> {
            Document document = DocumentHelper.parseText(xml);
            Map map = new HashMap<>();
            Element rootElement = document.getRootElement();
            retrieveElement(rootElement, map);
            return map;
        }).getOrNull();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy