org.apache.cocoon.selection
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 Selector.select() 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>
  </map:select>

  is interpreted as (pseudo-code):

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

  ie. aSelector.select(...) is called once for each <map:when>
  statement.
  

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 (aSelector.select("test-expr1", selectorContext)) {
   ...
  else if (aSelector.select("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. 
  

Version:
CVS $Id: SwitchSelector.html 1304258 2012-03-23 10:09:27Z ilgrosso $
Author:
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
select
 

Field Detail

ROLE

static final String ROLE
Method Detail

getSelectorContext

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

Parameters:
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.
Returns:
Selector context

select

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

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


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