`
lijunaccp
  • 浏览: 153677 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

综合编程

 
阅读更多
题:

三.plugin.xml
<?xml version="1.0" encoding="UTF-8" ?>
  <plugin name="HelloPlugin">
        <author>
                <person>LiTian</person>
                <email>litian@huawei.com</email>
                <site>http://www.huawei.com</site>
        </author>
        <item>
                <key>plugin</key>
                <value>com.huawei.HelloPlugin</value>
        </item>
        <item>
                <key>version</key>
                <value>1.0</value>
        </item>
        <item>
                <key>date</key>
                <value>2009-04-25</value>
        </item>
</plugin>

四.Author.java
package com.chinasoft.exam.demo;

public class Author {

                private String person;
                
                private String email;
                
                private String site;

                public String getPerson() {
                        return person;
                }

                public void setPerson(String person) {
                        this.person = person;
                }

                public String getEmail() {
                        return email;
                }

                public void setEmail(String email) {
                        this.email = email;
                }

                public String getSite() {
                        return site;
                }

                public void setSite(String site) {
                        this.site = site;
                }
                
}

五.Plugin.java
package com.chinasoft.exam.demo;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Plugin {

        private Author author = new Author();

        private ArrayList items = new ArrayList();

        private Map<String, String> item;

        public Author getAuthor() {
                return author;
        }

        public void setAuthor(Author author) {
                this.author = author;
        }

        public ArrayList getItems() {
                return items;
        }

        public void setItems(ArrayList items) {
                this.items = items;
        }

        public Map<String, String> getItem() {
                return item;
        }

        public void setItem(Map<String, String> item) {
                this.item = item;
        }

        public Plugin call(String name, int age) {
                // 测试参数
                System.out.println("name:" + name + "\t age:" + age);
                File file = new File("src/com/chiansoft/exam/properties/plugin.xml");
                try {
                        DocumentBuilderFactory domfac = DocumentBuilderFactory
                                        .newInstance();
                        DocumentBuilder dombuilder = domfac.newDocumentBuilder();
                        Document document = dombuilder.parse(file);
                        Element root = document.getDocumentElement();
                        NodeList nodeList = root.getChildNodes();
                        if (nodeList != null) {
                                for (int i = 0; i < nodeList.getLength(); i++) {
                                        if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE
                                                        && nodeList.item(i).getNodeName().equals("item")) {
                                                item = new HashMap<String, String>();
                                                String key = null;
                                                String value = null;
                                                for (Node node = nodeList.item(i).getFirstChild(); node != null; node = node
                                                                .getNextSibling()) {
                                                        if (node.getNodeType() == Node.ELEMENT_NODE
                                                                        && node.getNodeName().equals("key")) {
                                                                key = node.getFirstChild().getNodeValue();
                                                        }
                                                        if (node.getNodeType() == Node.ELEMENT_NODE
                                                                        && node.getNodeName().equals("value")) {
                                                                value = node.getFirstChild().getNodeValue();
                                                        }
                                                }
                                                item.put(key, value);
                                                items.add(item);
                                        } else if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE
                                                        && nodeList.item(i).getNodeName().equals("author")) {
                                                for (Node node = nodeList.item(i).getFirstChild(); node != null; node = node
                                                                .getNextSibling()) {
                                                        if (node.getNodeType() == Node.ELEMENT_NODE
                                                                        && node.getNodeName().equals("person")) {
                                                                String person = node.getFirstChild()
                                                                                .getNodeValue();
                                                                author.setPerson(person);
                                                        }
                                                        if (node.getNodeType() == Node.ELEMENT_NODE
                                                                        && node.getNodeName().equals("email")) {
                                                                String email = node.getFirstChild()
                                                                                .getNodeValue();
                                                                author.setEmail(email);
                                                        }
                                                        if (node.getNodeType() == Node.ELEMENT_NODE
                                                                        && node.getNodeName().equals("site")) {
                                                                String site = node.getFirstChild()
                                                                                .getNodeValue();
                                                                author.setSite(site);
                                                        }
                                                }
                                        }
                                }
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return this;
        }
}

六.Operate.java
package com.chinasoft.exam.demo;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Operate 
{
        
        private Plugin plugin = null;
        
        //反射知识
        public void setPlugin()
        {
                Object pluginObject = null;
                
                try 
                {
                        
                        Class<?> classDefine = Class.forName("com.chinasoft.exam.demo.Plugin");
                        pluginObject = classDefine.newInstance();
                        
                        //方式一:适用于一个参数
                        /*Method[] methods = classDefine.getDeclaredMethods();
                        for(int i=0;i<methods.length;i++)
                        {
                                Method method = methods;
                                String methodName = method.getName();
                                if(methodName.equals("call"))
                                {
                                        plugin = (Plugin)method.invoke(pluginObject, "zhangbai");
                                }
                        }*/
                        
                        //方式二:适用于多个参数
                        Class[] paramTypes = new Class[]{String.class,int.class};//参数类型数组
                        Object[] params = new Object[]{"zhangbai",23};//参数值数组
                        Method method = classDefine.getDeclaredMethod("call",paramTypes);//获取方法实例
                        plugin = (Plugin)method.invoke(pluginObject, params);//调用call方法并返回Plugin实例
                        
                } 
                catch (Exception e) 
                {
                        e.printStackTrace();
                }
        }
        
        
        public void doTest() 
        {
                
                ArrayList<Object> items = plugin.getItems();
                Iterator<Object> iterator = items.iterator();
                
                for (int i = 0; i < items.size(); i++) 
                {
                        if (iterator.hasNext()) 
                        {
                                
                                Map<String,String> item = (Map<String,String>) iterator.next();
                                Set set = item.entrySet();
                                Iterator keys = set.iterator();
                                
                                for (int j = 0; j < set.size(); j++) 
                                {
                                        if (keys.hasNext()) 
                                        {
                                                Object key = keys.next();
                                                if (key != null) 
                                                {
                                                        System.out.println(key);
                                                }
                                        }
                                }
                        }
                }
                
                Author author = plugin.getAuthor();
                String person = author.getPerson();
                String email = author.getEmail();
                String site = author.getSite();
                
                System.out.println("person:"+person+" \t email:"+email+" \t site:"+site);
        }

        public static void main(String[] args) {
                Operate operate = new Operate();
                operate.setPlugin();
                operate.doTest();
        }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics