反射 XMLUtil

package com.dys.util;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import com.dys.annotation.DysAnnotation;
import com.dys.model.BeanDefinition;
import com.dys.model.PropertyDefinition;

public class XMLUtils {
    private List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();
    private Map<String, Object> singletons = new HashMap<String, Object>();
    public XMLUtils(String fileName) {
        this.readXML(fileName);
        this.initilizeBeans();
        this.annotationInject();
        this.injectObject();
    }
    private void annotationInject() {
        try {
            for(String beanName : singletons.keySet()) {
                Object bean = singletons.get(beanName);
                if(bean != null) {
                    PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
                    for(PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                        Method setMethod = propertyDescriptor.getWriteMethod();
                        if(setMethod != null && setMethod.isAnnotationPresent(DysAnnotation.class)) {
                            DysAnnotation dysA = setMethod.getAnnotation(DysAnnotation.class);
                            Object beanValue = null;
                            if(dysA.name() != null && !"".equals(dysA.name())) {
                                String name = dysA.name();
                                beanValue = singletons.get(name);
                                
                            } else {
                                String nam = propertyDescriptor.getName();
                                beanValue = singletons.get(nam);
                                if(beanValue == null) {
                                    for(String key : singletons.keySet()) {
                                        if(propertyDescriptor.getPropertyType().isAssignableFrom(singletons.get(key).getClass())) {
                                            beanValue = singletons.get(key);
                                            break;
                                        }
                                    }
                                }
                            }
                            setMethod.setAccessible(true);
                            setMethod.invoke(bean, beanValue);
                        }
                    }
                }
                Field[] fields = bean.getClass().getDeclaredFields();
                for(Field field : fields) {
                    if(field != null && field.isAnnotationPresent(DysAnnotation.class)) {
                        DysAnnotation dysA = field.getAnnotation(DysAnnotation.class);
                        Object beanValue = null;
                        if(dysA.name() != null && !"".equals(dysA.name())) {
                            String name = dysA.name();
                            beanValue = singletons.get(name);
                            
                        } else {
                            String nam = field.getName();
                            beanValue = singletons.get(nam);
                            for(String key : singletons.keySet()) {
                                if(field.getType().isAssignableFrom(singletons.get(key).getClass())) {
                                    beanValue = singletons.get(key);
                                    break;
                                }
                            }
                        }
                        field.setAccessible(true);
                        field.set(bean, beanValue);
                    }
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    private void injectObject(){
        try {
            for(BeanDefinition beanDefinition:beanDefinitions) {
                Object object = singletons.get(beanDefinition.getId());
                if(object != null) {
                    PropertyDescriptor[] pds = Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors();
                    for(PropertyDefinition propertyDefinition : beanDefinition.getProperties()) {
                        for(PropertyDescriptor pd : pds) {
                            if(propertyDefinition.getName().equals(pd.getName())) {
                                Method setMethod = pd.getWriteMethod();
                                if(setMethod != null) {
                                    Object propertyV = singletons.get(propertyDefinition.getRef());
                                    setMethod.setAccessible(true);
                                    setMethod.invoke(object, propertyV); 
                                }
                                break;
                            }
                        }
                    }
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    private void initilizeBeans() {
        for(BeanDefinition beanDefinition : beanDefinitions) {
            try {
                if(beanDefinition.getName() != null && !"".equals(beanDefinition.getName().trim())) {
                    singletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getName()).newInstance());
                }
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    private void readXML(String filename) {
        SAXReader saxReader = new SAXReader();
        Document document = null;
        try {
            URL xmlpath = this.getClass().getClassLoader().getResource(filename);
            document = saxReader.read(xmlpath);
            Map<String, String> nsMap = new HashMap<String, String>();
            nsMap.put("ns", "http://www.springframework.org/schema/beans");
            XPath xsub = document.createXPath("//ns:beans/ns:bean");
            xsub.setNamespaceURIs(nsMap);
            List<Element> beans = xsub.selectNodes(document);
            for(Element element:beans){
                String id = element.attributeValue("id");
                String clazz = element.attributeValue("class");
                BeanDefinition beanDefinition = new BeanDefinition(id, clazz);
                XPath xsubProperty = element.createXPath("ns:property");
                xsubProperty.setNamespaceURIs(nsMap);
                List<Element> propertyElement = xsubProperty.selectNodes(element);
                for(Element ele:propertyElement) {
                    String name = ele.attributeValue("name");
                    String ref = ele.attributeValue("ref");
                    PropertyDefinition propertyDefinition = new PropertyDefinition(name, ref);
                    beanDefinition.getProperties().add(propertyDefinition);
                }
                beanDefinitions.add(beanDefinition);
            }            
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
    public Object getBean(String name) {
        Object beanClass = singletons.get(name);
        return beanClass;
    }
}

 

{"_declaration":{"attributes":{"version":"1.0","encoding":"utf-8"}},"_elements":[{"_type":"element","_name":"watchface","attributes":{"watch_category":"11"},"_elements":[{"_type":"element","_name":"element","attributes":{"label":"background","is_support_option":"false","res_preview":""},"_elements":[{"_type":"element","_name":"layer","attributes":{"index":"1","draw_type":"single_res","ambient_mode":"1","res_name":"A100_002.png","res_position":"0,0"}},{"_type":"element","_name":"layer","attributes":{"index":"2","draw_type":"single_res","ambient_mode":"0","res_name":"A100_003.png","res_position":"0,0"}}]},{"_type":"element","_name":"element","attributes":{"label":"date","is_support_option":"false","res_preview":""},"_elements":[{"_type":"element","_name":"layer","attributes":{"index":"1","draw_type":"selected_res","value_type":"51","ambient_mode":"0","res_name":"A100_004.png,A100_004.png,A100_005.png,A100_006.png,A100_007.png,A100_008.png,A100_009.png,A100_010.png,A100_011.png,A100_012.png,A100_013.png,A100_014.png,A100_015.png","res_position":"0,0"}},{"_type":"element","_name":"layer","attributes":{"index":"2","draw_type":"selected_res","value_type":"70","ambient_mode":"0","res_name":"A100_016.png,A100_017.png,A100_018.png,A100_019.png","res_position":"0,0"}},{"_type":"element","_name":"layer","attributes":{"index":"3","draw_type":"selected_res","value_type":"71","ambient_mode":"0","res_name":"A100_020.png,A100_021.png,A100_022.png,A100_023.png,A100_024.png,A100_025.png,A100_026.png,A100_027.png,A100_028.png,A100_029.png","res_position":"0,0"}},{"_type":"element","_name":"layer","attributes":{"index":"4","draw_type":"selected_res","value_type":"52","ambient_mode":"0","res_name":"A100_030.png,A100_030.png,A100_031.png,A100_032.png,A100_033.png,A100_034.png,A100_035.png,A100_036.png","res_position":"0,0"}}]},{"_type":"element","_name":"element","attributes":{"label":"time","is_support_option":"false","res_preview":""},"_elements":[{"_type":"element","_name":"layer","attributes":{"index":"1","draw_type":"hand_res","value_type":"150","ambient_mode":"0","res_left":"A100_037.png","rotate_start_angel":"0","rotate_end_angel":"360","rotate_point_hand":"25,240","rotate_rect":"7,7,466,466"}},{"_type":"element","_name":"layer","attributes":{"index":"2","draw_type":"hand_res","value_type":"153","ambient_mode":"0","res_left":"A100_038.png","rotate_start_angel":"0","rotate_end_angel":"360","rotate_point_hand":"25,240","rotate_rect":"7,7,466,466"}},{"_type":"element","_name":"layer","attributes":{"index":"3","draw_type":"hand_res","value_type":"154","ambient_mode":"0","res_left":"A100_039.png","rotate_start_angel":"0","rotate_end_angel":"360","rotate_point_hand":"25,240","rotate_rect":"7,7,466,466"}}]}]}]}这个JSON数据,帮我把他的层级梳理一下,做成一个类对象处理
最新发布
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值