* @return a style representing the retrieved property names and values
* @throws ComponentXmlException
*/
public Style createStyle(Element propertiesElement, String type)
throws ComponentXmlException {
MutableStyle propertyStyle = new MutableStyle();
if (propertiesElement == null) {
// No properties.
return new MutableStyle();
}
ComponentIntrospector ci;
try {
ci = ComponentIntrospector.forName(type, classLoader);
} catch (ClassNotFoundException ex) {
throw new ComponentXmlException("Unable to introspect component: " + type, ex);
}
Element[] propertyElements = DomUtil.getChildElementsByTagName(propertiesElement, "property");
for (int i = 0; i < propertyElements.length; ++i) {
String propertyName = propertyElements[i].getAttribute("name");
Class propertyClass;
if (propertyElements[i].hasAttribute("type")) {
try {
propertyClass = Class.forName(propertyElements[i].getAttribute("type"));
} catch (ClassNotFoundException ex) {
throw new ComponentXmlException("Custom property class not found: "
+ propertyElements[i].getAttribute("type"), ex);
}
} else {
propertyClass = ci.getPropertyClass(propertyName);
}
if (propertyClass == null) {
throw new ComponentXmlException("Property does not exist: " + propertyName, null);
}
Object propertyValue = getPropertyValue(ci.getObjectClass(), propertyClass, propertyElements[i]);
if (ci.isIndexedProperty(propertyName)) {
try {
int index = Integer.parseInt(propertyElements[i].getAttribute("index"));
propertyStyle.setIndexedProperty(propertyName, index, propertyValue);
} catch (NumberFormatException ex) {
throw new ComponentXmlException("Index not set.", ex);
}
} else {
propertyStyle.setProperty(propertyName, propertyValue);
}
}
return propertyStyle;
}