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.

Attachment: CoreTagLibrary.java.diff.txt
Description: CoreTagLibrary.java.diff.txt

Attachment: CaseTag.java
Description: CaseTag.java

Attachment: DefaultTag.java
Description: DefaultTag.java

Attachment: SwitchTag.java
Description: SwitchTag.java

Attachment: TestSwitchTag.java
Description: TestSwitchTag.java

Attachment: 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>

Reply via email to