I'm currently working on generating Java classes from XSD files. I created an XSD file using restrictions. As example heredown, I want to parse a date in a specific format MMYY. Example:
<xsd:element name="dummyDate" minOccurs="0">
<xsd:simpleType>
<xsd:restriction base="xsd:date">
<xsd:pattern value="MMYY" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
However, the generated class doesn't contain/import the restriction.
protected XMLGregorianCalendar dummyDate;
/**
* Gets the value of the dummyDate property.
*
* @return
* possible object is
* {@link XMLGregorianCalendar }
*
*/
public XMLGregorianCalendar getdummyDate() {
return dummyDate;
}
/**
* Sets the value of the dummyDate property.
*
* @param value
* allowed object is
* {@link XMLGregorianCalendar }
*
*/
public void setdummyDate(XMLGregorianCalendar value) {
this.dummyDate= value;
}
Since I'm blocked with this issue, I'm having the two following questions:
- What would be the best way to proceed in order to enable those restriction while parsing?
- How to implement it?
HINTS:
- JAXB Bindings [https://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.5/tutorial/doc/JAXBUsing4.html]
- krasa-jaxb-tools [https://github.com/krasa/krasa-jaxb-tools]
Update So I created I binding file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
jaxb:extensionBindingPrefixes="xjc">
<jaxb:globalBindings>
<xjc:simple />
<xjc:serializable uid="-1" />
<jaxb:javaType name="java.util.Date" xmlType="xs:date"
parseMethod="general.CalendarFormatConverter.parseDate"
printMethod="general.CalendarFormatConverter.printDate" />
</jaxb:globalBindings>
</jaxb:bindings>
The CalendarFormatConverter.java:
package general;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CalendarFormatConverter {
/**
* Calendar to custom format print to XML.
*
* @param val
* @return
*/
public static Date parseDate(String val) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMYY");
Date date = null;
try {
date = simpleDateFormat.parse(val);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
/**
* Date to custom format print to XML.
*
* @param val
* @return
*/
public static String printDate(Date val) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMYY");
return simpleDateFormat.format(val);
}
}
So I can convert now, but the format of the date isn't matching the restrinction...