【问题标题】:How to throw exception while code generation in Eclipse Jet?如何在 Eclipse Jet 中生成代码时抛出异常?
【发布时间】:2014-02-02 14:27:09
【问题描述】:

我正在使用 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>

我该如何处理?

【问题讨论】:

    标签: java xml eclipse code-generation jet


    【解决方案1】:

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

    1) 您可以在选择元素中执行测试,并使 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 元素,但测试将在处理完内容后运行,并且只有在测试为真时才会包含在输出中。您可以测试在该处理期间可能设置的变量,以防止生成的代码的输出。我将把该标签的实现留给另一个问题。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 2015-04-27
      • 2017-04-26
      • 2021-05-07
      • 1970-01-01
      • 2012-11-28
      • 2015-11-22
      相关资源
      最近更新 更多