【问题标题】:How to configure XStream to map to different classes depending on XML attributes?如何配置 XStream 以根据 XML 属性映射到不同的类?
【发布时间】:2012-09-16 04:16:51
【问题描述】:

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

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 实例,如下所示:

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

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

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)

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

如果我是正确的,那么我能想到的唯一解决方案就是有一个非常讨厌的“大杂烩”类,它形成了所有 Function 凝结物的联合,如下所示:

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 配置中:

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

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

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

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

app.setFunction(concretion);

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

【问题讨论】:

    标签: java xml xstream xmlmapper


    【解决方案1】:

    当您想在 XStream 中拥有自定义行为时,您应该使用 Converter tutorial 中描述的转换器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-12
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2013-05-04
      • 2017-04-08
      相关资源
      最近更新 更多