XML 和 Java 中的强大科学类型

发布于 2024-10-10 03:29:28 字数 2042 浏览 7 评论 0原文

我的项目包含一个大的 XSD 架构,并且架构实例绑定到 使用 JAXB 的 java 代码。 我想使用 JScience 将强类型添加到 java 代码中,并将它们绑定到 XSD 类型。

我当前的问题是 JAXB 处理泛型非常糟糕......它们根本没有被解析。

例如,我想要 XSD 中的电源类型。 我希望 JAXB 将其绑定为 JScience 类型 Measure

<xs:simpleType name="PowerType">
    <xs:annotation>
        <xs:appinfo>
            <jxb:javaType name="org.jscience.physics.measures.Measure&lt;javax.measure.quantities.Power&gt;" 
                          parseMethod="org.f4g.adapter.UnitAdapter.unmarshalPower" />
        </xs:appinfo>
    </xs:annotation>
<xs:restriction base="xs:double"/>        
</xs:simpleType>



解析方法是:

public class UnitAdapter{

public static Measure<Power> unmarshalPower(String value) {
        return Measure.valueOf(Double.valueOf(value), SI.WATT);
    }
}

这可以用 JXC 很好地编译。我的 getter 和 setter 具有很好的类型 Measure

但是当我尝试使用

// create an Unmarshaller
Unmarshaller u = JAXBContext.newInstance("org.f4g.schema.test").createUnmarshaller();


读取架构实例时 我得到:

Exception in thread "main" java.lang.reflect.MalformedParameterizedTypeException
at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.validateConstructorArguments(ParameterizedTypeImpl.java:60)
at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.<init>(ParameterizedTypeImpl.java:53)
at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.make(ParameterizedTypeImpl.java:95)
at sun.reflect.generics.factory.CoreReflectionFactory.makeParameterizedType(CoreReflectionFactory.java:104)
at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:140)
at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
at sun.reflect.generics.repository.FieldRepository.getGenericType(FieldRepository.java:85)
etc...


你知道我该怎么办吗? 是否已经完成了通过 JAXB 将 XSD 绑定到 JScience 类型的工作?

多谢, 科朗坦

My project contains a big XSD Schema, and schema instances are binded to the
java code using JAXB.
I would like to add strong types into the java code, using JScience, and bind them to XSD types.

My current problem is that JAXB handle very badly generics... They are not parsed at all.

For example, I want a power type in the XSD.
I want JAXB to bind it the JScience type Measure<Power>:

<xs:simpleType name="PowerType">
    <xs:annotation>
        <xs:appinfo>
            <jxb:javaType name="org.jscience.physics.measures.Measure<javax.measure.quantities.Power>" 
                          parseMethod="org.f4g.adapter.UnitAdapter.unmarshalPower" />
        </xs:appinfo>
    </xs:annotation>
<xs:restriction base="xs:double"/>        
</xs:simpleType>




The parse method is:

public class UnitAdapter{

public static Measure<Power> unmarshalPower(String value) {
        return Measure.valueOf(Double.valueOf(value), SI.WATT);
    }
}

This compiles fine with JXC. My getters and setters have the nice type Measure<Power>.


But when I'm trying to read a schema instance with

// create an Unmarshaller
Unmarshaller u = JAXBContext.newInstance("org.f4g.schema.test").createUnmarshaller();



I get:

Exception in thread "main" java.lang.reflect.MalformedParameterizedTypeException
at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.validateConstructorArguments(ParameterizedTypeImpl.java:60)
at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.<init>(ParameterizedTypeImpl.java:53)
at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.make(ParameterizedTypeImpl.java:95)
at sun.reflect.generics.factory.CoreReflectionFactory.makeParameterizedType(CoreReflectionFactory.java:104)
at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:140)
at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
at sun.reflect.generics.repository.FieldRepository.getGenericType(FieldRepository.java:85)
etc...



Do you know how can I do?
Is there are already work done in binding XSD to JScience's types through JAXB?

Thanks a lot,
Corentin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梨涡少年 2024-10-17 03:29:28

您的类型应为 org.jscience.physicals.measures.Measure ,因为泛型信息不用于实例化类(Class.forName(String) 不使用泛型信息实例化对象)。

Your type should be org.jscience.physics.measures.Measure as generics information is not used to instanciate class (Class.forName(String) don't use generics information to instanciate objects).

不念旧人 2024-10-17 03:29:28

我制作了以下课程作为测量适配器并且它有效。

public class MeasureAdapter extends XmlAdapter<String, Measure<Double, Quantity>> {

public String marshal(Measure<Double, Quantity> value) throws Exception {
    if (null == value) {
        return null;
    }

    return value.toString();
}

@SuppressWarnings("unchecked")
@Override
public Measure<Double, Quantity> unmarshal(String str) throws Exception {
    if(str == null || str.length() == 0) {
        return null;
    }

    String[] d = str.split(" ");
    Measure<Double, Quantity> m = null;
    m = (Measure<Double, Quantity>) Measure.valueOf(Double.parseDouble(d[0]), Unit.valueOf(d[1]));

    return m;
}

}

I made the following class as an Adapter for Measure and it worked.

public class MeasureAdapter extends XmlAdapter<String, Measure<Double, Quantity>> {

public String marshal(Measure<Double, Quantity> value) throws Exception {
    if (null == value) {
        return null;
    }

    return value.toString();
}

@SuppressWarnings("unchecked")
@Override
public Measure<Double, Quantity> unmarshal(String str) throws Exception {
    if(str == null || str.length() == 0) {
        return null;
    }

    String[] d = str.split(" ");
    Measure<Double, Quantity> m = null;
    m = (Measure<Double, Quantity>) Measure.valueOf(Double.parseDouble(d[0]), Unit.valueOf(d[1]));

    return m;
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文