You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by sy...@apache.org on 2002/04/17 23:50:46 UTC
cvs commit: xml-cocoon2/src/java/org/apache/cocoon/selection AbstractSwitchSelector.java SwitchSelector.java RequestParameterSelector.java
sylvain 02/04/17 14:50:46
Modified: . changes.xml
src/java/org/apache/cocoon/components/language/markup/sitemap/java
sitemap.xsl
src/java/org/apache/cocoon/components/treeprocessor/sitemap
MatchNodeBuilder.java SelectNodeBuilder.java
src/java/org/apache/cocoon/selection
RequestParameterSelector.java
Added: src/java/org/apache/cocoon/components/treeprocessor/sitemap
SwitchSelectNode.java
src/java/org/apache/cocoon/selection
AbstractSwitchSelector.java SwitchSelector.java
Log:
New SwitchSelector submitted by Marcus Crafter (crafterm@apache.org)
Patch #7244
Revision Changes Path
1.141 +6 -3 xml-cocoon2/changes.xml
Index: changes.xml
===================================================================
RCS file: /home/cvs/xml-cocoon2/changes.xml,v
retrieving revision 1.140
retrieving revision 1.141
diff -u -r1.140 -r1.141
--- changes.xml 14 Apr 2002 02:22:07 -0000 1.140
+++ changes.xml 17 Apr 2002 21:50:46 -0000 1.141
@@ -4,7 +4,7 @@
<!--
History of Cocoon changes
- $Id: changes.xml,v 1.140 2002/04/14 02:22:07 vgritsenko Exp $
+ $Id: changes.xml,v 1.141 2002/04/17 21:50:46 sylvain Exp $
-->
<changes title="History of Changes">
@@ -35,6 +35,9 @@
</devs>
<release version="@version@" date="@date@">
+ <action dev="SW" type="add" due-to="Marcus Crafter" due-to-email="crafterm@apache.org">
+ New SwitchSelector that uses a common context object for all map:when tests.
+ </action>
<action dev="VG" type="fix">
XScript works now (and all XScript SOAP samples).
</action>
@@ -539,7 +542,7 @@
Added RTF serialization of XSL-FO documents (requires jfor).
Patch submitted by Bertrand Delacretaz [bdelacretaz@codeconsult.ch].
Applied patch for the SQLTransformer to output namespaces for the
- generated elements. Patch submitted by Per-Olof Nor謠[pelle@alma.nu].
+ generated elements. Patch submitted by Per-Olof Noren [pelle@alma.nu].
</action>
<action dev="SW" type="add">
New ServerPagesAction and associated "action" and "capture" logicsheets
@@ -565,7 +568,7 @@
</action>
<action dev="CZ" type="update">
Applied patch for the SQLTransformer to output namespaces for the
- generated elements. Patch submitted by Per-Olof Nor謠[pelle@alma.nu].
+ generated elements. Patch submitted by Per-Olof Noren [pelle@alma.nu].
</action>
<action dev="OP" type="update">
Applied patch for incremental XSLT processing from Jörn Heid
1.12 +31 -5 xml-cocoon2/src/java/org/apache/cocoon/components/language/markup/sitemap/java/sitemap.xsl
Index: sitemap.xsl
===================================================================
RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/language/markup/sitemap/java/sitemap.xsl,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- sitemap.xsl 22 Mar 2002 02:20:24 -0000 1.11
+++ sitemap.xsl 17 Apr 2002 21:50:46 -0000 1.12
@@ -193,6 +193,7 @@
import org.apache.cocoon.matching.Matcher;
import org.apache.cocoon.matching.PreparableMatcher;
import org.apache.cocoon.selection.Selector;
+ import org.apache.cocoon.selection.SwitchSelector;
import org.apache.cocoon.sitemap.AbstractSitemap;
import org.apache.cocoon.components.pipeline.StreamPipeline;
import org.apache.cocoon.components.pipeline.EventPipeline;
@@ -215,7 +216,7 @@
* @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
* @author <a href="mailto:barozzi@nicolaken.com">Nicola Ken Barozzi</a>
* @author <a href="mailto:proyal@managingpartners.com">Peter Royal</a>
- * @version CVS $Id: sitemap.xsl,v 1.11 2002/03/22 02:20:24 vgritsenko Exp $
+ * @version CVS $Id: sitemap.xsl,v 1.12 2002/04/17 21:50:46 sylvain Exp $
*/
public class <xsl:value-of select="@file-name"/> extends AbstractSitemap {
static final String LOCATION = "<xsl:value-of select="translate(@file-path, '/', '.')"/>.<xsl:value-of select="@file-name"/>";
@@ -242,16 +243,36 @@
/**
* Method that handles selectors.
*/
- private boolean isSelected(String hint, String testValue, Parameters params, Map objectModel) throws Exception {
- Selector selector = (Selector)this.selectors.select(hint);
+ private boolean isSelected(String hint, String testValue, Parameters params, Map objectModel, Object selectorContext) throws Exception {
+ Component selector = (Selector)this.selectors.select(hint);
try {
- return selector.select(testValue, objectModel, params);
+ if (selector instanceof SwitchSelector)
+ return ((SwitchSelector) selector).select(
+ testValue, selectorContext
+ );
+ else // support normal selector
+ return ((Selector) selector).select(testValue, objectModel, params);
} finally {
this.selectors.release(selector);
}
}
/**
+ * Method that handles selectors.
+ */
+ private Object getSelectorContext(String hint, Map objectModel, Parameters params) throws Exception {
+ Component selector = this.selectors.select(hint);
+ try {
+ if (selector instanceof SwitchSelector)
+ return ((SwitchSelector) selector).getSelectorContext(objectModel, params);
+ } finally {
+ this.selectors.release(selector);
+ }
+ // non-SwitchSelector
+ return null;
+ }
+
+ /**
* Method that handles matchers.
*/
private Map matches(String hint, Object preparedPattern, String pattern, Parameters params, Map objectModel) throws Exception {
@@ -959,6 +980,11 @@
</xsl:choose>
</xsl:variable>
+ Object selectorContext =
+ getSelectorContext(
+ "<xsl:value-of select="$selector-type"/>", objectModel, <xsl:value-of select="$component-param"/>
+ );
+
<!-- loop through all the when cases -->
<xsl:for-each select="map:when">
<!-- remove all invalid chars from the test expression. The result is used to form the name of the generated method
@@ -974,7 +1000,7 @@
<xsl:call-template name="line-number"/>
<xsl:if test="position() > 1">
else </xsl:if>
- if (isSelected("<xsl:value-of select="$selector-type"/>", <xsl:apply-templates select="@test"/>, <xsl:value-of select="$component-param"/>, objectModel)) {
+ if (isSelected("<xsl:value-of select="$selector-type"/>", <xsl:apply-templates select="@test"/>, <xsl:value-of select="$component-param"/>, objectModel, selectorContext)) {
if (debug_enabled) getLogger().debug("Select <xsl:value-of select="$selector-type"/> Test <xsl:value-of select="XSLTFactoryLoader:escape($factory-loader, @test)"/>");
<xsl:apply-templates/>
}
1.3 +3 -3 xml-cocoon2/src/java/org/apache/cocoon/components/treeprocessor/sitemap/MatchNodeBuilder.java
Index: MatchNodeBuilder.java
===================================================================
RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/treeprocessor/sitemap/MatchNodeBuilder.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- MatchNodeBuilder.java 17 Mar 2002 21:55:22 -0000 1.2
+++ MatchNodeBuilder.java 17 Apr 2002 21:50:46 -0000 1.3
@@ -55,13 +55,13 @@
import org.apache.avalon.framework.component.Recomposable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.matching.Matcher;
import org.apache.cocoon.matching.PreparableMatcher;
import org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNodeBuilder;
import org.apache.cocoon.components.treeprocessor.MapStackResolver;
import org.apache.cocoon.components.treeprocessor.ProcessingNode;
-import org.apache.cocoon.components.treeprocessor.LinkedProcessingNodeBuilder;
import org.apache.cocoon.components.treeprocessor.SimpleSelectorProcessingNode;
import java.util.*;
@@ -69,11 +69,11 @@
/**
*
* @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
- * @version CVS $Id: MatchNodeBuilder.java,v 1.2 2002/03/17 21:55:22 sylvain Exp $
+ * @version CVS $Id: MatchNodeBuilder.java,v 1.3 2002/04/17 21:50:46 sylvain Exp $
*/
public class MatchNodeBuilder extends AbstractParentProcessingNodeBuilder
- implements Recomposable {
+ implements Recomposable, ThreadSafe {
private static final String SELECTOR_ROLE = Matcher.ROLE + "Selector";
1.2 +45 -17 xml-cocoon2/src/java/org/apache/cocoon/components/treeprocessor/sitemap/SelectNodeBuilder.java
Index: SelectNodeBuilder.java
===================================================================
RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/treeprocessor/sitemap/SelectNodeBuilder.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SelectNodeBuilder.java 5 Mar 2002 08:26:23 -0000 1.1
+++ SelectNodeBuilder.java 17 Apr 2002 21:50:46 -0000 1.2
@@ -55,6 +55,7 @@
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.selection.Selector;
+import org.apache.cocoon.selection.SwitchSelector;
import org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNodeBuilder;
import org.apache.cocoon.components.treeprocessor.MapStackResolver;
@@ -62,28 +63,40 @@
import org.apache.cocoon.components.treeprocessor.ProcessingNode;
import java.util.*;
+import org.apache.avalon.framework.component.Recomposable;
+import org.apache.avalon.framework.component.ComponentManager;
+import org.apache.avalon.framework.component.ComponentException;
/**
*
* @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
- * @version CVS $Id: SelectNodeBuilder.java,v 1.1 2002/03/05 08:26:23 sylvain Exp $
+ * @version CVS $Id: SelectNodeBuilder.java,v 1.2 2002/04/17 21:50:46 sylvain Exp $
*/
-public class SelectNodeBuilder extends AbstractParentProcessingNodeBuilder implements ThreadSafe {
+public class SelectNodeBuilder extends AbstractParentProcessingNodeBuilder implements ThreadSafe, Recomposable {
- public ProcessingNode buildNode(Configuration config) throws Exception {
+ private static final String SELECTOR_ROLE = Selector.ROLE + "Selector";
+
+ private ComponentManager manager;
+
+ public void compose(ComponentManager manager) {
+ this.manager = manager;
+ }
+
+ public void recompose(ComponentManager manager) {
+ this.manager = manager;
+ }
- String type = this.treeBuilder.getTypeForStatement(config, Selector.ROLE + "Selector");
+ public ProcessingNode buildNode(Configuration config) throws Exception {
- SelectNode node = new SelectNode(type);
- this.treeBuilder.setupNode(node, config);
+ String type = this.treeBuilder.getTypeForStatement(config, SELECTOR_ROLE);
// Lists of ProcessingNode[] and test resolvers for each "when"
List whenChildren = new ArrayList();
List whenTests = new ArrayList();
// Nodes for otherwise (if any)
- ProcessingNode[] otherwhiseNodes = null;
+ ProcessingNode[] otherwiseNodes = null;
Configuration[] childrenConfig = config.getChildren();
for (int i = 0; i < childrenConfig.length; i++) {
@@ -100,13 +113,13 @@
} else if ("otherwise".equals(name)) {
checkNamespace(childConfig);
- if (otherwhiseNodes != null) {
+ if (otherwiseNodes != null) {
String msg = "Duplicate " + name + " (only one is allowed) at " + childConfig.getLocation();
getLogger().error(msg);
throw new ConfigurationException(msg);
}
- otherwhiseNodes = buildChildNodes(childConfig);
+ otherwiseNodes = buildChildNodes(childConfig);
} else if (isParameter(childConfig)) {
// ignore it. It is handled automatically in setupNode()
@@ -118,13 +131,28 @@
throw new ConfigurationException(msg);
}
}
-
- node.setCases(
- (ProcessingNode[][])whenChildren.toArray(new ProcessingNode[0][0]),
- (MapStackResolver[])whenTests.toArray(new MapStackResolver[whenTests.size()]),
- otherwhiseNodes
- );
-
- return node;
+
+ ProcessingNode[][] whenChildrenNodes = (ProcessingNode[][])whenChildren.toArray(new ProcessingNode[0][0]);
+ MapStackResolver[] whenResolvers = (MapStackResolver[])whenTests.toArray(new MapStackResolver[whenTests.size()]);
+
+ // Get the type and class for this selector
+ ComponentsSelector compSelector = (ComponentsSelector)this.manager.lookup(SELECTOR_ROLE);
+
+ // Find selector class
+ Selector selector = (Selector)compSelector.select(type);
+ Class clazz = selector.getClass();
+ compSelector.release(selector);
+
+ if (SwitchSelector.class.isAssignableFrom(clazz)) {
+ SwitchSelectNode node = new SwitchSelectNode(type);
+ this.treeBuilder.setupNode(node, config);
+ node.setCases(whenChildrenNodes, whenResolvers, otherwiseNodes);
+ return node;
+ } else {
+ SelectNode node = new SelectNode(type);
+ this.treeBuilder.setupNode(node, config);
+ node.setCases(whenChildrenNodes, whenResolvers, otherwiseNodes);
+ return node;
+ }
}
}
1.1 xml-cocoon2/src/java/org/apache/cocoon/components/treeprocessor/sitemap/SwitchSelectNode.java
Index: SwitchSelectNode.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.components.treeprocessor.sitemap;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.ComponentSelector;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.environment.Environment;
import org.apache.cocoon.selection.Selector;
import org.apache.cocoon.selection.SwitchSelector;
import org.apache.cocoon.sitemap.PatternException;
import org.apache.cocoon.components.treeprocessor.InvokeContext;
import org.apache.cocoon.components.treeprocessor.MapStackResolver;
import org.apache.cocoon.components.treeprocessor.ParameterizableProcessingNode;
import org.apache.cocoon.components.treeprocessor.ProcessingNode;
import org.apache.cocoon.components.treeprocessor.SimpleSelectorProcessingNode;
import java.util.List;
import java.util.Map;
/**
*
* @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
* @version CVS $Id: SwitchSelectNode.java,v 1.1 2002/04/17 21:50:46 sylvain Exp $
*/
public class SwitchSelectNode extends SimpleSelectorProcessingNode
implements ParameterizableProcessingNode, Composable, Disposable {
/** The parameters of this node */
private Map parameters;
/** Pre-selected selector, if it's ThreadSafe */
protected SwitchSelector threadSafeSelector;
private ProcessingNode[][] whenNodes;
private MapStackResolver[] whenTests;
private ProcessingNode[] otherwhiseNodes;
public SwitchSelectNode(String name) throws PatternException {
super(name);
}
public void setParameters(Map parameterMap) {
this.parameters = parameterMap;
}
public void setCases(ProcessingNode[][] whenNodes, MapStackResolver[] whenTests, ProcessingNode[] otherwhiseNodes) {
this.whenNodes = whenNodes;
this.whenTests = whenTests;
this.otherwhiseNodes = otherwhiseNodes;
}
public void compose(ComponentManager manager) throws ComponentException {
setSelector((ComponentSelector)manager.lookup(Selector.ROLE + "Selector"));
// Get the selector, if it's ThreadSafe
this.threadSafeSelector = (SwitchSelector)this.getThreadSafeComponent();
}
public final boolean invoke(Environment env, InvokeContext context)
throws Exception {
// Prepare data needed by the action
Map objectModel = env.getObjectModel();
List mapStack = context.getMapStack();
Parameters resolvedParams = MapStackResolver.buildParameters(this.parameters, mapStack);
// If selector is ThreadSafe, avoid select() and try/catch block (faster !)
if (this.threadSafeSelector != null) {
Object ctx = this.threadSafeSelector.getSelectorContext(objectModel, resolvedParams);
for (int i = 0; i < this.whenTests.length; i++) {
if (this.threadSafeSelector.select(whenTests[i].resolve(mapStack), ctx)) {
return invokeNodes(this.whenNodes[i], env, context);
}
}
if (this.otherwhiseNodes != null) {
return invokeNodes(this.otherwhiseNodes, env, context);
}
return false;
} else {
SwitchSelector selector = (SwitchSelector)this.selector.select(this.componentName);
Object ctx = selector.getSelectorContext(objectModel, resolvedParams);
try {
for (int i = 0; i < this.whenTests.length; i++) {
if (selector.select(whenTests[i].resolve(mapStack), ctx)) {
return invokeNodes(this.whenNodes[i], env, context);
}
}
if (this.otherwhiseNodes != null) {
return invokeNodes(this.otherwhiseNodes, env, context);
}
return false;
} finally {
this.selector.release(selector);
}
}
}
public void dispose() {
if (this.threadSafeSelector != null) {
this.selector.release(this.threadSafeSelector);
}
}
}
1.5 +14 -10 xml-cocoon2/src/java/org/apache/cocoon/selection/RequestParameterSelector.java
Index: RequestParameterSelector.java
===================================================================
RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/selection/RequestParameterSelector.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- RequestParameterSelector.java 22 Feb 2002 07:03:54 -0000 1.4
+++ RequestParameterSelector.java 17 Apr 2002 21:50:46 -0000 1.5
@@ -53,7 +53,6 @@
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.logger.AbstractLoggable;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
@@ -74,10 +73,11 @@
* @author <a href="mailto:haul@informatik.tu-darmstadt.de">Christian Haul</a>
* @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
* @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
- * @version CVS $Id: RequestParameterSelector.java,v 1.4 2002/02/22 07:03:54 cziegeler Exp $
+ * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
+ * @version CVS $Id: RequestParameterSelector.java,v 1.5 2002/04/17 21:50:46 sylvain Exp $
*/
-public class RequestParameterSelector extends AbstractLoggable
- implements Configurable, ThreadSafe, Selector {
+public class RequestParameterSelector extends AbstractSwitchSelector
+ implements Configurable, ThreadSafe {
protected String defaultName;
@@ -85,20 +85,24 @@
this.defaultName = config.getChild("parameter-name").getValue(null);
}
- public boolean select(String expression, Map objectModel, Parameters parameters) {
+ public Object getSelectorContext(Map objectModel, Parameters parameters) {
+
String name = parameters.getParameter("parameter-name", this.defaultName);
if (name == null) {
getLogger().warn("No parameter name given -- failing.");
- return false;
+ return null;
}
- String value = ObjectModelHelper.getRequest(objectModel).getParameter(name);
- if (value == null) {
- getLogger().debug("Request parameter '" + name + "' not set -- failing.");
+ return ObjectModelHelper.getRequest(objectModel).getParameter(name);
+ }
+
+ public boolean select(String expression, Object selectorContext) {
+ if (selectorContext == null) {
+ getLogger().debug("Request parameter '" + selectorContext + "' not set -- failing.");
return false;
}
- return value.equals(expression);
+ return selectorContext.equals(expression);
}
}
1.1 xml-cocoon2/src/java/org/apache/cocoon/selection/AbstractSwitchSelector.java
Index: AbstractSwitchSelector.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.selection;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.logger.AbstractLoggable;
import java.util.Map;
/**
* Abstract SwitchSelector class.
*
* @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
* @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
* @version CVS $Id: AbstractSwitchSelector.java,v 1.1 2002/04/17 21:50:46 sylvain Exp $
*/
public abstract class AbstractSwitchSelector extends AbstractLoggable
implements SwitchSelector {
/**
* Method to create a selector context.
*
* @param objectModel The <code>Map</code> containing object of the
* calling environment which may be used
* to select values to test the expression.
* @param parameters The sitemap parameters, as specified by
* <parameter/> tags.
* @return selector context
*/
public abstract Object getSelectorContext(Map objectModel, Parameters parameters);
/**
* Selectors test pattern against some objects in a <code>Map</code>
* model and signals success with the returned boolean value
* @param expression The expression to test.
* @return boolean Signals successful test.
*/
public abstract boolean select(String expression, Object selectorContext);
/**
* Selectors test pattern against some objects in a <code>Map</code>
* model and signals success with the returned boolean value
* @param expr The expression to test.
* @return Signals successful test.
*/
public boolean select(String expr, Map objectModel, Parameters params) {
return select(expr, getSelectorContext(objectModel, params));
}
}
1.1 xml-cocoon2/src/java/org/apache/cocoon/selection/SwitchSelector.java
Index: SwitchSelector.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.selection;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import java.util.Map;
/**
* SwitchSelector is an enhanced Selector interface that allows a
* context object to be created to optimize selector conditional testing.
*
* <p>
* The original Selector interface supports an <code>if-then-else</code> 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.
* </p>
*
* <p>
* <pre>
* 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.
* </pre>
* </p>
*
* <p>
* 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.
* </p>
*
* <p>
* <pre>
* 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.
* </pre>
* </p>
*
* @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
* @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
* @version CVS $Id: SwitchSelector.java,v 1.1 2002/04/17 21:50:46 sylvain Exp $
*/
public interface SwitchSelector extends Selector, ThreadSafe {
String ROLE = "org.apache.cocoon.selection.SwitchSelector";
/**
* Method to create a selector context.
*
* @param objectModel The <code>Map</code> containing object of the
* calling environment which may be used
* to select values to test the expression.
* @param parameters The sitemap parameters, as specified by
* <parameter/> tags.
* @return Selector context
*/
Object getSelectorContext(Map objectModel, Parameters parameters);
/**
* Switch Selectors test patterns against a context object
* and signal success with the returned boolean value
* @param expression The expression to test.
* @param selectorContext The context this test should be performed in.
* @return true if the test was successful.
*/
boolean select(String expression, Object selectorContext);
}
----------------------------------------------------------------------
In case of troubles, e-mail: webmaster@xml.apache.org
To unsubscribe, e-mail: cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org