<?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.
 */
 -->

<!--
 ! Maven plugin for Simian
 !
 ! Author: Aslak Hellesoy (aslak@thoughtworks.com)
-->
<project 
  xmlns:j="jelly:core"
  xmlns:define="jelly:define"
  xmlns:doc="doc"
  xmlns:ant="jelly:ant"
  xmlns:simian="simian">

  <define:taglib uri="simian">
    <define:jellybean
      name="simianLog"
      className="org.apache.maven.simian.SimianLog"
      method="process"
    />
  </define:taglib>

  <goal name="maven-simian-plugin" 
        description="Generate Simian (Code Similarity Analyzer) report." 
        prereqs="maven-simian-plugin:report"/>

  <goal name="maven-simian-plugin:register">
    <j:if test="${sourcesPresent}">
      <doc:registerReport 
        name="Simian Report" 
        pluginName="maven-simian-plugin"
        link="simian-report"
        description="Simian (Code Similarity Analyzer) report."/>
    </j:if>
  </goal>
  
  <goal name="maven-simian-plugin:deregister">
    <doc:deregisterReport name="Simian"/>
  </goal>

  <goal name="maven-simian-plugin:analyze">

    <ant:taskdef name="simian" classname="org.apache.maven.simian.FileSimianTask">
      <ant:classpath>
        <ant:pathelement location="${plugin.dir}"/>
        <ant:pathelement location="${plugin.getDependencyPath('redhill:simian')}"/>
      </ant:classpath>
    </ant:taskdef>

    <ant:simian output="${maven.build.dir}/generated-xdocs/simian.log"
                lineCount="${maven.simian.linecount}"
                failOnDuplication="${maven.simian.failonduplication}">

      <ant:fileset dir="${pom.build.sourceDirectory}">
        <ant:include name="${maven.simian.includes}"/>
        <ant:exclude name="${maven.simian.excludes}"/>
        <!-- FIXME: This is a bad cut and paste -->
        <!-- handle source modifications -->
        <j:forEach var="sm" items="${pom.build.sourceModifications}">
          <ant:available property="classPresent" classname="${sm.className}"/>
          <j:if test="${classPresent != 'true'}">
            <j:forEach var="exclude" items="${sm.excludes}">
              <ant:exclude name="${exclude}"/>
            </j:forEach>
            <j:forEach var="include" items="${sm.includes}">
              <ant:include name="${include}"/>
            </j:forEach>
          </j:if>
        </j:forEach>

      </ant:fileset>

      <j:set var="includeTests" value="${maven.simian.includetests}"/>
      <j:if test="${includeTests == 'true'}">
        <j:if test="${unitTestSourcesPresent == 'true'}">
          <ant:fileset dir="${pom.build.unitTestSourceDirectory}" includes="**/*.java"/>
        </j:if>
      </j:if>

      <j:set var="ignoreStrings" value="${maven.simian.ignorestrings}"/>
      <j:if test="${ignoreStrings == 'true'}">
        <ant:setProperty name="ignoreStrings" value="${ignoreStrings}" />
      </j:if>

      <j:set var="ignoreStringCase" value="${maven.simian.ignorestringcase}"/>
      <j:if test="${ignoreStringCase == 'true'}">
        <ant:setProperty name="ignoreStringCase" value="${ignoreStringCase}" />
      </j:if>

      <j:set var="ignoreNumbers" value="${maven.simian.ignorenumbers}"/>
      <j:if test="${ignoreNumbers == 'true'}">
        <ant:setProperty name="ignoreNumbers" value="${ignoreNumbers}" />
      </j:if>

      <j:set var="ignoreSubtypeNames" value="${maven.simian.ignoresubtypenames}"/>
      <j:if test="${ignoreSubtypeNames == 'true'}">
        <ant:setProperty name="ignoreSubtypeNames" value="${ignoreSubtypeNames}" />
      </j:if>

      <j:set var="ignoreModifiers" value="${maven.simian.ignoremodifiers}"/>
      <j:if test="${ignoreModifiers == 'true'}">
        <ant:setProperty name="ignoreModifiers" value="${ignoreModifiers}" />
      </j:if>

    </ant:simian>

  </goal>

  <goal name="maven-simian-plugin:report">

    <j:choose>
      <j:when test="${sourcesPresent}">

        <attainGoal name="maven-simian-plugin:analyze"/>
        
        <simian:simianLog
          logFile="${maven.build.dir}/generated-xdocs/simian.log"
          jellyContext="${context}"
          sourceDirectory="${pom.build.sourceDirectory}"
          unitTestSourceDirectory="${pom.build.unitTestSourceDirectory}"
        />
        <j:set var="simLog" value="${context.getVariable('simianLog')}"/>
    
        <j:file name="${maven.build.dir}/generated-xdocs/simian-report.xml" prettyPrint="true">
          <j:whitespace xmlns="dummy">

<document>
  <properties>
    <title>Simian Report</title>
  </properties>
  <body>
    <section name="Summary">
      <p>${simLog.copyright1}</p>
      <p>${simLog.copyright2}</p>
      <table summary="Simian report summary">
        <tbody>
        <tr>
          <th>Similarity threshold (lines)</th>
          <td>${maven.simian.linecount}</td>
        </tr>
        <tr>
          <th>Total number of duplicate lines</th>
          <td>${simLog.totalDuplicateLineCount}</td>
        </tr>
        <tr>
          <th>Total number of duplicate blocks</th>
          <td>${simLog.blockCount}</td>
        </tr>
        <tr>
          <th>Total number of files with duplicates</th>
          <td>${simLog.fileWithDuplicateCount}</td>
        </tr>
        <tr>
          <th>Total number of processed lines</th>
          <td>${simLog.loc}</td>
        </tr>
        <tr>
          <th>Total number of processed files</th>
          <td>${simLog.fileParseCount}</td>
        </tr>
        <tr>
          <th>Scan time</th>
          <td>${simLog.time}</td>
        </tr>
        </tbody>
      </table>
    </section>

    <section name="Duplications">
      <j:forEach var="duplication" items="${simLog.duplications}">
        <subsection name="Duplication: ${duplication.lineCount} lines">
          <ul>
          <j:forEach var="duplicationArea" items="${duplication.duplicationAreas}">
            <j:set var="className" value="${duplicationArea.className}"/> 
            <li><a href="${duplicationArea.pathToClass}.html#${duplicationArea.startLine}">${duplicationArea.className}(${duplicationArea.startLine}-${duplicationArea.endLine})</a></li>
          </j:forEach>
          </ul>          
        </subsection>
      </j:forEach>
    </section>
  </body>
</document>

          </j:whitespace>
        </j:file>
        
      </j:when>
      <j:otherwise>
        <ant:echo>No sources to run Simian on</ant:echo>
      </j:otherwise>
    </j:choose>
    
  </goal>
</project>
