<?xml version="1.0"?>

<project xmlns:j="jelly:core"
         xmlns:ant="jelly:ant"
         xmlns:antlr="jelly:antlr"
         xmlns:util="jelly:util"
         xmlns:maven="jelly:maven">

  <j:set var="antlrSrcDir" value="${maven.antlr.src.dir}"/>

  <goal name="antlr:prepare-filesystem"
        description="Make any necessary directories for antlr processing">
    <j:if test="${!antlrSrcDir.equals('MAVEN_ANTLR_SRC_DIR_NOT_SET')}">
      <ant:mkdir dir="${maven.build.dir}/antlr"/>
    </j:if>
  </goal>

  <goal name="antlr:generate"
        description="Generate source from antlr grammars"
        prereqs="antlr:prepare-filesystem">
    <j:if test="${!antlrSrcDir.equals('MAVEN_ANTLR_SRC_DIR_NOT_SET')}">

      <ant:path id="maven.antlr.compile.src.set"
            location="${maven.build.dir}/antlr"/>

      <maven:addPath id="maven.compile.src.set"
                     refid="maven.antlr.compile.src.set"/>

      <util:tokenize var="grammars" delim=" ">
         ${maven.antlr.grammars}
      </util:tokenize>

      <antlr:antlr outputDir="${maven.build.dir}/antlr">
        <j:forEach var="grammar" items="${grammars.iterator()}">
          <antlr:grammar>${grammar}</antlr:grammar>
        </j:forEach>
      </antlr:antlr>
    </j:if>
  </goal>

  <preGoal name="java:compile">
    <j:if test="${!antlrSrcDir.equals('MAVEN_ANTLR_SRC_DIR_NOT_SET')}">
      <attainGoal name="antlr:generate"/>
    </j:if>
  </preGoal>

</project>
