|
| 1 | +package us.codecraft.tinyioc.xml; |
| 2 | + |
| 3 | +import org.w3c.dom.Document; |
| 4 | +import org.w3c.dom.Element; |
| 5 | +import org.w3c.dom.Node; |
| 6 | +import org.w3c.dom.NodeList; |
| 7 | +import us.codecraft.tinyioc.AbstractBeanDefinitionReader; |
| 8 | +import us.codecraft.tinyioc.BeanDefinition; |
| 9 | +import us.codecraft.tinyioc.PropertyValue; |
| 10 | +import us.codecraft.tinyioc.io.ResourceLoader; |
| 11 | + |
| 12 | +import javax.xml.parsers.DocumentBuilder; |
| 13 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 14 | +import java.io.InputStream; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author yihua.huang@dianping.com |
| 18 | + */ |
| 19 | +public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader { |
| 20 | + |
| 21 | + public XmlBeanDefinitionReader(ResourceLoader resourceLoader) { |
| 22 | + super(resourceLoader); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public void loadBeanDefinitions(String location) throws Exception { |
| 27 | + InputStream inputStream = getResourceLoader().getResource(location).getInputStream(); |
| 28 | + doLoadBeanDefinitions(inputStream); |
| 29 | + } |
| 30 | + |
| 31 | + protected void doLoadBeanDefinitions(InputStream inputStream) throws Exception { |
| 32 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 33 | + DocumentBuilder docBuilder = factory.newDocumentBuilder(); |
| 34 | + Document doc = docBuilder.parse(inputStream); |
| 35 | + // 解析bean |
| 36 | + registerBeanDefinitions(doc); |
| 37 | + inputStream.close(); |
| 38 | + } |
| 39 | + |
| 40 | + public void registerBeanDefinitions(Document doc) { |
| 41 | + Element root = doc.getDocumentElement(); |
| 42 | + |
| 43 | + parseBeanDefinitions(root); |
| 44 | + } |
| 45 | + |
| 46 | + protected void parseBeanDefinitions(Element root) { |
| 47 | + NodeList nl = root.getChildNodes(); |
| 48 | + for (int i = 0; i < nl.getLength(); i++) { |
| 49 | + Node node = nl.item(i); |
| 50 | + if (node instanceof Element) { |
| 51 | + Element ele = (Element) node; |
| 52 | + processBeanDefinition(ele); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + protected void processBeanDefinition(Element ele) { |
| 58 | + String name = ele.getAttribute("name"); |
| 59 | + String className = ele.getAttribute("class"); |
| 60 | + BeanDefinition beanDefinition = new BeanDefinition(); |
| 61 | + processProperty(ele,beanDefinition); |
| 62 | + beanDefinition.setBeanClassName(className); |
| 63 | + getRegistry().put(name, beanDefinition); |
| 64 | + } |
| 65 | + |
| 66 | + private void processProperty(Element ele,BeanDefinition beanDefinition) { |
| 67 | + NodeList propertyNode = ele.getElementsByTagName("property"); |
| 68 | + for (int i = 0; i < propertyNode.getLength(); i++) { |
| 69 | + Node node = propertyNode.item(i); |
| 70 | + if (node instanceof Element) { |
| 71 | + Element propertyEle = (Element) node; |
| 72 | + String name = propertyEle.getAttribute("name"); |
| 73 | + String value = propertyEle.getAttribute("value"); |
| 74 | + beanDefinition.getPropertyValues().addPropertyValue(new PropertyValue(name,value)); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments