<?xml version="1.0"?>
<!-- 
/*
 * 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.
 */
 -->


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

  <!--
     ========================================================================
       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 test="${context.getVariable('maven.pmd.cpd.enable') == 'true'}">
          <doc:registerReport 
            name="CPD Report"
            pluginName=""
            description="Detection of copy-paste code."
            link="cpd-report"/>
      </j:if>
    </j:if>
  </goal>

  <!--
     ========================================================================
       Deregister a PMD report.
     ========================================================================
  -->
  <goal name="maven-pmd-plugin:deregister">
    <j:if test="${sourcesPresent}">
      <doc:deregisterReport name="PMD Report"/>
      <j:if test="${context.getVariable('maven.pmd.cpd.enable') == 'true'}">
        <doc:deregisterReport name="CPD Report"/>
      </j:if>
    </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}"/>

    <!-- Setup the classpath for pmd plugins -->
    <path id="pmd-classpath">
      <pathelement path="${plugin.getDependencyPath('pmd:pmd')}" />
      <pathelement path="${plugin.getDependencyPath('jaxen:jaxen')}" />
      <pathelement path="${plugin.getDependencyPath('saxpath:saxpath')}" />
      <pathelement path="${pom.getDependencyClasspath()}" />
      <path refid="maven.dependency.classpath" />
    </path>

    <!-- Define a PMD task with the rulesets and all jars in the 
         classpath -->   
    <taskdef name="pmd" 
      classpathref="pmd-classpath"
      classname="net.sourceforge.pmd.ant.PMDTask" />
        
    <!-- 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}" 
        includes="${maven.pmd.includes}"
        excludes="${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"
    />

    <!-- Generate CPD report if it is enabled -->
    <j:set var="enable" value="${maven.pmd.cpd.enable}"/>

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

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

    <!-- Define a CPD task from the PMD jar -->
    <taskdef name="cpd"
      classpath="${plugin.getDependencyPath('pmd:pmd')}"
      classname="net.sourceforge.pmd.cpd.CPDTask"/>

    <!-- Run the CPD task  -->
    <echo>Running the CPD task with minimumTokenCount = ${maven.pmd.cpd.minimumtokencount} ...</echo>

    <ant:mkdir dir="${maven.build.dir}" />

    <cpd minimumTokenCount="${maven.pmd.cpd.minimumtokencount}"
      outputFile="${maven.build.dir}/cpd-raw-report.txt">
      <!-- cut and paste from pmd:report -->
      <fileset dir="${pom.build.sourceDirectory}" 
        includes="${maven.pmd.includes}"
        excludes="${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>
    </cpd>

    <!-- Convert raw text report into XDOC -->

    <j:set var="genDocs" value="${maven.gen.docs}" />
    <ant:mkdir dir="${genDocs}" />
    <doc:text-xdoc
      title="CPD Report"
      section="CPD Report"
      inputFile="${maven.build.dir}/cpd-raw-report.txt"
      output="${genDocs}/cpd-report.xml"
    />

  </goal>

</project>
