使用 MOXy 生成具有默认 minOccurs 的模式

发布于 2024-10-08 02:24:08 字数 757 浏览 7 评论 0原文

我尝试使用 EclipseLink MOXy 2.1.2 和 Java 1.6 基于我的域类生成架构,遵循以下示例: http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JAXB/GenerateSchema

但是,默认的 minOccurs 值存在问题。如果注释 @XmlElement 未指定必需的属性,则它应默认为 false,这意味着生成模式时 minOccurs 应为 0。这与 Sun 的 JAXB 实现的预期一致。但是,使用 Moxy 时,不会生成 minOccurs,并且隐含默认值 1。

例如,

public Integer getDuration() {
   return duration;
}

应该生成

<xs:element name="duration" type="xsd:int" minOccurs="0"/>

相反,如果使用 Moxy,它会生成

<xsd:element name="duration" type="xsd:int"/>

这意味着 minOccurs="1",因为这是默认值。这是一个错误吗?有什么解决方法吗?

提前致谢!

I'm trying to use EclipseLink MOXy 2.1.2 and Java 1.6 to generate a schema based on my domain classes, following this example: http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JAXB/GenerateSchema

However there is an issue with the default minOccurs value. If the annotation @XmlElement does not specify the required attribute, it should default to false, which means when generating the schema minOccurs should be 0. This works as expected with Sun's JAXB implementation. However when using Moxy minOccurs is not being generated and the default value of 1 is implied.

For example,

public Integer getDuration() {
   return duration;
}

Should have generated

<xs:element name="duration" type="xsd:int" minOccurs="0"/>

Instead if using Moxy it generates

<xsd:element name="duration" type="xsd:int"/>

which means minOccurs="1" since that is the default. Is this a bug? Is there any workaround for this?

Thanks in advance!

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

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

发布评论

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

评论(2

萤火眠眠 2024-10-15 02:24:09

感谢您的回复,布莱斯。问题似乎是由于我试图通过指定 propOrder = {} 来生成 xsd:all 。使用该注释,MOXy 不会生成 minOccurs="0"。我修改了您的演示类来演示该问题:

package moxy.test;

import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.bind.annotation.XmlType;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.persistence.Version;

public class Test {

    @XmlType(propOrder = {})
    public static class Root {

        private Integer duration;
        private Integer period;

        public Integer getPeriod() {
            return this.period;
        }

        public void setPeriod(Integer period) {
            this.period = period;
        }

        public Integer getDuration() {
            return duration;
        }

        public void setDuration(Integer duration) {
            this.duration = duration;
        }

    }

    public static class MySchemaOutputResolver extends SchemaOutputResolver {

        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            StreamResult result = new StreamResult(System.out);
            result.setSystemId(suggestedFileName);
            return result;
        }

    }

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        System.out.println(jc);
        System.out.println(Version.getVersionString());
        jc.generateSchema(new MySchemaOutputResolver());
    }
}

以下内容是使用 MOXy 生成的。请注意,没有 minOccurs 属性:

2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="root">
      <xsd:all>
         <xsd:element name="duration" type="xsd:int"/>
         <xsd:element name="period" type="xsd:int"/>
      </xsd:all>
   </xsd:complexType>
</xsd:schema>

如果删除 @XmlType(propOrder = {}) 注释,则生成的模式会存在 minOccurs,但元素作为序列:

org.eclipse.persistence.jaxb.JAXBContext@cdedfd
2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="root">
      <xsd:sequence>
         <xsd:element name="duration" type="xsd:int" minOccurs="0"/>
         <xsd:element name="period" type="xsd:int" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
</xsd:schema>

仅供参考,下面是 Sun 的 JAXB 参考实现生成的内容:

jar:file:/C:/Program%20Files/Java/jdk1.6.0_21/jre/lib/rt.jar!/com/sun/xml/internal/bind/v2/runtime/JAXBContextImpl.class Build-Id: 1.6.0_21
...

2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="root">
    <xs:all>
      <xs:element name="duration" type="xs:int" minOccurs="0"/>
      <xs:element name="period" type="xs:int" minOccurs="0"/>
    </xs:all>
  </xs:complexType>
</xs:schema>

提前致谢!

Thanks for your response, Blaise. The problem seems to be due to the fact that I'm trying to generate xsd:all by specifying propOrder = {}. With that annotation MOXy doesn't generate the minOccurs="0". I've modified your demo class to demonstrate the issue:

package moxy.test;

import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.bind.annotation.XmlType;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.persistence.Version;

public class Test {

    @XmlType(propOrder = {})
    public static class Root {

        private Integer duration;
        private Integer period;

        public Integer getPeriod() {
            return this.period;
        }

        public void setPeriod(Integer period) {
            this.period = period;
        }

        public Integer getDuration() {
            return duration;
        }

        public void setDuration(Integer duration) {
            this.duration = duration;
        }

    }

    public static class MySchemaOutputResolver extends SchemaOutputResolver {

        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            StreamResult result = new StreamResult(System.out);
            result.setSystemId(suggestedFileName);
            return result;
        }

    }

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        System.out.println(jc);
        System.out.println(Version.getVersionString());
        jc.generateSchema(new MySchemaOutputResolver());
    }
}

The following is produced with MOXy. Notice there is no minOccurs attribute:

2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="root">
      <xsd:all>
         <xsd:element name="duration" type="xsd:int"/>
         <xsd:element name="period" type="xsd:int"/>
      </xsd:all>
   </xsd:complexType>
</xsd:schema>

If you remove the @XmlType(propOrder = {}) annotation the generated schema has the minOccurs present but with the elements as a sequence:

org.eclipse.persistence.jaxb.JAXBContext@cdedfd
2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="root">
      <xsd:sequence>
         <xsd:element name="duration" type="xsd:int" minOccurs="0"/>
         <xsd:element name="period" type="xsd:int" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
</xsd:schema>

And just for reference, here is what is generated by Sun's JAXB reference implementation:

jar:file:/C:/Program%20Files/Java/jdk1.6.0_21/jre/lib/rt.jar!/com/sun/xml/internal/bind/v2/runtime/JAXBContextImpl.class Build-Id: 1.6.0_21
...

2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="root">
    <xs:all>
      <xs:element name="duration" type="xs:int" minOccurs="0"/>
      <xs:element name="period" type="xs:int" minOccurs="0"/>
    </xs:all>
  </xs:complexType>
</xs:schema>

Thanks in advance!

隔纱相望 2024-10-15 02:24:08

您是否有可能使用 EclipseLink 2.1.2 的预发行版本? EclipseLink 2.1.2 的发布版本可以在这里获取:

当我运行以下示例时:

package minoccurs;

import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.persistence.Version;

public class Demo {

    public static class Root {

        private Integer duration; 

        public Integer getDuration() {
            return duration;
        }

        public void setDuration(Integer duration) {
            this.duration = duration;
        }

    }

    public static class MySchemaOutputResolver extends SchemaOutputResolver {

        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            StreamResult result = new StreamResult(System.out);
            result.setSystemId(suggestedFileName);
            return result;
        }

    }

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        System.out.println(jc);
        System.out.println(Version.getVersionString());
        jc.generateSchema(new MySchemaOutputResolver());
    }
}

我得到:

org.eclipse.persistence.jaxb.JAXBContext@16cd7d5
2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="root">
      <xsd:sequence>
         <xsd:element name="duration" type="xsd:int" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
</xsd:schema>

UPDATE #1

基于您的 更新,这是一个问题。我打开了以下 EclipseLink MOXy bug:

更新 #2

我们已在 EclipseLink 2.2 流中解决了此错误。该修复程序可从 12 月 21 日开始的夜间下载中获取:

此修复最终也将包含在 2.1.3 补丁中。

Is there any chance you are on a pre-release version of EclipseLink 2.1.2? The released version of EclipseLink 2.1.2 can be obtained here:

When I run the following example:

package minoccurs;

import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.persistence.Version;

public class Demo {

    public static class Root {

        private Integer duration; 

        public Integer getDuration() {
            return duration;
        }

        public void setDuration(Integer duration) {
            this.duration = duration;
        }

    }

    public static class MySchemaOutputResolver extends SchemaOutputResolver {

        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            StreamResult result = new StreamResult(System.out);
            result.setSystemId(suggestedFileName);
            return result;
        }

    }

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        System.out.println(jc);
        System.out.println(Version.getVersionString());
        jc.generateSchema(new MySchemaOutputResolver());
    }
}

I get:

org.eclipse.persistence.jaxb.JAXBContext@16cd7d5
2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="root">
      <xsd:sequence>
         <xsd:element name="duration" type="xsd:int" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
</xsd:schema>

UPDATE #1

Based on your update, this is an issue. I have opened the following EclipseLink MOXy bug:

UPDATE #2

We have addressed this bug in the EclipseLink 2.2 stream. The fix can be obtained from the nightly downloads starting December 21st:

This fix will also eventually be included in the 2.1.3 patch.

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