<?xml version="1.0"?>
<!--

    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

    Copyright (c) 1997-2011 Oracle and/or its affiliates. All rights reserved.

    The contents of this file are subject to the terms of either the GNU
    General Public License Version 2 only ("GPL") or the Common Development
    and Distribution License("CDDL") (collectively, the "License").  You
    may not use this file except in compliance with the License.  You can
    obtain a copy of the License at
    https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
    or packager/legal/LICENSE.txt.  See the License for the specific
    language governing permissions and limitations under the License.

    When distributing the software, include this License Header Notice in each
    file and include the License file at packager/legal/LICENSE.txt.

    GPL Classpath Exception:
    Oracle designates this particular file as subject to the "Classpath"
    exception as provided by Oracle in the GPL Version 2 section of the License
    file that accompanied this code.

    Modifications:
    If applicable, add the following below the License Header, with the fields
    enclosed by brackets [] replaced by your own identifying information:
    "Portions Copyright [year] [name of copyright owner]"

    Contributor(s):
    If you wish your version of this file to be governed by only the CDDL or
    only the GPL Version 2, indicate your decision by adding "[Contributor]
    elects to include this software in this distribution under the [CDDL or GPL
    Version 2] license."  If you don't indicate a single choice of license, a
    recipient has the option to distribute your version of this file under
    either the CDDL, the GPL Version 2 or to extend the choice of license to
    its licensees as provided above.  However, if you add GPL Version 2 code
    and therefore, elected the GPL Version 2 license, then the option applies
    only if the new code is made subject to such option by the copyright
    holder.

-->

<project name="xjc" basedir=".." default="compile">

  <description>
    XJC Sub-Project
    
    The xjc project contains all of the source code necessary to process
    schemas and binding customization files into java sources.
  </description>
  
    <!-- import build properties -->
    <property file="${basedir}/build.properties" />

    <!-- libraries that are referenced when compiling Java source files -->
    <path id="javac.classpath">
      <pathelement path="${runtime.classes}" />
      <pathelement path="${schemagen.classes}" />
      <fileset dir="${jaxb.libs.redist}" includes="*.jar"/>
      <fileset dir="${jaxb.libs.rebundle}" includes="**/*.jar"/>
      <!-- XJCTask depends on Ant. -->
      <fileset dir="${jaxb.libs.util}" includes="ant.jar"/>
      <!-- dependency on dom4j ??? -->  
      <pathelement path="${jaxb.libs.util}/dom4j.jar" />
      <pathelement path="${jaxb.libs.compiler}/txw2.jar" />
    </path>

    <path id="txw.classpath">
      <path refid="javac.classpath"/>
      <pathelement path="${jaxb.libs.compiler}/txw2.jar"/>
      <pathelement path="${jaxb.libs.util}/txwc2.jar"/>
      <pathelement path="${jaxb.libs.util}/commons-cli-1.0.jar"/>
      <pathelement path="${jaxb.libs.util}/rngom.jar"/>
    </path>

    <target name="init">
        <ant dir="${jaxb.root}/tools/source-tidy" target="compile" />

      <taskdef name="licence" classname="com.sun.tools.LicenceTask">
        <classpath>
          <pathelement path="${apgen.classes}" />
          <pathelement path="${jaxb.libs.compiler}/istack-commons-tools.jar"/>
          <pathelement path="${jaxb.libs.compiler}/codemodel.jar" />
        </classpath>
      </taskdef>
    </target>

    <!-- check if RelaxNGCC needs to be run -->
    <target name="ngcc-check">
      <condition property="ngcc.notrequired">
        <uptodate
          targetfile="${xjc.src}/com/sun/tools/jxc/gen/config/Config.java">
          <srcfiles dir="${xjc.src}/com/sun/tools/jxc/gen/config" includes="config.rng"/>
        </uptodate>
      </condition>
    </target>

    <!-- run RELAXNGCC -->
    <target name="ngcc" 
            unless="ngcc.notrequired" 
            depends="ngcc-check">
      <mkdir dir="${xjc.gensrc}"/>

      <delete dir="${xjc.gensrc}/com/sun/tools/jxc/gen/config" />
      <mkdir  dir="${xjc.gensrc}/com/sun/tools/jxc/gen/config"/>
      <java classname="relaxngcc.RelaxNGCC" fork="true">
        <classpath refid="javac.classpath" />
        <classpath path="${jaxb.libs.util}/relaxngcc.jar"/>

        <!-- enable debug statements -->
        <!--arg value="-d"/-->

        <!-- specify output directory -->
        <arg value="--target"/>
        <arg path="${xjc.gensrc}/com/sun/tools/jxc/gen/config" />

        <!-- source file -->
        <arg path="${xjc.src}/com/sun/tools/jxc/gen/config/config.rng"/>
      </java>
      <licence licence="${jaxb.root}/licenceheader.txt">
        <fileset dir="${xjc.gensrc}" includes="com/sun/tools/jxc/**/*.java"/>
      </licence>
    </target>

  <target name="annotation-writers-check">
      <available property="writers.not.required" file="${xjc.gensrc}/com/sun/tools/xjc/generator/annotation/ri/OverrideAnnotationOfWriter.java"/>
  </target>
  
  <target name="annotation-writers" depends="annotation-writers-check"
          description="generate CodeModel typed annotation writers"
          unless="writers.not.required">
     <taskdef name="cac" classname="com.sun.codemodel.ac.ACTask">
       <classpath>
         <pathelement path="${jaxb.libs.compiler}/codemodel.jar" />
         <pathelement path="${jaxb.libs.util}/codemodel-annotation-compiler.jar" />
         <pathelement path="${jaxb.libs.compiler}/istack-commons-tools.jar" />
       </classpath>
     </taskdef>
     
     <property name="jaxb-api.path" value="${jaxb.libs.redist}/jaxb-api.jar"/>
     <mkdir dir="${xjc.gensrc}"/>
     <cac destdir="${xjc.gensrc}"
       package="com.sun.tools.xjc.generator.annotation.spec">
       <classes includes="javax.xml.bind.annotation.*"
                excludes="javax.xml.bind.annotation.XmlJavaTypeAdapter" />
       <classes includes="javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter" />
       <classpath>
         <pathelement path="${jaxb-api.path}"/>
       </classpath>
       <endorse path="${jaxb-api.path}"/>
     </cac>
     <cac destdir="${xjc.gensrc}"
       package="com.sun.tools.xjc.generator.annotation.ri">
       <classes includes="com.sun.xml.bind.annotation.*"/>
       <classpath>
         <pathelement path="${runtime.classes}"/>
       </classpath>
     </cac>
      <licence licence="${jaxb.root}/licenceheader.txt">
        <fileset dir="${xjc.gensrc}" includes="com/sun/tools/xjc/generator/annotation/ri/*.java"/>
        <fileset dir="${xjc.gensrc}" includes="com/sun/tools/xjc/generator/annotation/spec/*.java"/>
      </licence>
  </target>
    
     <!-- compile Java source files -->
    <target name="compile"
            depends="init, ngcc, annotation-writers"
            description="compile all xjc src into jaxb-ri/xjc/build/classes">
        <!-- no way to 'depend' on codemodel and runtime targets, so just -->
        <!-- call the targets to make sure they are up to date            -->
        <ant target="compile" antfile="${runtime.root}/build.xml" />

        <mkdir dir="${xjc.classes}" />

        <javac srcdir="${xjc.src}:${xjc.gensrc}" destdir="${xjc.classes}"
            debug="${compile.debug}" optimize="${compile.optimize}"
        	source="1.5" target="1.5"
            deprecation="${compile.deprecation}" >
            <classpath refid="javac.classpath" />
        </javac>
        
        <copy toDir="${xjc.classes}">
          <fileset dir="${xjc.src}" includes="**/*.purified.rng" />
          <fileset dir="${xjc.src}" includes="**/dtd/bindinfo/bindingfile.rng" />
          <fileset dir="${xjc.src}" includes="**/*.xsd" />
        </copy>
        
        <!-- always delete these prop files so that the latest build id can be set -->
        <delete file="${xjc.classes}/com/sun/tools/xjc/MessageBundle*.properties"/>

        <copy toDir="${xjc.classes}">
          <fileset dir="${xjc.src}" includes="**/*.properties" />
          <filterset begintoken="@@" endtoken="@@">
            <filter token="BUILD_FULL_VERSION" value="${build.full.version}"/>
            <filter token="BUILD_VERSION" value="${build.version}"/>
          </filterset>
        </copy>
        
        <!-- clean up the xjc runtime java source code -->
        <java classname="SourceTidy">
          <classpath>
            <pathelement location="${jaxb.root}/tools/source-tidy"/>
          </classpath>
          <arg path="${xjc.classes}/com/sun/tools/xjc/runtime" />
        </java>
    </target>
    
    <target name="compile-xjc-test" 
            depends="compile"
	    description="compile xjc/test">
        <javac srcdir="${xjc.test}" destdir="${xjc.classes}"
            debug="${compile.debug}" optimize="${compile.optimize}"
        	source="1.5" target="1.5">
            <classpath refid="javac.classpath" />
            <classpath path="${xjc.classes}" />
            <classpath path="${runtime.classes}"/>
            <classpath path="${spec.classes}"/>
            <classpath>
              <fileset dir="${jaxb.libs.util}" includes="*.jar" />
            </classpath>
        </javac>
    </target>
        
    <target name="run-xjc-test" depends="xjc-junit"
            description="execute xjc unit tests - requires junit.jar on system classpath"/>
    
    <target name="xjc-junit" depends="compile-xjc-test" description="runs XJC JUnit tests">
      <mkdir dir="${xjc.root}/log"/>
      <junit printsummary="yes" fork="no" haltonerror="yes" haltonfailure="yes">
        
        <classpath refid="javac.classpath"/>
        <classpath path="${xjc.classes}"/>
        <classpath>
            <fileset dir="${jaxb.libs.util}" includes="junit*.jar"/>
        </classpath> 

        <batchtest todir="${xjc.root}/log" filtertrace="false">
            <formatter usefile="true" type="xml"/>
            <fileset dir="${xjc.classes}" includes="**/*JUTest.class"/>
        </batchtest>

      </junit>
    </target>
    
    <path id="javadoc.classpath">
        <pathelement path="${xjc.classes}" />
        <pathelement path="${runtime.classes}" />
        <pathelement path="${schemagen.classes}" />
        <fileset dir="${jaxb.libs.redist}" includes="*.jar"/>
        <fileset dir="${jaxb.libs.rebundle}" includes="**/*.jar"/>
        <fileset dir="${jaxb.libs.util}" includes="*.jar"/>
    </path>

    <target name="javadoc" 
            description="generate javadoc for xjc into jaxb-ri/xjc/docs/api">
      <mkdir dir="${xjc.root}/build/javadoc" />
      <javadoc sourcepath="${xjc.src}"
            packagenames="com.sun.tools.xjc, com.sun.tools.jxc, com.sun.tools.xjc.api"
            destdir="${xjc.root}/build/javadoc"
            use="true" defaultexcludes="yes"
            author="false"
            version="false"
            useexternalfile="true"
            windowtitle="XJC"
            doctitle="XJC"
            access="public">
        <classpath refid="javadoc.classpath" />
        <arg value="-quiet"/>
      </javadoc>
    </target>
    
    <!-- remove all intermediate files -->
    <target name="clean"
            description="clean jaxb-ri/xjc/build">
      <!-- delete all RelaxNGCC-generated files -->
      <delete failonerror="false">
        <fileset dir="${xjc.src}/com/sun/tools/jxc/gen/config" includes="**/*.java"/>
      </delete>

      <delete dir="${xjc.root}/build" />

      <delete file="${xjc.root}/manifest.xjc" />
    </target>

</project>
