Search code examples
javajaxbjaxb2maven-jaxb2-plugin

Generate code from xsd, replace/remove suffix in name of resulting Classes


so I've got XSDs (out of my control) from which I generate java sources using the maven jaxb2 plugin (version 0.13.0) where each complex type has the suffix "structure", e.g. a "Connection" element is of the complex type "ConnectionStructure". I'd really like to remove these from the code, so the resulting class is not called ConnectionStructure, but "Connection".

I have read here which details how to rename a single class.

Accordingly, my code is

<bindings schemaLocation="MyXsd.xsd">
    <schemaBindings>
        <package name="mypackage.generated.a" />
    </schemaBindings>
  <bindings node="//xsd:complexType[@name='*ConnectionStructure']">
    <class name="Connection"/>
  </bindings>
<bindings

but I'd like to not just remove the suffix from ConnectionStructure, but from every class that ends in structure.I haven't quite found how I could achieve this with wild cards or anything like that.

Any help would be much appreciated!


Solution

  • As far as I know there is no bindings which would work this way. There are bindings to add prefixes and suffixes to the generated names, but not to remove them. :)

    I see a couple of options to solve it:

    • As VGR suggested, you could write an XSLT which generates a binding file from the schema. This might be quite easy if the schema is straightforward. But it can get really messy if the schema is more complex (imports, inner types etc.). I wrote a lot of XSLT for schema processing, it can be quite hard.
    • Write an XJC plugin which would do the renaming of the model classes. This may be quite hard, XJC internals are very complex. (I would do this, but I have relatively good experience writing XJC plugins.)
    • Use some automatic refactoring tool in the build process to rename a bunch of files. Something like JRefactory.
    • Or simply rename appropriate files and replace corresponding class names in other files.

    Update:

    I got another idea:

    XJC can also generate so-called episode file which is essentially a bindings file that contains bindings for all of the types of your schema. So instead of applying XSLT to the schema you could write and apply an XSLT onto the generated bindings file - and use it for the next generation. To give you an example, the episode file looks as follows:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <bindings xmlns="http://java.sun.com/xml/ns/jaxb" if-exists="true" version="2.1">
      <bindings xmlns:tns="http://www.opengis.net/wms" if-exists="true" scd="x-schema::tns">
        <schemaBindings map="false">
          <package name="net.opengis.wms.v_1_3_0"/>
        </schemaBindings>
        <bindings if-exists="true" scd="tns:WMS_Capabilities">
          <class ref="net.opengis.wms.v_1_3_0.WMSCapabilities"/>
        </bindings>
        <bindings if-exists="true" scd="tns:Service">
          <class ref="net.opengis.wms.v_1_3_0.Service"/>
        </bindings>
        <bindings if-exists="true" scd="tns:KeywordList">
          <class ref="net.opengis.wms.v_1_3_0.KeywordList"/>
        </bindings>
        <bindings if-exists="true" scd="tns:Keyword">
          <class ref="net.opengis.wms.v_1_3_0.Keyword"/>
        </bindings>
        <!-- ... -->
      </bindings>
    </bindings>
    

    So it is pretty trivial to write an XSLT which would generate the bindings file you need from this. It is definitley much easier than writing an XSLT for the schema.