Details
-
Type: Bug
-
Status: Closed
-
Priority: Major
-
Resolution: Fixed
-
Affects Version/s: 1.2.2
-
Fix Version/s: 1.3.1
-
Component/s: None
-
Labels:None
-
JDK version and platform:Sun 1.6.0_03-b05 for Windows
Description
Why can't I use "class" as an attribute name? Whenever I tried, an exception is thrown. Is this a bug or am I doing something wrong here? Please see the code below.
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.SingleValueConverter;
public class Software {
private static class AttributeConverter implements SingleValueConverter {
@SuppressWarnings("unchecked")
@Override
public boolean canConvert(Class type)
@Override
public String toString(Object obj)
@Override
public Object fromString(String name)
}
private static class Attribute {
public String value;
public Attribute(String value)
{ this.value = value; }}
public Attribute clazz;
public static void main(String[] args)
{ String xml = "<software id=\"test\"/>"; // Using "id" as an attribute is fine, // String xml = "<software class=\"test\"/>"; // but using "class" won't work!! XStream xstream = new XStream(); xstream.alias("software", Software.class); xstream.aliasAttribute("id", "clazz"); // Using "id" as an alias is fine, //xstream.aliasAttribute("class", "clazz"); // but using "class" won't work!! xstream.useAttributeFor("clazz", Attribute.class); xstream.registerConverter(new AttributeConverter()); Software sw = (Software) xstream.fromXML(xml); System.out.println(xstream.toXML(sw)); }}
Issue Links
- is duplicated by
-
XSTR-520 XML with attribute call 'reference'
Strangely, even when I tried using customed converter to get around this problem, it still doesn't work.
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
public class Software {
// Setting ALIAS to "class" will result in run-time exception
private static final String ALIAS = "xyz";
public String clazz;
private static class MyConverter implements Converter {
{ return clazz.equals(Software.class); }@SuppressWarnings("unchecked")
@Override
public boolean canConvert(Class clazz)
@Override
{ Software custom = (Software) source; if (custom.clazz != null) writer.addAttribute(ALIAS, custom.clazz); }public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context)
@Override
{ Software custom = new Software(); custom.clazz = reader.getAttribute(ALIAS); return custom; }public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context)
}
public static void main(String[] args)
{ String xml = "<software " + ALIAS + "=\"test\"/>"; XStream xstream = new XStream(); xstream.alias("software", Software.class); xstream.aliasAttribute(ALIAS, "clazz"); xstream.registerConverter(new MyConverter()); Software sw = (Software) xstream.fromXML(xml); System.out.println("before: " + xml); System.out.println("after : " + xstream.toXML(sw)); }}