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


<project
  xmlns:ant="jelly:ant"
  xmlns:j="jelly:core"
  xmlns:x="jelly:xml"  
  xmlns:html="jelly:html"
  xmlns:util="jelly:util">

  <!-- FIXME: This should work like other xdoc generating plugins -->
  <goal name="html2xdoc" prereqs="xdoc:init"
    description="Generates XDoc documentation from normal HTML files">

    <j:set var="destdir" value="${maven.gen.docs}"/>
    <!-- FIXME: Shouldn't need to do this check -->
    <j:if test="${empty(destdir)}">
      <j:set var="destdir" value="${maven.build.dir}/generated-xdocs"/>
    </j:if>
    
    <j:set var="srcdir" value="${maven.html2xdoc.dir}"/>    
    <j:if test="${empty(srcdir)}">
      <!-- FIXME? Should be maven.docs.src -->
      <j:set var="srcdir" value="${basedir}/xdocs"/>
    </j:if>
    
    <j:set var="outputencoding" value="${maven.docs.outputencoding}"/>

    <!-- mapper for determining output file name -->
    <j:new var="mapper" className="org.apache.maven.util.CaseInsensitiveGlobPatternMapper"/>

    <util:file var="srcdirFile" name="${srcdir}"/>
    <j:set var="srcdir" value="${srcdirFile.getCanonicalPath()}"/>
    <util:available file="${srcdir}">
      <util:file var="destdirFile" name="${destdir}"/>
      <j:set var="destdir" value="${destdirFile.getCanonicalPath()}"/>
     

      <util:replace var="fromPattern" oldChar="\" newChar="/" value="${srcdir}/*.html"/>
      <util:replace var="toPattern" oldChar="\" newChar="/" value="${destdir}/*.xml"/>
      <j:setProperties object="${mapper}" from="${fromPattern}" to="${toPattern}"/>

      <util:replace var="fromDirPattern" oldChar="\" newChar="/" value="${srcdir}*"/>
      <util:replace var="toDirPattern" oldChar="\" newChar="/" value="${destdir}*"/>
      
      <j:new var="dirMapper" className="org.apache.maven.util.CaseInsensitiveGlobPatternMapper"/>
      <j:setProperties object="${dirMapper}" from="${fromDirPattern}" to="${toDirPattern}"/>

      <ant:fileScanner var="docFiles">
        <ant:fileset dir="${srcdir}">
          <ant:patternset>
            <ant:include name="**/*.html"/>
          </ant:patternset>
        </ant:fileset>
      </ant:fileScanner>

      <j:forEach var="file" items="${docFiles.iterator()}">

        <!-- tool for converting HTML into XDoc -->
        <j:useBean class="org.apache.maven.html2xdoc.Html2XdocBean" var="htmlTool"/>

        <util:replace var="inDirForward" oldChar="\" newChar="/" value="${file.parent}"/>
        <j:set var="outDir" value="${dirMapper.mapFileName(inDirForward).0}"/>
        <ant:mkdir dir="${outDir}"/>

        <!-- generate output file name -->
        <util:replace var="outFileForward" oldChar="\" newChar="/" value="${file.toString()}"/>
        <j:set var="outFile" value="${mapper.mapFileName(outFileForward).0}"/>

        <ant:echo>Generating ${outFile} from ${file}</ant:echo>
        
        <!-- parse the HTML doc and pass it to the stylesheet -->
        <html:parse var="htmlDoc" html="${file}" element="lower" attribute="lower"/>
        
        <!-- now lets convert the document into XML -->
        <j:set var="xmlDoc" value="${htmlTool.convert(htmlDoc)}"/>

        <j:file name="${outFile}" encoding="${outputencoding}" outputMode="xml">
          <x:copyOf select="$xmlDoc"/>
        </j:file>
      </j:forEach>
    </util:available>
  </goal>

  <preGoal name="xdoc:jelly-transform">
    <j:set var="enabled" value="${maven.html2xdoc.enabled}"/>
    <j:if test="${enabled}">
      <echo>html2xdoc is enabled</echo>
      <attainGoal name="html2xdoc"/>
    </j:if>
  </preGoal>
</project>
