<?xml version="1.0" encoding="UTF-8"?>
<!-- 
Copyright © 2005 by Idiom Technologies, Inc. All rights reserved. 
IDIOM is a registered trademark of Idiom Technologies, Inc. and WORLDSERVER
and WORLDSTART are trademarks of Idiom Technologies, Inc. All other 
trademarks are the property of their respective owners. 

IDIOM TECHNOLOGIES, INC. IS DELIVERING THE SOFTWARE "AS IS," WITH 
ABSOLUTELY NO WARRANTIES WHATSOEVER, WHETHER EXPRESS OR IMPLIED,  AND IDIOM
TECHNOLOGIES, INC. DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
PURPOSE AND WARRANTY OF NON-INFRINGEMENT. IDIOM TECHNOLOGIES, INC. SHALL NOT
BE LIABLE FOR INDIRECT, INCIDENTAL, SPECIAL, COVER, PUNITIVE, EXEMPLARY,
RELIANCE, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF 
ANTICIPATED PROFIT), ARISING FROM ANY CAUSE UNDER OR RELATED TO  OR ARISING 
OUT OF THE USE OF OR INABILITY TO USE THE SOFTWARE, EVEN IF IDIOM
TECHNOLOGIES, INC. HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 

Idiom Technologies, Inc. and its licensors shall not be liable for any
damages suffered by any person as a result of using and/or modifying the
Software or its derivatives. In no event shall Idiom Technologies, Inc.'s
liability for any damages hereunder exceed the amounts received by Idiom
Technologies, Inc. as a result of this transaction.

These terms and conditions supersede the terms and conditions in any
licensing agreement to the extent that such terms and conditions conflict
with those set forth herein.
-->
<project name="dita.build" basedir=".">
  
  <import file="${dita.plugin.org.dita.pdf2.dir}/build_fop.xml"/>
  <import file="${dita.plugin.org.dita.pdf2.dir}/build_axf.xml"/>
  <import file="${dita.plugin.org.dita.pdf2.dir}/build_xep.xml"/>

  <target name="dita2pdf2.init">
    <condition property="preprocess.copy-image.skip" value="true">
      <isfalse value="${org.dita.pdf2.use-out-temp}"/>
    </condition>
    <condition property="retain.topic.fo" value="true">
      <isfalse value="${org.dita.pdf2.use-out-temp}"/>
    </condition>
    <!-- Legacy: pass empty input directory -->
    <condition property="user.input.dir.url" value="">
      <istrue value="${org.dita.pdf2.use-out-temp}"/>
    </condition>
  </target>

  <target name="dita2pdf2" depends="dita2pdf2.init, dita-preprocess, map2pdf2, topic2pdf2"/>

  <target name="transform.topic2pdf.init">
    <condition property="pdf2.temp.dir" value="${dita.temp.dir}" else="${dita.map.output.dir}">
      <isfalse value="${org.dita.pdf2.use-out-temp}"/>
    </condition>
    
    <property name="customization.dir" value="${dita.plugin.org.dita.pdf2.dir}/Customization"/>
    <property file="${customization.dir}/build.properties"/>
    
    <condition property="pdf2.i18n.skip" value="true">
      <and>
        <isset property="org.dita.pdf2.i18n.enabled"/>
        <isfalse value="${org.dita.pdf2.i18n.enabled}"/>
      </and>
    </condition>
    
    <property name="default.locale" value="en_US"/>
    
    <!-- legacy property -->
    <property name="ot.dir" location="${dita.dir}"/>
    <property name="lib.dir" value="${dita.dir}/lib"/>
    <property name="fo.lib.dir" value="${dita.plugin.org.dita.pdf2.dir}/lib"/>
    <property name="merged.file" value="${dita.map.filename.root}_MERGED.xml"/>
    
    <!-- configure FO engines -->
    <!-- default is FOP.  Can be changed here, or with pdf.formatter property. -->
    <property name="pdf.formatter" value="fop"/>
    
    <property name="artwork.dir" value="${pdf2.temp.dir}"/>
    <property name="disableRelatedLinks" value="yes"/>
    
    <condition property="file.protocol.prefix" value="file://">
      <os family="unix"/>
    </condition>
    
    <condition property="file.protocol.prefix" value="file:/">
      <not><isset property="file.protocol.prefix"/></not>
    </condition>
    
    <property name="xml.catalog.files" value="${customization.dir}/catalog.xml;${dita.plugin.org.dita.pdf2.dir}/cfg/catalog.xml;${dita.dir}/catalog-dita.xml"/>
    
    <property name="xsl.dir" value="${dita.plugin.org.dita.pdf2.dir}/xsl"/>
    <property name="xsl.fo.dir" value="${dita.plugin.org.dita.pdf2.dir}/xsl/fo"/>
    <!-- legacy property -->
    <property name="xsl.fo.diff.dir" value="${dita.plugin.org.dita.pdf2.dir}/xsl/fo/diff"/>
    <property name="xsl.common.dir" value="${dita.plugin.org.dita.pdf2.dir}/xsl/common"/>
    <property name="cfg.dir" value="${dita.plugin.org.dita.pdf2.dir}/cfg"/>
    <property name="cfg.fo.dir" value="${dita.plugin.org.dita.pdf2.dir}/cfg/fo"/>
    <property name="cfg.common.dir" value="${dita.plugin.org.dita.pdf2.dir}/cfg/common"/>
    <!-- legacy property -->
    <property name="dtd.dir" value="${dita.plugin.org.dita.pdf2.dir}/dtd"/>
    
    <property name="saxon.resolver.command.line"
      value="-warnings:silent -x:org.apache.xml.resolver.tools.ResolvingXMLReader -y:org.apache.xml.resolver.tools.ResolvingXMLReader -r:org.apache.xml.resolver.tools.CatalogResolver"/>
    
    <path id="project.class.path">
      <pathelement path="${java.class.path}"/>
      <fileset dir="${lib.dir}/saxon">
        <include name="**/*.jar"/>
      </fileset>
      <!--
        <pathelement location="${lib.dir}/xml-apis.jar"/>
        <pathelement location="${lib.dir}/xercesImpl.jar"/>
      -->
      <pathelement location="${lib.dir}/resolver.jar"/>
      <pathelement location="${lib.dir}/icu4j.jar"/>
      <pathelement location="${fo.lib.dir}/fo.jar"/>
    </path>
  </target>

  <target name="topic2pdf2" if="noMap">
    <antcall target="preview.topic.pdf"/>
  </target>

  <target name="preview.topic.pdf"
          depends="transform.topic2pdf.init,
                   preview.topic.pdf.init,
                   transform.topic2pdf"/>
  <target name="preview.topic.pdf.init">
    <property name="inputFile" location="${dita.temp.dir}/${user.input.file}"/>
    <!--property name="outputFile" location="${dita.map.output.dir}/${dita.topic.filename.root}.pdf"/-->
    <property name="outputFile.base" value="${dita.topic.filename.root}"/>
    <property name="artworkPrefix" value="${artwork.dir}"/>
  </target>

  <target name="map2pdf2" unless="noMap">
    <dirname property="dita.temp.dir.fullpath" file="${dita.temp.dir}${file.separator}dummy.file"/>
    <pipeline message="topicmerge" module="TopicMerge"
              inputmap="${dita.temp.dir.fullpath}${file.separator}${user.input.file}"
              tempdir="${dita.temp.dir.fullpath}">
      <param name="output" location="${dita.temp.dir.fullpath}${file.separator}${dita.map.filename.root}_MERGED.xml"/>
      <param name="style" location="${dita.plugin.org.dita.pdf2.dir}/xsl/common/topicmerge.xsl"/>
    </pipeline>
    <antcall target="publish.map.pdf"/>
  </target>

  <target name="publish.map.pdf"
          depends="transform.topic2pdf.init,
                   publish.map.pdf.init,
                   transform.topic2pdf"/>
  <target name="publish.map.pdf.init">
    <property name="inputFile" location="${dita.temp.dir}/${merged.file}"/>
    <!--property name="outputFile" location="${dita.map.output.dir}/${dita.map.filename.root}.pdf"/-->
    <property name="outputFile.base" value="${dita.map.filename.root}"/>
    <property name="artworkPrefix" value="${artwork.dir}"/>
  </target>

  <target name="transform.topic2fo"
          depends="transform.topic2fo.init,
                   transform.topic2fo.index,
                   transform.topic2fo.flagging,
                   transform.topic2fo.main,
                   transform.topic2fo.i18n"/>
    
  <target name="transform.topic2fo.init">
    <taskdef name="dita-version" classname="com.idiominc.ws.opentopic.xsl.extension.DitaVersion"
             classpathref="project.class.path"/>
    <taskdef name="detect-lang" classname="com.suite.sol.ditaot.DetectLang"
             classpathref="project.class.path"/>

    <makeurl file="${inputFile}" property="inputFile.url" validate="true"/>

    <dita-version documentPath="${inputFile.url}"/>

    <!-- Set document.locale from xml:lang -->
    <!-- The map takes precedence, followed by the first topic -->
    <detect-lang documentPath="${inputFile.url}"/>

    <condition property="document.locale" value="${default.locale}">
      <not><isset property="document.locale"/></not>
    </condition>

    <!-- WS runtime properties -->
    <property file="${cfg.common.dir}/properties/${document.locale}.properties"/>

    <condition property="document.language" value="${default.language}">
      <not><isset property="document.language"/></not>
    </condition>
    <echo level="info">Using document.locale=${document.locale}</echo>

    <condition property="formatter.specific.ext" value="_axf">
      <and>
        <not><isset property="formatter.specific.ext"/></not>
        <equals arg1="ah" arg2="${pdf.formatter}"/>
      </and>
    </condition>
    <condition property="formatter.specific.ext" value="_fop">
      <and>
        <not><isset property="formatter.specific.ext"/></not>
        <equals arg1="fop" arg2="${pdf.formatter}"/>
      </and>
    </condition>
    <condition property="formatter.specific.ext" value="_xep">
      <and>
        <not><isset property="formatter.specific.ext"/></not>
        <equals arg1="xep" arg2="${pdf.formatter}"/>
      </and>
    </condition>
    <condition property="formatter.specific.ext" value="">
      <not><isset property="formatter.specific.ext"/></not>
    </condition>

    <condition property="args.bookmark.style" value="">
      <not><isset property="args.bookmark.style"/></not>
    </condition>

    <condition property="args.chapter.layout" value="">
      <not><isset property="args.chapter.layout"/></not>
    </condition>

    <condition property="args.fo.include.rellinks" value="">
      <not><isset property="args.fo.include.rellinks"/></not>
    </condition>

    <condition property="args.draft" value="no">
      <not><isset property="args.draft"/></not>
    </condition>

    <condition property="publish.required.cleanup" value="${args.draft}">
      <and>
        <not><isset property="publish.required.cleanup"/></not>
        <isset property="args.draft"/>
      </and>
    </condition>

    <condition property="args.gen.task.lbl" value="">
      <not><isset property="args.gen.task.lbl"/></not>
    </condition>
    <!-- use customized xsl file for pdf transform.-->
    <condition property="temp.transformation.file" value="${args.xsl.pdf}">
      <and>
        <isset property="args.xsl.pdf"/>
        <available file="${args.xsl.pdf}" type="file"/>
      </and>
    </condition>
    <condition property="temp.transformation.file" value="${dita.plugin.org.dita.pdf2.dir}/${args.xsl.pdf}">
      <and>
        <not><isset property="temp.transformation.file"/></not>
        <isset property="args.xsl.pdf"/>
      </and>
    </condition>
    
    <condition property="args.bookmap-order" value="discard">
      <not><isset property="args.bookmap-order"/></not>
    </condition>

    <condition property="temp.transformation.file" value="${xsl.fo.dir}/topic2fo_shell_1.0${formatter.specific.ext}.xsl">
      <and>
        <not><isset property="temp.transformation.file"/></not>
        <or>
          <equals arg1="${ws.runtime.publishing.map.dita.version}" arg2="1.0"/>
          <equals arg1="${ws.runtime.publishing.map.dita.version}" arg2="1.1"/>
          <equals arg1="${ws.runtime.publishing.map.dita.version}" arg2="1.2"/>
        </or>
      </and>
    </condition>
    <condition property="temp.transformation.file" value="${xsl.fo.dir}/topic2fo_shell${formatter.specific.ext}.xsl">
      <not><isset property="temp.transformation.file"/></not>
    </condition>
  </target>

  <target name="transform.topic2fo.index">
    <taskdef classname="com.idiominc.ws.opentopic.fo.index2.IndexPreprocessorTask"
             name="index-preprocess" classpathref="project.class.path"/>
    
    <condition property="index.config.file" value="${customization.dir}/common/index/${document.locale}.xml">
      <and>
        <not><isset property="index.config.file"/></not>
        <available file="${customization.dir}/common/index/${document.locale}.xml"/>
      </and>
    </condition>
    <condition property="index.config.file" value="${cfg.dir}/common/index/${document.locale}.xml">
      <and>
        <not><isset property="index.config.file"/></not>
        <available file="${cfg.dir}/common/index/${document.locale}.xml"/>
      </and>
    </condition>
    <condition property="index.config.file" value="${customization.dir}/common/index/${document.language}.xml">
      <and>
        <not><isset property="index.config.file"/></not>
        <available file="${customization.dir}/common/index/${document.language}.xml"/>
      </and>
    </condition>
    <condition property="index.config.file" value="${cfg.dir}/common/index/${document.language}.xml">
      <and>
        <not><isset property="index.config.file"/></not>
        <available file="${cfg.dir}/common/index/${document.language}.xml"/>
      </and>
    </condition>
    <condition property="index.config.file" value="${cfg.dir}/common/index/${default.language}.xml">
      <not><isset property="index.config.file"/></not>
    </condition>

    <index-preprocess input="${inputFile.url}" output="${dita.temp.dir}/stage1.xml"
                      indexConfig="${index.config.file}" locale="${document.locale}"
                      catalogs="${xml.catalog.files}"/> 
  </target>

  <target name="transform.topic2fo.flagging"
          depends="transform.topic2fo.flagging.filter,
                   transform.topic2fo.flagging.no-filter"/>
  <target name="transform.topic2fo.flagging.filter" if="dita.input.valfile">
    <condition property="_org.dita.pdf2.valfile" value="${dita.input.valfile}">
      <available file="${dita.input.valfile}" type="file"/>
    </condition>
    <condition property="_org.dita.pdf2.valfile" value="${dita.plugin.org.dita.pdf2.dir}/${dita.input.valfile}">
      <not><isset property="_org.dita.pdf2.valfile"/></not>
    </condition>
    <makeurl property="_org.dita.pdf2.valfile.url" file="${_org.dita.pdf2.valfile}" validate="no"/>
    <xslt in="${dita.temp.dir}/stage1.xml" out="${dita.temp.dir}/stage1a.xml" style="${xsl.fo.dir}/flagging-preprocess.xsl">
      <param name="filterFile" expression="${_org.dita.pdf2.valfile.url}"/>
      <xmlcatalog>
        <catalogpath path="${xml.catalog.files}"/>
      </xmlcatalog>
    </xslt>
  </target>
  <target name="transform.topic2fo.flagging.no-filter" unless="dita.input.valfile">
    <copy file="${dita.temp.dir}/stage1.xml" tofile="${dita.temp.dir}/stage1a.xml"/>
  </target>  

  <target name="transform.topic2fo.main">
    <!--makeurl seems to output file:/C: style instead of file:///C:, but xep, fop, and ah all accept it.-->
    <makeurl property="artworkPrefixUrl" file="${artworkPrefix}"/>
    <makeurl property="dita.map.output.dir.url" file="${pdf2.temp.dir}" validate="no"/>
    <makeurl property="work.dir.url" file="${dita.temp.dir}" validate="no"/>
    
    <!--Decide which version of saxon to use.-->
    <available classname="net.sf.saxon.Transform" property="fo.saxon.classname"
               value="net.sf.saxon.Transform" classpathref="project.class.path"/>
    <property name="fo.saxon.classname" value="com.icl.saxon.StyleSheet"/>
    
    <makeurl property="customization.dir.url" file="${customization.dir}"/>
    <makeurl property="user.input.dir.url" file="${user.input.dir}"/>
    <!-- must fork or catalog resolution will fail due to sharing of -->
    <!-- resolver and catalogs with the Open Toolkit -->
    <echo level="info" message="Java maximum memory=${maxJavaMemory}"/>
    <echo level="info">Processing ${dita.temp.dir}/stage1a.xml to ${dita.temp.dir}/stage2.fo</echo>
    <echo level="info">Loading stylesheet ${temp.transformation.file}</echo>
    <java classname="${fo.saxon.classname}" classpathref="project.class.path" fork="true"
          maxmemory="${maxJavaMemory}" failonerror="true">
      <jvmarg line="${baseJVMArgLine}"/>
      <arg line='-o:"${dita.temp.dir}/stage2.fo"'/>
      <arg line="${saxon.resolver.command.line}"/>
      <arg line='"${dita.temp.dir}/stage1a.xml"'/>
      <arg line='"${temp.transformation.file}"'/>
      <arg line='"locale=${document.locale}"'/>
      <arg line='"customizationDir.url=${customization.dir.url}"'/>
      <arg line='"input.dir.url=${user.input.dir.url}"'/>
      <arg line='"DRAFT=${args.draft}"'/>
      <arg line='"output.dir.url=${dita.map.output.dir.url}"'/>
      <arg line='"work.dir.url=${work.dir.url}"'/>
      <arg line='"artworkPrefix=${artworkPrefixUrl}"'/>
      <arg line='"publishRequiredCleanup=${publish.required.cleanup}"'/>
      <arg line='"disableRelatedLinks=${disableRelatedLinks}"'/>
      <arg line='"antArgsBookmarkStyle=${args.bookmark.style}"'/>
      <arg line='"antArgsChapterLayout=${args.chapter.layout}"'/>
      <arg line='"antArgsIncludeRelatedLinks=${args.fo.include.rellinks}"'/>
      <arg line='"antArgsGenerateTaskLabels=${args.gen.task.lbl}"'/>
      <arg line='"pdfFormatter=${pdf.formatter}"'/>
      <arg line='"useFrameIndexMarkup=${org.dita.pdf2.index.frame-markup}"'/>
      <arg line='"bookmap-order=${args.bookmap-order}"'/>
      <sysproperty key="xml.catalog.files" value="${xml.catalog.files}"/>
    </java>
  </target>

  <target name="transform.topic2fo.i18n"
          depends="transform.topic2fo.i18n.filter,
                   transform.topic2fo.i18n.no-filter"/>
  <target name="transform.topic2fo.i18n.filter" unless="pdf2.i18n.skip">
    <taskdef classname="com.idiominc.ws.opentopic.fo.i18n.PreprocessorTask" name="i18n-preprocess"
             classpathref="project.class.path"/>
    
    <condition property="i18n.config.file" value="${customization.dir}/fo/i18n/${document.locale}.xml">
      <and>
        <not><isset property="i18n.config.file"/></not>
        <available file="${customization.dir}/fo/i18n/${document.locale}.xml"/>
      </and>
    </condition>
    <condition property="i18n.config.file" value="${cfg.fo.dir}/i18n/${document.locale}.xml">
      <and>
        <not><isset property="i18n.config.file"/></not>
        <available file="${cfg.fo.dir}/i18n/${document.locale}.xml"/>
      </and>
    </condition>
    <condition property="i18n.config.file" value="${customization.dir}/fo/i18n/${document.language}.xml">
      <and>
        <not><isset property="i18n.config.file"/></not>
        <available file="${customization.dir}/fo/i18n/${document.language}.xml"/>
      </and>
    </condition>
    <condition property="i18n.config.file" value="${cfg.fo.dir}/i18n/${document.language}.xml">
      <and>
        <not><isset property="i18n.config.file"/></not>
        <available file="${cfg.fo.dir}/i18n/${document.language}.xml"/>
      </and>
    </condition>
    <condition property="i18n.config.file" value="${cfg.fo.dir}/i18n/${default.language}.xml">
      <not><isset property="i18n.config.file"/></not>
    </condition>

    <i18n-preprocess input="${dita.temp.dir}/stage2.fo" output="${dita.temp.dir}/stage3.fo"
                     config="${i18n.config.file}" catalogs="${xml.catalog.files}"/> 

    <xslt in="${dita.temp.dir}/stage3.fo" out="${pdf2.temp.dir}/topic.fo" style="${xsl.fo.dir}/i18n-postprocess.xsl">
      <xmlcatalog>
        <catalogpath path="${xml.catalog.files}"/>
      </xmlcatalog>
    </xslt>
  </target>
  <target name="transform.topic2fo.i18n.no-filter" if="pdf2.i18n.skip">
    <echo level="info">Skip I18N font processing.</echo>
    <move file="${dita.temp.dir}/stage2.fo" tofile="${pdf2.temp.dir}/topic.fo"/>
  </target>

  <target name="transform.topic2pdf"
          xmlns:dita="http://dita-ot.sourceforge.net"
          dita:extension="depends org.dita.dost.platform.InsertDependsAction"
          dita:depends="copyCoreArtwork,
                        transform.topic2fo,
                        {depend.org.dita.pdf2.format.pre},
                        transform.fo2pdf,
                        {depend.org.dita.pdf2.format.post},
                        delete.fo2pdf.topic.fo"/>

  <target name="delete.fo2pdf.topic.fo" unless="retain.topic.fo">
    <delete>
      <!--Deleting the stage files isn't necessary, since it will be cleaned by clean-temp, and is useful for debugging.-->
      <!--<fileset dir="${dita.temp.dir}" includes="stage?.*"/>-->
      <fileset dir="${pdf2.temp.dir}" includes="topic.fo"/>
    </delete>
  </target>

  <target name="transform.fo2pdf"
          xmlns:dita="http://dita-ot.sourceforge.net"
          dita:extension="depends org.dita.dost.platform.InsertDependsAction"
          dita:depends="transform.fo2pdf.xep,
                        transform.fo2pdf.ah,
                        transform.fo2pdf.fop,
                        {depend.org.dita.pdf2.format}"/>

  <target name="copyCoreArtwork">
    <copy todir="${artwork.dir}/Configuration/OpenTopic" failonerror="false">
      <fileset dir="${dita.plugin.org.dita.pdf2.dir}" includes="cfg/common/artwork/**/*.*"/>
    </copy>
    <copy todir="${artwork.dir}/Customization/OpenTopic" failonerror="false">
      <fileset dir="${customization.dir}" includes="common/artwork/**/*.*"/>
    </copy>
  </target>

</project>
