# HG changeset patch # User Raimund Renkert # Date 1395318945 -3600 # Node ID 2c221acebb766141670aca1e368d219258bf6489 # Parent c053b2d813f99e2c15fa778ab2ba6434e56a2996 New method to extract multi attribute data. diff -r c053b2d813f9 -r 2c221acebb76 gwt-client/src/main/java/org/dive4elements/river/client/server/DataFactory.java --- a/gwt-client/src/main/java/org/dive4elements/river/client/server/DataFactory.java Thu Mar 20 13:35:13 2014 +0100 +++ b/gwt-client/src/main/java/org/dive4elements/river/client/server/DataFactory.java Thu Mar 20 13:35:45 2014 +0100 @@ -8,9 +8,13 @@ package org.dive4elements.river.client.server; +import java.util.HashMap; + import javax.xml.xpath.XPathConstants; import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.apache.log4j.Logger; @@ -27,6 +31,8 @@ import org.dive4elements.river.client.shared.model.IntegerData; import org.dive4elements.river.client.shared.model.IntegerOptionsData; import org.dive4elements.river.client.shared.model.IntegerRangeData; +import org.dive4elements.river.client.shared.model.MultiAttributeData; +import org.dive4elements.river.client.shared.model.MultiDataItem; import org.dive4elements.river.client.shared.model.StringData; import org.dive4elements.river.client.shared.model.StringOptionsData; import org.dive4elements.river.client.shared.model.LongRangeData; @@ -91,6 +97,9 @@ else if (type.equals(LongRangeData.TYPE)) { return createLongRangeData(element, name, label); } + else if (type.equals(MultiAttributeData.TYPE)) { + return createMultiAttributeData(element, name, label); + } else { return createDefaultData(element, name, label); } @@ -103,6 +112,52 @@ } + public static Data createMultiAttributeData( + Element element, + String name, + String label) { + return new MultiAttributeData( + name, + label, + extractMultiDataItems(element)); + } + + + protected static DataItem[] extractMultiDataItems(Element element) { + NodeList itemList = (NodeList) XMLUtils.xpath( + element, + "art:item", + XPathConstants.NODESET, + ArtifactNamespaceContext.INSTANCE); + + if (itemList == null || itemList.getLength() == 0) { + logger.debug("No old data items found."); + return null; + } + + int count = itemList.getLength(); + + MultiDataItem[] items = new MultiDataItem[count]; + + for (int i = 0; i < count; i++) { + Element tmp = (Element) itemList.item(i); + + HashMap data = new HashMap(); + String label = tmp.getAttributeNS(NS_URI, "label"); + NamedNodeMap attributes = tmp.getAttributes(); + for (int j = 0; j < attributes.getLength(); j++) { + Node n = attributes.item(j); + if (n.getNodeName().equals("label")) { + continue; + } + data.put(n.getNodeName(), n.getNodeValue()); + } + items[i] = new MultiDataItem(label, label, data); + } + return items; + } + + /** * This method creates a new instance of DefaultData which has no real type * set.