XStream

Cannot use "class" as an attribute alias

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major 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

Activity

Hide
Don Ngo added a comment -

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)); }
}

Show
Don Ngo added a comment - 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)); } }
Hide
Joerg Schaible added a comment -

"class" is an internally used attribute and there's currently no real way to use it for your own purpose, sorry. Any further questions please go to the user's list.

Show
Joerg Schaible added a comment - "class" is an internally used attribute and there's currently no real way to use it for your own purpose, sorry. Any further questions please go to the user's list.
Hide
Don Ngo added a comment -

Just one more question before I try the user's list. As part of my system integration project requirement, I need to parse some XML documents that have some tags with "class" as the attribute name, and I also need to create some XML documents with some tags with "class" as the attribute name as well. Do you have any idea how I can get around this problem?

Show
Don Ngo added a comment - Just one more question before I try the user's list. As part of my system integration project requirement, I need to parse some XML documents that have some tags with "class" as the attribute name, and I also need to create some XML documents with some tags with "class" as the attribute name as well. Do you have any idea how I can get around this problem?
Hide
Joerg Schaible added a comment -

Next version of XStream will have a method XStream.aliasSystemAttribute that allows you to tell XStream to use another name without side effects.

Show
Joerg Schaible added a comment - Next version of XStream will have a method XStream.aliasSystemAttribute that allows you to tell XStream to use another name without side effects.
Hide
Joerg Schaible added a comment -

Set correct fix version.

Show
Joerg Schaible added a comment - Set correct fix version.
Hide
Joerg Schaible added a comment -

Fixed for upcoming release.

Show
Joerg Schaible added a comment - Fixed for upcoming release.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: