<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!--                                                                         -->
<!-- Generate health reports for the XQuery Test Suite                       -->
<!--                                                                         -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<!--

Please proceed with the following steps:

1) Optionally, get necessary WG files by executing the following:

      ant getPrivateFiles

      this requires providing your username and password, as follows:

      ant getPrivateFiles -Dusername=... -Dpassword=...

   Otherwise, public files will be used.

2) run reports:
      ant reportAll

      or one of:

      ant validate
      ant parseTest
      ant existence

3) A temp directory will be craeted on your behalf and will contain the
   generated reports.      

XQueryX validation will not take place unless the validateXQueryX property is set.

-->



<project name="Generate health reports for XQTS" default="reportAll">
   
   <description>Generate health reports for XQuery Test Suite</description>
   
   
   <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
   <!-- Properties that may be set by the user                               -->
   <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
   
   
   <property
      name="catalogDir"
      value="../../TestSuiteStagingArea/"
      />
   
   <property
      name="destination"
      value="temp/"
      />
   
   <property name="emailName"
      value="${user.name}"
      />
   
   
   <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
   <!-- Derived properties                                                   -->
   <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
   
   
   <property
      name="sourceDir"
      value="${catalogDir}/TestSources/"
      />
   
   <property
      name="resultDir"
      value="${catalogDir}/ReportingResults/"
      />
   
   <property
      name="catalog"
      location="${catalogDir}XQTSCatalog.xml"
      />
   
   <property
      name="catalogSchema"
      location="${catalogDir}XQTSCatalog.xsd"
      />
   
   <property
      name="catalogSchemaLocation"
      value="http://www.w3.org/2005/02/query-test-XQTSCatalog ${catalogSchema}"/>
   
   <property
      name="result"
      location="${resultDir}XQTSSampleResult.xml"
      />
   
   <property
      name="resultSchema"
      location="${resultDir}XQTSResult.xsd"
      />
   
   <property
      name="resultSchemaLocation"
      value="http://www.w3.org/2005/02/query-test-XQTSResult ${resultSchema}"/>
   
   <!-- Queries -->
   
   <property
      name="QueryDir"
      value="${catalogDir}Queries/"
      />
   
   <property
      name="XQueryDir"
      value="${QueryDir}XQuery/"
      />
   
   <property
      name="XQueryXDir"
      value="${QueryDir}XQueryX/"
      />
   
   <!-- XQueryX schema files -->
   
   <property
      name="XQueryXSchema"
      value="http://www.w3.org/2005/XQueryX"/>
   
   <property
      name="XQueryXSchemaFile"
      value="file:temp/xqueryx.xsd"/>
   
   <property
      name="XQueryXSchemaLocation"
      value="${XQueryXSchema} ${XQueryXSchemaFile}"/>
   
   
   
   
   <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
   <!-- Targets                                                              -->
   <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
   
   
   <target name="reportAll" depends="validate, parseTest, existence" description="Generate the health reports">            
      <fail message="sourceDir property must be set.">
         <condition>
            <not>
               <isset property="sourceDir" />
            </not>
         </condition>
      </fail>
   </target>
   
   
   <target name="clean" description="Remove all generated files">
      <delete dir="temp" />
   </target>
   
   
   <target name="createTemp">
      <mkdir dir="temp" />
   </target>
   
   
   <target name="checkFill">
      <available property="tempFilled" file="temp/applet.jar"/>
   </target>
   
   
   <target name="fillTemp" depends="checkFill" unless="tempFilled">
      <antcall target="getPublicFiles" />
   </target>
   
   
   <target
      name="getPublicFiles"
      depends="createTemp"
      description="get public XQuery parser and schema files">
      
      <get
         src="http://www.w3.org/2010/02/qt-applets/xquery10/applet.jar"
         dest="temp/applet.jar" />
      <get
         src="http://www.w3.org/2005/XQueryX/xqueryx.xsd"
         dest="temp/xqueryx.xsd" />
      
      <delete file="temp/applet-ant.zip" />
   </target>   
   
   
   <target
      name="getPrivateFiles"
      depends="createTemp"
      description="get private XQuery parser and schema files">
      
      <get
         src="http://www.w3.org/XML/Group/qtspecs/specifications/grammar-11/parser/applets/xquery10/applet.jar"
         dest="temp/applet.jar"
         username="${username}"
         password="${password}" />
      
      <get
         src="http://www.w3.org/XML/Group/qtspecs/specifications/xqueryx-11/src/xqueryx.xsd"
         dest="temp/xqueryx.xsd"
         username="${username}"
         password="${password}" />
      
      <delete file="temp/applet-ant.zip" />
   </target>   
   
   
   <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
   <!-- Parse Test                                                           -->
   <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
   
   
   <target name="parseTest"
      depends="fillTemp, runTest, reportSuccesses, reportFailures"
      />   
   
   <target name="runTest">
      
      <javac classpath="temp/applet.jar" srcdir="." includes="*.java" destdir="temp" target="1.4"/>
      
      <java
         classname="XQTSParse"
         fork="true"
         newenvironment="true"
         maxmemory="256m"
         output="temp/ParseTest.out"
         >
         <arg value="${catalogDir}"/>
         <arg value="XQTSCatalog.xml"/>
         <arg value="temp/ParseResultsRaw.xml"/>
         <classpath>
            <pathelement path="temp"/>
            <pathelement path="${java.class.path}"/>
            <pathelement path="temp/applet.jar"/>
         </classpath>
         <!--
         <sysproperty key="XQTSParse.username" value="your name"/>
         <sysproperty key="XQTSParse.email" value="your email address"/>
         -->
      </java>
      
      <xslt
         in="temp/ParseResultsRaw.xml"
         destdir="."
         basedir="."
         style="RemoveExceptions.xsl"
         out="temp/ParseResults.xml"
         force="yes">
         <param name="exceptions" expression="Exceptions.xml"/> 
      </xslt>     

      <pathconvert property="parseResult">
         <path location="temp/ParseResults.xml"/>
      </pathconvert>
      
      
      <concat destfile='temp/Parse.xml'>
         <header>
            &lt;results&gt;
         </header>
         <string>
            &lt;results&gt;${parseResult}&lt;/results&gt;
         </string>
         <footer>
            &lt;/results&gt;
         </footer>
      </concat>
      
      
   </target>
   
   <target name="reportSuccesses">
      
      <tstamp>
         <format property="now" pattern="MM/dd/yyyy HH:mm" />
      </tstamp>
      
      <pathconvert property="parse">
         <path location="temp/Parse.xml"/>
      </pathconvert>

      <xslt
         in="${catalogDir}/XQTSCatalog.xml"
         destdir="."
         basedir="."
         style="${catalogDir}/ReportingResults/XQTSResults.xsl"
         out="${destination}ParseTestResults.html"
         force="yes">
         <param name="creationDate" expression="${now}"/> 
         <param name="documentAuthor" expression="${emailName}"/> 
         <param name="resultFiles" expression="${parse}"/> 
         <param name="summary" expression="false"/>
         <param name="impdef" expression="false"/>
         <param name="details" expression="true"/>
         <param name="failures" expression="false"/>
      </xslt>     
      
   </target>
   
   <target name="reportFailures">
      
      <tstamp>
         <format property="now" pattern="MM/dd/yyyy HH:mm" />
      </tstamp>
      
      <xslt
         in="${catalogDir}/XQTSCatalog.xml"
         destdir="."
         basedir="."
         style="${catalogDir}/ReportingResults/XQTSResults.xsl"
         out="${destination}ParseTestFailures.html"
         force="yes">
         <param name="creationDate" expression="${now}"/> 
         <param name="documentAuthor" expression="${emailName}"/> 
         <param name="resultFiles" expression="${parse}"/> 
         <param name="summary" expression="false"/>
         <param name="impdef" expression="false"/>
         <param name="details" expression="true"/>
         <param name="failures" expression="true"/> 
      </xslt>     
      
   </target>
   
   <target name="checkBuild">
      <uptodate property="buildNotRequired" targetfile="temp/applet-ant.zip">
         <srcfiles dir="." includes="*.java" />
      </uptodate>
   </target>
   
   
   <target name="compile" depends="checkBuild" unless="buildNotRequired" >
      
      <fail message="Necessary XQuery files are missing.">
         <condition>
            <not>
               <resourcecount count="1">
                  <dirset dir="." includes="temp" />                  
               </resourcecount>
            </not>
         </condition>
      </fail>
      
      <javac classpath="temp/applet.zip" srcdir="." includes="*.java" destdir="temp" target="1.4"/>
   </target>
   
   
   <target name="build" depends="checkBuild, compile" unless="buildNotRequired" >
      <zip destfile="temp/applet-ant.zip">
         <fileset dir="temp" includes="**/*.class" />
         <zipfileset src="temp/applet.zip" />
      </zip>
   </target>
   
   
   <target name="declare" depends="build">
      <taskdef name="generateXQueryX" classname="org.w3.xqtools.GenXQueryXTask">
         <classpath>
            <pathelement location="temp/applet-ant.zip"/>
         </classpath>
      </taskdef>
   </target>
   
   
   
   <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
   <!-- Validate                                                             -->
   <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
   
   
   <target
      name="validate"
      depends="validateCatalog, validateSourceFiles, validateResult, possiblyValidateXQueryX"
      description="Validate everything">
   </target>
   
   <target name='validateCatalog'>
      <xmlvalidate
         failonerror="yes"
         lenient="no"
         warn="yes"
         file='${catalog}'
         >
         <attribute name="http://xml.org/sax/features/validation" value="true"/>
         <attribute name="http://apache.org/xml/features/validation/schema"  value="true"/>
         <attribute name="http://xml.org/sax/features/namespaces" value="true"/>
         <property name="http://apache.org/xml/properties/schema/external-schemaLocation" value="${catalogSchemaLocation}"/>
      </xmlvalidate>
   </target>
   
   <target name="validateSourceFiles">
      
      <xslt
         in="${catalog}"
         out="temp/validateSourceFiles.xml"
         style="validateSourceFiles.xsl"
         destdir="."
         basedir="."
         force="yes"
         >
         <param name="catalogDir" expression="${catalogDir}"/>
      </xslt>
      
      <ant antfile="temp/validateSourceFiles.xml" />
      
   </target>
   
   <target name='validateResult'>
      <xmlvalidate
         failonerror="yes"
         lenient="no"
         warn="yes"
         file='${result}'
         >
         <attribute name="http://xml.org/sax/features/validation" value="true"/>
         <attribute name="http://apache.org/xml/features/validation/schema"  value="true"/>
         <attribute name="http://xml.org/sax/features/namespaces" value="true"/>
         <property name="http://apache.org/xml/properties/schema/external-schemaLocation" value="${resultSchemaLocation}"/>
      </xmlvalidate>
   </target>
   
   
   <target name='possiblyValidateXQueryX' description="Validate XQueryX files" if="validateXQueryX">
      <antcall target="validateXQueryX" />
   </target>


   <target name='validateXQueryX' depends="fillTemp">
      
      <fail message="sourceDir property must be set.">
         <condition>
            <not>
               <isset property="sourceDir" />
            </not>
         </condition>
      </fail>
      
      <schemavalidate>
         <fileset dir="${XQueryXDir}" includes="**/*.xqx"/>
         <schema namespace="http://www.w3.org/2005/XQueryX" file="temp/xqueryx.xsd" />
      </schemavalidate>
   </target>
   
   
   
   <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
   <!-- Existence                                                            -->
   <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
   
   
   <target name='existence' depends="createTemp">
      <tstamp>
         <format property="now" pattern="MM/dd/yyyy HH:mm" />
      </tstamp>
      
      <pathconvert property="repository">
         <path location="${catalogDir}"/>
      </pathconvert>
      
      <!-- build a list of files in the repository -->
      
      <exec executable='cmd.exe' dir='${repository}' output='temp/repository.xml' os='Windows XP'>
         <arg line='/c dir /B /s'/>
      </exec>
      
      <exec executable='find' dir='${repository}' output='temp/repository.xml' os='Linux'>
         <arg line='${repository} -type f'/>
      </exec>
      
      <!-- make this a valid xml document -->
      
      <replace file='temp/repository.xml'>
         <replacefilter token='${repository}${file.separator}'/>
         <replacefilter token='\' value='/'/>
      </replace>
      <replaceregexp file='temp/repository.xml' match='^' replace='&lt;file name="' byline='true'/>
      <replaceregexp file='temp/repository.xml' match='$' replace='"/&gt;' byline='true'/>
      <concat destfile='temp/repository2.xml'>
         <header>&lt;files&gt;</header>
         <path path='temp/repository.xml'/>
         <footer>&lt;/files&gt;</footer>
      </concat>
      
      <!-- Use XSLT to generate the existence report. -->
      
      <condition property="listXQueryX">
         <isset property="XQueryX"/>
      </condition>
      
      <xslt
         in="${catalog}"
         out="${destination}Existence.html"
         basedir="."
         style="Existence.xsl"
         force="yes">
         <param name="filenames" expression="temp/repository2.xml"/> 
         <param name="now" expression="${now}"/>
         <param name="XQueryX" expression="${listXQueryX}" />
         <param name="userEmail" expression="${emailName}" />
      </xslt>     
      
      <!-- cleanup temporary files -->
      
      <delete file="temp/repository.xml"/>
      <delete file="temp/repository2.xml"/>
      
   </target>
   
   
   <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
   <!-- Testing                                                              -->
   <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
   
   
   <target name="test">
      <echo>${os.name}</echo>
      <echo>${os.arch}</echo>
      <echo>${os.version}</echo>
      <echo>${osfamily}</echo>
      <pathconvert property="fulldir">
         <path location=".."/>
      </pathconvert>
      <echo>${fulldir}</echo>
   </target>
   
</project>
