提交 1602813b 编写于 作者: K Kohsuke Kawaguchi

Improving functionality

上级 d318bbae
......@@ -36,8 +36,8 @@ import com.thoughtworks.xstream.io.xml.AbstractXmlWriter;
import com.thoughtworks.xstream.io.xml.DocumentReader;
import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
import hudson.Util;
import hudson.util.VariableResolver;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
......@@ -119,6 +119,29 @@ public class XStreamDOM {
return (T)xs.unmarshal(newReader(),root);
}
/**
* Recursively expands the variables in text and attribute values and return the new DOM.
*
* The expansion uses {@link Util#replaceMacro(String, VariableResolver)}, so any unresolved
* references will be left as-is.
*/
public XStreamDOM expandMacro(VariableResolver<String> vars) {
String[] newAttributes = new String[attributes.length];
for (int i=0; i<attributes.length; i+=2) {
newAttributes[i+0] = attributes[i]; // name
newAttributes[i+1] = Util.replaceMacro(attributes[i+1],vars);
}
List<XStreamDOM> newChildren = null;
if (children!=null) {
newChildren = new ArrayList<XStreamDOM>(children.size());
for (XStreamDOM d : children)
newChildren.add(d.expandMacro(vars));
}
return new XStreamDOM(tagName,newAttributes,newChildren,Util.replaceMacro(value,vars));
}
public String getAttribute(String name) {
for (int i=0; i<attributes.length; i+=2)
if (attributes[i].equals(name))
......
......@@ -29,6 +29,8 @@ import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* @author Kohsuke Kawaguchi
......@@ -97,4 +99,19 @@ public class XStreamDOMTest extends TestCase {
}
public static class Name_That_Gets_Escaped {}
public static class DomInMap {
Map<String,XStreamDOM> values = new HashMap<String, XStreamDOM>();
}
public void testDomInMap() {
DomInMap v = new DomInMap();
v.values.put("foo",createSomeFoo().bar);
String xml = xs.toXML(v);
System.out.println(xml);
Object v2 = xs.fromXML(xml);
System.out.println(v2);
}
public void test
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册