题:
三.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();
}
}
分享到:
相关推荐
C语言综合编程训练,新手可以试试~~~~ 都是打基础的,不是很难~~~
包含: MATLAB 5.X程序设计语言.pdf、 MATLAB 5.X入门与应用.pdf、 MATLAB 5.X应用与技巧.pdf、 MATLAB 编程风格指南.pdf、 MATLAB编程.pdf、 MatLab工程数学应用.pdf、
ASIC芯片设计静态时序分析(STA)验证综合编程规范IC设计与方法等文档资料: ASIC Guide from Atmel.pdf asic_design_guide.pdf ASIC中的异步时序设计.doc ASIC设计教程.pdf asynchronous signals in a synchronous ...
一个c语言综合编程训练文档,综合训练的PPT
综合编程实验项目.doc
三菱GX Works2 PLC综合编程软件(中文)适于在win10下使用
数控车床综合编程实例解析(ppt-15页).ppt
数控车床综合编程实例.pptx
综合编程题及解答.pdf
浙大中控DCS系统AdvanTrol Pro软件培训-编程综合编程案例.ppt
加工中心综合编程实例.ppt
2022年数控车床综合编程实例解析(共14张PPT).pptx
第13讲 jsp综合编程实例.ppt(I) 成绩管理系统
综合编程训练PPT学习教案.pptx
数控车床综合编程实例解析.pptx
数控铣床综合编程实训.pptx
第七章-复杂零件综合编程实例(共90张PPT).pptx
浙大中控DCS系统AdvanTrol Pro软件培训编程综合编程案例PPT课件.pptx