简体   繁体   English

使用Maven xjc进行编译时,JAXB不会为xsd电子邮件模式元素创建类

[英]JAXB is not creating class for xsd email pattern element when compile using maven xjc

I am using xsd in which i am validation email like 我正在使用xsd,其中我是验证电子邮件

<xsd:element name="Company" type="Company"/>  
<xsd:complexType name="Company">  
    <xsd:sequence>  
        <xsd:element name="Name" type="xsd:string" minOccurs="1" maxOccurs="1" />
        <xsd:element name="Website" type="xsd:string" minOccurs="0" maxOccurs="1" />
        <xsd:element name="Email" type="EmailAddress" minOccurs="0" maxOccurs="1" />
    </xsd:sequence>
    <xsd:attribute name="AccountStatus" type="AccountStatus" use="optional" default="Active" />
</xsd:complexType>

<xsd:simpleType name="EmailAddress">
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="[A-Za-z0-9_]+([-+.'][A-Za-z0-9_]+)*@[A-Za-z0-9_]+([-.][A-Za-z0-9_]+)*\.[A-Za-z0-9_]+([-.][A-Za-z0-9_]+)*"/>
    </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="AccountStatus">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="Active"/>
        <xsd:enumeration value="Inactive"/>
    </xsd:restriction>
</xsd:simpleType>

Here is my pom.xml file that is using xjc snippet 这是我使用xjc片段的pom.xml文件

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>1.5</version>
    <executions>
        <execution>
            <goals>
                <goal>xjc</goal>
            </goals>
            <phase>generate-sources</phase>
        </execution>
    </executions>
    <configuration>
        <clearOutputDir>false</clearOutputDir>
        <outputDirectory>src/main/java</outputDirectory>
        <schemaDirectory>src/main/webapp/schemas</schemaDirectory>
        <includeSchema>**/*.xsd</includeSchema>
        <bindingDirectory>src/main/resources/bindings</bindingDirectory>
        <bindingFiles>binding.xml</bindingFiles>
        <enableIntrospection>false</enableIntrospection>
    </configuration>
</plugin>

here is my binding.xml file 这是我的binding.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<jaxb:bindings>
    <jaxb:globalBindings generateElementProperty="false" />
</jaxb:bindings>

Here is the generated Company.java class 这是生成的Company.java类

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Company", propOrder = {
    "name",
    "website",
    "email"
})
public class Company {

    @XmlElement(name = "Name", required = true)
    protected String name;
    @XmlElement(name = "Website")
    protected String website;
    @XmlElement(name = "Email")
    protected String email;
    @XmlAttribute(name = "AccountStatus")
    protected AccountStatus accountStatus;

    //setter and getters

}

See there is no email pattern applying to my email attrubute. 看到没有适用于我的电子邮件属性的电子邮件模式。 Why ? 为什么呢

when i compile my code using maven compile then there is no EmailAddress.java class that apply pattern to my email attribute in Company.java. 当我使用Maven编译我的代码时,没有EmailAddress.java类将模式应用于Company.java中的我的电子邮件属性。 AccountStatus is also a simple type but maven creates AccountStatus.java class. AccountStatus也是一个简单的类型,但是maven创建了AccountStatus.java类。 Why maven is not creating EmailAddress.java class when i compile using maven ? 当我使用Maven进行编译时,为什么Maven没有创建EmailAddress.java类? is there something wrong with my pattern ? 我的图案有问题吗?

Thanks 谢谢

AccountStatus is generated because the corresponding schema type is an enum. 因为对应的架构类型是枚举,所以生成了AccountStatus For other simple types (ie ones with patterns) a class will not be generated. 对于其他简单类型(即带有模式的类型),将不会生成类。 There are a couple of reasons for not generating a class for the EmailAddress type: 不为EmailAddress类型生成类的原因有两个:

  1. It makes the class model unnecessarily large. 这会使类模型不必要地变大。
  2. JAXB doesn't validate content down to that level, so having an extra generated class provides no value. JAXB不会验证该级别的内容,因此拥有额外的生成类不会提供任何价值。
  3. That value can be validated by setting an instance of Schema on the Marshaller / Unmarshaller (see: http://blog.bdoughan.com/2010/12/jaxb-and-marshalunmarshal-schema.html ). 可以通过在Marshaller / Unmarshaller上设置Schema实例来验证该值(请参阅: http : //blog.bdoughan.com/2010/12/jaxb-and-marshalunmarshal-schema.html )。

Instead the property will be based on the base schema type. 而是,该属性将基于基本架构类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM