java - 如何在 Eclipse Jet 中生成代码时抛出异常?

标签 java xml eclipse code-generation jet

我正在使用 Eclipse Jet 使用给定的 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>

请注意,在此用法中,选择元素类似于 if-then-else。

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

// 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>

我认为这不是您要找的

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

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

关于java - 如何在 Eclipse Jet 中生成代码时抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21115063/

相关文章:

java - 安卓 : Nested if statements jump between statements

java - Java 打印非英文字符不正确

java - 从sqlite数据库java中检索图像

xml - Eclipse:保存时自动格式化 XML 文件

Android 模拟器在缩放到实际大小时显示很奇怪

c++ - Ubuntu C++ 可执行文件

java - 如何使我的自定义匹配器通用

java - 将 java 类字节码从 jvm 移动到 jvm

xml - SEO:xml站点地图中hreflang元素的顺序

php - 如何处理通过 POST 发送的 XML?