KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > xml > BeanComponentInstanceFactory


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  *****************************************************************************/

9 package org.nanocontainer.script.xml;
10
11 import org.picocontainer.ComponentAdapter;
12 import org.picocontainer.Parameter;
13 import org.picocontainer.PicoContainer;
14 import org.picocontainer.defaults.BeanPropertyComponentAdapter;
15 import org.picocontainer.defaults.ComponentAdapterFactory;
16 import org.picocontainer.defaults.DefaultComponentAdapterFactory;
17 import org.w3c.dom.Element JavaDoc;
18 import org.w3c.dom.Node JavaDoc;
19 import org.w3c.dom.NodeList JavaDoc;
20
21 import java.net.MalformedURLException JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 /**
25  * Implementation of XMLComponentInstanceFactory that uses BeanPropertyComponentAdapter
26  * to create instances from DOM elements.
27  *
28  * @author Paul Hammant
29  * @author Marcos Tarruella
30  * @author Mauro Talevi
31  */

32 public class BeanComponentInstanceFactory implements XMLComponentInstanceFactory {
33     
34     private static final String JavaDoc NAME_ATTRIBUTE = "name";
35     
36     public Object JavaDoc makeInstance(PicoContainer pico, Element JavaDoc element, ClassLoader JavaDoc classLoader) throws ClassNotFoundException JavaDoc, MalformedURLException JavaDoc {
37         String JavaDoc className = element.getNodeName();
38         Object JavaDoc instance = null;
39
40         if (element.getChildNodes().getLength() == 1) {
41             instance = BeanPropertyComponentAdapter.convert(className, element.getFirstChild().getNodeValue(), classLoader);
42         } else {
43             BeanPropertyComponentAdapter propertyComponentAdapter =
44                     new BeanPropertyComponentAdapter(createComponentAdapter(className, classLoader));
45             Properties JavaDoc properties = createProperties(element.getChildNodes());
46             propertyComponentAdapter.setProperties(properties);
47             instance = propertyComponentAdapter.getComponentInstance(pico);
48         }
49         return instance;
50     }
51
52     private ComponentAdapter createComponentAdapter(String JavaDoc className, ClassLoader JavaDoc classLoader) throws ClassNotFoundException JavaDoc {
53         Class JavaDoc implementation = classLoader.loadClass(className);
54         ComponentAdapterFactory factory = new DefaultComponentAdapterFactory();
55         return factory.createComponentAdapter(className, implementation, new Parameter[]{});
56     }
57
58     private Properties JavaDoc createProperties(NodeList JavaDoc nodes) {
59         Properties JavaDoc properties = new Properties JavaDoc();
60         for (int i = 0; i < nodes.getLength(); i++) {
61             Node JavaDoc n = nodes.item(i);
62             if (n.getNodeType() == Node.ELEMENT_NODE) {
63                 String JavaDoc name = n.getNodeName();
64                 
65                 //Provide for a new 'name' attribute in properties.
66
if (n.hasAttributes()) {
67                     String JavaDoc mappedName = n.getAttributes().getNamedItem(NAME_ATTRIBUTE).getNodeValue();
68                     if (mappedName != null) {
69                         name = mappedName;
70                     }
71                 }
72
73                 String JavaDoc value = n.getFirstChild().getNodeValue();
74                 properties.setProperty(name, value);
75             }
76         }
77         return properties;
78     }
79 }
80
Popular Tags