<?xml version="1.0"?>

<project name="ognl-docs" default="doc-docbook" basedir=".">

    <path id="cp">
        <fileset dir="lib">
            <include name="**/*.jar"/>
        </fileset>
    </path>

    <property file="./docbook.properties"/>

    <!-- **************************************************************** -->
    <!-- *************************** TaskDefs *************************** -->
    <!-- **************************************************************** -->
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="cp"/>

    <!-- **************************************************************** -->
    <!-- *************************** Docbook **************************** -->
    <!-- **************************************************************** -->

    <!-- ==================== Initialization properties ===================== -->
    <!-- Project information -->
    <property name="project.docbook" location="docbook"/>
    <property name="project.docbook.filelist" value="LanguageGuide,DeveloperGuide"/>

    <!-- Build results setup -->
    <property name="output.docbook.style" location="build/docbook/style"/>
    <property name="output.docbook.fop" location="build/docbook/fop"/>
    <property name="output.docbook.html" location="build/docbook/html"/>
    <property name="output.docbook.pdf" location="build/docbook/pdf"/>

    <!-- DocBook -->

    <property name="docbook.xml.dir" location="${docbook.xml.path}"/>
    <property name="docbook.xsl.dir" location="${docbook.xsl.path}"/>
    <property name="docbook.html.chunked.xsl" location="${output.docbook.style}/${docbook.xsl.base}-html-chunked.xsl"/>
    <property name="docbook.fop.xsl" location="${output.docbook.style}/${docbook.xsl.base}-fop.xsl"/>
    <property name="fop.dir" location="${fop.path}"/>

    <xmlcatalog id="docbook.catalog">
        <dtd publicId="-//OASIS//DTD DocBook XML V${docbook.xml.version}//EN" location="${docbook.xml.dir}/docbookx.dtd"/>
        <dtd publicId="-//OASIS//DTD DocBook CALS Table Model V${docbook.xml.version}//EN" location="${docbook.xml.dir}/calstblx.dtd"/>
        <dtd publicId="-//OASIS//DTD XML Exchange Table Model 19990315//EN" location="${docbook.xml.dir}/soextblx.dtd"/>
        <dtd publicId="-//OASIS//ELEMENTS DocBook Information Pool V${docbook.xml.version}//EN" location="${docbook.xml.dir}/dbpoolx.mod"/>
        <dtd publicId="-//OASIS//ELEMENTS DocBook Document Hierarchy V${docbook.xml.version}//EN" location="${docbook.xml.dir}/dbhierx.mod"/>
        <dtd publicId="-//OASIS//ENTITIES DocBook Additional General Entities V${docbook.xml.version}//EN" location="${docbook.xml.dir}/dbgenent.mod"/>
        <dtd publicId="-//OASIS//ENTITIES DocBook Notations V${docbook.xml.version}//EN" location="${docbook.xml.dir}/dbnotnx.mod"/>
        <dtd publicId="-//OASIS//ENTITIES DocBook Character Entities V${docbook.xml.version}//EN" location="${docbook.xml.dir}/dbcentx.mod"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Diacritical Marks//EN//XML" location="${docbook.xml.dir}/ent/iso-dia.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Numeric and Special Graphic//EN//XML" location="${docbook.xml.dir}/ent/iso-num.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Publishing//EN//XML" location="${docbook.xml.dir}/ent/iso-pub.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES General Technical//EN//XML" location="${docbook.xml.dir}/ent/iso-tech.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Added Latin 1//EN//XML" location="${docbook.xml.dir}/ent/iso-lat1.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Added Latin 2//EN//XML" location="${docbook.xml.dir}/ent/iso-lat2.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Greek Letters//EN//XML" location="${docbook.xml.dir}/ent/iso-grk1.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Monotoniko Greek//EN//XML" location="${docbook.xml.dir}/ent/iso-grk2.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Greek Symbols//EN//XML" location="${docbook.xml.dir}/ent/iso-grk3.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Alternative Greek Symbols//EN//XML" location="${docbook.xml.dir}/ent/iso-grk4.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Added Math Symbols: Arrow Relations//EN//XML" location="${docbook.xml.dir}/ent/iso-amsa.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Added Math Symbols: Binary Operators//EN//XML" location="${docbook.xml.dir}/ent/iso-amsb.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Added Math Symbols: Delimiters//EN//XML" location="${docbook.xml.dir}/ent/iso-amsc.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Added Math Symbols: Negated Relations//EN//XML" location="${docbook.xml.dir}/ent/iso-amsn.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Added Math Symbols: Ordinary//EN//XML" location="${docbook.xml.dir}/ent/iso-amso.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Added Math Symbols: Relations//EN//XML" location="${docbook.xml.dir}/ent/iso-amsr.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Box and Line Drawing//EN//XML" location="${docbook.xml.dir}/ent/iso-box.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Russian Cyrillic//EN//XML" location="${docbook.xml.dir}/ent/iso-cyr1.ent"/>
        <dtd publicId="ISO 8879:1986//ENTITIES Non-Russian Cyrillic//EN//XML" location="${docbook.xml.dir}/ent/iso-cyr2.ent"/>
        <entity publicId="html/chunk.xsl" location="${docbook.xsl.dir}/html/chunk.xsl"/>
        <entity publicId="fo/docbook.xsl" location="${docbook.xsl.dir}/fo/docbook.xsl"/>
    </xmlcatalog>

    <!-- ============================= Targets ============================== -->
    <target name="doc-docbook-html" depends="docbook-html-doc" description="Build OGNL DocBook HTML documentation"/>

    <target name="doc-docbook-pdf" depends="docbook-pdf-doc" description="Build OGNL DocBook PDF documentation"/>

    <target name="doc-docbook" depends="doc-docbook-html, doc-docbook-pdf" description="Build all OGNL DocBook documentation"/>

    <target name="docbook-init">
        <available property="docbook.xml.available" file="${docbook.xml.dir}" type="dir"/>
        <available property="docbook.xsl.available" file="${docbook.xsl.dir}" type="dir"/>
        <available property="fop.available" file="${fop.dir}" type="dir"/>
        <mkdir dir="${output.docbook.style}"/>

        <antcall target="docbook.xml-check"/>
        <antcall target="docbook.xsl-check"/>
        <antcall target="fop-check"/>
    </target>

    <target name="docbook.xml-check" unless="docbook.xml.available">
        <echo>
            Cannot run docbook: DocBook XML is required at extlib/docbook-xml-4.2

            Please download at:
            http://www.docbook.org/xml/4.2/docbook-xml-4.2.zip
        </echo>
        <fail/>
    </target>

    <target name="docbook.xsl-check" unless="docbook.xsl.available">
        <echo>
            Cannot run docbook: DocBook XSL is required at extlib/docbook-xsl-1.65.1

            Please download at:
            http://sourceforge.net/project/showfiles.php?group_id=21935&amp;package_id=16608
        </echo>
        <fail/>
    </target>

    <target name="fop-check" unless="fop.available">
        <echo>
            Cannot run docbook: FOP is required at extlib/fop-0.20.5

            Please download at:
            http://www.apache.org/dyn/closer.cgi/xml/fop
        </echo>
        <fail/>
    </target>

    <target name="docbook-info">
        <echo message="*          project.docbook: ${project.docbook}"/>
        <echo message="*"/>
        <echo message="*         docbook.xml.base: ${docbook.xml.base}"/>
        <echo message="*          docbook.xml.dir: ${docbook.xml.dir}"/>
        <echo message="*         docbook.xsl.base: ${docbook.xsl.base}"/>
        <echo message="*          docbook.xsl.dir: ${docbook.xsl.dir}"/>
        <echo message="* docbook.html.chunked.xsl: ${docbook.html.chunked.xsl}"/>
        <echo message="*          docbook.fop.xsl: ${docbook.fop.xsl}"/>
        <echo message="*"/>
        <echo message="*                 fop.base: ${fop.base}"/>
        <echo message="*                  fop.dir: ${fop.dir}"/>
        <echo message="*"/>
        <echo message="*     output.docbook.style: ${output.docbook.style}"/>
        <echo message="*       output.docbook.fop: ${output.docbook.fop}"/>
        <echo message="*      output.docbook.html: ${output.docbook.html}"/>
        <echo message="*       output.docbook.pdf: ${output.docbook.pdf}"/>
    </target>

    <target name="docbook-clean">
        <delete file="${output.docbook.style}"/>
        <delete file="${output.docbook.html}"/>
        <delete file="${output.docbook.fop}"/>
        <delete file="${output.docbook.pdf}"/>
    </target>

    <!--
        Takes "docbook.file" as a parameter to the file to generate
     -->
    <target name="docbook-html-doc-single">
        <copy file="${project.docbook}/style/html-xsl.template" tofile="${docbook.html.chunked.xsl}">
            <filterset>
                <filter token="docbook.xsl.base" value="${docbook.xsl.base}"/>
                <filter token="docbook.xsl.dir" value="${docbook.xsl.dir}"/>
                <filter token="format" value="chunk"/>
            </filterset>
        </copy>
        <replace token="\" value="/" file="${docbook.html.chunked.xsl}"/>

        <copy todir="${output.docbook.html}">
            <fileset dir="${project.docbook}">
                <include name="**/*.gif"/>
                <include name="**/*.jpg"/>
                <include name="**/*.jpeg"/>
            </fileset>
        </copy>

        <copy todir="${output.docbook.html}" file="${project.docbook}/style/docbook.css"/>
        <uptodate property="uptodate.html" targetfile="${output.docbook.html}/${docbook.file}/index.html">
            <srcfiles dir="${project.docbook}">
                <include name="${docbook.file}.xml"/>
            </srcfiles>
        </uptodate>
        <antcall target="docbook-generate-html-chunked">
            <param name="docbook.file" value="${docbook.file}"/>
        </antcall>
    </target>

    <target name="docbook-generate-html-chunked" unless="uptodate.html">
        <xslt basedir="${project.docbook}"
            style="${docbook.html.chunked.xsl}"
            processor="trax"
            force="true"
            in="${project.docbook}/${docbook.file}.xml"
            out="${output.docbook.html}/${docbook.file}/index.html">
            <param name="base.dir" expression="${output.docbook.html}/${docbook.file}/"/>
            <param name="chunk.quietly" expression="1"/>
            <param name="root.filename" expression="index"/>
            <param name="use.id.as.filename" expression="1"/>
            <xmlcatalog refid="docbook.catalog"/>
        </xslt>
    </target>

    <target name="docbook-html-doc" depends="docbook-init">
        <foreach target="docbook-html-doc-single" list="${project.docbook.filelist}" param="docbook.file"/>
    </target>

    <target name="docbook-pdf-doc-single" depends="docbook-init">
        <mkdir dir="${output.docbook.fop}"/>
        <copy file="${project.docbook}/style/fop-xsl.template" tofile="${docbook.fop.xsl}">
            <filterset>
                <filter token="docbook.xsl.base" value="${docbook.xsl.base}"/>
                <filter token="docbook.xsl.dir" value="${docbook.xsl.dir}"/>
                <filter token="format" value="docbook"/>
            </filterset>
        </copy>
        <replace token="\" value="/" file="${docbook.fop.xsl}"/>

        <copy todir="${output.docbook.fop}/standard-images">
            <fileset dir="${project.docbook}/images">
                <include name="callouts/**/*.gif"/>
            </fileset>
        </copy>
        <copy todir="${output.docbook.fop}/common-images">
            <fileset dir="${project.docbook}/images">
                <include name="navigation/**/*.gif"/>
                <include name="admon/**/*.gif"/>
            </fileset>
            <mapper type="flatten"/>
        </copy>

        <mkdir dir="${output.docbook.pdf}"/>

        <!-- copy project-local images (if they exist) -->
        <copy todir="${output.docbook.fop}">
            <fileset dir="${project.docbook}">
                <include name="images/**/*.gif"/>
                <include name="images/**/*.jpg"/>
                <include name="images/**/*.jpeg"/>
            </fileset>
        </copy>

        <uptodate property="uptodate.fop" targetfile="${output.docbook.fop}/${docbook.file}.fop">
            <srcfiles dir="${project.docbook}">
                <include name="*.xml"/>
            </srcfiles>
        </uptodate>
        <antcall target="docbook-generate-fop"/>

        <uptodate property="uptodate.pdf" targetfile="${output.docbook.pdf}/${docbook.file}.pdf">
            <srcfiles dir="${project.docbook}">
                <include name="*.xml"/>
            </srcfiles>
        </uptodate>
        <antcall target="docbook-convert-fo-to-pdf"/>
    </target>

    <target name="docbook-generate-fop" unless="uptodate.fop">
        <xslt destdir="${output.docbook.fop}"
            style="${docbook.fop.xsl}"
            processor="trax"
            force="true"
            in="${project.docbook}/${docbook.file}.xml"
            out="${output.docbook.fop}/${docbook.file}.fop">
            <param name="base.dir" expression="${output.docbook.html}/${docbook.file}/"/>
            <param name="project.docbook.file" expression="${docbook.file}"/>
            <param name="use.id.as.filename" expression="1"/>
            <xmlcatalog refid="docbook.catalog"/>
        </xslt>
    </target>

    <target name="docbook-convert-fo-to-pdf" unless="uptodate.pdf">
        <echo>Converting FO to PDF ...</echo>
        <java classname="org.apache.fop.apps.Fop" fork="true">
            <classpath>
                <fileset dir="${fop.dir}">
                    <include name="**/*.jar"/>
                </fileset>
            </classpath>
            <arg line="-q"/>
            <arg line="-fo ${output.docbook.fop}/${docbook.file}.fop"/>
            <arg line="-pdf ${output.docbook.pdf}/${docbook.file}.pdf"/>
        </java>
    </target>

    <target name="docbook-pdf-doc" depends="docbook-init">
        <foreach target="docbook-pdf-doc-single" list="${project.docbook.filelist}" param="docbook.file"/>
    </target>

</project>
