Java 如何在EclipseJet中生成代码时引发异常?

Java 如何在EclipseJet中生成代码时引发异常?,java,xml,eclipse,code-generation,jet,Java,Xml,Eclipse,Code Generation,Jet,我使用EclipseJet使用给定的XML文件生成代码。但我需要验证输入文件并在生成时引发异常。例如: <c:choose select="count(/topItem/childItems)"> <c:when test="0"> // throw exception </c:when> <c:otherwise> // continue code generation </c:otherwise> </c:choose>

我使用EclipseJet使用给定的XML文件生成代码。但我需要验证输入文件并在生成时引发异常。例如:

<c:choose select="count(/topItem/childItems)">
<c:when test="0">
// throw exception
</c:when>
<c:otherwise>
// continue code generation
</c:otherwise>
</c:choose>

//抛出异常
//继续代码生成

我该如何处理这个问题?

没有任何功能完全如您所描述的那样-能够简单地终止代码生成。您可以做一些事情来提供等效的功能:

1) 您可以在choose元素中执行测试,并使when元素的范围成为要执行的代码生成的剩余部分。如果将测试放在main.jet中,则此范围可能是整个代码生成

// in main.jet: 

<c:choose>
    <c:when test="..a condition that should be true in order to continue..">
        // include a template that drives all processing to be
        // performed if the test resolves to true 
        <c:include template="" /> 
    </c:when>
    <c:otherwise>
        // Log the failure to validate and don't process
        <c:log severity="error">Describe the error here</c:log>
    </c:otherwise>
</c:choose>
//在main.jet中:
//包括一个模板,该模板驱动所有处理
//如果测试解析为true,则执行
//记录验证失败并停止处理
请在此处描述错误
注意,在这种用法中,choose元素类似于if-then-else

2) 基本上与(1)相同,只是将choose放在用于生成文件文本/内容的模板中。不同之处在于,此用法不会阻止生成文件。它只是减少了写入单个文件的内容

// in a content-producing template: 

<c:choose>
    <c:when test="..a condition that should be true in order to continue..">
        // Put the remainder of the template logic inside the
        // scope of this when element 
    </c:when>
    <c:otherwise>
        // Log the failure to validate and don't process
        <c:log severity="error">Describe the error here</c:log>
    </c:otherwise>
</c:choose>
//在内容生成模板中:
//将模板逻辑的其余部分放入
//此when元素的范围
//记录验证失败并停止处理
请在此处描述错误
我认为这不是你想要的

3) 这是一个高级主题,但是您可以编写一个自定义JET标记来执行这种条件处理。它类似于if元素,但测试将在处理内容后运行,并且仅当测试为true时才会包含在输出中。您可以测试可能在该处理过程中设置的变量,以防止生成代码的输出。我将把该标记的实现留给另一个问题

我认为您要做的是(1)的一种形式:在main.jet中执行一组输入文件的验证,这是您在main.jet中执行的第一个操作,然后使main.jet的其余部分以该验证的结果为条件