CloudStack CLOUDSTACK-723 Enhanced baremetal servers support on Cisco UCS able to dump xmlobject
Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/fb050894 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/fb050894 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/fb050894 Branch: refs/heads/javelin Commit: fb050894f5864b8e01e14d0002bcab0b639c9a4e Parents: 53473c0 Author: frank <[email protected]> Authored: Thu Jan 17 16:11:15 2013 -0800 Committer: frank <[email protected]> Committed: Thu Jan 17 16:11:15 2013 -0800 ---------------------------------------------------------------------- utils/src/com/cloud/utils/xmlobject/XmlObject.java | 53 ++++++++++++++- .../com/cloud/utils/xmlobject/TestXmlObject2.java | 37 ++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/fb050894/utils/src/com/cloud/utils/xmlobject/XmlObject.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/xmlobject/XmlObject.java b/utils/src/com/cloud/utils/xmlobject/XmlObject.java index 4ebf371..0c8f190 100755 --- a/utils/src/com/cloud/utils/xmlobject/XmlObject.java +++ b/utils/src/com/cloud/utils/xmlobject/XmlObject.java @@ -5,6 +5,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -28,7 +29,11 @@ public class XmlObject { XmlObject() { } - XmlObject putElement(String key, Object e) { + public XmlObject(String tag) { + this.tag = tag; + } + + public XmlObject putElement(String key, Object e) { Object old = elements.get(key); if (old == null) { System.out.println(String.format("no %s, add new", key)); @@ -83,16 +88,58 @@ public class XmlObject { return text; } - void setText(String text) { + public XmlObject setText(String text) { this.text = text; + return this; } public String getTag() { return tag; } - void setTag(String tag) { + public XmlObject setTag(String tag) { this.tag = tag; + return this; + } + + public String dump() { + StringBuilder sb = new StringBuilder(); + sb.append("<").append(tag); + List<XmlObject> children = new ArrayList<XmlObject>(); + for (Map.Entry<String, Object> e : elements.entrySet()) { + String key = e.getKey(); + Object val = e.getValue(); + if (val instanceof String) { + sb.append(String.format(" %s=\"%s\"", key, val.toString())); + } else if (val instanceof XmlObject) { + children.add((XmlObject) val); + } else if (val instanceof List) { + children.addAll((Collection<? extends XmlObject>) val); + } else { + throw new CloudRuntimeException(String.format("unsupported element type[tag:%s, class: %s], only allowed type of [String, List<XmlObject>, Object]", key, val.getClass().getName())); + } + } + + if (!children.isEmpty() && text != null) { + throw new CloudRuntimeException(String.format("element %s cannot have both text[%s] and child elements", tag, text)); + } + + if (!children.isEmpty()) { + sb.append(">"); + for (XmlObject x : children) { + sb.append(x.dump()); + } + sb.append(String.format("</%s>", tag)); + } else { + if (text != null) { + sb.append(">"); + sb.append(text); + sb.append(String.format("</%s>", tag)); + } else { + sb.append(" />"); + } + } + return sb.toString(); } @Override http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/fb050894/utils/test/com/cloud/utils/xmlobject/TestXmlObject2.java ---------------------------------------------------------------------- diff --git a/utils/test/com/cloud/utils/xmlobject/TestXmlObject2.java b/utils/test/com/cloud/utils/xmlobject/TestXmlObject2.java new file mode 100755 index 0000000..ccef3aa --- /dev/null +++ b/utils/test/com/cloud/utils/xmlobject/TestXmlObject2.java @@ -0,0 +1,37 @@ +package com.cloud.utils.xmlobject; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class TestXmlObject2 { + void p(String str) { + System.out.println(str); + } + + XmlObject xo(String name) { + return new XmlObject(name); + } + + @Test + public void test() { + XmlObject root = new XmlObject("test"); + root.putElement("key1", "value1").putElement("key2", "value2"); + p(root.dump()); + + XmlObject c1 = new XmlObject("child1"); + XmlObject c2 = new XmlObject("child2"); + c2.putElement("ckey1", "value1"); + c1.putElement(c2.getTag(), c2); + root.putElement(c1.getTag(), c1); + p(root.dump()); + + root = xo("test2").putElement("key1", "value1").putElement("child1", xo("child1").setText("yyy")) + .putElement("child1", xo("child1") + .putElement("child2", xo("child2") + .putElement("child3", xo("child3").putElement("key3", "value3").setText("xxxxx")))); + + p(root.dump()); + } + +}
