You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by jh...@apache.org on 2005/11/03 15:00:48 UTC
svn commit: r330548 [51/132] - in
/cocoon/whiteboard/maven2/cocoon-flat-layout: ./ cocoon-ajax-block/
cocoon-ajax-block/api/ cocoon-ajax-block/api/src/
cocoon-ajax-block/api/src/main/ cocoon-ajax-block/api/src/main/java/
cocoon-ajax-block/api/src/main/...
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/AbstractRegexpSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/AbstractRegexpSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/AbstractRegexpSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/AbstractRegexpSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,162 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+import java.util.HashMap;
+import java.util.Map;
+
+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.thread.ThreadSafe;
+import org.apache.regexp.RE;
+import org.apache.regexp.RECompiler;
+import org.apache.regexp.REProgram;
+import org.apache.regexp.RESyntaxException;
+
+/**
+ * <p>The {@link AbstractRegexpSelector} abstract class defines a simple selector
+ * operating over configured regular-expression patterns.</p>
+ *
+ * <p>Configuration of an {@link AbstractRegexpSelector} is quite simple: first of
+ * all the patterns used for selections must be configured:</p>
+ *
+ * <pre>
+ * <map:components>
+ * ...
+ * <map:selectors default="...">
+ * <map:selector name="..." src="org.apache.cocoon.selection....">
+ * <pattern name="empty">^$</pattern>
+ * <pattern name="number">^[0-9]+$</pattern>
+ * <pattern name="string">^.+$</pattern>
+ * </map:selector>
+ * </map:selectors>
+ * </map:components>
+ * </pre>
+ *
+ * <p>Then, each configured pattern can be referenced in the pipelines section of
+ * the sitemap:</p>
+ *
+ * <pre>
+ * <map:pipelines>
+ * ...
+ * <map:match ...>
+ * ...
+ * <map:select type="browser">
+ * <map:when test="empty">...</map:when>
+ * <map:when test="number">...</map:when>
+ * <map:when test="string">...</map:when>
+ * <map:otherwise>...</map:otherwise>
+ * </map:select>
+ * ...
+ * </map:match>
+ * ...
+ * </map:pipelines>
+ * </pre>
+ *
+ * @author <a href="mailto:pier@apache.org">Pier Fumagalli</a>
+ * @version CVS $Id: AbstractRegexpSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+public abstract class AbstractRegexpSelector extends AbstractSwitchSelector
+implements Configurable, ThreadSafe {
+
+ /** <p>A {@link Map} of regular expression programs by name.</p> */
+ protected Map patterns = new HashMap();
+
+ /**
+ * <p>Create a new {@link AbstractRegexpSelector} instance.</p>
+ */
+ protected AbstractRegexpSelector() {
+ super();
+ }
+
+ /**
+ * <p>Select a pipeline fragment based on a previously configured pattern.</p>
+ *
+ * @param patternName the name of the configured pattern.
+ * @param selectorContext the string to be matched by the named pattern.
+ * @return <b>true</b> if the contexts is matched by the configured pattern.
+ */
+ public boolean select(String patternName, Object selectorContext) {
+
+ /* Check that the context selection returned something */
+ if (selectorContext == null) return(false);
+
+ /* Check that we actually have a configured pattern */
+ REProgram pattern = (REProgram) this.patterns.get(patternName);
+ if (pattern == null) {
+ if (this.getLogger().isWarnEnabled()) {
+ this.getLogger().warn("The specified pattern name \"" + patternName
+ + "\" was not configured in this instance");
+ }
+ return(false);
+ }
+
+ /* Pattern matching */
+ return(new RE(pattern).match(selectorContext.toString()));
+ }
+
+ /**
+ * <p>Configure this instance parsing all regular expression patterns.</p>
+ *
+ * @param configuration the {@link Configuration} instance where configured
+ * patterns are defined.
+ * @throws ConfigurationException if one of the regular-expression to configure
+ * could not be compiled.
+ */
+ public void configure(Configuration configuration)
+ throws ConfigurationException {
+ Configuration patterns[] = configuration.getChildren("pattern");
+ for (int x = 0; x < patterns.length; x++) {
+ String name = patterns[x].getAttribute("name");
+ String pattern = patterns[x].getValue();
+ this.patterns.put(name, this.compile(pattern));
+ }
+ }
+
+ /**
+ * <p>Compile the pattern in a {@link REProgram}.</p>
+ *
+ * @param pattern the regular expression pattern in a textual format.
+ * @return a compiled regular expression pattern.
+ * @throws ConfigurationException in the pattern could not be compiled.
+ */
+ protected REProgram compile(String pattern)
+ throws ConfigurationException {
+ if (pattern == null) {
+ throw new ConfigurationException("Null pattern");
+ }
+
+ if (pattern.length() == 0) {
+ pattern = "^$";
+ if (this.getLogger().isWarnEnabled()) {
+ this.getLogger().warn("The empty pattern string was rewritten to "
+ + "'^$' to match for empty strings. If you "
+ + "intended to match all strings, please "
+ + "change your pattern to '.*'");
+ }
+ }
+
+ try {
+ RECompiler compiler = new RECompiler();
+ REProgram program = compiler.compile(pattern);
+ return program;
+ } catch (RESyntaxException rse) {
+ getLogger().debug("Failed to compile the pattern '" + pattern + "'", rse);
+ throw new ConfigurationException(rse.getMessage(), rse);
+ }
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/AbstractRegexpSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/AbstractSwitchSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/AbstractSwitchSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/AbstractSwitchSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/AbstractSwitchSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,64 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.parameters.Parameters;
+
+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 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+public abstract class AbstractSwitchSelector extends AbstractLogEnabled
+ 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));
+ }
+}
+
+
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/AbstractSwitchSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/BrowserSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/BrowserSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/BrowserSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/BrowserSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,54 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.cocoon.environment.ObjectModelHelper;
+
+import java.util.Map;
+
+/**
+ * Tests a specific browser pattern against the requesting user-agent.
+ *
+ * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
+ * @author <a href="mailto:Giacomo.Pati@pwr.ch">Giacomo Pati</a>
+ * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
+ * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
+ * @version CVS $Id: BrowserSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+
+public class BrowserSelector extends NamedPatternsSelector {
+
+ public void configure(Configuration conf) throws ConfigurationException {
+ configure(conf, "browser", "name", "useragent");
+ }
+
+ public boolean select(String expression, Map objectModel, Parameters parameters) {
+ // Inform proxies that response varies with the user-agent header
+ ObjectModelHelper.getResponse(objectModel).addHeader("Vary", "User-Agent");
+
+ // Get the user-agent request header
+ String userAgent = ObjectModelHelper.getRequest(objectModel).getHeader("User-Agent");
+ if (userAgent == null) {
+ getLogger().debug("No User-Agent header -- failing.");
+ return false;
+ }
+
+ return checkPatterns(expression, userAgent);
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/BrowserSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/CookieSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/CookieSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/CookieSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/CookieSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,83 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+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.AbstractLogEnabled;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.thread.ThreadSafe;
+
+import org.apache.cocoon.environment.Cookie;
+import org.apache.cocoon.environment.ObjectModelHelper;
+
+import java.util.Map;
+
+/**
+ * A <code>Selector</code> that matches a string against a configurable cookie's value.
+ *
+ * <p><b>Global and local configuration</b></p>
+ * <table border="1">
+ * <tr>
+ * <td><code>cookie-name</code></td>
+ * <td>Name of the cookie whose value to match against</td>
+ * </tr>
+ * </table>
+ *
+ * @author <a href="mailto:matteodg@infinito.it">Matteo Di Giovinazzo</a>
+ * @version CVS $Id: CookieSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+public class CookieSelector extends AbstractLogEnabled
+ implements Configurable, Selector, ThreadSafe {
+
+ protected String defaultName;
+
+ public void configure(Configuration config) throws ConfigurationException {
+ this.defaultName = config.getChild("cookie-name").getValue(null);
+ }
+
+ public boolean select(String expression, Map objectModel, Parameters parameters) {
+
+ String name = parameters.getParameter("cookie-name", this.defaultName);
+ if (name == null) {
+ getLogger().warn("No cookie name given -- failing.");
+ return false;
+ }
+
+ Cookie[] cookies = ObjectModelHelper.getRequest(objectModel).getCookies();
+ if (cookies == null) {
+ getLogger().debug("Cookie '" + name + "' not set -- failing");
+ return false;
+ }
+
+ // TODO: this is not optimized
+ String value = null;
+ for (int i = 0; i < cookies.length; i++) {
+ if (cookies[i].getName().equals(name)) {
+ value = cookies[i].getValue();
+ break;
+ }
+ }
+
+ if (value == null) {
+ getLogger().debug("Cookie '" + name + "' not set -- failing");
+ return false;
+ }
+
+ return value.equals(expression);
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/CookieSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ExceptionSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ExceptionSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ExceptionSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ExceptionSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,193 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+import java.util.Map;
+
+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.parameters.Parameters;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.util.ClassUtils;
+import org.apache.commons.lang.exception.ExceptionUtils;
+
+/**
+ * In a <map:handle-errors>, selects depending on the exception that caused the error.
+ * The configuration of this selector allows to map exception class names to symbolic names
+ * that are used in the <map:when> alternatives.
+ * <p>
+ * Example configuration :
+ * <pre>
+ * <map:selector type="error" src="....ExceptionSelector">
+ * <exception class="org.xml.sax.SAXException" name="sax" unroll="true"/>
+ * <exception name="not-found" class="org.apache.cocoon.ResourceNotFoundException"/>
+ * <exception class="org.apache.cocoon.ProcessingException" unroll="true"/>
+ * <exception name="denied" class="java.security.SecurityException"/>
+ * <exception name="denied" class="my.comp.auth.AuthenticationFailure"/>
+ * </map:selector>
+ * </pre>
+ * This example shows several features :
+ * <li>the "class" is the class name of the exception (which can be any <code>Throwable</code>),</li>
+ * <li>an exception can be given a name, which is used in the <map:when> tests,</li>
+ * <li>an exception can be unrolled, meaning we try to get its cause and then consider this cause for
+ * the exception name</li>
+ * Note that both "name" and "unroll" can be specified. In that case, we first try to unroll the exception,
+ * and if none of the causes has a name, then the "name" attribute is considered.
+ *
+ * @author <a href="mailto:juergen.seitz@basf-it-services.com">Jürgen Seitz</a>
+ * @author <a href="mailto:bluetkemeier@s-und-n.de">Björn Lütkemeier</a>
+ * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
+ * @since 2.1
+ * @version CVS $Id: ExceptionSelector.java 280632 2005-09-13 19:35:46Z sylvain $
+ */
+
+public class ExceptionSelector extends AbstractSwitchSelector implements Configurable {
+
+ /** Exception classes */
+ private Class[] clazz;
+
+ /** Associated symbolic names (can be null) */
+ private String[] name;
+
+ /** Do we want to unroll them ? */
+ private boolean[] unroll;
+
+ public void configure(Configuration conf) throws ConfigurationException {
+
+ Configuration[] children = conf.getChildren("exception");
+
+ this.clazz = new Class[children.length];
+ this.name = new String[children.length];
+ this.unroll = new boolean[children.length];
+
+ for (int i = 0; i < children.length; i++) {
+ Configuration child = children[i];
+
+ String childClassName = child.getAttribute("class");
+ Class childClass = null;
+ try {
+ childClass = ClassUtils.loadClass(childClassName);
+ }
+ catch (Exception e) {
+ throw new ConfigurationException("Cannot load class '" + childClassName + "' at " + child.getLocation());
+ }
+
+ // Check that this class is not hidden by a more general class already declared
+ for (int j = 0; j < i; j++) {
+ if (this.clazz[j].isAssignableFrom(childClass)) {
+ throw new ConfigurationException("Class '" + this.clazz[j].getName() + "' hides its subclass '" +
+ childClassName + "' at " + child.getLocation());
+ }
+ }
+
+ this.clazz[i] = childClass;
+ this.name[i] = child.getAttribute("name", null);
+ this.unroll[i] = child.getAttributeAsBoolean("unroll", false);
+
+ if (this.name[i] == null && !this.unroll[i]) {
+ throw new ConfigurationException("Must specify one of 'name' or 'unroll' at " + child.getLocation());
+ }
+ }
+ }
+
+ /**
+ * Compute the exception type, given the configuration and the exception stored in the object model.
+ *
+ * @see ObjectModelHelper#getThrowable(java.util.Map)
+ */
+ public Object getSelectorContext(Map objectModel, Parameters parameters) {
+ // Get the name of the exception
+ Throwable thr = ObjectModelHelper.getThrowable(objectModel);
+ if (thr == null) {
+ throw new IllegalStateException("No exception in object model. ExceptionSelector can only be used in <map:handle-errors>");
+ }
+
+ return find(thr);
+ }
+
+ private FindResult find(Throwable thr) {
+ // Now find the proper name
+ for (int i = 0; i < this.clazz.length; i++) {
+ if (this.clazz[i].isInstance(thr)) {
+
+ // If exception needs to be unrolled, and it has a cause,
+ // return the cause name, if not null (recursively)
+ if (this.unroll[i]) {
+ Throwable cause = ExceptionUtils.getCause(thr);
+ if (cause != null) {
+ FindResult result = find(cause);
+ if (result != null) {
+ return result;
+ }
+ }
+ }
+
+ // Not unrolled
+ return new FindResult(this.name[i], thr);
+ }
+ }
+
+ // Not found
+ return null;
+ }
+
+ public boolean select(String expression, Object selectorContext) {
+ if ( selectorContext == null ) {
+ return false;
+ }
+ // Just compare the expression with the previously found name
+ boolean result = expression.equals(((FindResult)selectorContext).getName());
+
+ if (result) {
+ if (getLogger().isDebugEnabled())
+ getLogger().debug("select succesfull for condition " + selectorContext.toString());
+ }
+
+ return result;
+ }
+
+ static class FindResult {
+ private String name;
+ private Throwable throwable;
+
+ public FindResult(String name, Throwable throwable) {
+ this.name = name;
+ this.throwable = throwable;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public Throwable getThrowable() {
+ return this.throwable;
+ }
+
+ public void setThrowable(Throwable throwable) {
+ this.throwable = throwable;
+ }
+
+ public String toString() {
+ return this.name;
+ }
+ }
+
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ExceptionSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/HeaderSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/HeaderSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/HeaderSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/HeaderSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,83 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+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.AbstractLogEnabled;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.thread.ThreadSafe;
+
+import org.apache.cocoon.environment.ObjectModelHelper;
+
+import java.util.Map;
+
+/**
+ * A <code>Selector</code> that matches a string against a configurable
+ * request header, e.g. "referer".
+ *
+ * <p><b>Global and local configuration</b></p>
+ * <table border="1">
+ * <tr><td><code>header-name</code></td><td>Name of the request header to
+ * match against</td></tr>
+ * </table>
+ *
+ * @author <a href="mailto:haul@apache.org">Christian Haul</a>
+ * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
+ * @version CVS $Id: HeaderSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+public class HeaderSelector extends AbstractLogEnabled
+ implements Configurable, ThreadSafe, Selector {
+
+ protected String defaultName;
+
+ public void configure(Configuration config) throws ConfigurationException {
+ // Check old name
+ this.defaultName = config.getChild("parameter-name").getValue(null);
+ if (defaultName != null) {
+ getLogger().warn("'parameter-name' is deprecated. Please use 'header-name'");
+ }
+ // Load with new one
+ this.defaultName = config.getChild("header-name").getValue(this.defaultName);
+ }
+
+ public boolean select(String expression, Map objectModel, Parameters parameters) {
+ // Check old name
+ String name = parameters.getParameter("parameter-name", null);
+ if (name != null) {
+ getLogger().warn("'parameter-name' is deprecated. Please use 'header-name'");
+ } else {
+ name = this.defaultName;
+ }
+
+ // Load with new one.
+ name = parameters.getParameter("header-name", name);
+
+ if (name == null) {
+ getLogger().warn("No header name given -- failing.");
+ return false;
+ }
+
+ String value = ObjectModelHelper.getRequest(objectModel).getHeader(name);
+ if (value == null) {
+ getLogger().debug("Header '" + name + "' not set -- failing.");
+ return false;
+ }
+
+ return value.equals(expression);
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/HeaderSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/HostSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/HostSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/HostSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/HostSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,71 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.cocoon.environment.ObjectModelHelper;
+
+import java.util.Map;
+
+/**
+ * A <code>Selector</code> that matches a string from within the host parameter
+ * of the HTTP request.
+ *
+ * <p>Configuration:
+ * <pre>
+ * <map:selector name="host" src="org.apache.cocoon.selection.HostSelector">
+ * <host name="uk-site" value="www.foo.co.uk"/>
+ * </map:selector>
+ * </pre>
+ * <p>Usage:
+ * <pre>
+ * <map:select type="host">
+ * <map:when test="uk-site">
+ * <map:transform src="stylesheets/page/uk.xsl"/>
+ * </map:when>
+ * <map:otherwise>
+ * <map:transform src="stylesheets/page/us.xsl"/>
+ * </map:otherwise>
+ * </map:select>
+ * </pre>
+ *
+ * @author <a href="mailto:cbritton@centervilletech.com">Colin Britton</a>
+ * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
+ * @version CVS $Id: HostSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+
+public class HostSelector extends NamedPatternsSelector {
+
+ public void configure(Configuration conf) throws ConfigurationException {
+ configure(conf, "host", "name", "value");
+ }
+
+ public boolean select(String expression, Map objectModel, Parameters parameters) {
+ // Inform proxies that response varies with the Host header
+ ObjectModelHelper.getResponse(objectModel).addHeader("Vary", "Host");
+
+ // Get the host request header
+ String host = ObjectModelHelper.getRequest(objectModel).getHeader("Host");
+ if (host == null) {
+ getLogger().debug("No Host header -- failing.");
+ return false;
+ }
+
+ return checkPatterns(expression, host);
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/HostSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/NamedPatternsSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/NamedPatternsSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/NamedPatternsSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/NamedPatternsSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,128 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+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.AbstractLogEnabled;
+import org.apache.avalon.framework.thread.ThreadSafe;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Abstract class for selectors that select a value when it matches
+ * some patterns associated to the select expression.
+ *
+ * @see BrowserSelector
+ * @see HostSelector
+ * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
+ * @version CVS $Id: NamedPatternsSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+
+public abstract class NamedPatternsSelector extends AbstractLogEnabled
+ implements Configurable, ThreadSafe, Selector {
+
+ /**
+ * Association of names to String[] of values.
+ */
+ private Map strings;
+
+ /**
+ * Setup the association from expressions to a list of patterns. The configuration
+ * should look like :
+ * <pre>
+ * <map:selector name="foo" src="...">
+ * <confName nameAttr="expression" valueAttr="pattern"/>
+ * ... others (expression, pattern) associations ...
+ * </map:selector>
+ * </pre>
+ *
+ * @param conf the configuration
+ * @param confName the name of children of <code>conf</code> that will be used to
+ * build associations
+ * @param nameAttr the name of the attribute that holds the expression
+ * @param valueAttr the name of the attribute that holds the pattern
+ */
+ protected void configure(Configuration conf, String confName, String nameAttr, String valueAttr)
+ throws ConfigurationException {
+ Configuration confs[] = conf.getChildren(confName);
+ Map configMap = new HashMap();
+
+ // Build a list of strings for each name
+ for (int i = 0; i < confs.length; i++) {
+ String name = confs[i].getAttribute(nameAttr);
+ String value = confs[i].getAttribute(valueAttr);
+
+ // Get value list for this name
+ List nameList = (List)configMap.get(name);
+ if (nameList == null) {
+ nameList = new ArrayList();
+ configMap.put(name, nameList);
+ }
+
+ // Add the current value
+ nameList.add(value);
+ }
+
+ // Convert lists to arrays for faster lookup
+ Iterator entries = configMap.entrySet().iterator();
+ while(entries.hasNext()) {
+ Map.Entry entry = (Map.Entry)entries.next();
+ List nameList = (List)entry.getValue();
+ entry.setValue(nameList.toArray(new String[nameList.size()]));
+ }
+
+ this.strings = configMap;
+ }
+
+ /**
+ * Checks if <code>value</code> is a substring of one of the patterns associated
+ * to <code>expression</code>
+ *
+ * @param expression the expression that is selected
+ * @param value the value to check
+ * @return true if <code>value</code> matches one of the patterns
+ */
+ protected boolean checkPatterns(String expression, String value) {
+ if (value == null) {
+ getLogger().debug("No value given -- failing.");
+ return false;
+ }
+ // Get patterns for 'expression'
+ String[] patterns = (String[])this.strings.get(expression);
+ if (patterns == null) {
+ getLogger().warn("No configuration for expression '" + expression + "' -- failing.");
+ return false;
+ }
+
+ // Does a pattern match 'value' ?
+ for (int i = 0; i < patterns.length; i++) {
+ if (value.indexOf(patterns[i]) != -1) {
+ getLogger().debug(expression + " selected value " + value);
+ return true;
+ }
+ }
+
+ // No match
+ return false;
+ }
+
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/NamedPatternsSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ParameterSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ParameterSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ParameterSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ParameterSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,56 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.thread.ThreadSafe;
+
+import java.util.Map;
+
+/**
+ * A <code>Selector</code> that matches a string in the parameters object passed to it.
+ *
+ * <pre>
+ * <map:selector name="parameter" factory="org.apache.cocoon.selection.ParameterSelector"/>
+ *
+ * <map:select type="parameter">
+ * <map:parameter name="parameter-selector-test" value="{mySitemapParameter}"/>
+ *
+ * <map:when test="myParameterValue">
+ * <!-- executes iff {mySitemapParameter} == "myParameterValue" -->
+ * <map:transform src="stylesheets/page/uk.xsl"/>
+ * </map:when>
+ * <map:otherwise>
+ * <map:transform src="stylesheets/page/us.xsl"/>
+ * </map:otherwise>
+ * </map:select>
+ * </pre>
+ *
+ * The purpose of this selector is to allow an action to set parameters
+ * and to be able to select between different pipeline configurations
+ * depending on those parameters.
+ *
+ * @author <a href="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
+ * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
+ * @version CVS $Id: ParameterSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+public class ParameterSelector implements ThreadSafe, Selector {
+
+ public boolean select(String expression, Map objectModel, Parameters parameters) {
+ String compareToString = parameters.getParameter("parameter-selector-test", null);
+ return compareToString != null && compareToString.equals(expression);
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ParameterSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RegexpHeaderSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RegexpHeaderSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RegexpHeaderSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RegexpHeaderSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,112 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+import java.util.Map;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.cocoon.environment.ObjectModelHelper;
+
+/**
+ * <p>The {@link RegexpHeaderSelector} class defines a selector matching
+ * specific headers to configured regular-expression patterns.</p>
+ *
+ * <p>The configuration of an {@link RegexpHeaderSelector} follows exactly
+ * what has been outlined in {@link AbstractRegexpSelector} regarding regular
+ * expression patterns, and additionally it requires an extra configuration element
+ * specifying the header whose value needs to be matched:</p>
+ *
+ * <pre>
+ * <map:components>
+ * ...
+ * <map:selectors default="...">
+ * <map:selector name="..." src="org.apache.cocoon.selection....">
+ * <pattern name="empty">^$</pattern>
+ * <pattern name="number">^[0-9]+$</pattern>
+ * <pattern name="string">^.+$</pattern>
+ * <header-name>...</header-name>
+ * </map:selector>
+ * </map:selectors>
+ * </map:components>
+ * </pre>
+ *
+ * <p>If not configured, or if it needs to be overriddent, the header name can
+ * also be specified as a <code><map:parameter .../></code> inside the
+ * pipeline itself.</p>
+ *
+ * @version CVS $Id: RegexpHeaderSelector.java 124800 2005-01-10 14:34:31Z vgritsenko $
+ */
+public class RegexpHeaderSelector extends AbstractRegexpSelector {
+
+ /** <p>The name of the header to work on.</p> */
+ protected String headerName;
+
+ /**
+ * <p>Create a new {@link RegexpHeaderSelector} instance.</p>
+ */
+ public RegexpHeaderSelector() {
+ super();
+ }
+
+ /**
+ * <p>Configure this instance parsing all regular expression patterns and
+ * storing the header name upon which selection occurs.</p>
+ *
+ * @param configuration the {@link Configuration} instance where configured
+ * patterns are defined.
+ * @throws ConfigurationException if one of the regular-expression to configure
+ * could not be compiled.
+ */
+ public void configure(Configuration configuration)
+ throws ConfigurationException {
+ super.configure(configuration);
+ this.headerName = configuration.getChild("header-name").getValue(null);
+ }
+
+ /**
+ * <p>Return the value of the header identified by the configured header
+ * name, if any.</p>
+ *
+ * @param objectModel the Cocoon object model.
+ * @param parameters the {@link Parameters} associated with the pipeline.
+ * @return the value of the configured request parameter or <b>null</b>.
+ */
+ public Object getSelectorContext(Map objectModel, Parameters parameters) {
+ String name = parameters.getParameter("header-name", this.headerName);
+ if (name == null) {
+ this.getLogger().warn("No header name given -- failing.");
+ return null;
+ }
+ return ObjectModelHelper.getRequest(objectModel).getHeader(name);
+ }
+
+ /**
+ * 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) {
+ // Inform proxies that response varies with the selector header
+ String name = params.getParameter("header-name", this.headerName);
+ if (name != null)
+ ObjectModelHelper.getResponse(objectModel).addHeader("Vary", name);
+ return select(expr, getSelectorContext(objectModel, params));
+ }
+
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RegexpHeaderSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RegexpRequestParameterSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RegexpRequestParameterSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RegexpRequestParameterSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RegexpRequestParameterSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,98 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+import java.util.Map;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.cocoon.environment.ObjectModelHelper;
+
+/**
+ * <p>The {@link RegexpRequestParameterSelector} class defines a selector matching
+ * specific request parameters to configured regular-expression patterns.</p>
+ *
+ * <p>The configuration of an {@link RegexpRequestParameterSelector} follows exactly
+ * what has been outlined in {@link AbstractRegexpSelector} regarting regular
+ * expression patterns, and additionally it requires an extra configuration element
+ * specifying the request parameter whose value needs to be matched:</p>
+ *
+ * <pre>
+ * <map:components>
+ * ...
+ * <map:selectors default="...">
+ * <map:selector name="..." src="org.apache.cocoon.selection....">
+ * <pattern name="empty">^$</pattern>
+ * <pattern name="number">^[0-9]+$</pattern>
+ * <pattern name="string">^.+$</pattern>
+ * <parameter-name>...</parameter-name>
+ * </map:selector>
+ * </map:selectors>
+ * </map:components>
+ * </pre>
+ *
+ * <p>If not configured, or if it needs to be overriddent, the parameter name can
+ * also be specified as a <code><map:parameter .../></code> inside the
+ * pipeline itself.</p>
+ *
+ * @version CVS $Id: RegexpRequestParameterSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ * @author <a href="mailto:pier@apache.org">Pier Fumagalli</a>
+ */
+public class RegexpRequestParameterSelector extends AbstractRegexpSelector {
+
+ /** <p>The name of the parameter to work on.</p> */
+ protected String parameterName;
+
+ /**
+ * <p>Create a new {@link RegexpRequestParameterSelector} instance.</p>
+ */
+ public RegexpRequestParameterSelector() {
+ super();
+ }
+
+ /**
+ * <p>Configure this instance parsing all regular expression patterns and
+ * storing the parameter name upon which selection occurs.</p>
+ *
+ * @param configuration the {@link Configuration} instance where configured
+ * patterns are defined.
+ * @throws ConfigurationException if one of the regular-expression to configure
+ * could not be compiled.
+ */
+ public void configure(Configuration configuration)
+ throws ConfigurationException {
+ super.configure(configuration);
+ this.parameterName = configuration.getChild("parameter-name").getValue(null);
+ }
+
+ /**
+ * <p>Return the value of the parameter identified by the configured parameter
+ * name, if any.</p>
+ *
+ * @param objectModel the Cocoon object model.
+ * @param parameters the {@link Parameters} associated with the pipeline.
+ * @return the value of the configured request parameter or <b>null</b>.
+ */
+ public Object getSelectorContext(Map objectModel, Parameters parameters) {
+ String name = parameters.getParameter("parameter-name", this.parameterName);
+ if (name == null) {
+ this.getLogger().warn("No parameter name given -- failing.");
+ return null;
+ }
+ return ObjectModelHelper.getRequest(objectModel).getParameter(name);
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RegexpRequestParameterSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestAttributeSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestAttributeSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestAttributeSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestAttributeSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,67 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+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.AbstractLogEnabled;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.environment.ObjectModelHelper;
+
+import java.util.Map;
+
+/**
+ * A <code>Selector</code> that matches a string against a configurable request
+ * attribute's string represenation.
+ *
+ * <p><b>Global and local configuration</b></p>
+ * <table border="1">
+ * <tr><td><code>attribute-name</code></td><td>String identifying the request attribute.</td></tr>
+ * </table>
+ *
+ * @author <a href="mailto:haul@apache.org">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: RequestAttributeSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+public class RequestAttributeSelector extends AbstractLogEnabled
+ implements Configurable, ThreadSafe, Selector {
+
+ protected String defaultName;
+
+ public void configure(Configuration config) throws ConfigurationException {
+ this.defaultName = config.getChild("attribute-name").getValue(null);
+ }
+
+ public boolean select(String expression, Map objectModel, Parameters parameters) {
+ String name = parameters.getParameter("attribute-name", this.defaultName);
+
+ if (name == null) {
+ getLogger().warn("No attribute name given -- failing.");
+ return false;
+ }
+
+ Object value = ObjectModelHelper.getRequest(objectModel).getAttribute(name);
+ if (value == null) {
+ getLogger().debug("Request attribute '" + name + "' not set -- failing.");
+ return false;
+ }
+
+ return value.toString().equals(expression);
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestAttributeSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestMethodSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestMethodSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestMethodSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestMethodSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,40 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.environment.ObjectModelHelper;
+
+import java.util.Map;
+
+/**
+ * A <code>Selector</code> that matches a getMethod() of the HTTP request.
+ *
+ * @author <a href="mailto:maciejka@tiger.com.pl">Maciek Kaminski</a>
+ * @version CVS $Id: RequestMethodSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+public class RequestMethodSelector extends AbstractLogEnabled
+ implements ThreadSafe, Selector {
+
+ public boolean select(
+ String expression, Map objectModel, Parameters parameters)
+ {
+ String method = ObjectModelHelper.getRequest(objectModel).getMethod();
+ return method.equals(expression);
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestMethodSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestParameterSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestParameterSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestParameterSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestParameterSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,71 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+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.parameters.Parameters;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.environment.ObjectModelHelper;
+
+import java.util.Map;
+
+/**
+ * A <code>Selector</code> that matches a string against a configurable request parameter's value.
+ *
+ * <p><b>Global and local configuration</b></p>
+ * <table border="1">
+ * <tr><td><code>parameter-name</code></td><td>Name of the request
+ * parameter whose value to match against</td></tr>
+ * </table>
+ *
+ * @author <a href="mailto:haul@apache.org">Christian Haul</a>
+ * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
+ * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
+ * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
+ * @version CVS $Id: RequestParameterSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+public class RequestParameterSelector extends AbstractSwitchSelector
+ implements Configurable, ThreadSafe {
+
+ protected String defaultName;
+
+ public void configure(Configuration config) throws ConfigurationException {
+ this.defaultName = config.getChild("parameter-name").getValue(null);
+ }
+
+ 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 null;
+ }
+
+ 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 selectorContext.equals(expression);
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/RequestParameterSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ResourceExistsSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ResourceExistsSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ResourceExistsSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ResourceExistsSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,114 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+import java.util.Map;
+
+import org.apache.avalon.framework.activity.Disposable;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceNotFoundException;
+import org.apache.excalibur.source.SourceResolver;
+
+/**
+ * Selects the first of a set of Resources (usually files) that exists.
+ *
+ * <p>
+ * A parameter 'prefix',
+ * <pre>
+ * <map:parameter src="prefix" value="<code>some/path</code>"/<
+ * </pre>
+ * may be supplied to the selector instance. This prefix is prepended to all
+ * test expressions before evaluation. The default prefix is '' (empty string),
+ * meaning that all expressions are relative to the current sitemap, unless
+ * explicitly overridden.
+ *
+ * <p><b>NOTE:</b>
+ * Provided resource URI is resolved as Source, relative to the current
+ * sitemap, which differs from behavior of selector in previous versions.
+ * To resolve resource paths relative to the context root, provide prefix
+ * parameter:
+ * <pre>
+ * <map:parameter name="prefix" value="context://"/<
+ * </pre>
+ *
+ * <p>
+ * For example, we could define a ResourceExistsSelector with:
+ * <pre>
+ * <map:selector name="resource-exists"
+ * logger="sitemap.selector.resource-exists"
+ * src="org.apache.cocoon.selection.ResourceExistsSelector" /<
+ * </pre>
+ * And use it to build a PDF from XSL:FO or a higher-level XML format with:
+ *
+ * <pre>
+ * <map:match pattern="**.pdf"<
+ * <map:select type="resource-exists"<
+ * <map:when test="context/xdocs/{1}.fo"<
+ * <map:generate src="content/xdocs/{1}.fo" /<
+ * </map:when<
+ * <map:otherwise<
+ * <map:generate src="content/xdocs/{1}.xml" /<
+ * <map:transform src="stylesheets/document2fo.xsl" /<
+ * </map:otherwise<
+ * </map:select<
+ * <map:serialize type="fo2pdf" /<
+ * </pre>
+ *
+ * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
+ * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
+ * @version CVS $Id: ResourceExistsSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+public class ResourceExistsSelector extends AbstractLogEnabled
+ implements ThreadSafe, Serviceable, Disposable, Selector {
+
+ private ServiceManager manager;
+ private SourceResolver resolver;
+
+ public void service(ServiceManager manager) throws ServiceException {
+ this.manager = manager;
+ this.resolver = (SourceResolver)manager.lookup(SourceResolver.ROLE);
+ }
+
+ public void dispose() {
+ this.manager.release(this.resolver);
+ this.resolver = null;
+ this.manager = null;
+ }
+
+ public boolean select(String expression, Map objectModel, Parameters parameters) {
+ String resourceURI = parameters.getParameter("prefix", "") + expression;
+ Source source = null;
+ try {
+ source = resolver.resolveURI(resourceURI);
+ return source.exists();
+ } catch (SourceNotFoundException e) {
+ return false;
+ } catch (Exception e) {
+ getLogger().warn("Exception resolving resource " + resourceURI, e);
+ return false;
+ } finally {
+ if (source != null) {
+ resolver.release(source);
+ }
+ }
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/ResourceExistsSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/Selector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/Selector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/Selector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/Selector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,44 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+import org.apache.avalon.framework.parameters.Parameters;
+
+import java.util.Map;
+
+/**
+ *
+ * @author <a href="mailto:Giacomo.Pati@pwr.ch">Giacomo Pati</a>
+ * @author <a href="mailto:leo.sutic">Leo Sutic</a>
+ * @version CVS $Id: Selector.java 55389 2004-10-23 15:46:58Z cziegeler $
+ */
+public interface Selector {
+
+ String ROLE = Selector.class.getName();
+ /**
+ * 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.
+ * @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 boolean Signals successfull test.
+ */
+ boolean select (String expression, Map objectModel, Parameters parameters);
+}
+
+
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/Selector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SessionAttributeSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SessionAttributeSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SessionAttributeSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SessionAttributeSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,66 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+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.AbstractLogEnabled;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.environment.ObjectModelHelper;
+
+import java.util.Map;
+
+/**
+ * A <code>Selector</code> that matches a string against a configurable session
+ * attribute's string represenation.
+ *
+ * <p><b>Global and local configuration</b></p>
+ * <table border="1">
+ * <tr><td><code>attribute-name</code></td><td>String identifying the session attribute.</td></tr>
+ * </table>
+ *
+ * @author <a href="mailto:haul@apache.org">Christian Haul</a>
+ * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
+ * @version CVS $Id: SessionAttributeSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+public class SessionAttributeSelector extends AbstractLogEnabled
+ implements Configurable, ThreadSafe, Selector {
+
+ protected String defaultName;
+
+ public void configure(Configuration config) throws ConfigurationException {
+ this.defaultName = config.getChild("attribute-name").getValue(null);
+ }
+
+ public boolean select(String expression, Map objectModel, Parameters parameters) {
+ String name = parameters.getParameter("attribute-name", this.defaultName);
+
+ if (name == null) {
+ getLogger().warn("No attribute name given -- failing.");
+ return false;
+ }
+
+ Object value = ObjectModelHelper.getRequest(objectModel).getSession().getAttribute(name);
+ if (value == null) {
+ getLogger().debug("Session attribute '" + name + "' not set -- failing.");
+ return false;
+ }
+
+ return value.toString().equals(expression);
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SessionAttributeSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SimpleSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SimpleSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SimpleSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SimpleSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,66 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+import java.util.Map;
+
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.thread.ThreadSafe;
+
+/**
+ * A very simple selector that operates on string literals, useful especially
+ * in conjunction with input modules. Usage example:
+ * <pre>
+ * <map:selector name="simple" src="org.apache.cocoon.selection.SimpleSelector"/>
+ *
+ * <map:select type="simple">
+ * <map:parameter name="value" value="{request:method}"/>
+ * <map:when test="GET">
+ * ...
+ * </map:when>
+ * <map:when test="POST">
+ * ...
+ * </map:when>
+ * <map:when test="PUT">
+ * ...
+ * </map:when>
+ * <map:otherwise>
+ * ...
+ * </map:otherwise>
+ * </map:select>
+ * </pre>
+ *
+ * @author <a href="mailto:haul@apache.org">Christian Haul</a>
+ * @version CVS $Id: SimpleSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ * @since 2.1
+ */
+public class SimpleSelector extends AbstractSwitchSelector implements ThreadSafe {
+
+ public Object getSelectorContext(Map objectModel, Parameters parameters) {
+ return parameters.getParameter("value", "");
+ }
+
+ public boolean select(String expression, Object selectorContext) {
+ if (selectorContext == null) {
+ if (getLogger().isWarnEnabled())
+ getLogger().warn("Value not set -- failing.");
+ return false;
+ }
+
+ return selectorContext.equals(expression);
+ }
+
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SimpleSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SwitchSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SwitchSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SwitchSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SwitchSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,111 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+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 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+public interface SwitchSelector extends Selector, ThreadSafe {
+
+ String ROLE = SwitchSelector.class.getName();
+
+ /**
+ * 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);
+}
+
+
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/SwitchSelector.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/XPathExceptionSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/XPathExceptionSelector.java?rev=330548&view=auto
==============================================================================
--- cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/XPathExceptionSelector.java (added)
+++ cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/XPathExceptionSelector.java Thu Nov 3 05:41:06 2005
@@ -0,0 +1,122 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.selection;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+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.parameters.Parameters;
+import org.apache.commons.collections.map.LinkedMap;
+import org.apache.commons.jxpath.CompiledExpression;
+import org.apache.commons.jxpath.JXPathContext;
+
+/**
+ * Additional to the inherited functionality from its superclass ExceptionSelector,
+ * this selector allows to define xpath expressions to evaluate supplemental information
+ * given in the thrown exception.
+ * The configuration of this selector allows to map not only exceptions but also
+ * xpath expressions to symbolic names that are used in the <map:when> alternatives.
+ * <p>
+ * Example configuration :
+ * <pre>
+ * <map:selector type="error" src="....XPathExceptionSelector">
+ * <exception name="denied" class="my.comp.auth.AuthenticationFailure">
+ * <xpath name="PasswordWrong" test="authCode=10"/>
+ * <xpath name="PasswordExpired" test="errorCode=11"/>
+ * <xpath name="AccessForbidden" test="errorCode>11"/>
+ * </exception>
+ * </map:selector>
+ * </pre>
+ * This example shows several features :
+ * <li>the test is the xpath expression that will be evaluated against the exception ,</li>
+ * <li>an xpath expression can be given a name, which is used in the <map:when> tests,</li>
+ *
+ * @author <a href="mailto:juergen.seitz@basf-it-services.com">Jürgen Seitz</a>
+ * @author <a href="mailto:bluetkemeier@s-und-n.de">Björn Lütkemeier</a>
+ * @since 2.1
+ * @version CVS $Id: XPathExceptionSelector.java 30941 2004-07-29 19:56:58Z vgritsenko $
+ */
+public class XPathExceptionSelector extends ExceptionSelector
+ implements Configurable {
+
+ private Map exception2XPath = new HashMap();
+
+ public void configure(Configuration conf) throws ConfigurationException {
+
+ super.configure(conf);
+
+ Configuration[] children = conf.getChildren("exception");
+ Configuration[] xPathChildren;
+
+ for (int i = 0; i < children.length; i++) {
+ // Check if there are XPath-Expressions configured
+ xPathChildren = children[i].getChildren("xpath");
+ Map xPathMap = new LinkedMap(11);
+
+ for (int j = 0; j < xPathChildren.length; j++) {
+ Configuration xPathChild = xPathChildren[j];
+
+ String xPathName = xPathChild.getAttribute("name");
+ CompiledExpression xPath = JXPathContext.compile(xPathChild.getAttribute("test"));
+
+ xPathMap.put(xPathName, xPath);
+ }
+ if (xPathMap.size() > 0) {
+ // store xpath - config if there is some
+ exception2XPath.put(children[i].getAttribute("name", null),
+ xPathMap);
+ }
+ }
+ }
+
+ /**
+ * Compute the exception type, given the configuration and the exception stored in the object model.
+ */
+ public Object getSelectorContext(Map objectModel, Parameters parameters) {
+
+ // get exception from super class
+ FindResult selectorContext = (FindResult) super.getSelectorContext(objectModel,
+ parameters);
+
+ if (selectorContext != null) {
+ String exceptionName = selectorContext.getName();
+ Throwable t = selectorContext.getThrowable();
+
+ Map xPathMap = (Map) exception2XPath.get(exceptionName);
+
+ if (xPathMap != null) {
+ // create a context for the thrown exception
+ JXPathContext context = JXPathContext.newContext(t);
+
+ for (Iterator iterator = xPathMap.entrySet().iterator(); iterator.hasNext(); ) {
+ Map.Entry entry = (Map.Entry) iterator.next();
+
+ if (((CompiledExpression) entry.getValue()).getValue(context).equals(Boolean.TRUE)) {
+ // set the configured name if the expression is succesfull
+ selectorContext.setName((String) entry.getKey());
+ return selectorContext;
+ }
+ }
+ }
+ }
+
+ return selectorContext;
+ }
+}
Propchange: cocoon/whiteboard/maven2/cocoon-flat-layout/cocoon-core/src/main/java/org/apache/cocoon/selection/XPathExceptionSelector.java
------------------------------------------------------------------------------
svn:eol-style = native