我试图找出如何将代码注入xsd simpleType,这是一个枚举。
我的XSD就像下面的commons.xsd
Text
我的绑定就像下面的
<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>
我在maven插件中传递了参数-Xinject-code-extension
但观察到的结果并没有改变。 我在StackOverflow中阅读了一些文章,但是我不清楚这是否是JAXB的限制或者我错过了什么。
我的想法是生成一个类,如下所示
<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>
因为F和PF是同义词而某些底层系统为同一个实体返回不同的值,所以其中一些返回F其他PF以及J和PJ的相同行为。
怎么做?可能吗?
答案 0 :(得分:0)
我从未设法将代码注入到JAXB中的枚举中。我只能将代码注入complexTypes。
下面你可以找到一个替代的例子,它将你问题中的fromValue
方法注入一个复杂的类型,也可能很有用。
<?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>
如果您使用:
运行mavenmvn clean package
您将生成TPessoa
课程和PessoaType
。后者将包含您指定的静态方法。
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>
* <complexType name="pessoaType">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="type" type="{http://xmlns.tui.com/int/customer360/cdm/customer/v1}tPessoa"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </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);
}
}
这不是你想要的100%,但它非常接近。