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

com.rt.logic.util.LogicUtil Maven / Gradle / Ivy

The newest version!
package com.rt.logic.util;

import com.json.JSONObject;
import com.rt.core.constant.RTConst;
import com.rt.core.util.RTUtil;
import com.rt.core.util.XMLUtil;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 规则助手
 */
public class LogicUtil {

    /**
     * 获取配置文件节点
     *
     * @param config config
     * @param id id
     * @return Element ProductElement
     */
    public static Element findElementById(Document config, String id) {
        // 取得节点
        return XMLUtil.selectSingleElement(config, "/beans/bean[@id='" + id
                + "']");
    }

    /**
     * 配置文件element to Property
     *
     * @param element element
     * @param property property
     * @return Property
     */
    public static JSONObject elementToProperty(Element element, JSONObject property) {
        if (element == null) {
            return property;
        }
        if (property == null) {
            property = new JSONObject();
        }
        // 设置节点名
        property.put("qname", element.getName());
        // 复制所有属性
        for (Iterator attributeIterator = element.attributeIterator(); attributeIterator
                .hasNext(); ) {
            Attribute elementAttribute = (Attribute) attributeIterator.next();
            property.put(elementAttribute.getName(),
                    elementAttribute.getValue());
        }
        // 检查是否有id属性,如果没有,随机生成一个放进去,确保唯一性.
        if (RTUtil.isEmpty(property.getId())) {
            property.setId(RTUtil.getUUID());
        }
        // 设置值
        if (RTUtil.isNotEmpty(element.getTextTrim())) {
            property.setValue(element.getText());
        }
        // 如果有子节点 递归赋值
        Iterator elementIterator = element.elementIterator();
        if (elementIterator != null && elementIterator.hasNext()) {
            for (Iterator iterator = elementIterator; iterator.hasNext(); ) {
                Element subElement = (Element) iterator.next();
                // 先尝试获取id
                String name = subElement.getName();
                Map subPropertyMap;
                // 检查属性是否已经存在.
                if (property.containsKey(name)) {
                    subPropertyMap = (Map) property.get(name);
                } else {
                    // 第一次增加属性.
                    subPropertyMap = new JSONObject();
                    property.put(name, subPropertyMap);
                }
                JSONObject subProperty = elementToProperty(subElement,
                        new JSONObject());
                subPropertyMap.put(subProperty.getId(), subProperty);
            } // end subElement for
        } // end has subElement if
        return property;
    }

    /**
     * 配置文件element to Property
     *
     * @param property property
     * @param element element
     * @return Property
     */
    public static Element propertyToElement(JSONObject property, Element element) {
        if (property == null || element == null) {
            return null;
        }
        RTConst.clearObject(property);
        // 复制所有属性
        for (Iterator iterator = property.keys(); iterator.hasNext(); ) {
            String key = (String) iterator.next();
            Object value = property.get(key);
            // 列表属性
            if (value instanceof List) {
                List list = (List) value;
                for (Iterator listIterator = list.iterator(); listIterator
                        .hasNext(); ) {
                    Object o = listIterator.next();
                    if (o instanceof JSONObject) {
                        JSONObject prop = new JSONObject((JSONObject) o);
                        propertyToElement(prop, element.addElement(key));
                    }
                }
            } else if (value instanceof Map) {
                // 对象属性
            } else {
                if (RTConst.VALUE.equals(key) && RTUtil.isNotEmpty(value)) {
                    element.addCDATA(RTUtil.val(value));
                } else {
                    // 普通属性
                    Attribute attribute = element.attribute(key);
                    if (attribute != null) {
                        attribute.setValue(RTUtil.val(value));
                    } else {
                        element.addAttribute(key, RTUtil.val(value));
                    }
                }
            }
        }
        return element;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy