<?xml version="1.0"?>

<!--
  =============================================================================
    PMD Plugin. Generate PMD reports using the PMD framework.
  =============================================================================
-->
<project xmlns:j="jelly:core" xmlns:doc="doc">

  <!--
     ========================================================================
       Register a PMD report.
     ========================================================================
  -->
  <goal name="maven-pmd-plugin:register">
    <j:if test="${sourcesPresent}">
      <doc:registerReport 
        name="PMD Report"
        pluginName="pmd"
        description="Verification of coding rules."
        link="pmd-report"/>
    </j:if>
  </goal>

  <!--
     ========================================================================
       Deregister a PMD report.
     ========================================================================
  -->
  <goal name="maven-pmd-plugin:deregister">
    <j:if test="${sourcesPresent}">
      <doc:deregisterReport name="PMD Report"/>
    </j:if>
  </goal>

  <!--
     ========================================================================
       Main PMD plugin goal.
     ========================================================================
  -->
  <goal name="pmd" description="Static Code Analyzer">

    <!-- Only run PMD if it is enabled -->
    <j:set var="enable" value="${maven.pmd.enable}"/>    

    <j:if test="${enable == 'true'}">
      <attainGoal name="pmd:report"/>
    </j:if>    

  </goal>

  <!--
     ========================================================================
       Generate the PMD report.
     ========================================================================
  -->
  <goal name="pmd:report" 
    description="Generate source code report with PMD">

    <!-- Create the dirs if we start from a previously cleaned project -->   
    <mkdir dir="${maven.build.dir}"/>
    <mkdir dir="${maven.docs.dest}"/>

    <!-- Define a PMD task with the rulesets and all jars in the 
         classpath -->   
    <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask">
      <classpath>
        <pathelement location="${plugin.getDependencyPath('pmd:pmd')}"/>
        <pathelement location="${plugin.getDependencyPath('jaxen:jaxen')}"/>
        <pathelement location="${plugin.getDependencyPath('saxpath:saxpath')}"/>
      </classpath>
    </taskdef>
        
    <!-- Run the PMD task - need a way to define the rulesets dynamically -->
    <echo>Running the PMD task with ${maven.pmd.rulesetfiles} ...</echo>

    <!-- Prepare empty raw report because no file is generated if no rule is 
         violated and that causes the report generation to fail later on -->
    <echo file="${maven.build.dir}/pmd-raw-report.xml" 
        message="&lt;?xml version='1.0'?&gt;&lt;pmd/&gt;"/>
    
    <pmd rulesetfiles="${maven.pmd.rulesetfiles}">
      <formatter type="xml" toFile="${maven.build.dir}/pmd-raw-report.xml"/>
      <fileset dir="${pom.build.sourceDirectory}">
        <include name="${maven.pmd.includes}"/>
        <exclude name="${maven.pmd.excludes}"/>

        <!-- FIXME: This is a bad cut and paste -->
        <!-- handle source modifications -->
        <j:forEach var="sm" items="${pom.build.sourceModifications}">
          <available property="classPresent" classname="${sm.className}"/>
          <j:if test="${classPresent != 'true'}">
            <j:forEach var="exclude" items="${sm.excludes}">
              <exclude name="${exclude}"/>
            </j:forEach>
            <j:forEach var="include" items="${sm.includes}">
              <include name="${include}"/>
            </j:forEach>
          </j:if>
        </j:forEach>
      </fileset>
    </pmd>
    
    <!-- Run JSL to transform the report into XDOC -->
          
    <echo>Converting the PMD report to xdoc ...</echo>

    <doc:jsl
      input="${maven.build.dir}/pmd-raw-report.xml"
      output="pmd-report.xml"
      stylesheet="${plugin.resources}/pmd.jsl"
      outputMode="xml"
      prettyPrint="true"
    />

  </goal>
  
</project>
