* @author Tom Baeyens
*/
public class PropertiesBinding implements Binding {
public Object parse(Element element, Parse parse, Parser parser) {
PropertiesDescriptor descriptor = new PropertiesDescriptor();
if (element.hasAttribute("file")) {
descriptor.setFile(element.getAttribute("file"));
}
if (element.hasAttribute("resource")) {
descriptor.setResource(element.getAttribute("resource"));
}
if (element.hasAttribute("url")) {
descriptor.setUrl(element.getAttribute("url"));
}
if (element.hasAttribute("is-xml")) {
String isXmlText = element.getAttribute("is-xml");
Boolean isXml = XmlUtil.booleanEquals(isXmlText, null);
if (isXml!=null) {
descriptor.setXml(isXml);
}
}
List<Descriptor> keyDescriptors = new ArrayList<Descriptor>();
List<Descriptor> valueDescriptors = new ArrayList<Descriptor>();
List<Element> elements = XmlUtil.elements(element);
if (elements!=null) {
for (Element propertyElement: elements) {
if ("property".equals(XmlUtil.getTagName(propertyElement))) {
// key
String name = XmlUtil.attribute(propertyElement, "name");
// value
String value = XmlUtil.attribute(propertyElement, "value");
if ( (name!=null)
&& (value!=null)
) {
StringDescriptor nameDescriptor = new StringDescriptor();
nameDescriptor.setValue(name);
keyDescriptors.add(nameDescriptor);
StringDescriptor valueDescriptor = new StringDescriptor();
valueDescriptor.setValue(value);
valueDescriptors.add(valueDescriptor);
} else {
parse.addProblem("property must have name and value attributes: "+XmlUtil.toString(propertyElement));
}
} else {
parse.addProblem("properties can only contain property elements: "+XmlUtil.toString(propertyElement));
}
}
}
descriptor.setKeyDescriptors(keyDescriptors);
descriptor.setValueDescriptors(valueDescriptors);
return descriptor;
}