<?xml version="1.0"?>

<!-- ===================================================================

Ant build file for JiBX data binding schema to code starter example.

=================================================================== -->

<project basedir="." default="help">
  
  <!-- The following block is intended to set the jibx-home location. It first
   checks the relative location of the JiBX libraries when this starter example
   is run directly from the JiBX distribution, then (if that fails), looks for
   an environmental variable JIBX_HOME with the installation path. If you prefer
   to just set the path directly in this file, uncomment the following line and
   set the value to the appropriate directory, then delete the rest of the Ant
   commands down to the end of this block. -->
<!--  <property name="jibx-home" value="jibx-home-directory-path"/> -->
  <available file="../../lib/jibx-bind.jar" property="jibx-home" value="../.."/>
  <property environment="env"/>
  <condition property="jibx-home" value="${env.JIBX_HOME}">
    <and>
      <not>
        <isset property="jibx-home"/>
      </not>
      <available file="${env.JIBX_HOME}/lib"/>
    </and>
  </condition>
  <!-- End of jibx-home location setting block. -->
  
  <!-- make sure required jars are present -->
  <condition property="runtime-jars-found">
    <available file="${jibx-home}/lib/jibx-run.jar"/>
  </condition>
  <condition property="binding-jars-found">
    <and>
      <available file="${jibx-home}/lib/bcel.jar"/>
      <available file="${jibx-home}/lib/jibx-bind.jar"/>
      <available file="${jibx-home}/lib/jibx-run.jar"/>
    </and>
  </condition>
  <available property="extras-jar-found" file="${jibx-home}/lib/jibx-extras.jar"/>
  
  <!-- set classpath for compiling and running application with JiBX -->
  <path id="classpath">
    <pathelement location="."/>
    <fileset dir="${jibx-home}/lib" includes="*.jar"/>
    <pathelement location="bin"/>
  </path>
  
  <!-- make sure runtime jars are present -->
  <target name="check-runtime">
    <fail unless="jibx-home">JiBX home directory not found - define JIBX_HOME system property or set path directly in build.xml file.</fail>
    <fail unless="runtime-jars-found">Required JiBX runtime jar jibx-run.jar was not found in JiBX home lib directory (${jibx-home}/lib)</fail>
  </target>
  
  <!-- make sure extras jars are present -->
  <target name="check-extras" depends="check-runtime">
    <fail unless="extras-jar-found">Required JiBX extras jar jibx-extras.jar was not found in JiBX home lib directory (${jibx-home}/lib)</fail>
  </target>
  
  <!-- make sure binding jars are present -->
  <target name="check-binding" depends="check-runtime">
    <fail unless="binding-jars-found">Required JiBX binding jar jibx-bind.jar or bcel.jar was not found in JiBX home lib directory (${jibx-home}/lib)</fail>
  </target>
  
  <!-- clean compiled class files and generated code+binding -->
  <target name="clean">
    <delete quiet="true" dir="${basedir}/bin"/>
    <delete quiet="true" dir="${basedir}/gen"/>
  </target>
  
  <!-- generate using default settings -->
  <target name="codegen" depends="check-runtime,clean">
    
    <echo message="Running code generation from schema"/>
    <java classname="org.jibx.schema.codegen.CodeGen" fork="yes"
        classpathref="classpath" failonerror="true">
      <arg value="-t"/>
      <arg value="${basedir}/gen/src"/>
      <arg value="otasubset/OTA_AirLowFareSearch*.xsd"/>
    </java>
    
  </target>
  
  <!-- generate using first customizations -->
  <target name="custgen1" depends="check-runtime,clean">
    
    <echo message="Running code generation from schema"/>
    <java classname="org.jibx.schema.codegen.CodeGen" fork="yes"
        classpathref="classpath" failonerror="true">
      <arg value="-t"/>
      <arg value="${basedir}/gen/src"/>
      <arg value="-c"/>
      <arg value="${basedir}/custom1.xml"/>
      <arg value="otasubset/OTA_AirLowFareSearch*.xsd"/>
    </java>
    
  </target>
  
  <!-- generate using second customizations -->
  <target name="custgen2" depends="check-runtime,clean">
    
    <echo message="Running code generation from schema"/>
    <java classname="org.jibx.schema.codegen.CodeGen" fork="yes"
        classpathref="classpath" failonerror="true">
      <arg value="-t"/>
      <arg value="${basedir}/gen/src"/>
      <arg value="-c"/>
      <arg value="${basedir}/custom2.xml"/>
      <arg value="otasubset/OTA_AirLowFareSearch*.xsd"/>
    </java>
    
  </target>
  
  <!-- compile the classes -->
  <target name="compile" depends="check-runtime">
  
    <echo message="Compiling Java source code"/>
    <mkdir dir="${basedir}/bin"/>
    <javac srcdir="${basedir}/gen/src" destdir="${basedir}/bin" debug="on">
      <classpath refid="classpath"/>
    </javac>
    <javac srcdir="${basedir}/src" destdir="${basedir}/bin" debug="on">
      <classpath refid="classpath"/>
    </javac>
    
  </target>  
  
  <!-- bind as a separate step -->
  <target name="bind" depends="check-binding">
    
    <echo message="Running JiBX binding compiler"/>
    <taskdef name="bind" classname="org.jibx.binding.ant.CompileTask">
      <classpath>
        <fileset dir="${jibx-home}/lib" includes="*.jar"/>
      </classpath>
    </taskdef>
    <bind binding="${basedir}/gen/src/binding.xml">
      <classpath refid="classpath"/>
    </bind>
    
  </target>
  
  <!-- run the included test program to read and then write to separate file -->
  <target name="run" depends="check-runtime">
    <echo message="Running the sample application..."/>
    <java classname="org.jibx.starter.Test" fork="true" failonerror="true">
      <classpath refid="classpath"/>
      <arg value="org.opentravel.ota"/>
      <arg value="${basedir}/samples"/>
    </java>
  </target>
  
  <!-- run the included test program to read and then write to separate file -->
  <target name="runcust" depends="check-runtime">
    <echo message="Running the sample application..."/>
    <java classname="org.jibx.starter.Test" fork="true" failonerror="true">
      <classpath refid="classpath"/>
      <arg value="org.ota.air"/>
      <arg value="${basedir}/samples"/>
    </java>
  </target>
  
  <!-- compile, generate, compile binding, run test -->
  <target name="full" depends="codegen,compile,bind,run"/>
  <target name="custom1" depends="custgen1,compile,bind,runcust"/>
  <target name="custom2" depends="custgen2,compile,bind,runcust"/>
  
  <!-- show list of targets -->
  <target name="help">
    <echo message="Targets are:"/>
    <echo/>
    <echo message="clean         delete all class files and generated code"/>
    <echo message="codegen       generate default code and binding"/>
    <echo message="custgen1      generate code and binding using custom1.xml"/>
    <echo message="custgen2      generate code and binding using custom2.xml"/>
    <echo message="compile       compile class files"/>
    <echo message="bind          compile JiBX bindings"/>
    <echo message="run           run test application with default code package"/>
    <echo message="runcust       run test application with custom code package"/>
    <echo message="full          generate default, compile, bind, and test"/>
    <echo message="custom1       generate using custom1.xml, compile, bind, and test"/>
    <echo message="custom2       generate using custom2.xml, compile, bind, and test"/>
  </target>

</project>
