mirror of https://github.com/apache/cloudstack.git
CloudStack CLOUDSTACK-723
Enhanced baremetal servers support on Cisco UCS able to dump xmlobject
This commit is contained in:
parent
53473c07b9
commit
fb050894f5
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue