diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/Element.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/Element.java index ec63d87f3df42f7c24fd89cc59e6f07af06e5281..75955a2ac0bb396ea63e33139152638ab2e874f2 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/Element.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/Element.java @@ -17,7 +17,7 @@ package ch.systemsx.cisd.openbis.generic.shared.managed_property.structured; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -36,9 +36,9 @@ public class Element implements IElement private final String name; - private Map<String, String> attributes = new HashMap<String, String>(); + private final Map<String, String> attributes = new HashMap<String, String>(); - private List<IElement> children = new ArrayList<IElement>(); + private final List<IElement> children = new ArrayList<IElement>(); private String data; @@ -64,14 +64,14 @@ public class Element implements IElement return data; } - public IElement[] getChildren() + public List<IElement> getChildren() { - return children.toArray(new IElement[children.size()]); + return Collections.unmodifiableList(children); } public Map<String, String> getAttributes() { - return attributes; + return Collections.unmodifiableMap(attributes); } @@ -82,15 +82,17 @@ public class Element implements IElement { validateAttribute(entry.getKey(), entry.getValue()); } - this.attributes = newAttributes; + this.attributes.clear(); + this.attributes.putAll(newAttributes); return this; } - public IElement setChildren(IElement[] newChildren) + public IElement setChildren(List<IElement> newChildren) { assert newChildren != null : "Setting null children is not allowed."; - this.children = new ArrayList<IElement>(Arrays.asList(newChildren)); + this.children.clear(); + this.children.addAll(newChildren); return this; } diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlStructuredPropertyConverter.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlStructuredPropertyConverter.java index 8f513208122bb1f7599e0e2cf4b4c183497c7829..a9cf79fc4b23921fdaeeace0a51164b7e6ba53db 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlStructuredPropertyConverter.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlStructuredPropertyConverter.java @@ -17,6 +17,8 @@ package ch.systemsx.cisd.openbis.generic.shared.managed_property.structured; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -57,17 +59,17 @@ public class XmlStructuredPropertyConverter implements IStructuredPropertyConver this.factory = factory; } - public IElement[] convertToElements(IManagedProperty property) + public List<IElement> convertToElements(IManagedProperty property) { - return property.isSpecialValue() ? new IElement[0] + return property.isSpecialValue() ? Collections.<IElement> emptyList() : convertStringToElements(property.getValue()); } - public IElement[] convertStringToElements(String propertyValue) + public List<IElement> convertStringToElements(String propertyValue) { if (StringUtils.isBlank(propertyValue)) { - return new IElement[0]; + return Collections.<IElement> emptyList(); } Document document = XmlUtils.parseXmlDocument(propertyValue); @@ -75,7 +77,7 @@ public class XmlStructuredPropertyConverter implements IStructuredPropertyConver return root.getChildren(); } - public String convertToString(IElement[] elements) + public String convertToString(List<IElement> elements) { IElement root = createRootElement(elements); Document doc = createEmptyDocument(); @@ -85,7 +87,7 @@ public class XmlStructuredPropertyConverter implements IStructuredPropertyConver return XmlUtils.serializeDocument(doc); } - private IElement createRootElement(IElement[] elements) + private IElement createRootElement(List<IElement> elements) { IElement root = new Element(ROOT_NAME); root.setChildren(elements); @@ -136,22 +138,23 @@ public class XmlStructuredPropertyConverter implements IStructuredPropertyConver } - result.setChildren(children.toArray(new IElement[children.size()])); + result.setChildren(children); return result; } private void transformAttributesFromDOM(Node node, IElement result) { NamedNodeMap domAttributes = node.getAttributes(); - Map<String, String> attributes = result.getAttributes(); if (domAttributes != null) { + Map<String, String> attributes = new HashMap<String, String>(); for (int i = 0; i < domAttributes.getLength(); i++) { Attr domAttr = (Attr) domAttributes.item(i); String unescapedValue = StringEscapeUtils.unescapeXml(domAttr.getValue()); attributes.put(domAttr.getName(), unescapedValue); } + result.setAttributes(attributes); } } diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/StructuredPropertyConverterPythonTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/StructuredPropertyConverterPythonTest.java index e42c43d2a4dc635442fe228dc7a2fc47c36d62f8..4deeb5aaa401c239d6f3e134646b4ec57ebc99e5 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/StructuredPropertyConverterPythonTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/StructuredPropertyConverterPythonTest.java @@ -18,6 +18,7 @@ package ch.systemsx.cisd.openbis.generic.shared.managed_property.structured; import java.io.File; import java.io.IOException; +import java.util.List; import org.apache.commons.io.FileUtils; import org.testng.AssertJUnit; @@ -53,10 +54,10 @@ public class StructuredPropertyConverterPythonTest extends AssertJUnit evaluator.configureUI(managedProperty); // the script will create several elements and serialize them in the property value - IElement[] elements = + List<IElement> elements = ScriptUtilityFactory.createPropertyConverter().convertToElements(managedProperty); - assertEquals(3, elements.length); + assertEquals(3, elements.size()); } /** diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlStructuredPropertyConverterTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlStructuredPropertyConverterTest.java index 9032931a23fcb1d3c22e07b85cc77ed70237fb32..ae6abfd80fb5f381946571000c95becc71dd0f18 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlStructuredPropertyConverterTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlStructuredPropertyConverterTest.java @@ -49,10 +49,10 @@ public class XmlStructuredPropertyConverterTest extends AssertJUnit "materialAttrKey", "materialAttrVal") ); - String persistentValue = converter.convertToString(elements.toArray(new IElement[0])); - IElement[] deserialized = converter.convertStringToElements(persistentValue); + String persistentValue = converter.convertToString(elements); + List<IElement> deserialized = converter.convertStringToElements(persistentValue); - assertEquals(elements, Arrays.asList(deserialized)); + assertEquals(elements, deserialized); } @Test @@ -70,9 +70,9 @@ public class XmlStructuredPropertyConverterTest extends AssertJUnit factory.createMaterialLink("materialCode", "typeCode").addChildren( factory.createElement("nested2").addAttribute("na2", "nav2"))); - String persistentValue = converter.convertToString(elements.toArray(new IElement[0])); - IElement[] deserialized = converter.convertStringToElements(persistentValue); + String persistentValue = converter.convertToString(elements); + List<IElement> deserialized = converter.convertStringToElements(persistentValue); - assertEquals(elements, Arrays.asList(deserialized)); + assertEquals(elements, deserialized); } } diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/structured-property-test.py b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/structured-property-test.py index 259c2a2528c9e9664507778d582328e6aea0e2bb..6bb17aa6fe388b4e55e1cea6c87d6343386f7ab5 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/structured-property-test.py +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/structured-property-test.py @@ -25,13 +25,13 @@ def configureUI(): # # test updating the property contents # - elements = converter.convertToElements(property) + elements = list(converter.convertToElements(property)) elements[0] = factory.createSampleLink("modifiedLink") elements[1].children = [ factory.createElement("nested1").addAttribute("na1", "na2") ] - elements[2].attributes["key2"] = "modifiedvalue" + elements[2].addAttribute("key2", "modifiedvalue") property.value = converter.convertToString(elements)