How to unmarshal Map using XStream

给你一囗甜甜゛ 提交于 2019-12-14 02:37:41

问题


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...?


回答1:


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


来源:https://stackoverflow.com/questions/17086757/how-to-unmarshal-map-using-xstream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!