<?xml version="1.0"?>
<!--
  Copyright 2002-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 default="validate" basedir="." name="validate">
<!--
  <target name="validate" depends="validation-props, validate-xdocs,
    validate-sitemap, validate-skins, validate-skinchoice,
    validate-stylesheets"  description="Validates XML doc files in the project"/>
-->   
    
  <target name="validate"/>
        
             
  <target name="validation-props" depends="examine-proj">
    <!-- Validation flags -->
    <property name="forrest.validate" value="true" />
    <condition property="validate">
      <istrue value="${forrest.validate}"/>
    </condition>
    <property name="forrest.validate.failonerror" value="true" />
    <property name="forrest.validate.includes" value="**/*"/>
    <property name="forrest.validate.excludes" value=""/>
    <property name="forrest.validate.xdocs.includes" value="**/*.xml"/>
    <property name="forrest.validate.xdocs.excludes" value="site.xml,.*.swp"/>
    <property name="forrest.validate.skinconf.includes" value="${skinconf-file}"/>
    <property name="forrest.validate.skinconf.excludes" value=""/>
    <property name="forrest.validate.stylesheets.includes" value="**/*.xsl"/>
    <property name="forrest.validate.skinconf.excludes" value=""/>
    <property name="forrest.validate.skins.includes" value="**/*"/>
    <property name="forrest.validate.skins.excludes" value="**/*.xsl"/>
    <property name="forrest.validate.skins.stylesheets.includes" value="**/*.xsl"/>
    <property name="forrest.validate.skins.stylesheets.excludes" value=""/>
  </target>    
        
  <target name="validate-site" depends="load-project-props">
    <condition property="forrest.validate.skinconf.disable-xml-link">
      <and>
        <istrue value="${skinconfig.disable-xml-link}"/>
      </and>
    </condition>
    <fail if="forrest.validate.skinconf.disable-xml-link">
---------------------------------------     
${skinconfig.project-name} have disable-xml-link
please change it on ${skinconf} 
to validate this site.
---------------------------------------     
    </fail>
    <xmlvalidate failonerror="${forrest.validate.site.failonerror}" lenient="no" warn="yes">
      <attribute name="http://apache.org/xml/features/validation/dynamic"  value="true"/>
      <xmlcatalog id="fcatalog" >
        <catalogpath>
          <pathelement
            location="${forrest.home}/context/resources/schema/catalog.xcat"/>
          <pathelement location="${catalog}"/>
        </catalogpath>
      </xmlcatalog>
      <fileset dir="${project.site-dir}"
        includes="${forrest.validate.xdocs.includes}"
        excludes="${forrest.validate.xdocs.excludes}"/>
    </xmlvalidate>
    <echo message="...validated site"/>
  </target>

  <target name="validate-xdocs" depends="validation-props">
    <xmlvalidate failonerror="${forrest.validate.failonerror}" lenient="no" warn="yes">
      <attribute name="http://apache.org/xml/features/validation/dynamic"  value="true"/>
      <xmlcatalog refid="fcatalog" />
      <fileset dir="${xdocs-dir}"
        includes="${forrest.validate.xdocs.includes}"
        excludes="${forrest.validate.xdocs.excludes}"/>
    </xmlvalidate>
    <echo message="...validated xdocs"/>
  </target>

  <target name="validate-sitemap" depends="validation-props"
    if="validate.sitemap">
    <available property="version-specific-sitemap" file="${sitemap-dir}/forrest-${version}.xmap" type="file"/>
    <available property="version-specific-sitemap" file="${sitemap-dir}/aggregate-${version}.xmap" type="file"/>
    <available property="version-specific-sitemap" file="${sitemap-dir}/dtd-${version}.xmap" type="file"/>
    <available property="version-specific-sitemap" file="${sitemap-dir}/faq-${version}.xmap" type="file"/>
    <available property="version-specific-sitemap" file="${sitemap-dir}/forrest-${version}.xmap" type="file"/>
    <available property="version-specific-sitemap" file="${sitemap-dir}/issues-${version}.xmap" type="file"/>
    <available property="version-specific-sitemap" file="${sitemap-dir}/linkmap-${version}.xmap" type="file"/>
    <available property="version-specific-sitemap" file="${sitemap-dir}/menu-${version}.xmap" type="file"/>
    <available property="version-specific-sitemap" file="${sitemap-dir}/profiler-${version}.xmap" type="file"/>
    <available property="version-specific-sitemap" file="${sitemap-dir}/raw-${version}.xmap" type="file"/>
    <available property="version-specific-sitemap" file="${sitemap-dir}/resources-${version}.xmap" type="file"/>
    <available property="version-specific-sitemap" file="${sitemap-dir}/revisions-${version}.xmap" type="file"/>
    <available property="version-specific-sitemap" file="${sitemap-dir}/sitemap-${version}.xmap" type="file"/>
    <available property="version-specific-sitemap" file="${sitemap-dir}/status-${version}.xmap" type="file"/>

    <jing rngfile="${forrest.home}/context/resources/schema/relaxng/sitemap-v06.rng">
      <fileset dir="${content-dir}"
        includes="${forrest.validate.sitemap.includes}"
        excludes="${forrest.validate.sitemap.excludes}">
        <include name="*.xmap" unless="version-specific-sitemap"/>
        <include name="*-${version}.xmap" if="version-specific-sitemap"/>
      </fileset>
    </jing>
    <echo message="...validated sitemap"/>
  </target>

  <target name="validate-stylesheets" depends="validation-props">
    <jing rngfile="${forrest.home}/context/resources/schema/relaxng/xslt.rng">
      <fileset dir="${stylesheets-dir}"
        includes="${forrest.validate.stylesheets.includes}"
        excludes="${forrest.validate.stylesheets.excludes}" />
    </jing>
    <echo message="...validated stylesheets"/>
  </target>

  <target name="validate-skins" depends="validate-skins-stylesheets"/>

  <target name="validate-skins-stylesheets" depends="validation-props">
    <jing rngfile="${forrest.home}/context/resources/schema/relaxng/xslt.rng">
      <fileset dir="${skins-dir}"
        includes="${forrest.validate.skins.stylesheets.includes}"
        excludes="${forrest.validate.skins.stylesheets.excludes}"/>
    </jing>
    <echo message="...validated skin stylesheets"/>
  </target>

  <!-- Check that ${project.skin} points to an existing skin. -->
  <target name="validate-skinchoice">
    <condition property="project.skin.present">
      <available file="${project.skin}" type="dir">
        <filepath path="${skins-dir}"/>
        <filepath path="${forrest.skins-dir}"/>
      </available>
    </condition>

    <loadfile property="skin-not-found-message" srcFile="${forrest.home}/var/skin-not-found-message.txt">
      <filterchain><expandproperties/></filterchain>
    </loadfile>
      
    <fail unless="project.skin.present">
      ${skin-not-found-message}
    </fail>
    <echo message="...validated existence of skin '${project.skin}'"/>
  </target>
  
  </project>
