题:
[img]http://dl.iteye.com/upload/picture/pic/97398/803a35b3-e143-3f22-8045-e0b83ad74bfb.jpg[/img]
三.plugin.xml
LiTian
litian@huawei.com
http://www.huawei.com
plugin
com.huawei.HelloPlugin
version
1.0
date
2009-04-25
四.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 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 getItem() {
return item;
}
public void setItem(Map item) {
this.item = item;
}
public Plugin call(String name, int age) {
// 测试参数
System.out.println("name:" + name + " 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 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 {
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