I am new to the xsd stuff. I want to convert a directory of xsd files to java with @Pattern
annotations, I am working with a lot of dynamic data. A lot of elements contain patterns, which defines what to expect as input for elements. The patterns are found in following structure in combination with elements, here a fragment of one of the xsd files:
<xs:element name="preparationDateAndTime" minOccurs="1" maxOccurs="1">
<xs:annotation>
<xs:documentation>
<aie:name xml:lang="en" value="Preparation date and time"/>
<aie:name xml:lang="de" value="Zeitpunkt der Erstellung"/>
<aie:description value="Datum und Zeit der Erstellung der Übertragungsdatei"/>
<aie:status value="R"/>
<aie:format value="DateTime (n14)"/>
<aie:pcre value="A(?!....-(?:02|04|06|09|11)-31|....-02-30|..(?:.[13579]|[02468][26]|[13579][048])-02-29)^(?:20[0-9][0-9])-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])T(?:[01][0-9]|2[0-3]):(?:[0-5][0-9]):(?:[0-5][0-9])Z"/>
</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:dateTime">
<xs:minInclusive value="2000-01-01T00:00:00"/>
<xs:maxInclusive value="2099-12-31T23:59:59"/>
<xs:pattern value="[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="testIndicator" minOccurs="0" maxOccurs="0">
<xs:annotation>
<xs:documentation>
<aie:name xml:lang="en" value="Test indicator"/>
<aie:name xml:lang="de" value="Test-Indikator"/>
<aie:status value="N"/>
<aie:format value="n1"/>
<aie:pcre value="A1Z"/>
</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="1"/>
<xs:enumeration value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="messageIdentification" minOccurs="1" maxOccurs="1">
<xs:annotation>
<xs:documentation>
<aie:name xml:lang="en" value="Message identification"/>
<aie:name xml:lang="de" value="Nachrichtenkennung"/>
<aie:description value="Eindeutige Nachrichtenreferenz"/>
<aie:status value="R"/>
<aie:format value="an..35"/>
<aie:pcre value="A.{1,35}Z"/>
</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="alpha">
<xs:minLength value="1"/>
<xs:maxLength value="35"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
I did a lot of research without finding what I need. This is the script that I am using in a Spring Boot project using following library in the build.gradle.kts
file:
plugins {
id("de.qaware.gradle.plugin.xsd2java") version "3.0.0"
// more plugins
}
dependencies {
implementation("javax.xml.bind:jaxb-api:2.3.1")
implementation("org.glassfish.jaxb:jaxb-runtime:2.3.2")
implementation("javax.annotation:javax.annotation-api:1.3.2")
}
xsd2java {
schemas {
create("generate$taskName") {
val packageNameString = "com.example.xsdToJavaProj.backend.generate.${dir.name}"
schemaDirPath = dir.toPath() // directory with xsd files
this.packageName = packageName
}
}
extension = true
arguments = listOf("-verbose", "-XautoNameResolution")
outputDir = file("${project.buildDir}/generated-sources/xsd2java")
}
After executing the script, I get the generated java, in the comments I see the pattern and everything but the java implementation does not contain any further annotations:
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="identificationNumber">
* <simpleType>
* <restriction base="{}alpha">
* <length value="5"/>
* <pattern value="[1-9][0-9]{4}"/>
* </restriction>
* </simpleType>
* </element>
* <element name="note">
* <simpleType>
* <restriction base="{http://www.w3.org/2001/XMLSchema}string">
* <length value="2"/>
* <enumeration value="TB"/>
* <enumeration value="TF"/>
* <enumeration value="TS"/>
* </restriction>
* </simpleType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"identificationNumber",
"note"
})
public static class Scenario {
@XmlElement(required = true)
protected String identificationNumber;
@XmlElement(required = true)
protected String note;
/**
* Gets the value of the identificationNumber property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getIdentificationNumber() {
return identificationNumber;
}
/**
* Sets the value of the identificationNumber property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setIdentificationNumber(String value) {
this.identificationNumber = value;
}
/**
* Gets the value of the note property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getNote() {
return note;
}
/**
* Sets the value of the note property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setNote(String value) {
this.note = value;
}
}
How can I generate the @Pattern
annotations as well for java? I tried the approach with adding -Xannotate
to the arguments but it always threw an -Xannotate unknown property
error, and I tried creating a bindings.xjb
file, which also did not really work as expected.
The expected results would be something like that:
public static class Scenario {
@XmlElement(required = true)
@Pattern("[1-9][0-9]{4}")
protected String identificationNumber;
@XmlElement(required = true)
@Pattern("[1-9][0-9]{4}")
protected String note;
// getters and setters
}