Java tutorial
/** * Copyright (c) 2010-2011 love320.com * * Licensed under the Apache License, Version 2.0 (the "License"); * * Founder admin@love320.com */ package com.love320.templateparser.factory.impl; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; import org.dom4j.Element; import org.dom4j.Node; import com.love320.templateparser.cache.Cache; import com.love320.templateparser.factory.Factory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /* * * */ public class FactoryImpl implements Factory { private final static Logger logger = LoggerFactory.getLogger(FactoryImpl.class); private Cache cache;// private String cacheKey = "WWW.LOVE320.COMKeyksdjfksdjglksjdfkdhsgksdjfkljsdlkfjsdlkfj>";//() private Element docroot; public FactoryImpl(Element docroot) { this.docroot = docroot; } //? public FactoryImpl factoryInit() { // cache = (Cache) procreationXML("cache"); return this; } @Override public Object getbean(String beanName) { Object object = cache.getObject(cacheKey + beanName);//?? if (object != null) { return object; } else { object = procreationXML(beanName);//ID?? cache.putObject(cacheKey + beanName, object);// return object; } } //? private Object procreationXML(String beanName) { Node node = docroot.selectSingleNode("/beans/bean[@id='" + beanName + "']/@class"); Object object = null; try { object = Class.forName(node.getText()).newInstance();//? List ls = docroot.selectNodes("/beans/bean[@id='" + beanName + "']/property");//??? for (int i = 0; i < ls.size(); i++) { Element element = (Element) ls.get(i); String propertyName = element.attributeValue("name");//beanName Node propertyBeanNode = docroot.selectSingleNode( "/beans/bean[@id='" + beanName + "']/property[@name='" + propertyName + "']/ref/@bean"); if (propertyBeanNode != null) { Object refObject = null; if (cache != null) { refObject = getbean(propertyBeanNode.getText());// } else { refObject = procreationXML(propertyBeanNode.getText());// } //?? Method method = object.getClass().getMethod( "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1), refObject.getClass()); method.invoke(object, refObject); } else { Node valueNode = docroot.selectSingleNode( "/beans/bean[@id='" + beanName + "']/property[@name='" + propertyName + "']/value"); if (valueNode != null) { //?? try { Double.parseDouble(valueNode.getText().trim()); Method method = object.getClass().getMethod( "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1), int.class); method.invoke(object, Integer.parseInt(valueNode.getText().trim())); // } catch (NumberFormatException e) { // Method method = object.getClass().getMethod( "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1), valueNode.getText().getClass()); method.invoke(object, valueNode.getText()); } } else { logger.debug("<valueNode>" + valueNode); } } } } catch (InstantiationException e) { logger.error("InstantiationException", e); } catch (IllegalAccessException e) { logger.error("IllegalAccessException", e); } catch (ClassNotFoundException e) { logger.error("ClassNotFoundException", e); } catch (SecurityException e) { logger.error("SecurityException", e); } catch (NoSuchMethodException e) { logger.error("NoSuchMethodException", e); } catch (IllegalArgumentException e) { logger.error("IllegalArgumentException", e); } catch (InvocationTargetException e) { logger.error("InvocationTargetException", e); } return object; } public void setCache(Cache cache) { this.cache = cache; } }