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
- detect in my bindings file that I'm handling "configuration" node (how ?)
- make the
Configuration
class extend aXmlHolder
class able to hold the XML as a string (with agetDom()
and asetXml(String)
method) - use an
XmlHolderAdapter
adapter class to extract the XML chunk as a String and set it on theConfiguration
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,