2014-01-14 29 views
2

我使用Eclipse Jet生成給定XML文件的代碼。但我需要驗證輸入文件並在生成時拋出異常。例如:如何在Eclipse Jet中生成代碼時拋出異常?

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

我該如何處理?

回答

1

完全沒有你描述的任何功能 - 簡單地終止代碼生成的能力。有一些事情可以提供等效功能:

1)您可以在choose元素中執行測試,並使when元素的作用域成爲要執行的代碼生成的其餘部分。這個範圍可以是整個代碼生成,如果你把測試(S)在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> 

注意,在此使用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的其餘部分有條件在驗證的結果上。