如何配置 XStream 以根据 XML 属性映射到不同的类? [英] How to configure XStream to map to different classes depending on XML attributes?

查看:20
本文介绍了如何配置 XStream 以根据 XML 属性映射到不同的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Java 对象层次结构:

I have the following Java object hierarchy:

public interface Function {
    public void calculate(long t);
}

public class ConstantFunction implements Function {
    private double constant;

    @Override
    public void calculate(long t) {
        // ...
    }
}

public class LinearFunction implements Function {
    private double slope;

    private double yIntercept;

    @Override
    public void calculate(long t) {
        // ...
    }
}

用户可以通过在 XML 中定义它们来创建 ConstantFunctionLinearFunction 实例,如下所示:

Users can create ConstantFunction and LinearFunction instances by defining them inside XML like so:

<myapp>
    <function type="ConstantFunction>
        <!-- ... -->
    </function>
    <function type="LinearFunction>
        <!-- ... -->
    </function>
</myapp>

我正在使用 XStream 将用户定义的 XML OX 映射到 Java POJO.我目前正在尝试使用别名配置 XStream 映射器,以便它知道要绑定到 function 元素的 Java 类:

I am using XStream to OX-map the user-defined XML into Java POJOs. I'm currently trying to configure the XStream mapper with aliases so that it knows what Java class to bind to the function element:

XStream oxmapper = new XStream();
oxmapper.alias("myapp", MyApp.class);
oxmapper.alias("function", ???);

问题是,我需要使用以下逻辑配置 XStream:*如果 function/typeConstantFunction,然后使用 oxmapper.alias("function", ConstantFunction.class);但如果它的值为 LinearFunction,则使用 oxmapper.alias("function", LinearFunction.class).

The problem is, I need to configure XStream with logic that says: *if function/type is ConstantFunction, then use oxmapper.alias("function", ConstantFunction.class); but if its value is LinearFunction, then use oxmapper.alias("function", LinearFunction.class).

问题是,我不认为 XStream API 提供了一种方法来检查 XML 以我需要的方式来实现此逻辑.如果我错了,请指出正确的方向!

The problem is, I don't think XStream API gives provides a way to inspect the XML the way I need it to in order to implement this logic. If I am incorrect, please point me in the right direction!

如果我是对的,那么我能想到的唯一解决方案就是拥有一个非常讨厌的大杂烩"类,它形成所有 Function 实体的联合,如下所示:

If I am correct, then the only solution I can think of would be to have a really nasty "hodgepodge" class that forms a union of all the Function concretions like so:

public class FunctionFactory implements Function {
    private double constant;
    private double slope;
    private double yIntercept;

    private Class<? extends Function> concreteClass;

    @Override
    public void calculate(long t) {
        // Do nothing. This class is a workaround to limitations with XStream.
        return;
    }
}

在 OX-mapper 配置中:

In the OX-mapper config:

oxampper.alias("function", FunctionFactory.class);
oxmapper.aliasField("function", "type", "concreteClass");

现在,每次我将 XML 实例读入 MyApp 实例时,我都需要更正转换:

Now, every time I read an XML instance into a MyApp instance, I need to correct the conversion:

XStream oxmapper = getConfiguredMapper();
MyApp app = oxmapper.fromXml("<myapp>...</myapp>");

FunctionFactory factory = app.getFunction();
Function concretion = factory.getConcreteClass();

app.setFunction(concretion);

这是我唯一能想到的解决方法,但感觉真的很糟糕,我不得不相信有更好的方法来做到这一点.提前致谢!

This is the only workaround I can cook up, but it feels really nasty, and I have to believe there's a better way to do this. Thanks in advance!

推荐答案

当你想在 XStream 中有自定义行为时,你应该使用 转换器教程.

When you want to have custom behaviors in XStream, you should use Converters as described in Converter tutorial.

这篇关于如何配置 XStream 以根据 XML 属性映射到不同的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆