From 38424efe2074124cd94ecbb00fadef16b1d17166 Mon Sep 17 00:00:00 2001 From: kaloyane <kaloyane> Date: Thu, 3 Feb 2011 14:27:33 +0000 Subject: [PATCH] [LMS-2050] use unmodifiable lists in the "elements" API for managed properties SVN: 19739 --- .../managed_property/structured/Element.java | 20 ++++++++++--------- .../XmlStructuredPropertyConverter.java | 19 ++++++++++-------- ...StructuredPropertyConverterPythonTest.java | 5 +++-- .../XmlStructuredPropertyConverterTest.java | 12 +++++------ .../structured/structured-property-test.py | 4 ++-- 5 files changed, 33 insertions(+), 27 deletions(-) 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 ec63d87f3df..75955a2ac0b 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 8f513208122..a9cf79fc4b2 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 e42c43d2a4d..4deeb5aaa40 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 9032931a23f..ae6abfd80fb 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 259c2a2528c..6bb17aa6fe3 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) -- GitLab