1

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:

  1. JAXB Bindings [https://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.5/tutorial/doc/JAXBUsing4.html]
  2. 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...

Greg86
  • 67
  • 1
  • 2
  • 15
  • You can validate against the schema during marshalling and unmarshalling, which will also check against the restrictions in the XSD. – Jesper Nov 22 '17 at 14:52
  • @Jesper: Indeed, but the plan would be to handle it over to someone less technical, this person would only execute a one line command... So the less there is to handover the better it is... – Greg86 Nov 22 '17 at 15:03
  • Have you looked into jaxb global bindings yet? Maybe sth. like this would work: https://stackoverflow.com/questions/3679266/simple-conversion-between-java-util-date-and-xmlgregoriancalendar – giro Nov 23 '17 at 11:22

0 Answers0