I frequently find myself writing Jelly scripts like:
<j:choose>
<j:when test="${some.complicated.expression().equals('foo')}">
<!-- do something -->
</j:when>
<j:when test="${some.complicated.expression().equals('bar')}">
<!-- do something else -->
</j:when>
<!-- ... -->
<j:otherwise>
<!-- do something else -->
</j:otherwise>
</j:choose>
The redudancy of some.complicated.expression() is required, since
choose/when/otherwise is implementing an if/else if/else expression. What I really
want is a switch/case expression, e.g.,:
<j:switch on="${some.complicated.expression()}">
<j:case value="foo">
<!-- do something -->
</j:case>
<j:case value="bar">
<!-- do something else -->
</j:case>
<!-- ... -->
<j:default>
<!-- do something else -->
</j:default>
</j:switch>
(I am aware that I could set/unset a variable to factor out
"some.complicated.expression()", but that doesn't solve the problem very elegantly,
IMO. Also true switch/case/default support lets you do more interesting things than
if/else--like doubling up cases by "falling thru".)
The attached patch adds support for these tags, as well as a few simple unit tests for
them.
PS: I already have karma to commons and commons-sandbox and hence can commit this
directly if you want to avoid dealing with the patch itself. I just wanted to run
this by everyone first.
- R.
CoreTagLibrary.java.diff.txt
Description: CoreTagLibrary.java.diff.txt
CaseTag.java
Description: CaseTag.java
DefaultTag.java
Description: DefaultTag.java
SwitchTag.java
Description: SwitchTag.java
TestSwitchTag.java
Description: TestSwitchTag.java
testSwitchTag.jelly
Description: testSwitchTag.jelly
-- To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@;jakarta.apache.org> For additional commands, e-mail: <mailto:commons-dev-help@;jakarta.apache.org>
