0 Replies Latest reply: Nov 27, 2012 7:54 PM by jmsjr RSS

    wsimport, mapping of xs:date to java.util.Date via ext file, and -B option

    jmsjr
      Summary:

      JDK 1.7.0_09 and wsimport and xjc that comes with it.
      Global JAXB binding to map xs:date to java.util.Date

      I have the following external bindings file:
      <?xml version="1.0" encoding="UTF-8"?>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
           elementFormDefault="qualified" attributeFormDefault="unqualified"
           jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.1">
           <xs:annotation>
                <xs:appinfo>
                     <jaxb:globalBindings>
                          <xjc:serializable />
                          <jaxb:javaType name="java.util.Date" xmlType="xs:date" parseMethod="au.com.xxx.jaxb.DateAdapter.parseDate" printMethod="au.com.xxx.jaxb.DateAdapter.printDate" />
                     </jaxb:globalBindings>
                </xs:appinfo>
           </xs:annotation>
      </xs:schema>
      The au.com.xxx.jaxb.DateAdapter code is as follows:
      package au.com.xxx.jaxb;
      
      import java.util.Calendar;
      import java.util.Date;
      import java.util.GregorianCalendar;
      
      import javax.xml.bind.DatatypeConverter;
      
      public class DateAdapter {
           
           public static Date parseDate(String s) {
                return DatatypeConverter.parseDate(s).getTime();
           }
        
           public static String printDate(Date dt) {
                Calendar cal = new GregorianCalendar();
                cal.setTime(dt);
                return DatatypeConverter.printDate(cal);
           }
      }
      When I run the following wsimport from the command line, I get:
      salvojo@AUD20901BL /cygdrive/c/workspace/JSF/insurance
      $ /cygdrive/c/java/jdk1.7.0_09/x64/bin/wsimport -keep -s gen-src -b external/wsdl/jaxb-bindings.xml -wsdllocation /wsdl/Member.wsdl -d WebContent/WEB-INF/classes external/wsdl/Member.wsdl
      parsing WSDL...
      
      
      Generating code...
      
      
      Compiling code...
      
      C:\workspace\JSF\insurance\gen-src\org\w3\_2001\xmlschema\Adapter1.java:13: error: package au.com.xxx.jaxb does not exist
              return (au.com.xxx.jaxb.DateAdapter.parseDate(value));
                                     ^
      C:\workspace\JSF\insurance\gen-src\org\w3\_2001\xmlschema\Adapter1.java:17: error: package au.com.xxx.jaxb does not exist
              return (au.com.xxx.jaxb.DateAdapter.printDate(value));
                                     ^
      2 errors
      compilation failed, errors should have been reported
      Which means that wsimport or xjc needs to know the classpath to find au.com.xxx.jaxb.DateAdapter.
      But how do I pass the classpath from wsimport to the JAXB compiler ?

      There is the -B option in wsimport, but I could not get it to work.
      If I read it correctly, I should be able to pass the -classpath option to the JAXB compiler from wsimport via -B.
      I tried:
      salvojo@AUD20901BL /cygdrive/c/workspace/JSF/insurance
      $ /cygdrive/c/java/jdk1.7.0_09/x64/bin/wsimport -keep -s gen-src -B"-classpath WebContent/WEB-INF/classes" -b external/wsdl/jaxb-bindings.xml -wsdllocation /wsdl/Member.wsdl -d WebContent/WEB-INF/classes external/wsdl/Member.wsdl
      no such JAXB option: -classpath WebContent/WEB-INF/classes
      
      
      Usage: wsimport [options] <WSDL_URI>
      
      where [options] include:
        -b <path>                 specify jaxws/jaxb binding files or additional schemas
                                  (Each <path> must have its own -b)
        -B<jaxbOption>            Pass this option to JAXB schema compiler
        -catalog <file>           specify catalog file to resolve external entity references
                                  supports TR9401, XCatalog, and OASIS XML Catalog format.
        -d <directory>            specify where to place generated output files
      
      <...snipped...>
      ... where WebContent/WEB-INF/classes is the classpath where au.com.xxx.jaxb.DateAdapter.class could be found. Obviously it did not like it.

      Also, why is wsimport generate org.w3._2001.xmlschema.Adapter1.java ? All it is doing is wrapping up the exact same call that I have specified in my DateAdapter. How can I tell wsimport or xjc to NOT create that extra Adapter1.java and simply directly use my DateAdapter ??