JAXB-将代码注入simpleType

JAXB-将代码注入simpleType,jaxb,maven-3,xjc,Jaxb,Maven 3,Xjc,我试图找出如何将代码注入到一个xsd simpleType中,它是一个Enumeration 在commons.XSD中,我的XSD如下所示 <xsd:simpleType name="tPessoa"> <xsd:annotation> <xsd:documentation>Enumeracao que descreve se e uma pessoa fisica ou juridica

我试图找出如何将代码注入到一个xsd simpleType中,它是一个Enumeration

在commons.XSD中,我的XSD如下所示

<xsd:simpleType name="tPessoa">
    <xsd:annotation>
        <xsd:documentation>Enumeracao que descreve se e uma pessoa fisica ou
            juridica
            Valores possiveis:
            - pf: para PESSOA FiSICA;
            - pj: para PESSOA
            JURiDICA.
        </xsd:documentation>
    </xsd:annotation>
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="pf" />
        <xsd:enumeration value="pj" />
    </xsd:restriction>
</xsd:simpleType>
因为F和PF是同义词,一些底层系统为同一实体返回不同的值,所以其中一些系统为F和其他系统返回PF,为J和PJ返回相同的行为


怎么做?有可能吗?

我从来没有在JAXB中将代码注入到枚举中。我只能将代码注入complexTypes

下面您可以找到另一个示例,它将问题中的
fromValue
方法注入到复杂类型中,可能也很有用

专家 * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name=“pessoaType”,proporter={ “类型” }) 公共类PessoaType{ @XmlElement(必需=true) @XmlSchemaType(name=“string”) 保护型TPessoa; /** *获取类型属性的值。 * *@返回 *可能的对象是 *{@link TPessoa} * */ 公共TPessoa getType(){ 返回类型; } /** *设置类型属性的值。 * *@param值 *允许的对象是 *{@link TPessoa} * */ 公共void集合类型(TPessoa值){ this.type=值; } 公共静态TPessoa fromValue(字符串v){ 如果(((“F”.equalsIgnoreCase(v))| |(“PF”.equalsIgnoreCase(v)))返回TPessoa.PF; 如果((((“J.equalsIgnoreCase(v))| |((“PJ.equalsIgnoreCase(v)))返回TPessoa.PJ; 抛出新的IllegalArgumentException(v); } }
这不是100%您想要的,但它非常接近。

我从未设法将代码注入到JAXB中的枚举中。我只能将代码注入complexTypes

下面您可以找到另一个示例,它将问题中的
fromValue
方法注入到复杂类型中,可能也很有用

专家 * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name=“pessoaType”,proporter={ “类型” }) 公共类PessoaType{ @XmlElement(必需=true) @XmlSchemaType(name=“string”) 保护型TPessoa; /** *获取类型属性的值。 * *@返回 *可能的对象是 *{@link TPessoa} * */ 公共TPessoa getType(){ 返回类型; } /** *设置类型属性的值。 * *@param值 *允许的对象是 *{@link TPessoa} * */ 公共void集合类型(TPessoa值){ this.type=值; } 公共静态TPessoa fromValue(字符串v){ 如果(((“F”.equalsIgnoreCase(v))| |(“PF”.equalsIgnoreCase(v)))返回TPessoa.PF; 如果((((“J.equalsIgnoreCase(v))| |((“PJ.equalsIgnoreCase(v)))返回TPessoa.PJ; 抛出新的IllegalArgumentException(v); } } 这不是你想要的100%,但它非常接近

<jxb:bindings schemaLocation="../src/main/resources/commons.xsd"
    node="//xsd:schema">
    <jxb:schemaBindings>
        <jxb:package name="my.canonical.commons" />
    </jxb:schemaBindings>

    <jxb:bindings node="/xsd:schema/xsd:simpleType[@name='tPessoa']">
        <ci:code>
            public static TPessoa fromValue(String v) {
            if(("F".equalsIgnoreCase(v)) || ("PF".equalsIgnoreCase(v))) return PF;
            if(("J".equalsIgnoreCase(v)) || ("PJ".equalsIgnoreCase(v))) return PJ;
            throw new IllegalArgumentException(v);
            }
        </ci:code>
    </jxb:bindings>
</jxb:bindings>
@XmlType(name = "tPessoa")
@XmlEnum
public enum TPessoa {

    @XmlEnumValue("pf")
    PF("pf"),
    @XmlEnumValue("pj")
    PJ("pj");
    private final String value;

    TPessoa(String v) {
        value = v;
    }

    public String value() {
        return value;
    }

    public static TPessoa fromValue(String v) {
        if(("F".equalsIgnoreCase(v)) || ("PF".equalsIgnoreCase(v))) return PF;
        if(("J".equalsIgnoreCase(v)) || ("PJ".equalsIgnoreCase(v))) return PJ;
        throw new IllegalArgumentException(v);
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.fernandes</groupId>
    <artifactId>jaxb-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <extension>true</extension>
                    <schemaDirectory>${basedir}/src/main/xsd</schemaDirectory>
                    <bindingDirectory>${basedir}/src/main/xjb</bindingDirectory>
                    <generatePackage>com.fernandes.jaxb</generatePackage>
                    <args>
                        <arg>-Xinject-code</arg>
                    </args>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>1.11.1</version>
                        </plugin>
                    </plugins>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://xmlns.tui.com/int/customer360/cdm/customer/v1"
            targetNamespace="http://xmlns.tui.com/int/customer360/cdm/customer/v1"
            elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:complexType name="pessoaType">
        <xsd:sequence>
            <xsd:element name="type" type="tPessoa"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:simpleType name="tPessoa">
        <xsd:annotation>
            <xsd:documentation>Enumeracao que descreve se e uma pessoa fisica ou
                juridica
                Valores possiveis:
                - pf: para PESSOA FiSICA;
                - pj: para PESSOA
                JURiDICA.
            </xsd:documentation>
        </xsd:annotation>
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="pf"/>
            <xsd:enumeration value="pj"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
              xmlns:ci="http://jaxb.dev.java.net/plugin/code-injector">
<jxb:bindings schemaLocation="../xsd/schema.xsd">
    <jxb:globalBindings>
        <xjc:simple/>
    </jxb:globalBindings>
    <jxb:bindings node="/xsd:schema/xsd:complexType[@name='pessoaType']">
        <ci:code>
            <![CDATA[
    public static TPessoa fromValue(String v) {
        if(("F".equalsIgnoreCase(v)) || ("PF".equalsIgnoreCase(v))) return TPessoa.PF;
        if(("J".equalsIgnoreCase(v)) || ("PJ".equalsIgnoreCase(v))) return TPessoa.PJ;
        throw new IllegalArgumentException(v);
    }
                ]]>
        </ci:code>
    </jxb:bindings>
</jxb:bindings>
</jxb:bindings>
package com.fernandes.jaxb;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for pessoaType complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="pessoaType"&gt;
 *   &lt;complexContent&gt;
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
 *       &lt;sequence&gt;
 *         &lt;element name="type" type="{http://xmlns.tui.com/int/customer360/cdm/customer/v1}tPessoa"/&gt;
 *       &lt;/sequence&gt;
 *     &lt;/restriction&gt;
 *   &lt;/complexContent&gt;
 * &lt;/complexType&gt;
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "pessoaType", propOrder = {
    "type"
})
public class PessoaType {

    @XmlElement(required = true)
    @XmlSchemaType(name = "string")
    protected TPessoa type;

    /**
     * Gets the value of the type property.
     * 
     * @return
     *     possible object is
     *     {@link TPessoa }
     *     
     */
    public TPessoa getType() {
        return type;
    }

    /**
     * Sets the value of the type property.
     * 
     * @param value
     *     allowed object is
     *     {@link TPessoa }
     *     
     */
    public void setType(TPessoa value) {
        this.type = value;
    }


    public static TPessoa fromValue(String v) {
        if(("F".equalsIgnoreCase(v)) || ("PF".equalsIgnoreCase(v))) return TPessoa.PF;
        if(("J".equalsIgnoreCase(v)) || ("PJ".equalsIgnoreCase(v))) return TPessoa.PJ;
        throw new IllegalArgumentException(v);
    } 
}