<project name="pmd" default="alljavacc" basedir="../../">

    <property name="javacc-home.path" value="target/lib" />

    <target name="alljavacc"
            description="Generates all JavaCC aspects within PMD"
            depends="checkUpToDate,init,jspjjtree,cleanup" />

    <target name="checkUpToDate">
        <uptodate property="javaccBuildNotRequired" targetfile="${target}/last-generated-timestamp">
            <srcfiles dir="etc/grammar" includes="*.jj*"/>
        </uptodate>
        <echo message="up to date check: javaccBuildNotRequired=${javaccBuildNotRequired}"/>
    </target>

    <target name="init" unless="javaccBuildNotRequired">
        <mkdir dir="${javacc-home.path}" />
        <copy file="${javacc.jar}" tofile="${javacc-home.path}/javacc.jar" />

        <mkdir dir="${target}"/>
        <touch file="${target}/last-generated-timestamp"/>
    </target>

    <target name="cleanup">
        <delete dir="${javacc-home.path}" />
    </target>

    <target name="jspjjtree" description="Generates the JSP parser and AST source files" unless="javaccBuildNotRequired">
        <delete dir="${target}/net/sourceforge/pmd/lang/jsp/ast/" />
        <mkdir dir="${target}/net/sourceforge/pmd/lang/jsp/ast/" />
        <jjtree target="etc/grammar/JspParser.jjt"
                outputdirectory="${target}/net/sourceforge/pmd/lang/jsp/ast/"
                javacchome="${javacc-home.path}" />
        <!-- Ensure generated using CharStream interface -->
        <javacc static="false"
                usercharstream="true"
                unicodeinput="true"
                javaunicodeescape="false"
                target="${target}/net/sourceforge/pmd/lang/jsp/ast/JspParser.jj"
                outputdirectory="${target}/net/sourceforge/pmd/lang/jsp/ast"
                javacchome="${javacc-home.path}" />
        <delete file="${target}/net/sourceforge/pmd/lang/jsp/ast/Node.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/jsp/ast/SimpleNode.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/jsp/ast/CharStream.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/jsp/ast/TokenMgrError.java" />
        <replace file="${target}/net/sourceforge/pmd/lang/jsp/ast/JspParserTokenManager.java"
                 token="class JspParserTokenManager"
                 value="class JspParserTokenManager extends net.sourceforge.pmd.lang.ast.AbstractTokenManager" />
        <replace file="${target}/net/sourceforge/pmd/lang/jsp/ast/JspParser.java"
                 token="throw new Error"
                 value="throw new RuntimeException" />
        <replace file="${target}/net/sourceforge/pmd/lang/jsp/ast/ParseException.java"
                 token="extends Exception"
                 value="extends net.sourceforge.pmd.lang.ast.ParseException" />
        <replace file="${target}/net/sourceforge/pmd/lang/jsp/ast/JspParserVisitor.java"
                 token="SimpleNode"
                 value="JspNode" />
        <replace file="${target}/net/sourceforge/pmd/lang/jsp/ast/JJTJspParserState.java">
            <replacetoken>public class</replacetoken>
            <replacevalue><![CDATA[import net.sourceforge.pmd.lang.ast.Node;

public class]]></replacevalue>
        </replace>
    	<replace file="${target}/net/sourceforge/pmd/lang/jsp/ast/Token.java">
    	    <replacetoken>public class Token</replacetoken>
    	    <replacevalue><![CDATA[import net.sourceforge.pmd.lang.ast.GenericToken;

public class Token extends GenericToken]]></replacevalue>
		</replace>

        <delete>
            <fileset dir="${target}/net/sourceforge/pmd/lang/jsp/ast">
                <include name="AST*.java" />
            </fileset>
        </delete>
    </target>

</project>
