1

I cannot seem to be able to unmarshall a Map properly using XStream.

I have the following code:

Storage class

public class Storage
{

    @XStreamAsAttribute
    private String basedir;

    @XStreamAsAttribute
    private Map<String, Repository> repositories = new LinkedHashMap<String, Repository>();

...

}

Repository class

public class Repository
{

    @XStreamAsAttribute
    private String name;

    @XStreamAsAttribute
    private String basedir;

    @XStreamAsAttribute
    private int policy;

    @XStreamAsAttribute
    private int layout;

    @XStreamAsAttribute
    private int type;

...

}

ConfigurationParser class

public class ConfigurationParser
{

    public Storage parseConfiguration(String xmlFile)
            throws FileNotFoundException
    {
        FileInputStream fis = new FileInputStream(xmlFile);

        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);

        xstream.alias("storage", Storage.class);
        xstream.alias("repositories", Map.class);
        xstream.alias("repository", Repository.class);

        return (Storage) xstream.fromXML(fis);
    }

}

configuration.xml

<storage>
    <basedir>storages/storage0</basedir>
    <repositories>
        <repository>
            <name>repository0</name>
            <policy>snapshots</policy>
            <layout>maven2</layout>
            <type>hosted</type>
        </repository>
    </repositories>
</storage>

Could somebody please give me some hints as to what is wrong. This is the error message:

com.thoughtworks.xstream.converters.ConversionException: name : name
---- Debugging information ----
message             : name
cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : name
class               : java.util.HashMap
required-type       : java.util.HashMap
converter-type      : com.thoughtworks.xstream.converters.collections.MapConverter
path                : /storage/repositories/repository/name
line number         : 5
class[1]            : org.foo.storage.Storage
converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version             : null
-------------------------------
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1058)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1042)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:922)
    at org.foo.configuration.ConfigurationParser.parseConfiguration(ConfigurationParser.java:36)
    at org.foo.configuration.ConfigurationParserTest.testParseConfiguration(ConfigurationParserTest.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:196)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:64)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: name
    at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:56)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:45)
    at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
    at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:70)
    at com.thoughtworks.xstream.converters.collections.MapConverter.putCurrentEntryIntoMap(MapConverter.java:86)
    at com.thoughtworks.xstream.converters.collections.MapConverter.populateMap(MapConverter.java:78)
    at com.thoughtworks.xstream.converters.collections.MapConverter.populateMap(MapConverter.java:72)
    at com.thoughtworks.xstream.converters.collections.MapConverter.unmarshal(MapConverter.java:67)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    ... 42 more

Update 1:

Right, so... if I have:

ConfigurationParser class

public Storage parseConfiguration(String xmlFile)
        throws FileNotFoundException
{
    FileInputStream fis = new FileInputStream(xmlFile);

    XStream xstream = new XStream();
    xstream.autodetectAnnotations(true);
    xstream.alias("storage", Storage.class);

    return (Storage) xstream.fromXML(fis);
}

Repository class

public class Repository
{

    @XStreamAlias(value = "name")
    private String name;

    @XStreamAlias (value = "basedir")
    private String basedir;

    @XStreamAlias (value = "policy")
    private String policy = RepositoryPolicyEnum.MIXED.getPolicy();

    @XStreamAlias (value = "layout")
    private String layout = RepositoryLayoutEnum.MAVEN_2.getLayout();

    @XStreamAlias (value = "type")
    private String type = RepositoryTypeEnum.HOSTED.getType();

...

}

and:

configuration.xml

<storage>
    <basedir>storages/storage0</basedir>
    <repository>
        <basedir>/here</basedir>
        <name>repository0</name>
        <policy>snapshots</policy>
        <layout>maven2</layout>
        <type>hosted</type>
    </repository>
    <repository>
        <basedir>/here2</basedir>
        <name>repository2</name>
        <policy>snapshots</policy>
        <layout>maven2</layout>
        <type>hosted</type>
    </repository>
</storage>

It works. However, I would like the configure.xml to look like:

<storage>
    <basedir>storages/storage0</basedir>
    <repositories>
        <repository>
            <name>repository0</name>
            <policy>snapshots</policy>
            <layout>maven2</layout>
            <type>hosted</type>
        </repository>
    </repositories>
</storage>

Any suggestions...?

carlspring
  • 31,231
  • 29
  • 115
  • 197
  • checkout this one http://stackoverflow.com/questions/1537207/how-to-convert-xml-to-java-util-map-and-vice-versa – fGo Jun 13 '13 at 12:24

1 Answers1

5

Try this one

XStream xstream = new XStream();
xstream.alias("storage", Storage.class);
xstream.alias("repository", Map.class);
xstream.registerConverter(new MapEntryConverter());
Storage storage = (Storage) xstream.fromXML(fis);
System.out.println(storage);

and your converter

public static class MapEntryConverter implements Converter {

    public boolean canConvert(Class clazz) {
        return AbstractMap.class.isAssignableFrom(clazz);
    }

    public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
        AbstractMap map = (AbstractMap) value;
        for (Object obj : map.entrySet()) {
            Entry entry = (Entry) obj;
            writer.startNode(entry.getKey().toString());
            writer.setValue(entry.getValue().toString());
            writer.endNode();
        }
    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {

      Repository repo = new Repository();
      while (reader.hasMoreChildren()) {
        reader.moveDown();
        String nodeName = reader.getNodeName();
        if ("name".equalsIgnoreCase(nodeName)) {
          repo.setName(reader.getValue());
        } else if ("policy".equalsIgnoreCase(nodeName)) {
          repo.setPolicy(reader.getValue());
        } else if ("layout".equalsIgnoreCase(nodeName)) {
          repo.setLayout(reader.getValue());
        } else if ("type".equalsIgnoreCase(nodeName)) {
          repo.setType(reader.getValue());
        }
        reader.moveUp();
  }
      return repo;
    }

}

And my classes are simple pojos

public class Storage {
  private String basedir;
  private List<Repository> repositories = new ArrayList<Repository>();
  ...
}

public class Repository {
  private String name;
  private String policy;
  private String layout;
  private String type;
  ...
}
fGo
  • 1,146
  • 5
  • 11
  • @carlspring please verify the reader javadoc in http://xstream.codehaus.org/javadoc/com/thoughtworks/xstream/io/HierarchicalStreamReader.html to be completly sure that you are reading the parameters in the right order – fGo Jun 13 '13 at 12:33
  • Yeah, I'd seen that kind of example, but couldn't seem to get it to work. Could you please show me a working example, as the code above doesn't seem to be working...? For one -- you're marking `repository` as a `Map.class`. Is this what you had in mind...? Much appreciated! – carlspring Jun 13 '13 at 14:14
  • Furthermore, in your example -- regarding the `moveDown()` invocation -- wouldn't that require that the elements are exactly in the expected order you're parsing them? – carlspring Jun 13 '13 at 14:16
  • @carlspring i modified the code to avoid a fix position in the `repository` child tags – fGo Jun 14 '13 at 09:56
  • 1
    Hi, thanks! In your example you've changed the `private Map repositories = new LinkedHashMap();` to a list. I know how to do it with a `List`. I am interested in the case where it is a map and that every ``'s `` element is the key of that map. Are you trying to do this, or I am missing something? What am I not getting? – carlspring Jun 14 '13 at 14:21
  • Have you tried your code? Does it work with the XML file I placed as an example? For me it doesn't. Could you please have another look? Thanks! – carlspring Jun 16 '13 at 14:00
  • @carlspring I posted the code after testing it. If it does not for you then you should verify your dependencies (xmlpull-1.1.3.1, xpp3_min-1.1.4c + xstream core). Anyway, i'm going to verify the repository as a _map_ and i'll post it – fGo Jun 17 '13 at 07:52
  • The only way I managed to get it to work is by making the map converter read attributes, instead of tags. I was hoping to get it to work by reading tags instead. I'm not sure what I'm doing wrong, but I tried your code and, I'm afraid, it didn't work. Much appreciate your help! – carlspring Jun 17 '13 at 09:52