自己实现的一个XML对象容器

IApplicationContent为容器,通过其中的方法可以添加配置文件,根据ID获得注入对象。注入对象xml规范见template.dtd。添加的配置文件交由IXMLParse负责解析。IXMLParse将文件解析的信息存放入配件IBean中,IBean中存放着关于对象类型,生产函数及其参数(不设定name属性或属性值为空时为构造函数),还有一些预定义的属性及值,参数和属性均支持引用注入的其他对象。属性及参数对应IProperty。
template.dtd

<?xml version="1.0" encoding="UTF-8"?>

<!ELEMENT ApplicationContent (bean*)>
<!ELEMENT bean (property*,construct?)>
<!ELEMENT property (#PCDATA)>
<!ELEMENT construct (param*)>
<!ELEMENT param (#PCDATA)>

<!ATTLIST bean id ID #REQUIRED>
<!ATTLIST bean class CDATA #REQUIRED>

<!ATTLIST property name CDATA #REQUIRED>
<!ATTLIST property type CDATA #REQUIRED>
<!ATTLIST property ref CDATA #IMPLIED>

<!ATTLIST construct name CDATA #IMPLIED>

<!ATTLIST param name CDATA #REQUIRED>
<!ATTLIST param type CDATA #REQUIRED>
<!ATTLIST param ref CDATA #IMPLIED>


IApplicationContent

/**
* 配置文件名,可以连续添加多个配置文件并解析为基本配件IBean的列表
* @return
*/
public IApplicationContent AddConfigFile(String fileName);
/**
* 根据ID获得实例的引用,如果实例已生成过则直接返回,否则用反射生产
* @param id
* @return
*/
public Object getObjectByID(String id);


实现ApplicationContent

public IApplicationContent AddConfigFile(String fileName) {
if (!fileList.contains(fileName)) {
fileList.add(fileName);
parse.setFileName(fileName);
List list = parse.parse();
if (list != null) {
for (Object obj : list) {
try {
if (objs.containsKey(((IBean) obj).getID())) {
throw (new BeanException())
.setMessage("配置文件中的Bean的ID不能重复");
}
} catch (BeanException e) {
e.printStackTrace();
}
objs.put(((IBean) obj).getID(), obj);
}
}
}
return this;
}

public Object getObjectByID(String id) {
IBean bean = (IBean) objs.get(id);
Object obj = bean.getInstance();
if (obj != null) {
return obj;
}
try {
Class cls = Class.forName(bean.getClassName());
Class[] ps = new Class[bean.getParams().size()];
Object[] ops = new Object[bean.getParams().size()];
Object pobj = null;
Object method = null;
IProperty prop = null;
String mstr = bean.getConstruct();
int i = 0;
int count = bean.getParams().size();
for (; i < count; i++) {
ps[i] = Class.forName(((IProperty) bean.getParams().get(i))
.getType());
}
for (i = 0; i < count; i++) {
prop = (IProperty) bean.getParams().get(i);
if (prop.isRef()) {
ops[i] = this.getObjectByID((String)prop.getValue());
} else {
ops[i] = prop.getValue();
}
}

try {
if (mstr != null
&& mstr != ""
&& !mstr.equals(bean.getClassName().substring(
bean.getClassName().lastIndexOf(".")))) {
method = cls.getDeclaredMethod(mstr, ps);
obj = cls.newInstance();
((Method) method).invoke(obj, ops);
} else {
method = cls.getDeclaredConstructor(ps);
obj = ((Constructor) method).newInstance(ops);
}
if (obj != null) {
count = bean.getProperties().size();
for (i = 0; i < count; i++) {
prop = ((IProperty) bean.getProperties().get(i));
mstr = (String) prop.getName();
mstr = "set" + mstr.substring(0, 1).toUpperCase()
+ mstr.substring(1);
method = cls.getDeclaredMethod(mstr,
new Class[] { Class.forName(prop.getType()) });
pobj = prop.getValue();
if (prop.isRef()) {
pobj = this.getObjectByID((String) pobj);
}
((Method) method).invoke(obj, new Object[] { pobj });
}
}

bean.setObj(obj);
return bean.getInstance();

} catch (SecurityException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (NoSuchMethodException e) {
System.out.println("类 " + bean.getClassName() + " 不存在方法 "
+ mstr + ":" + e.getMessage());
e.printStackTrace();
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (InstantiationException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (IllegalAccessException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (InvocationTargetException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}

bean.setObj(null);
return null;
}


IXMLParse

public String getFileName();
public IXMLParse setFileName(String fileName);
/**
* 从配置文件解析实例信息并返回
* @param fileName
* @return
*/
public List parse();

实现BeanParse
[code="java"]
public List parse() {
if(fileName == null || "".equals(fileName)){
System.out.println("Please set the config file");
return null;
}
if(list == null){
list = new ArrayList();
}
DocumentBuilder builder;
IContentFactory factory = ContentFactory.instance();
Document doc = null;
Element eleb = null,elet = null;
IBean bean = null;
IProperty prop;
NodeList beans;
NodeList props;
NodeList cons;
int i = 0,j = 0,countb = 0, countp = 0;
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
doc = builder.parse(new BufferedInputStream(new FileInputStream(fileName)));
eleb = doc.getDocumentElement();
beans = eleb.getElementsByTagName("bean");
countb = beans.getLength();
for(;i < countb; i++){
bean = factory.getBean();
eleb = ((Element)beans.item(i));

bean.setID(eleb.getAttribute("id"));
bean.setClassName(eleb.getAttribute("class"));


props = eleb.getElementsByTagName("property");
countp = props.getLength();
for(j = 0;j < countp; j++){
prop = factory.getProperty();
elet = (Element)props.item(j);

prop.setName(elet.getAttribute("name"));
prop.setType(elet.getAttribute("type"));
prop.setRef(elet.hasAttribute("ref"));
if(prop.isRef()){
prop.setValue(elet.getAttribute("ref"));
}else{
prop.setValue(elet.getTextContent());
}
bean.addProperty(prop);
}

cons = eleb.getElementsByTagName("construct");
if(cons.getLength() > 0){
elet = (Element)cons.item(0);

bean.setConstruct(elet.getAttribute("name"));

props = elet.getElementsByTagName("param");
countp = props.getLength();
for(j = 0;j < countp; j++){
prop = factory.getProperty();
elet = (Element)props.item(j);

prop.setName(elet.getAttribute("name"));
prop.setType(elet.getAttribute("type"));
prop.setRef(elet.hasAttribute("ref"));
if(prop.isRef()){
prop.setValue(elet.getAttribute("ref"));
}else{
prop.setValue(elet.getTextContent());
}
bean.addParam(prop);
}
}

list.add(bean);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
{/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值