Interface SwitchSelector

All Superinterfaces:
Component, Selector, ThreadSafe
All Known Implementing Classes:
AbstractRegexpSelector, AbstractSwitchSelector, AjaxRequestSelector, ExceptionSelector, MailCommandSelector, RegexpHeaderSelector, RegexpRequestParameterSelector, RequestParameterSelector, RequestSelector, SimpleSelector, XPathExceptionSelector

public interface SwitchSelector
extends Selector, ThreadSafe

SwitchSelector is an enhanced Selector interface that allows a context object to be created to optimize selector conditional testing.

The original Selector interface supports an if-then-else style of conditional testing depending on whether a particular expression is true. This causes to be invoked for each <map:when> statement which may be undesirable due to performance or logic reasons.

  Example, the following sitemap snippet:

  <map:select type="aSelector">
   <map:when test="test-expr1">...</map:when>
   <map:when test="test-expr2">...</map:when>

  is interpreted as (pseudo-code):

  if ("test-expr1", objectModel, params)) {
  } else if ("test-expr2", objectModel, params)) {

  ie. is called once for each <map:when>

SwitchSelector allows the developer to first create a context object which is passed with each call to select(). This context object is created before any conditional tests are made, and hence can be used to optimize conditional testing.

  The above example implemented as a SwitchSelector would be
  interpreted as (psuedo-code):

  Object selectorContext = aSelector.getSelectorContext(objectModel, params);
  if ("test-expr1", selectorContext)) {
  else if ("test-expr2", selectorContext)) {

  ie. the bulk of the selector's work is done in getSelectorContext(),
  select() simply compares whether the expression should be considered true. 

CVS $Id: SwitchSelector.html 1304258 2012-03-23 10:09:27Z ilgrosso $
Marcus Crafter, Sylvain Wallez

Field Summary
static String ROLE
Method Summary
 Object getSelectorContext(Map objectModel, Parameters parameters)
          Method to create a selector context.
 boolean select(String expression, Object selectorContext)
          Switch Selectors test patterns against a context object and signal success with the returned boolean value
Methods inherited from interface org.apache.cocoon.selection.Selector

Field Detail


static final String ROLE
Method Detail


Object getSelectorContext(Map objectModel,
                          Parameters parameters)
Method to create a selector context.

objectModel - The Map containing object of the calling environment which may be used to select values to test the expression.
parameters - The sitemap parameters, as specified by <parameter/> tags.
Selector context


boolean select(String expression,
               Object selectorContext)
Switch Selectors test patterns against a context object and signal success with the returned boolean value

expression - The expression to test.
selectorContext - The context this test should be performed in.
true if the test was successful.

Copyright © 1999-2010 The Apache Software Foundation. All Rights Reserved.