1

Here is the description of the configuration element extracted from the Apache Maven 4.0.0 POM model xsd.

<xs:element minOccurs="0" name="configuration">
  <xs:annotation>
    <xs:documentation source="version">0.0.0+</xs:documentation>
    <xs:documentation source="description">
      <p>The configuration as DOM object.</p> <p>By default, every element content is trimmed, but starting with Maven 3.1.0, you can add <code>xml:space="preserve"</code> to elements you want to preserve whitespace.</p> <p>You can control how child POMs inherit configuration from parent POMs by adding <code>combine.children</code> or <code>combine.self</code> attributes to the children of the configuration element:</p> <ul> <li><code>combine.children</code>: available values are <code>merge</code> (default) and <code>append</code>,</li> <li><code>combine.self</code>: available values are <code>merge</code> (default) and <code>override</code>.</li> </ul> <p>See <a href="http://maven.apache.org/pom.html#Plugins">POM Reference documentation</a> and <a href="http://plexus.codehaus.org/plexus-utils/apidocs/org/codehaus/plexus/util/xml/Xpp3DomUtils.html">Xpp3DomUtils</a> for more information.</p>
    </xs:documentation>
  </xs:annotation>
  <xs:complexType>
    <xs:sequence>
      <xs:any minOccurs="0" maxOccurs="unbounded" processContents="skip"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

I use the following bindings file to generate classes from the above xsd:

<jxb:bindings version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
  jxb:extensionBindingPrefixes="xjc" 
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">

  <jxb:globalBindings>
    <!-- use plural method names for repeatable elements -->
    <xjc:simple />
  </jxb:globalBindings>

  <jxb:bindings schemaLocation="maven-4.0.0.xsd">
    <!-- rename all the node of type "any" to "elements" to improve readability -->
    <jxb:bindings multiple="true" node="//xs:any" >
      <jxb:property name="elements"/>
    </jxb:bindings>
  </jxb:bindings>
</jxb:bindings>

This works fine. For instance, it generates a Configuration class that has a getElements() method that returns Element instances.

However, when I encounter a configuration tag, I want the generated Configuration class to be able to hold the XML of all underlying elements as a String (as it was written in the parser XML file, keeping comments, new lines, blank lines and spaces.)

For instance, let's imagine I have the following XML file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>test</artifactId>
  <version>1.0.1-SNAPSHOT</version>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>${plugin.maven-compiler-plugin.version}</version>
          <configuration>
            <source>${build.jdk.source.version}</source>
              <target>${build.jdk.target.version}</target>
            <encoding>${project.build.sourceEncoding}</encoding>
                  <fork>true</fork>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

After having parsed this XML file and having loaded it into my generated model, I would like to be able to call a getXML() method on a Configuration instance and obtain:

<configuration>
  <source>${build.jdk.source.version}</source>
    <target>${build.jdk.target.version}</target>
  <encoding>${project.build.sourceEncoding}</encoding>
        <fork>true</fork>
</configuration>

(wrong indentation is kept as is)

I was thinking about the following approach

  1. detect in my bindings file that I'm handling "configuration" node (how ?)
  2. make the Configuration class extend a XmlHolder class able to hold the XML as a string (with a getDom() and a setXml(String) method)
  3. use an XmlHolderAdapter adapter class to extract the XML chunk as a String and set it on the Configuration class.

My questions:

  • is the approach correct ?
  • is there a better approach ?
  • How can I modify my bindings file, and what should be the content of my adapter class to achieve this (if I'm not wrong I get a XMLStreamReader as a source) ?

Thanks a lot in advance,

Kraal
  • 2,779
  • 1
  • 19
  • 36

1 Answers1

0

I also in the past search answer on similar question and need related XSD part of class in runtime. Eventially I wrote xjc-documentation-annotation-plugin for that.

How to generally use it you may see in that answer: How to make generated classes contain Javadoc from XML Schema documentation

I also prepare example and test for you particular question. Please look at commit for example gradle project.

For full understanding go step-by-step:

  1. In gradle I use gradle-xjc-plugin to invoke XJC (you may use similar maven plugin or invoke it directly):
plugins {
    id 'java'
    id 'org.unbroken-dome.xjc' version '1.4.1' // https://github.com/unbroken-dome/gradle-xjc-plugin
}

dependencies {
    xjcClasspath 'info.hubbitus:xjc-documentation-annotation-plugin:1.0'
    ...
}
// Results by default in `build/xjc/generated-sources`
xjcGenerate {
    /**
     * There:
     * 1. CadastralBlock.xsd is minimal example of functional.
     * 2. maven-4.0.0.xsd - example by SOq https://stackoverflow.com/questions/42223784/how-can-i-generate-a-class-from-which-i-can-retrieve-the-xml-of-a-node-as-a-stri
     */
    source = fileTree('src/main/resources') { include '*.xsd' }
    bindingFiles = fileTree('src/main/jaxb') { include '*.xjb' }
    outputDirectory = file('src/main/generated-java')
    packageLevelAnnotations = false
    targetPackage = 'info.hubbitus.xjc.plugin.example'
    extraArgs = [ '-XPluginDescriptionAnnotation' ]
}
  1. I also add generated classes to source path:
sourceSets.main.java.srcDir new File(buildDir, xjcGenerate.outputDirectory.absolutePath)
// If you use IntellyJ Idea:
idea {
    module {
        // Marks the already(!) added srcDir as "generated"
        generatedSourceDirs += xjcGenerate.outputDirectory
    }
}
  1. Then generate sources:
./gradlew xjcGenerate

Your classes will be in directory src/main/generated-java with @XsdInfo annotation. For your case most interest xsdElementPart element of it.

  1. In runtime you may just query that and use as appropriate (Plugin class contains Configuration as inner class):
    XsdInfo xsdAnnotation = plugin.getClass().getDeclaredAnnotation(XsdInfo.class);
    System.out.println(xsdAnnotation.xsdElementPart());

I prepare test TestGeneratedMavenModel to fully demonstrate it.

Hubbitus
  • 5,161
  • 3
  • 41
  • 47