<?xml version="1.0" encoding="UTF-8"?>
<!-- 
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 -->

<project xmlns:j="jelly:core" xmlns:u="jelly:util" xmlns:ant="jelly:ant" xmlns:maven="jelly:maven">
    <goal name="pdf:init" prereqs="xdoc:init">
        <j:set var="docsXdocSrc" value="${pom.getPluginContext('maven-xdoc-plugin').getVariable('maven.docs.src')}"/>
        <j:set var="docsDest" value="${pom.getPluginContext('maven-xdoc-plugin').getVariable('maven.docs.dest')}"/>
        <j:set var="docsGenSrc" value="${pom.getPluginContext('maven-xdoc-plugin').getVariable('maven.gen.docs')}"/>
        <j:set var="docsPdf" value="${maven.build.dir}/pdf"/>
        <j:set var="docsToc" value="${pom.getPluginContext('maven-xdoc-plugin').getVariable('maven.docs.src')}/${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.navigationFile')}"/>
        <j:set var="confidential" value="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.confidential')}"/>
        <j:set var="paperType" value="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.paperType')}"/>
        <j:set var="companyIncName" value="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.companyIncName')}"/>
        <j:set var="publicationYear" value="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.copyrightYear')}"/>
        <j:set var="coverProjectCompany" value="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.cover.projectCompany')}"/>
        <j:set var="coverProjectName" value="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.cover.projectName')}"/>
        <j:set var="coverType" value="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.cover.type')}"/>
        <j:set var="coverVersion" value="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.cover.version')}"/>
        <j:set var="coverDate" value="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.cover.date')}"/>
        <tstamp>
            <format property="coverDate" pattern="${pom.getPluginContext('maven-xdoc-plugin').getVariable('maven.xdoc.date.format')}" locale="${pom.getPluginContext('maven-xdoc-plugin').getVariable('maven.xdoc.date.locale')}"/>
        </tstamp>
        <j:choose>
            <j:when test="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.projectLogo').startsWith('http://')}">
                <j:set var="projectLogo" value="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.projectLogo')}"/>
            </j:when>
            <j:otherwise>
                <j:set var="maven.bin.script" value="${maven.home}/bin/maven"/>
                <maven:makeAbsolutePath var="projectLogo" basedir="${pom.getPluginContext('maven-xdoc-plugin').getVariable('maven.docs.src')}" path="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.projectLogo')}" trim="true"/>
            </j:otherwise>
        </j:choose>
        <j:choose>
            <j:when test="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.companyLogo').startsWith('http://')}">
                <j:set var="companyLogo" value="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.companyLogo')}"/>
            </j:when>
            <j:otherwise>
                <j:set var="maven.bin.script" value="${maven.home}/bin/maven"/>
                <maven:makeAbsolutePath var="companyLogo" basedir="${pom.getPluginContext('maven-xdoc-plugin').getVariable('maven.docs.src')}" path="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.companyLogo')}" trim="true"/>
            </j:otherwise>
        </j:choose>
        <j:set var="imageDpi" value="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.imageDpi')}"/>
        <j:set var="debug" value="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.debug')}"/>
        <j:set var="pdfName" value="${pom.getPluginContext('maven-pdf-plugin').getVariable('maven.pdf.pdfName')}"/>
        <mkdir dir="${docsPdf}"/>
    </goal>
    <goal name="fo" prereqs="fo:fo" description="This simply pre-requisites the fo:fo goal. "/>
    <goal name="fo:fo" prereqs="pdf:prepare" description="Generates an fo xml document from the ${maven.pdf.navigationFile} file supplied as part of the documentation. Please note that as yet, no sanity check is done to make sure this file exists.">
        <echo>Generating ${docsDest}/project.fo ...</echo>
        <available file="${docsToc}" property="maven.fo.navigation.available"/>
        <fail message="PDF generation cannot complete as ${docsToc} is missing" unless="maven.fo.navigation.available"/>
        <!-- 'Major hack.' Stolen from the Latka plugin.jelly. All of this could be avoided by putting
         xalan into $MAVEN_HOME/lib/endorsed directory in the distribution, but never mind -->
    ${systemScope.setProperty('javax.xml.transform.TransformerFactory','org.apache.xalan.processor.TransformerFactoryImpl')}
    <style in="${docsToc}" out="${docsPdf}/project.fo" style="${plugin.resources}/project2fo.xslt" force="true">
            <param name="basePath" expression="${docsPdf}"/>
            <param name="confidential" expression="${confidential}"/>
            <param name="paperType" expression="${paperType}"/>
            <param name="companyIncName" expression="${companyIncName}"/>
            <param name="publicationYear" expression="${publicationYear}"/>
            <param name="coverProjectCompany" expression="${coverProjectCompany}"/>
            <param name="coverProjectName" expression="${coverProjectName}"/>
            <param name="coverType" expression="${coverType}"/>
            <param name="coverVersion" expression="${coverVersion}"/>
            <param name="coverDate" expression="${coverDate}"/>
            <param name="companyLogo" expression="${companyLogo}"/>
            <param name="projectLogo" expression="${projectLogo}"/>
            <param name="imageDpi" expression="${imageDpi}"/>
            <param name="debug" expression="${debug}"/>
        </style>
    </goal>
    <goal name="pdf" prereqs="fo:fo,pdf:prepare,pdf:pdf" description="This is the default goal of the plugin and simply pre-requisites the pdf:pdf goal. "/>
    <goal name="pdf:prepare" prereqs="pdf:init" description="Stages all files needed to generate the PDF">
        <!-- First, copy all of the generated xdocs -->
        <copy todir="${docsPdf}">
            <fileset dir="${docsGenSrc}" force="true">
                <include name="**/*.*"/>
            </fileset>
        </copy>
        <!-- Copy all of the user-supplied xdocs -->
        <copy todir="${docsPdf}" force="true" overwrite="true">
            <fileset dir="${docsXdocSrc}">
                <include name="**/*.*"/>
            </fileset>
        </copy>
        <!-- Then, copy all of images generated -->
        <copy todir="${docsPdf}" force="true">
            <fileset dir="${docsDest}">
                <include name="**/*.gif"/>
                <include name="**/*.jpg"/>
                <include name="**/*.png"/>
            </fileset>
        </copy>
        <!-- Finally, copy the FOP config file and all of the fonts -->
        <copy todir="${docsPdf}" force="true">
            <fileset dir="${plugin.resources}">
                <include name="userconfig.xml"/>
                <include name="fonts/*.*"/>
            </fileset>
        </copy>
    </goal>
    <goal name="pdf:pdf" description="Generates a PDF document containing all project documentation. The PDF is generated from the .fo file generated in the fo:fo goal." prereqs="pdf:prepare">
        <echo>Generating ${docsPdf}/${pdfName} ...</echo>
        <echo>Config file: ${docsPdf}/userconfig.xml</echo>
        <java classname="org.apache.fop.apps.Fop" fork="yes" failonerror="true" maxmemory="500m">
            <classpath>
                <pathelement location="${plugin.getDependencyPath('fop')}"/>
                <pathelement location="${plugin.getDependencyPath('batik')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-awt-util')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-bridge')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-css')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-dom')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-ext')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-extension')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-gui-util')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-gvt')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-parser')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-rasterizer')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-script')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-svg-dom')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-svggen')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-swing')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-transcoder')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-util')}"/>
                <pathelement location="${plugin.getDependencyPath('batik:batik-xml')}"/>
                <pathelement location="${plugin.getDependencyPath('logkit')}"/>
                <pathelement location="${plugin.getDependencyPath('avalon-framework')}"/>
                <pathelement location="${plugin.getDependencyPath('xerces')}"/>
                <pathelement location="${plugin.getDependencyPath('xml-apis')}"/>
            </classpath>
            <arg value="-c"/>
            <arg value="${docsPdf}/userconfig.xml"/>
            <if test="${debug}">
                <arg value="-d"/>
            </if>
            <arg value="-fo"/>
            <arg value="${docsPdf}/project.fo"/>
            <arg value="-pdf"/>
            <arg value="${docsPdf}/${pdfName}"/>
            <sysproperty key="javax.xml.transform.TransformerFactory" value="org.apache.xalan.processor.TransformerFactoryImpl"/>
        </java>
        <copy todir="${docsDest}" file="${docsPdf}/${pdfName}"/>
    </goal>
</project>
