1 29 30 package nextapp.echo2.app.componentxml; 31 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import nextapp.echo2.app.MutableStyle; 36 import nextapp.echo2.app.Style; 37 import nextapp.echo2.app.util.DomUtil; 38 import nextapp.echo2.app.util.PeerFactory; 39 40 import org.w3c.dom.Element ; 41 42 46 public class PropertyLoader { 47 48 private static final String PROPERTY_XML_PEERS_PATH = "META-INF/nextapp/echo2/PropertyXmlPeers.properties"; 49 50 53 private static final Map classLoaderToPropertyLoaderMap = new HashMap (); 54 55 62 public static PropertyLoader forClassLoader(ClassLoader classLoader) { 63 synchronized(classLoaderToPropertyLoaderMap) { 64 PropertyLoader propertyLoader = (PropertyLoader) classLoaderToPropertyLoaderMap.get(classLoader); 65 if (propertyLoader == null) { 66 propertyLoader = new PropertyLoader(classLoader); 67 classLoaderToPropertyLoaderMap.put(classLoader, propertyLoader); 68 } 69 return propertyLoader; 70 } 71 } 72 73 private ClassLoader classLoader; 74 private PeerFactory propertyXmlPeerFactory; 75 76 82 private PropertyLoader(ClassLoader classLoader) { 83 super(); 84 this.classLoader = classLoader; 85 propertyXmlPeerFactory = new PeerFactory(PROPERTY_XML_PEERS_PATH, classLoader); 86 } 87 88 98 public Style createStyle(Element propertiesElement, String type) 99 throws ComponentXmlException { 100 MutableStyle propertyStyle = new MutableStyle(); 101 102 if (propertiesElement == null) { 103 return new MutableStyle(); 105 } 106 107 ComponentIntrospector ci; 108 try { 109 ci = ComponentIntrospector.forName(type, classLoader); 110 } catch (ClassNotFoundException ex) { 111 throw new ComponentXmlException("Unable to introspect component: " + type, ex); 112 } 113 114 Element [] propertyElements = DomUtil.getChildElementsByTagName(propertiesElement, "property"); 115 for (int i = 0; i < propertyElements.length; ++i) { 116 String propertyName = propertyElements[i].getAttribute("name"); 117 Class propertyClass; 118 if (propertyElements[i].hasAttribute("type")) { 119 try { 120 propertyClass = Class.forName(propertyElements[i].getAttribute("type")); 121 } catch (ClassNotFoundException ex) { 122 throw new ComponentXmlException("Custom property class not found: " 123 + propertyElements[i].getAttribute("type"), ex); 124 } 125 } else { 126 propertyClass = ci.getPropertyClass(propertyName); 127 } 128 129 if (propertyClass == null) { 130 throw new ComponentXmlException("Property does not exist: " + propertyName, null); 131 } 132 133 Object propertyValue = getPropertyValue(ci.getObjectClass(), propertyClass, propertyElements[i]); 134 135 if (ci.isIndexedProperty(propertyName)) { 136 try { 137 int index = Integer.parseInt(propertyElements[i].getAttribute("index")); 138 propertyStyle.setIndexedProperty(propertyName, index, propertyValue); 139 } catch (NumberFormatException ex) { 140 throw new ComponentXmlException("Index not set.", ex); 141 } 142 } else { 143 propertyStyle.setProperty(propertyName, propertyValue); 144 } 145 } 146 147 return propertyStyle; 148 } 149 150 159 public Object getPropertyValue(Class objectClass, Class propertyClass, Element propertyElement) 160 throws InvalidPropertyException { 161 PropertyXmlPeer propertyXmlPeer 162 = (PropertyXmlPeer) propertyXmlPeerFactory.getPeerForObject(propertyClass, false); 163 if (propertyXmlPeer == null) { 164 throw new InvalidPropertyException("Peer not found for property class: " + propertyClass, null); 165 } 166 Object propertyValue = propertyXmlPeer.getValue(classLoader, objectClass, propertyElement); 167 return propertyValue; 168 } 169 170 176 public PropertyXmlPeer getPropertyXmlPeer(Class propertyClass) { 177 return (PropertyXmlPeer) propertyXmlPeerFactory.getPeerForObject(propertyClass, false); 178 } 179 } 180 | Popular Tags |