<project name="Castor XML - code generator" default="compile.codegen" basedir="."
	xmlns:artifact="antlib:org.apache.maven.artifact.ant">

    <property environment="env" />
    <property name="source"               value="${basedir}/src"/>
    <property name="main.java"            value="${source}/main/java"/>
    <property name="main.resources"       value="${source}/main/resources"/>
    <property name="main.generated"       value="${basedir}/target/generated-sources/castor"/>
	<property name="test.java"            value="${source}/test/java"/>
    <property name="test.resources"       value="${source}/test/resources"/>
    <property name="build"                value="${basedir}/build"/>
    <property name="classes"              value="${build}/classes"/>
	<property name="test.classes"         value="${build}/test"/>
    <property name="archive"              value="${project}-${version}${release}-codegen"/>
    <property name="gen.src"              value="${build}/generated-sources"/>

	<artifact:dependencies filesetId="dependency.fileset" useScope="test">
	    <artifact:pom id="core" file="${basedir}/pom.xml" />
    </artifact:dependencies>	

    <path id="build.compile.classpath">
        <pathelement location="${basedir}/build/classes"/>
        <pathelement location="${basedir}/../build/classes"/>
        <pathelement location="${basedir}/../schema/build/classes"/>
        <pathelement location="${basedir}/../anttask/build/classes"/>
        <pathelement location="${basedir}/../core/build/classes"/>
    	
    	<!-- JARs -->
    	<!--
    	<pathelement location="${basedir}/../lib/commons-logging-1.1.jar"/>
    	<pathelement location="${basedir}/../lib/junit_3.8.2.jar"/>
    	<pathelement location="${basedir}/../lib/velocity-1.5.jar"/>
    	-->
    </path>

    <path id="build.test-compile.classpath">
        <path refid="build.compile.classpath"/>
        <pathelement location="${basedir}/build/test"/>
        <pathelement location="${basedir}/../build/test"/>
    </path>

    <path id="test.classpath">
        <pathelement location="${basedir}/build/classes"/>
        <pathelement location="${basedir}/build/test"/>
        <pathelement location="${basedir}/../build/classes"/>
    	<pathelement location="${basedir}/../lib/commons-logging-1.1.jar"/>
    	<pathelement location="${basedir}/../lib/junit_3.8.2.jar"/>
    	<pathelement location="${basedir}/../lib/velocity-1.5.jar"/>
    	<pathelement location="${basedir}/../lib/commons-collections-3.1.jar"/>
      	<pathelement location="${basedir}/../lib/commons-lang-2.1.jar"/>
        <pathelement location="${basedir}/../anttask/build/classes"/>
    	<pathelement location="${basedir}/../lib/junit_3.8.2.jar"/>
    	<pathelement location="${basedir}/../bin/lib/ant-junit-1.6.jar"/>
    </path>

	<target name="derive.codegen.libs">
		<mkdir dir="${basedir}/lib" />  	
	    <copy todir="${basedir}/lib" verbose="true">
	      <fileset refid="dependency.fileset"/>
	      <mapper type="flatten"/>
	    </copy>
	</target>	

    <target name="prepare.codegen" unless="prepare.codegen.executed">
        <mkdir dir="${build}"/>
        <mkdir dir="${classes}"/>
        <property name="prepare.codegen.executed" value="true"/>
    </target>

    <target name="clean.codegen" description="Removes CodeGen build directory">
        <delete dir="${build}"/>
    </target>

    <target name="clean.tests.codegen" description="Removes CodeGen test build directory">
        <delete dir="${test.classes}"/>
    </target>

    <target name="compile.codegen" depends="prepare.codegen, derive.codegen.libs" description="Compile CodeGen">
        <javac srcdir="${main.generated}" destdir="${classes}" 
               debug="on" encoding="ISO-8859-1" deprecation="false"
               source="1.5" target="1.5">
            <classpath refid="build.compile.classpath"/>
        	<classpath>
        		<fileset dir="${basedir}/lib"/>
        	</classpath>
        </javac>
        <javac srcdir="${main.java}" destdir="${classes}" 
               debug="on" encoding="ISO-8859-1" deprecation="false"
        	   source="1.5" target="1.5">
            <classpath refid="build.compile.classpath"/>
        	<classpath>
        		<fileset dir="${basedir}/lib"/>
        	</classpath>
        </javac>
        <copy todir="${classes}">
            <fileset dir="${main.generated}">
                <include name="**/*.cdr"/>
            </fileset>
            <fileset dir="${main.resources}">
                <include name="**"/>
            </fileset>
            <filterset begintoken="$${" endtoken="}">
	            <filter token="VERSION" value="${version}"/>
	            <filter token="RELEASE" value="${release}"/>
            </filterset>
        </copy>
    </target>

    <target name="jar.codegen" depends="compile.codegen" description="Build AntTask JAR file">
        <jar jarfile="${build}/${archive}.jar" basedir="${classes}" includes="**"
             manifest="${classes}/META-INF/MANIFEST.MF"/>
        <copy file="${build}/${archive}.jar" tofile="${basedir}/../dist/${archive}.jar"/>
    </target>

    <target name="compile.bugs.codegen" depends="compile.codegen" 
    	    description="Build the tests packages">
        <mkdir dir="${test.classes}"/>
        <javac srcdir="${test.java}" destdir="${test.classes}"
               debug="on" encoding="ISO-8859-1" deprecation="${deprecation}"
        	   source="1.5" target="1.5">
            <classpath refid="build.test-compile.classpath"/>
        </javac>

        <copy todir="${test.classes}">
            <fileset dir="${test.resources}"/>
        </copy>
    </target>
	
	<target name="tests.codegen" description="Runs
		JUnit tests" depends="compile.bugs.codegen">
		<junit printsummary="true">
			<formatter type="plain" />
			<batchtest>
				<fileset dir="${test.classes}">
					<include name="**/Test*"/>
					<include name="**/*Test"/>
				</fileset>
			</batchtest>
			<classpath refid="test.classpath" />
		</junit>
	</target>
	
    <target name="gen.binding" depends="compile.codegen"
            description="Use the Source Generator to create the Java classes for binding.xsd">
        <delete dir="${gen.src}/binding"/>
        <mkdir dir="${gen.src}/binding"/>
        <taskdef name="castor-srcgen" classname="org.castor.anttask.CastorCodeGenTask"
                 classpathref="build.compile.classpath"/>
        <castor-srcgen file="${main.resources}/org/exolab/castor/builder/binding/binding.xsd"
                       todir="${gen.src}/binding"
                       package="org.exolab.castor.builder.binding.xml"
                       lineseparator="unix"
        			   properties="${classes}/org/exolab/castor/builder/binding.generation.properties"
                       types="j2"
                       warnings="true"/>
        <javac destdir="${classes}" excludes="${excludes}"
               debug="${main.opt.debug}" encoding="ISO-8859-1" deprecation="off"
               optimize="on" source="1.4" target="1.4">
            <src path="${gen.src}/binding" />
            <classpath refid="build.compile.classpath"/>
        </javac>
    </target>

<!--
                       bindingfile="${main.resources}/org/exolab/castor/builder/binding/binding-binding.xml"
-->	

</project>
