Question

I am generating code with given XML file with Eclipse Jet. But I need to validate input file and throw an exception while generation. For example:

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

How can I handle this?

Was it helpful?

Solution

There is not any function exactly as you describe - the ability to simply terminate code generation. There are some things you can do that would provide the equivalent function:

1) You can perform your test in a choose element and make the scope of the when element be the remainder of the code generation to be performed. This scope could be the entire code generation if you put the test(s) in the 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>

Note that in this usage a choose element is like an if-then-else.

2) Basically the same as (1), except you put the choose in a template being used to generate text/content for a file. This differs in that this usage doesn't prevent the file from being generated. It just reduces the content being written to that single file.

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

I don't think this is what you're looking for

3) This is an advanced topic, but you could write a custom JET tag to perform this kind of conditional processing. It would be like an if element, but the test would be run after the content was processed and would be included in the output only if the test were true. You could test a variable that might be set during that processing in order to prevent the output of the generated code. I'll leave the implementation of that tag to another question.

I think what you want to do is a form of (1): Perform a set of validations of the input file s the first action you take in the main.jet and then make the rest of the main.jet conditional on the results of that validation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top