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) {
return type.equals(Attribute.class);
}
@Override
public String toString(Object obj) {
return obj == null ? null : ((Attribute) obj).value;
}
@Override
public Object fromString(String name) {
return new Attribute(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
| This issue 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 {
@SuppressWarnings("unchecked")
@Override
public boolean canConvert(Class clazz) { return clazz.equals(Software.class); }
@Override
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) { Software custom = (Software) source; if (custom.clazz != null) writer.addAttribute(ALIAS, custom.clazz); }
@Override
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) { Software custom = new Software(); custom.clazz = reader.getAttribute(ALIAS); return custom; }
}
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)); }
}