<?xml version="1.0"?>
<!-- Copyright 2002-2007, 2009-2013, 2018-2026 Elliotte Rusty Harold

This library is free software; you can redistribute 
it and/or modify it under the terms of version 2.1 of 
the GNU Lesser General Public License as published by  
the Free Software Foundation.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General 
Public License along with this library. If not, see
<https://www.gnu.org/licenses/>.

You can contact Elliotte Rusty Harold by sending e-mail to
elharo@ibiblio.org. Please include the word "XOM" in the
subject line. The XOM home page is https://xom.nu/
-->
<project name="XOM" default="help" basedir="." xmlns:ivy="antlib:org.apache.ivy.ant">

    <description>
      Build XOM
    </description>

  <!-- Ivy properties for dependency management -->
  <property name="ivy.version"  value="2.5.3"/>
  <property name="ivy.jar.dir"  value="${user.home}/.ant/lib"/>
  <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy-${ivy.version}.jar"/>
  <property name="ivy.lib.dir"  value="./ivy"/>
  <property name="docbook.xsl.version" value="1.79.1"/>
   
  <!-- set global properties for this build -->
  <target name="init" depends="ivy-resolve">
    <property environment="env"/>
    <!-- Fixed ISO-8601 UTC timestamp applied to all JAR entries for reproducible builds. -->
    <property name="build.modtime" value="2026-05-18T20:13:26Z"/>
    <property name="Name"    value="XOM"/>
    <property name="name"    value="xom"/>
    <property name="majorversion" value="1"/>
    <property name="minorversion" value="4"/>
    <property name="microversion" value="4"/>
    <!-- put a1, b2 etc. here in non-releases -->
    <property name="versionqualifier" value=""/>
    <property name="version" value="${majorversion}.${minorversion}.${microversion}${versionqualifier}"/>

    <!-- OSGI wants a period before the a1/b2/d3 qualifier -->
    <!-- Can I have an if statement here to check if ${microversion} is an empty string before adding a period? 
         Problem is release version doesn't get a version qualifier. -->
    <property name="bundleversion" value="${majorversion}.${minorversion}.${microversion}.osgi"/>
    <property name="jaxenversion" value="2.0.3"/>

    <property name="year"    value="2026"/>

    <echo message="----------- ${Name} ${version} ------------"/>

    <property name="debug"       value="off"/>
    <property name="optimize"    value="on"/>
    <property name="deprecation" value="off"/>
    <property name="jvm"         value="java"/>

    <property name="src.dir"      value="./src"/>
    <property name="fat.src"      value="./fatsrc"/>
    <property name="packages"     value="nu.xom.*"/>

    <property name="build.dir"       value="./build"/>
    <property name="build.src"       value="./build/src"/>
    <property name="build.dest"      value="./build/classes"/>
    <property name="build.javadocs"  value="./build/apidocs"/>
    <property name="jaxen.dir"       value="${build.dir}/jaxen-classes"/>
    <property name="testoutput.dir"  value="./testresults"/>
    <property name="dist.dir"        value="./dist"/>    
    <property name="testreports.dir" value="./testreports" />
    <property name="xercesVersion"   value="2.12.2"/>
    <property name="xmlapis.jar"     value="${ivy.lib.dir}/xml-apis-1.4.01.jar"/>
    <property name="parser.jar"      value="${ivy.lib.dir}/xercesImpl-${xercesVersion}.jar"/>
    <property name="tagsoup.jar"     value="${ivy.lib.dir}/tagsoup-1.2.1.jar"/>
    <property name="servlet.jar"     value="${ivy.lib.dir}/servlet-api-2.5.jar"/>
    <property name="jarjar.jar"      value="${ivy.lib.dir}/jarjar-1.3.jar"/>
    <property name="saxon.jar"       value="${ivy.lib.dir}/saxon-6.5.3.jar"/>
    <property name="junit.jar"       value="${ivy.lib.dir}/junit-4.13.2.jar"/>
    <property name="clover.jar"      value="${ivy.lib.dir}/clover-4.5.2.jar"/>
    <property name="xom.jar"         value="${build.dir}/${name}-${version}.jar"/>
    <property name="xom-core"        value="${build.dest}"/>

    <property name="clover.db.dir"      value="${build.dir}/clover/db"/>
    <property name="clover.report.dir"  value="${build.dir}/clover/report"/>
    <property name="clover.instr.src"   value="${build.dir}/clover/src"/>
    <property name="clover.classes.dir" value="${build.dir}/clover/classes"/>
    
    <property name="excludes" value=".DS_Store, **/.DS_Store, **/.AppleFileInfo, **/*.zip, **/.thumbnails/**, data/XInclude-Test-Suite/**, data/xmlconf/**, data/canonical/xmlconf/**, data/oasis*/**, **/testresults/**, **/workspace/**, **/junit*properties, **/.nautilus-metafile.xml, website/**, **/.project, **/.classpath, build/**, dist/**, .settings/**, ivy/**, xom.fb, jester*, trademark*, .idea/**, **/*.iml, nbproject/**, .vscode/**"/>

    <property name="test.outputFormat" value="xml"/>   
    <property name="binary.jar" location="${dist.dir}/maven2/xom-${version}.jar" />
    <property name="sources.jar" location="${dist.dir}/maven2/xom-${version}-sources.jar" />
    <property name="javadoc.jar" location="${dist.dir}/maven2/xom-${version}-javadoc.jar" />
    
    <property name="ossrh-snapshots-repository-url" value="https://oss.sonatype.org/content/repositories/snapshots/" />
    <property name="ossrh-staging-repository-url" value="https://oss.sonatype.org/service/local/staging/deploy/maven2/" />
    <property name="ossrh-server-id" value="ossrh" /> <!-- the server ID in the Maven settings.xml -->
    
    <path id="compile.class.path">
      <pathelement location="${parser.jar}"/>
      <pathelement location="${xmlapis.jar}"/>
      <pathelement location="${junit.jar}"/>
      <pathelement location="${jaxen.dir}"/>
      <pathelement location="${servlet.jar}"/>
    </path>

    <path id="run.class.path">
      <pathelement location="${xmlapis.jar}"/>
      <pathelement location="${parser.jar}"/>
      <pathelement location="${xom-core}"/>
      <pathelement location="${jaxen.dir}"/>
    </path>
    
    <path id="test.class.path">
      <path refid="run.class.path"/>
      <pathelement location="${junit.jar}"/>
    </path>
    
    <path id="doc.class.path">
      <path refid="test.class.path"/>
      <pathelement location="${build.dest}"/>
      <pathelement location="${tagsoup.jar}"/>
    </path>

    <!-- set these properties for debugging -->
    <property name="compile.classpath" refid="compile.class.path"/>
    <property name="test.classpath" refid="test.class.path"/>
    <property name="run.classpath" refid="run.class.path"/>
    <property name="doc.classpath" refid="doc.class.path"/>

    <available property="servlet.jar.installed" file="${servlet.jar}"/>
    
  </target>
  
  <target name="help">
    <echo>
XOM Build file
-------------------------------------------------------------
    
  available targets are:
    
    help           --> print this message
    jar            --> build the xom.jar file
    samples        --> build the xom-samples.jar file
    compile        --> compile the source code
    javadoc        --> generate the API documentation
    test           --> run JUnit test suite
    clover         --> run tests with OpenClover code coverage
    clean          --> clean up the build directory
    dist           --> the zip and .tar.gz files
    upload         --> upload archive files
    sign           --> GPG-sign Maven artifacts (use -Dgpg.keyname=ID to select key)
    bundle         --> create bundle.jar of signed artifacts for Maven Central upload
      
  Use the option -Dfat=true to make Text objects bigger but faster.

-------------------------------------------------------------
    </echo>
  </target>

  <target name="ivy-bootstrap" description="Download Apache Ivy if not present">
    <mkdir dir="${ivy.jar.dir}"/>
    <get src="https://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.version}/ivy-${ivy.version}.jar"
         dest="${ivy.jar.file}"
         skipexisting="true"
         usetimestamp="true"/>
  </target>

  <target name="ivy-resolve" depends="ivy-bootstrap" description="Resolve dependencies using Apache Ivy">
    <taskdef resource="org/apache/ivy/ant/antlib.xml"
             uri="antlib:org.apache.ivy.ant"
             classpath="${ivy.jar.file}"/>
    <mkdir dir="${ivy.lib.dir}"/>
    <ivy:settings id="ivy.settings" file="ivysettings.xml"/>
    <ivy:resolve file="ivy.xml" settingsRef="ivy.settings"/>
    <ivy:retrieve pattern="${ivy.lib.dir}/[artifact]-[revision].[ext]"
                  conf="compile"
                  settingsRef="ivy.settings"/>
    <ivy:retrieve pattern="${ivy.lib.dir}/[artifact]-[revision].[ext]"
                  conf="servlets"
                  settingsRef="ivy.settings"/>
    <ivy:retrieve pattern="${ivy.lib.dir}/[artifact]-[revision].[ext]"
                  conf="betterdoc"
                  settingsRef="ivy.settings"/>
    <ivy:retrieve pattern="${ivy.lib.dir}/[artifact]-[revision].[ext]"
                  conf="jarjar"
                  settingsRef="ivy.settings"/>
    <ivy:retrieve pattern="${ivy.lib.dir}/[artifact]-[revision].[ext]"
                  conf="saxon"
                  settingsRef="ivy.settings"/>
    <ivy:retrieve pattern="${ivy.lib.dir}/[artifact]-[revision].[ext]"
                  conf="clover"
                  settingsRef="ivy.settings"/>
    <ivy:retrieve pattern="${ivy.lib.dir}/[artifact]-[revision].[ext]"
                  conf="junit"
                  settingsRef="ivy.settings"/>
    <ivy:retrieve xmlns:ivy="antlib:org.apache.ivy.ant"
                  pattern="${ivy.lib.dir}/[artifact]-[revision]-[classifier].[ext]"
                  conf="docbook"
                  settingsRef="ivy.settings"/>
    <unzip src="${ivy.lib.dir}/docbook-xsl-${docbook.xsl.version}-resources.zip"
           dest="${ivy.lib.dir}"
           overwrite="false"/>
  </target>

  <target name="prepare" depends="init, prepare-dirs, prepare-fat" />
  
  <target name="prepare-dirs" depends="init">
    <mkdir dir="${dist.dir}"/>
    <mkdir dir="${dist.dir}/maven2"/>
    <mkdir dir="${testoutput.dir}"/>
    <mkdir dir="${build.dir}"/>
    <mkdir dir="${build.src}"/>
    <mkdir dir="${build.dest}"/>
    <mkdir dir="${build.dir}/jaxen"/>
    <mkdir dir="${build.dir}/jaxen-classes"/>

    <delete file="${build.src}/nu/xom/Text.java" failonerror="false"/>

    <copy todir="${build.src}">
      <fileset dir="${src.dir}"/>
    </copy>
    
  </target>

  <!-- use Text.java that stores contents in String -->
  <target name="prepare-fat" depends="init" if="fat">
   <copy todir="${build.src}" overwrite="true">
      <fileset dir="${fat.src}"/>
   </copy>
  </target>


  <target name="compile-core" depends="prepare, compile-jaxen" 
          description="Compile the source code">
    <echo message="Using Classpath: ${compile.classpath}"/>
    <javac srcdir="${build.src}"
           destdir="${build.dest}"
           debug="${debug}"
           optimize="${optimize}"
           deprecation="${deprecation}"
           target="1.6"
           source="1.6"
           encoding="UTF-8"
           fork="true"
           includeAntRuntime="false"
           excludes="nu/xom/tools/* nu/xom/samples/*Servlet.java">
       <classpath refid="compile.class.path"/>
    </javac>
    <copy file="${build.src}/nu/xom/characters.dat" tofile="${build.dest}/nu/xom/characters.dat"/>
    
    <!-- update version number -->
    <echo file="${build.dest}/nu/xom/version.txt">${version}</echo>

    <copy file="${build.src}/nu/xom/compositions.dat" tofile="${build.dest}/nu/xom/compositions.dat" />
  </target>


  <target name="compile" depends="compile-core, compile-servlets" 
          description="Compile the source code" />
  
  <target name="compile-servlets" depends="compile-core" if="servlet.jar.installed"
          description="Compile the source code including the servlet samples">
    <javac srcdir="${build.src}"
           sourcepath=""
           destdir="${build.dest}"
           debug="${debug}"
           optimize="${optimize}"
           deprecation="${deprecation}"
           target="1.6"
           source="1.6"
           encoding="UTF-8"
           includeAntRuntime="false">
       <classpath refid="compile.class.path"/>
       <include name="nu/xom/samples/*Servlet.java" />
    </javac>
  </target>

  <target name="minimal" depends="compile" description="Create xom-minimal.jar">
    <jar jarfile="${build.dir}/${name}-${version}-minimal.jar"
         basedir="${build.dest}"
         index="no"
         compress="yes"
         modificationtime="${build.modtime}"
         includes="nu/xom/* nu/xom/xslt/* nu/xom/xinclude/* nu/xom/converters/* nu/xom/canonical/* nu/xom/tests/XOMTestCase.class"
         excludes="nu/xom/samples/* nu/xom/benchmarks/* nu/xom/tools/* nu/xom/integrationtests/*">
      <manifest>
        <attribute name="Sealed" value="true"/>
        <attribute name="Specification-Title"    value="XOM"/>
        <attribute name="Specification-Version"  value="${version}"/>
        <attribute name="Specification-Vendor"   value="Elliotte Rusty Harold"/>
        <attribute name="Implementation-Title"   value="XOM"/>
        <attribute name="Implementation-Version" value="${version}"/>
        <attribute name="Implementation-Vendor"  value="Elliotte Rusty Harold"/>          
        <attribute name="Main-Class" value="nu.xom.Info"/>
        <attribute name="Automatic-Module-Name" value="nu.xom"/>
        <section name="nu/xom/">
          <attribute name="Specification-Title"   value="XOM core classes"/>
          <attribute name="Implementation-Title"  value="nu.xom"/>
        </section>
        <section name="nu/xom/xslt/">
          <attribute name="Specification-Title"   value="XOM XSLT interface"/>
          <attribute name="Implementation-Title"  value="nu.xom.xslt"/>
        </section>
        <section name="nu/xom/xinclude/">
          <attribute name="Specification-Title"   value="XOM XInclude engine"/>
          <attribute name="Implementation-Title"  value="nu.xom.xinclude"/>
        </section>
        <section name="nu/xom/converters/">
          <attribute name="Specification-Title"   value="XOM converters to other object models"/>
          <attribute name="Implementation-Title"  value="nu.xom.converters"/>
        </section>
        <section name="nu/xom/canonical/">
          <attribute name="Specification-Title"   value="XOM Canonical XML support"/>
          <attribute name="Implementation-Title"  value="nu.xom.canonical"/>
        </section>
      </manifest>
    </jar>
  </target>


  <target name="jar" depends="compile" description="Create xom.jar">
    <taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask"
             classpath="${jarjar.jar}"/>
    <jarjar jarfile="${build.dir}/${name}-${version}.jar"
         basedir="${build.dest}"
         index="no"
         compress="yes"
         modificationtime="${build.modtime}"
         includes="nu/xom/* nu/xom/xslt/* nu/xom/xinclude/* nu/xom/converters/* nu/xom/canonical/* nu/xom/tests/XOMTestCase.class"
         excludes="nu/xom/samples/* nu/xom/benchmarks/* nu/xom/tools/*  nu/xom/integrationtests/*">

      <fileset dir="${jaxen.dir}"
         excludes="**Demo.class **/package.html org/jaxen/xom/** org/jaxen/XPathTestBase.class org/jaxen/jdom/** org/jaxen/dom4j/** org/jaxen/javabean/** org/jaxen/dom/** org/jaxen/**Test.class org/jaxen/saxpath/base/**Test.class org/jaxen/saxpath/helpers/**Test.class org/jaxen/saxpath/helpers/**Test.class org/jaxen/pattern/** org/jaxen/saxpath/SAXPathParseException.class org/w3c/dom/UserDataHandler.class org/jaxen/pattern/PriorityTest.class">
      </fileset>
    
      <rule pattern="org.jaxen.**" result="nu.xom.jaxen.@1"/>

      <manifest>
        <attribute name="Specification-Title"    value="XOM"/>
        <attribute name="Specification-Version"  value="${version}"/>
        <attribute name="Specification-Vendor"   value="Elliotte Rusty Harold"/>
        <attribute name="Implementation-Title"   value="XOM"/>
        <attribute name="Implementation-Version" value="${version}"/>
        <attribute name="Implementation-Vendor"  value="Elliotte Rusty Harold"/>          
        <attribute name="Main-Class" value="nu.xom.Info"/>
        <attribute name="Automatic-Module-Name" value="nu.xom"/>
        <attribute name="Build-Jdk" value="${java.version}"/>
        <attribute name="Build-Jdk-Vendor" value="${java.vendor}"/>
        
        <!-- OSGI Information -->
        <attribute name="Manifest-Version" value="1.0"/>
        <attribute name="Bundle-ManifestVersion" value="2"/>
        <attribute name="Bundle-Name" value="${Name}"/>
        <attribute name="Bundle-SymbolicName" value="nu.xom"/>
        <attribute name="Bundle-Version" value="${bundleversion}"/>
        <attribute name="Bundle-Vendor" value="xom.nu"/>
        <attribute name="Bundle-RequiredExecutionEnvironment" value="JavaSE-1.6"/>
        <attribute name="Export-Package" value="nu.xom, nu.xom.canonical, nu.xom.converters, nu.xom.xinclude, nu.xom.xslt"/>
        <attribute name="Import-Package" value='javax.xml.transform.sax, javax.xml.transform, org.w3c.dom, org.xml.sax, org.xml.sax.helpers, org.xml.sax.ext,
          org.apache.xerces.parsers;resolution:=optional;version="${xercesVersion}", org.apache.xerces.impl;resolution:=optional;version="${xercesVersion}", 
          org.apache.xerces.jaxp;resolution:=optional;version="${xercesVersion}", junit.framework;resolution:=optional;version="3.8.1",
          com.sun.org.apache.xerces.internal.jaxp;resolution:=optional, com.sun.org.apache.xerces.internal.parsers;resolution:=optional'/>

        <section name="nu/xom/">
          <attribute name="Sealed" value="true"/>
          <attribute name="Specification-Title"    value="XOM core classes"/>
          <attribute name="Implementation-Title"   value="nu.xom"/>
          <attribute name="Implementation-Version" value="${version}"/>
          <attribute name="Implementation-Vendor"  value="Elliotte Rusty Harold"/>          
       </section>
        <section name="nu/xom/jaxen/">
          <attribute name="Specification-Title"    value="Jaxen XPath engine"/>
          <attribute name="Implementation-Title"   value="org.jaxen"/>
          <attribute name="Implementation-Version" value="${jaxenversion}"/>
          <attribute name="Implementation-Vendor"  value="Jaxen"/>
        </section>
        <section name="nu/xom/xslt/">
          <attribute name="Sealed" value="true"/>
          <attribute name="Specification-Title"   value="XOM XSLT interface"/>
          <attribute name="Implementation-Title"  value="nu.xom.xslt"/>
        </section>
        <section name="nu/xom/xinclude/">
          <attribute name="Sealed" value="true"/>
          <attribute name="Specification-Title"   value="XOM XInclude engine"/>
          <attribute name="Implementation-Title"  value="nu.xom.xinclude"/>
        </section>
        <section name="nu/xom/converters/">
          <attribute name="Sealed" value="true"/>
          <attribute name="Specification-Title"   value="XOM converters to other object models"/>
          <attribute name="Implementation-Title"  value="nu.xom.converters"/>
        </section>
        <section name="nu/xom/canonical/">
          <attribute name="Sealed" value="true"/>
          <attribute name="Specification-Title"   value="XOM Canonical XML support"/>
          <attribute name="Implementation-Title"  value="nu.xom.canonical"/>
        </section>
      </manifest>
    </jarjar>
    
    <!-- Put a copy in the dist dir too, because I never remember to upload 
         the jar from the build directory. -->
    <copy file="${build.dir}/${name}-${version}.jar" todir="${dist.dir}"/>
  </target>

  
  <target name="samples" depends="compile" description="Create xom-samples.jar">
    <jar jarfile="${build.dir}/${name}-samples.jar"
         basedir="${build.dest}"
         index="yes"
     compress="yes"
         modificationtime="${build.modtime}"
         includes="nu/xom/samples/* nu/xom/benchmarks/*"
         excludes="nu/xom/xslt/* nu/xom/xinclude/* nu/xom/tools/* nu/xom/converters/* nu/xom/canonical/* nu/xom/integrationtests/*">
      <manifest>
        <attribute name="Specification-Title"    value="XOM Samples"/>
        <attribute name="Specification-Version"  value="${version}"/>
        <attribute name="Specification-Vendor"   value="Elliotte Rusty Harold"/>
        <attribute name="Implementation-Title"   value="XOM Samples"/>
        <attribute name="Implementation-Version" value="${version}"/>
        <attribute name="Implementation-Vendor"  value="Elliotte Rusty Harold"/>          
        <!-- TODO I can't get this to work yet
        <attribute name="Main-Class" value="nu.xom.samples.XMLPrinter"/>
        <attribute name="Class-Path" value="${name}-${version}.jar"/> -->
      </manifest>
    </jar>
  </target>

  
  <target name="javadoc" depends="compile"
          description="Generate the API documentation">
    <mkdir dir="${build.javadocs}"/>
    <javadoc packagenames="nu.xom.xslt, nu.xom.canonical, nu.xom.xinclude, nu.xom, nu.xom.converters"
             sourcepath="${build.src}"
             destdir="${build.javadocs}"
             author="true"
             version="true"
             overview="overview.html"
             use="true"
             splitindex="true"
             noindex="false"
             windowtitle="${Name} ${version}  API"
             docencoding="UTF-8"
             charset="UTF-8"
             noqualifier="java.lang:java.io"
             link="https://junit.org/junit4/javadoc/4.13.2"
             doctitle="${Name} ${version}"
             bottom="Copyright 2002-${year} &lt;a href='https://www.elharo.com/'&gt;Elliotte Rusty Harold&lt;/a&gt;
       &lt;br/&gt; &lt;a href='mailto:elharo%40ibiblio%2Eorg?Subject=XOM'&gt;elharo&#64;ibiblio&#46;org&lt;/a&gt;">
      <classpath refid="test.class.path"/>
      
    <fileset dir="${build.src}" defaultexcludes="yes">
      <include name="nu/xom/tests/XOMTestCase.java" />
    </fileset>
    </javadoc>
    
    <javac srcdir="${build.src}"
           destdir="${build.dest}"
           debug="${debug}"
           optimize="${optimize}"
           deprecation="${deprecation}"
           target="1.6"
           source="1.6"
           encoding="UTF-8"
           includeAntRuntime="false"
           includes="nu/xom/tools/*">
      <classpath>
        <pathelement path="${compile.class.path}"/>
        <pathelement path="${tagsoup.jar}"/> 
      </classpath>
    </javac>
    
    <java classname="nu.xom.tools.XHTMLJavaDoc" fork="true"> 
      <classpath refid="doc.class.path" />
      <arg value="build/apidocs"/> 
    </java>
  </target>
  
  
  <!-- I can never remember the exact target name -->
  <target name="javadocs"   depends="javadoc" />
  <target name="apidocs"    depends="javadoc"/>
  <target name="apidoc"     depends="javadoc"/>

  <property name="prefix" value="${name}-${version}" />
  
  <target name="dist" depends="zip, tar.gz, maven, website" />

  <!-- invoke as
      ant -Dpassword=secret upload -->
  <property name="password" value="" />
  
  <!-- This task depends on Jsch library not included in the Ant distribution. -->
  <target name="upload" depends="dist">

    <scp password="${password}" 
        todir="elharo:${password}@login.ibiblio.org:/export/sunsite/users/elharo/xml/XOM">
        <fileset file="${dist.dir}/${name}-${version}-src.zip" />
        <fileset file="${dist.dir}/${name}-${version}.zip" />
        <fileset file="${dist.dir}/${name}-${version}.tar.gz" />
        <fileset file="${dist.dir}/${name}-${version}-src.tar.gz" />
        <fileset file="${dist.dir}/${name}-${version}.jar" />
        <fileset file="${dist.dir}/maven2/xom-maven-${version}.jar" />
    </scp>
  </target>
    
  <target name="zip" depends="jar, javadoc, samples" 
          description="Build zip file for distro">

    <zip destfile="${dist.dir}/${name}-${version}-src.zip">
      <zipfileset prefix="XOM" dir="." excludes="${excludes}"/> 
    </zip>

    <zip destfile="${dist.dir}/${name}-${version}.zip">
      <zipfileset prefix="XOM" dir="." excludes="${excludes}"/>   
      <zipfileset prefix="XOM/apidocs" dir="./${build.dir}/apidocs" 
        excludes="**/.thumbnails/**, **/.nautilus-metafile.xml, website/**, **/.project, **/.classpath, **/.DS_Store"/>   
      <zipfileset fullpath="XOM/${name}-${version}.jar" dir="./${build.dir}" includes="${name}-${version}.jar"/> 
      <zipfileset fullpath="XOM/${name}-samples.jar" dir="./${build.dir}" includes="${name}-samples.jar"/>
      <zipfileset fullpath="XOM/lib/junit-4.13.2.jar" file="${junit.jar}"/>
      <zipfileset fullpath="XOM/lib/xercesImpl-${xercesVersion}.jar" file="${parser.jar}"/>
      <zipfileset fullpath="XOM/lib/xml-apis-1.4.01.jar" file="${xmlapis.jar}"/>
      <zipfileset prefix="XOM/lib" dir="./licenses"/>
    </zip>

  </target>
  
  
  <target name="tar" depends="jar, javadoc, samples" 
          description="Build tar file for distro">

    <tar destfile="${dist.dir}/${name}-${version}-src.tar">
      <tarfileset prefix="XOM" dir="." 
        excludes="${excludes}"/> 
    </tar>

    <tar destfile="${dist.dir}/${name}-${version}.tar">
      <tarfileset prefix="XOM" dir="." 
        excludes="${excludes}"/>   
      <tarfileset prefix="XOM/apidocs" dir="./${build.dir}/apidocs" 
        excludes="**/.thumbnails/**, **/.nautilus-metafile.xml, website/**, **/.project, **/.classpath, **/.DS_Store"/>   
      <tarfileset fullpath="XOM/${name}-${version}.jar" dir="./${build.dir}" includes="${name}-${version}.jar"/> 
      <tarfileset fullpath="XOM/${name}-samples.jar" dir="./${build.dir}" includes="${name}-samples.jar"/>
      <tarfileset fullpath="XOM/lib/junit-4.13.2.jar" file="${junit.jar}"/>
      <tarfileset fullpath="XOM/lib/xercesImpl-${xercesVersion}.jar" file="${parser.jar}"/>
      <tarfileset fullpath="XOM/lib/xml-apis-1.4.01.jar" file="${xmlapis.jar}"/>
      <tarfileset prefix="XOM/lib" dir="./licenses"/>
    </tar>

  </target>
  
  
  <target name="tar.gz" depends="tar" description="Build tar.gz file for distro">
     <gzip src="${dist.dir}/${name}-${version}.tar" zipfile="${dist.dir}/${name}-${version}.tar.gz"/>
     <gzip src="${dist.dir}/${name}-${version}-src.tar" zipfile="${dist.dir}/${name}-${version}-src.tar.gz"/>
  </target>

 
  <!-- This task requires the ANT optional.jar -->
  <target name="test" depends="compile" description="Run JUnit tests">

    <junit fork="true" jvm="${jvm}" failureproperty="testsfailed">
       <classpath refid="test.class.path" />
       <sysproperty key="jaxen.data.dir" value="${build.dir}/jaxen-${jaxenversion}"/>
       <formatter type="${test.outputFormat}" />
       <formatter type="brief" usefile="false" />
       <batchtest fork="true" todir="${testoutput.dir}">
         <fileset dir="${build.src}">
           <include name="**/*Test.java" />
           <exclude name="**/MegaTest.java" />
            <exclude name="**/benchmarks/*.java" />
            <exclude name="**/integrationtests/*.java" />
           <exclude name="**/EBCDICTest.java" />
         </fileset>
      </batchtest>
    </junit>

    <junitreport todir="${testoutput.dir}">
      <fileset dir="${testoutput.dir}">
        <include name="TEST-*.xml" />
      </fileset>
      <report todir="${testoutput.dir}" />
    </junitreport>

    <fail message="tests failed" if="testsfailed"/>

  </target>
 
 
  <target name="with.clover" depends="prepare" description="Instrument XOM sources with OpenClover">
    <taskdef resource="cloverlib.xml" classpath="${clover.jar}"/>
    <mkdir dir="${clover.db.dir}"/>
    <mkdir dir="${clover.instr.src}"/>
    <!-- Pre-instrument source files at the source level; avoids CloverCompilerAdapter classloading issues -->
    <clover-instr srcdir="${build.src}" destdir="${clover.instr.src}"
                  initstring="${clover.db.dir}/coverage.db"
                  encoding="UTF-8">
      <excludes>
        <exclude name="nu/xom/samples/**"/>
        <exclude name="nu/xom/benchmarks/**"/>
        <exclude name="nu/xom/tools/**"/>
      </excludes>
    </clover-instr>
  </target>

  <target name="compile-core-clover" depends="compile-jaxen, with.clover"
          description="Compile OpenClover-instrumented XOM sources">
    <mkdir dir="${clover.classes.dir}"/>
    <javac srcdir="${clover.instr.src}"
           destdir="${clover.classes.dir}"
           debug="${debug}"
           optimize="${optimize}"
           deprecation="${deprecation}"
           target="1.6"
           source="1.6"
           encoding="UTF-8"
           includeAntRuntime="false"
           excludes="nu/xom/tools/* nu/xom/samples/* nu/xom/benchmarks/*">
      <classpath>
        <path refid="compile.class.path"/>
        <pathelement location="${clover.jar}"/>
      </classpath>
    </javac>
    <copy file="${build.src}/nu/xom/characters.dat"  tofile="${clover.classes.dir}/nu/xom/characters.dat"/>
    <copy file="${build.src}/nu/xom/compositions.dat" tofile="${clover.classes.dir}/nu/xom/compositions.dat"/>
    <echo file="${clover.classes.dir}/nu/xom/version.txt">${version}</echo>
  </target>

  <target name="test-clover" depends="compile-core-clover"
          description="Run JUnit tests against OpenClover-instrumented code">
    <junit fork="true" failureproperty="testsfailed">
      <classpath>
        <pathelement location="${clover.classes.dir}"/>
        <pathelement location="${clover.jar}"/>
        <path refid="compile.class.path"/>
      </classpath>
      <sysproperty key="jaxen.data.dir" value="${build.dir}/jaxen-${jaxenversion}"/>
      <formatter type="${test.outputFormat}"/>
      <formatter type="brief" usefile="false"/>
      <batchtest fork="true" todir="${testoutput.dir}">
        <fileset dir="${build.src}">
          <include name="**/*Test.java"/>
          <exclude name="**/MegaTest.java"/>
          <exclude name="**/benchmarks/*.java"/>
          <exclude name="**/integrationtests/*.java"/>
          <exclude name="**/EBCDICTest.java"/>
        </fileset>
      </batchtest>
    </junit>

    <junitreport todir="${testoutput.dir}">
      <fileset dir="${testoutput.dir}">
        <include name="TEST-*.xml"/>
      </fileset>
      <report todir="${testoutput.dir}"/>
    </junitreport>

    <fail message="tests failed" if="testsfailed"/>
  </target>

  <target name="clover-report" depends="init" description="Generate OpenClover code coverage report">
    <taskdef resource="cloverlib.xml" classpath="${clover.jar}"/>
    <mkdir dir="${clover.report.dir}"/>
    <clover-report initstring="${clover.db.dir}/coverage.db">
      <current outfile="${clover.report.dir}" title="${Name} ${version}">
        <format type="html"/>
      </current>
      <current outfile="${clover.report.dir}/clover.xml" title="${Name} ${version}">
        <format type="xml"/>
      </current>
    </clover-report>
  </target>

  <target name="clover" depends="test-clover, clover-report"
          description="Run tests with OpenClover code coverage"/>


  <target name="testjar" depends="jar" description="Run JUnit tests using jar file">
    <junit printsummary="on" fork="no">
       <classpath>
          <pathelement location="${xom.jar}"/>
          <pathelement location="${junit.jar}"/>
       </classpath>
       <classpath refid="run.class.path" />
       <formatter type="${test.outputFormat}" />
       <formatter type="brief" usefile="false" />
       <batchtest fork="true" todir="${testoutput.dir}">
         <fileset dir="${build.src}">
           <include name="**/*Test.java" />
           <exclude name="**/MegaTest.java" />
           <exclude name="**/benchmarks/*.java" />
           <exclude name="**/EBCDICTest.java" />
         </fileset>
      </batchtest>
    </junit>
  </target>
    
      
  <target name="clean" description="Remove build files">
    <delete dir="./build"/>
    <delete dir="./dist"/>
    <delete dir="./testresults"/>
    <delete>
      <fileset dir="." includes="junit*properties"/>
    </delete>
    <ant antfile="build.xml" dir="website" target="clean"/>
  </target>

  
  <property name="ant-home" value="/opt/ant"/> 

  <target name="maven" depends="maven2" />

  <target name="sources.jar" depends="init, compile">
    <jar jarfile="${sources.jar}" basedir="${build.dir}/src"
         modificationtime="${build.modtime}">
      <exclude name= "nu/xom/benchmarks/**" />
      <exclude name= "nu/xom/tools/**" />
      <exclude name= "nu/xom/tests/**" />
      <exclude name= "nu/xom/samples/**" />
    </jar>
  </target>
  
  <target name="javadoc.jar" depends="init, compile, javadoc">
    <jar jarfile="${javadoc.jar}" basedir="${build.dir}/apidocs"
         modificationtime="${build.modtime}">
    </jar>
  </target>
    
  <!-- Generate a Maven JAR for uploading to OSRRH and hence placement onto ibiblio and mirrors. 
      See https://maven.apache.org/guides/mini/guide-central-repository-upload.html -->
  <target name="maven2" depends="jar, sources.jar, javadoc.jar" >
      <property name="pom2.template" location="project2.xml" />
      <property name="pom2.file" location="${build.dir}/maven2/project.xml" />
      <property name="package.jar" location="${build.dir}/${name}-${version}.jar"/>
      <property name="license" location="LICENSE.txt" />
      <copy file="${pom2.template}" tofile="${pom2.file}">
        <filterset>
         <filter token="VERSION" value="${version}"/>
       </filterset>
      </copy>
      <copy file="${pom2.file}" tofile="${dist.dir}/maven2/xom-${version}.pom">
        <filterset>
         <filter token="VERSION" value="${version}"/>
       </filterset>
      </copy>
      <copy file="${package.jar}" tofile="${dist.dir}/maven2/${name}-${version}.jar">
      </copy>
  </target>

  <!-- Sign the Maven artifacts with GPG.
       Optionally pass -Dgpg.keyname=YOURKEYID to select a specific key. -->
  <target name="sign" depends="maven2"
          description="Sign Maven artifacts with GPG for upload to Maven Central">
    <condition property="gpg.key.args" value="--default-key ${gpg.keyname}" else="">
      <isset property="gpg.keyname"/>
    </condition>
    <condition property="gpg.passphrase.args"
               value="--pinentry-mode loopback --passphrase ${gpg.passphrase}" else="">
      <isset property="gpg.passphrase"/>
    </condition>
    <condition property="gpg.arg.separator" value=" " else="">
      <and>
        <length string="${gpg.key.args}" when="greater" length="0"/>
        <length string="${gpg.passphrase.args}" when="greater" length="0"/>
      </and>
    </condition>
    <property name="gpg.args" value="${gpg.key.args}${gpg.arg.separator}${gpg.passphrase.args}"/>
    <exec executable="gpg" failonerror="true">
      <arg line="-ab ${gpg.args} ${dist.dir}/maven2/xom-${version}.pom"/>
    </exec>
    <exec executable="gpg" failonerror="true">
      <arg line="-ab ${gpg.args} ${dist.dir}/maven2/xom-${version}.jar"/>
    </exec>
    <exec executable="gpg" failonerror="true">
      <arg line="-ab ${gpg.args} ${dist.dir}/maven2/xom-${version}-javadoc.jar"/>
    </exec>
    <exec executable="gpg" failonerror="true">
      <arg line="-ab ${gpg.args} ${dist.dir}/maven2/xom-${version}-sources.jar"/>
    </exec>
  </target>

  <!-- Bundle the signed Maven artifacts into bundle.zip for upload to Maven Central. -->
  <target name="bundle" depends="sign"
          description="Create bundle.jar of signed Maven artifacts for upload to Maven Central">
    <checksum algorithm="SHA-256" fileext=".sha256">
      <fileset dir="${dist.dir}/maven2">
        <include name="xom-${version}.pom"/>
        <include name="xom-${version}.jar"/>
        <include name="xom-${version}-javadoc.jar"/>
        <include name="xom-${version}-sources.jar"/>
      </fileset>
    </checksum>
    <zip destfile="${dist.dir}/maven2/bundle.zip">
      <zipfileset dir="${dist.dir}/maven2" prefix="xom/xom/${version}">
        <include name="xom-${version}.pom"/>
        <include name="xom-${version}.pom.asc"/>
        <include name="xom-${version}.pom.sha256"/>
        <include name="xom-${version}.jar"/>
        <include name="xom-${version}.jar.asc"/>
        <include name="xom-${version}.jar.sha256"/>
        <include name="xom-${version}-javadoc.jar"/>
        <include name="xom-${version}-javadoc.jar.asc"/>
        <include name="xom-${version}-javadoc.jar.sha256"/>
        <include name="xom-${version}-sources.jar"/>
        <include name="xom-${version}-sources.jar.asc"/>
        <include name="xom-${version}-sources.jar.sha256"/>
      </zipfileset>
    </zip>
  </target>

  <target name="compile-jaxen" description="Compile Jaxen" depends="get-jaxen">
    <javac srcdir="${build.dir}/jaxen-${jaxenversion}/core/src/java/main"
           destdir="${build.dir}/jaxen-classes"
           debug="${debug}"
           optimize="${optimize}"
           deprecation="${deprecation}"
           target="1.6"
           source="1.6"
           encoding="UTF-8"
           includeAntRuntime="false"
           excludes="org/jaxen/dom/** 
                     org/jaxen/jdom/** 
                     org/jaxen/dom4j/** 
                     org/jaxen/pattern/** 
                     org/jaxen/javabean/** 
                     org/jaxen/xom/**
                     org/jaxen/function/xslt/**
                     org/jaxen/function/ext/**
                     org/w3c/dom/**
                     org/jaxen/saxpath/expr/DefaultPredicated.java
                     org/jaxen/saxpath/helpers/DefaultXPathHandler.java
                     org/jaxen/util/StackedIterator.java
                     org/jaxen/util/XPath2XMLVisitor.java
                     org/jaxen/util/LinkedIterator.java
                     org/jaxen/util/SelfAxisIterator.java
                     ">
       <classpath refid="compile.class.path"/>
    </javac>  
  </target>
  
  <target name="get-jaxen" description="Download Jaxen source code" depends="prepare">
      <unzip src="jaxen-${jaxenversion}-core-src.zip" dest="${build.dir}" />
  </target>

  <target name="debug" depends="init" description="Print various Ant properties">
    <echoproperties/>
  </target>
  
  <target name="website" depends="samples, javadoc" description="Generate the website">
    <ant antfile="build.xml" dir="website" target="dist"/>
    <copy todir="${dist.dir}/website/apidocs">  
      <fileset dir="${build.javadocs}" includes="**"/>  
    </copy>
  </target>
  
</project>
