<?xml version="1.0" encoding="utf-8"?>
<project basedir="." default="dist" name="teidocx">
  <!--
	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.
	
	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, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
	
        Id: $Id: build-to.xml 11933 2013-04-14 14:45:01Z rahtz $
        Copyright: 2011, TEI Consortium<
-->
  <!--
 Convert TEI documents into docx files.
 -->
  <property name="verbose" value="false"/>
  <!-- Path to TEI XML file. -->
  <property name="inputFile" value=""/>
  <!-- Path where the output docx file will be saved. -->
  <property name="outputFile" value=""/>
  <!-- which profile directory-->
  <property name="profiledir" value="../profiles"/>
  <!-- which profile -->
  <property name="profile" value="default"/>
  <!-- which language -->
  <property name="lang" value="en"/>
  <property name="catalogfile" value="../catalog.xml"/>
  <!-- where does oxygen keep stuff -->
  <property name="oxygenlib" value=""/>
  <!-- Stylesheet for transformation -->
  <property name="teitoDocx" value="${profiledir}/${profile}/docx/to.xsl"/>
  <property name="listgraphics" value="../tools/makegraphicsanttask.xsl"/>
  <dirname property="xslDir" file="${teitoDocx}"/>
  <dirname property="inputFileDir" file="${inputFile}"/>
  <property name="inputFilePath" location="${inputFile}"/>
  <dirname property="outputDir" file="${outputFile}"/>
  <property name="outputTempDir" value="${outputDir}/temp-dir-for-ant"/>
  <path id="classpath">
    <pathelement path="${java.class.path}"/>
  </path>
  <condition property="makecoverpages">
    <available file="${profiledir}/${profile}/docx/makecoverpages.xsl"/>
  </condition>
  <import file="../odds2/build-to.xml"/>
  <target name="setup">
    <!-- Recreate temporary output directory -->
    <taskdef name="images" classname="ImageTask" classpath="../tools/ImageInfo/ImageInfo.jar"/>
    <delete dir="${outputTempDir}"/>
    <mkdir dir="${outputTempDir}"/>
  </target>
  <target name="nocoverpages" unless="makecoverpages">
    <move file="${outputTempDir}/newdocument.xml" tofile="${outputTempDir}/word/document.xml" overwrite="true"/>
  </target>
  <target name="coverpages" if="makecoverpages">
    <echo>Make cover pages</echo>
    <xslt style="${profiledir}/${profile}/docx/makecoverpages.xsl" in="${outputTempDir}/word/document.xml" out="${outputTempDir}/tmp3.xml">
      <xmlcatalog>
        <catalogpath>
          <pathelement location="$catalogfile"/>
        </catalogpath>
      </xmlcatalog>
      <factory name="net.sf.saxon.TransformerFactoryImpl"/>
      <classpath location="${oxygenlib}/saxon9he.jar"/>
      <param name="headerFile" expression="file:///${outputTempDir}/tmp2.xml"/>
      <param name="documentFile" expression="file:///${outputTempDir}/newdocument.xml"/>
    </xslt>
    <move file="${outputTempDir}/tmp3.xml" tofile="${outputTempDir}/word/document.xml" overwrite="true"/>
    <delete file="${outputTempDir}/newdocument.xml"/>
  </target>
  <target name="transform">
    <!-- The transformation for DOCX output. -->
    <echo level="info">unzip Word template</echo>
    <unzip src="${xslDir}/template.docx" dest="${outputTempDir}"/>
    <echo level="info">remove custom document properties, notes,
    footers, headers etc from Word template</echo>
    <delete includeEmptyDirs="true" verbose="true">
      <fileset dir="${outputTempDir}">
        <include name="docProps/custom.xml"/>
        <include name="word/webSettings.xml"/>
        <include name="word/endnotes.xml"/>
        <include name="word/footnotes.xml"/>
        <include name="word/footer*"/>
        <include name="word/header*"/>
      </fileset>
    </delete>
    <delete dir="${outputTempDir}/customXml"/>
    <delete dir="${outputTempDir}/word/printerSettings"/>
    <echo level="info">fix Word .rels file</echo>
    <xslt style="tools/fixdotrels.xsl" in="${outputTempDir}/_rels/.rels" out="${outputTempDir}/_rels/.rels_">
      <factory name="net.sf.saxon.TransformerFactoryImpl"/>
      <classpath location="${saxon.jar}"/>
    </xslt>
    <move file="${outputTempDir}/_rels/.rels_" tofile="${outputTempDir}/_rels/.rels" verbose="true"/>
    <echo level="info">Fix Word styles file</echo>
    <xslt style="tools/fixstyle.xsl" in="${outputTempDir}/word/styles.xml" out="${outputTempDir}/word/_styles.xml">
      <factory name="net.sf.saxon.TransformerFactoryImpl"/>
      <classpath location="${saxon.jar}"/>
    </xslt>
    <move file="${outputTempDir}/word/_styles.xml" tofile="${outputTempDir}/word/styles.xml" verbose="true"/>
    <echo level="info">Get names of graphics files and write an ant task</echo>
    <xslt force="yes" style="${listgraphics}" in="${outputTempDir}/tmp2.xml" out="${outputTempDir}/copy.xml">
      <xmlcatalog>
        <catalogpath>
          <pathelement location="$catalogfile"/>
        </catalogpath>
      </xmlcatalog>
      <factory name="net.sf.saxon.TransformerFactoryImpl"/>
      <classpath location="${oxygenlib}/saxon9ee.jar"/>
      <param name="inputDir" expression="${inputFileDir}"/>
      <param name="inputFile" expression="${inputFilePath}"/>
      <param name="mediaDir" expression="word/media"/>
    </xslt>
    <echo level="info">Copy image files by running ant task</echo>
    <ant antfile="${outputTempDir}/copy.xml"/>
    <echo level="info">Read dimensions of image files and write info file</echo>
    <images outputFile="${outputTempDir}/image-size-info.xml">
      <fileset dir="${outputTempDir}/word" includes="media/*.*"/>
    </images>
    <echo level="info">Generate new files for docx </echo>
    <xslt force="yes" style="${teitoDocx}" in="${outputTempDir}/tmp2.xml" out="${outputTempDir}/newdocument.xml">
      <xmlcatalog>
        <catalogpath>
          <pathelement location="$catalogfile"/>
        </catalogpath>
      </xmlcatalog>
      <factory name="net.sf.saxon.TransformerFactoryImpl"/>
      <classpath location="${oxygenlib}/saxon9ee.jar"/>
      <param name="word-directory" expression="file:///${outputTempDir}"/>
      <!-- name of original input file -->
      <param name="inputFile" expression="${inputFilePath}"/>
      <!-- directory of original input file -->
      <param name="inputDir" expression="${inputFileDir}"/>
      <!-- supply name of language-->
      <param name="lang" expression="${lang}" if="lang"/>
      <!-- emit some diagnostics"-->
      <param name="debug" expression="${debug}" if="debug"/>
      <!-- be verbose-->
      <param name="verbose" expression="${verbose}" if="verbose"/>
    </xslt>
    <move file="${outputTempDir}/docProps/newcore.xml" tofile="${outputTempDir}/docProps/core.xml" overwrite="true"/>
    <move file="${outputTempDir}/docProps/newapp.xml" tofile="${outputTempDir}/docProps/app.xml" overwrite="true"/>
    <move file="${outputTempDir}/docProps/newcustom.xml" tofile="${outputTempDir}/docProps/custom.xml" overwrite="true"/>
  </target>
  <target name="zip">
    <echo level="info">ZIP: put files from ${outputTempDir} together into ${outputFile}</echo>
    <zip destfile="${outputDir}/temp-for-ant.zip" basedir="${outputTempDir}" compress="true" excludes="tmp1.xml,tmp2.xml,image-size-info.xml,copy.xml"/>
    <move file="${outputDir}/temp-for-ant.zip" tofile="${outputFile}" overwrite="true"/>
  </target>
  <target name="cleanup" unless="debug">
    <delete dir="${outputTempDir}"/>
  </target>
  <target name="dist" depends="setup,odd,notodd,transform,nocoverpages,coverpages,zip,cleanup"/>
</project>
