Wednesday, June 03, 2009

Working currently on an RelaxNG project, I needed to automate conversion of RNG schemas to a W3C compliant schema in NetBeans. The tool I used to perform the transform is Trang. I added this macro to the build.xml file:

<macrodef name="rng2xsd" description="Conversion from RNG to XSD schemas">
    <attribute name="rng" />
    <attribute name="xsd" />
    <sequential>
        <echo message="Convert RNG schema (trang/oxygen): @{rng}"/>
        <java classname="com.thaiopensource.relaxng.translate.Driver"
               failonerror="true" maxmemory="128m" fork="true">
            <arg value="-I"/>
            <arg value="rng"/>
            <arg value="-O"/>
            <arg value="XSD"/>
            <arg value="@{rng}"/>
            <arg value="@{xsd}"/>
            <classpath>
                <pathelement location="resources/tools/trang-20081028.jar"/>
            </classpath>
        </java>
    </sequential>
</macrodef>

All necessary libraries reside in the ./resources/tools directory. Now, in order to use this macro on a number of RNG files, I decided to use the <for> directive from ant-contrib. James Allen has good instructions on how to integrate ant-contrib within NetBeans (or arbitrary ant environments) without having to drop the ant-contrib Jar into the ant/NetBeans installation.

<target name="convertRng2Xsd">
    <echo message="Converting RNG Schemas..."/>
    <mkdir dir="${xsd-schemas}"/>
    <for list="${rng-files}" param="file">
        <sequential>
            <rng2xsd rng="${rng-schemas}/@{file}.rng" xsd="${xsd-schemas}/@{file}.xsd" />
        </sequential>
    </for>
</target>
Here I am iterating over the ${rng-files} property that contains a comma delimited list of the RNG files you want to convert (without the .rng extension). I filled this through <pathconvert>:
<pathconvert property="rng-files" pathsep=",">
    <mapper>
        <chainedmapper>
            <flattenmapper />
            <globmapper from="*.rng" to="*" />
        </chainedmapper>
    </mapper>
    <path>
        <fileset dir="resources/schemas" includes="*.rng" />
    </path>
</pathconvert>

Obviously, these XSDs can then be used with any other tools, such as JAXB.

Wednesday, June 03, 2009 2:00:09 PM (Eastern Standard Time, UTC-05:00)  #    Comments [3]  | 

Copyright by Gerald Beuchelt.