cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From u..@apache.org
Subject svn commit: rev 36062 - cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection
Date Sat, 07 Aug 2004 12:37:52 GMT
Author: ugo
Date: Sat Aug  7 05:37:51 2004
New Revision: 36062

Added:
   cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/AbstractRegexpSelector.java
   cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/RegexpHeaderSelector.java
   cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/RegexpRequestParameterSelector.java
Log:
Backported regexp selectors from trunk

Added: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/AbstractRegexpSelector.java
==============================================================================
--- (empty file)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/AbstractRegexpSelector.java
Sat Aug  7 05:37:51 2004
@@ -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>
+ * &lt;map:components&gt;
+ *   ...
+ *   &lt;map:selectors default="..."&gt;
+ *     &lt;map:selector name="..." src="org.apache.cocoon.selection...."&gt;
+ *       &lt;pattern name="empty"&gt;^$&lt;/pattern&gt;
+ *       &lt;pattern name="number"&gt;^[0-9]+$&lt;/pattern&gt;
+ *       &lt;pattern name="string"&gt;^.+$&lt;/pattern&gt;
+ *     &lt;/map:selector&gt;
+ *  &lt;/map:selectors&gt;
+ * &lt;/map:components&gt;
+ * </pre>
+ * 
+ * <p>Then, each configured pattern can be referenced in the pipelines section of
+ * the sitemap:</p>
+ * 
+ * <pre>
+ * &lt;map:pipelines&gt;
+ *   ...
+ *   &lt;map:match ...&gt;
+ *     ...
+ *     &lt;map:select type="browser"&gt;
+ *       &lt;map:when test="empty"&gt;...&lt;/map:when&gt;
+ *       &lt;map:when test="number"&gt;...&lt;/map:when&gt;
+ *       &lt;map:when test="string"&gt;...&lt;/map:when&gt;
+ *       &lt;map:otherwise&gt;...&lt;/map:otherwise&gt;
+ *     &lt;/map:select&gt;
+ *     ...
+ *   &lt;/map:match&gt;
+ *   ...
+ * &lt;/map:pipelines&gt;
+ * </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);
+        }
+    }
+}

Added: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/RegexpHeaderSelector.java
==============================================================================
--- (empty file)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/RegexpHeaderSelector.java
Sat Aug  7 05:37:51 2004
@@ -0,0 +1,113 @@
+/*
+ * 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>
+ * &lt;map:components&gt;
+ *   ...
+ *   &lt;map:selectors default="..."&gt;
+ *     &lt;map:selector name="..." src="org.apache.cocoon.selection...."&gt;
+ *       &lt;pattern name="empty"&gt;^$&lt;/pattern&gt;
+ *       &lt;pattern name="number"&gt;^[0-9]+$&lt;/pattern&gt;
+ *       &lt;pattern name="string"&gt;^.+$&lt;/pattern&gt;
+ *       &lt;header-name&gt;...&lt;/header-name&gt;
+ *     &lt;/map:selector&gt;
+ *  &lt;/map:selectors&gt;
+ * &lt;/map:components&gt;
+ * </pre>
+ * 
+ * <p>If not configured, or if it needs to be overriddent, the header name can
+ * also be specified as a <code>&lt;map:parameter&nbsp;.../&gt;</code>
inside the
+ * pipeline itself.</p>
+ * 
+ * @version CVS $Revision: 1.0 $
+ * @author <a href="mailto:colin@colina.demon.co.uk">Colin Adams</a>
+ */
+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));
+    }
+
+}

Added: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/RegexpRequestParameterSelector.java
==============================================================================
--- (empty file)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/RegexpRequestParameterSelector.java
Sat Aug  7 05:37:51 2004
@@ -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>
+ * &lt;map:components&gt;
+ *   ...
+ *   &lt;map:selectors default="..."&gt;
+ *     &lt;map:selector name="..." src="org.apache.cocoon.selection...."&gt;
+ *       &lt;pattern name="empty"&gt;^$&lt;/pattern&gt;
+ *       &lt;pattern name="number"&gt;^[0-9]+$&lt;/pattern&gt;
+ *       &lt;pattern name="string"&gt;^.+$&lt;/pattern&gt;
+ *       &lt;parameter-name&gt;...&lt;/parameter-name&gt;
+ *     &lt;/map:selector&gt;
+ *  &lt;/map:selectors&gt;
+ * &lt;/map:components&gt;
+ * </pre>
+ * 
+ * <p>If not configured, or if it needs to be overriddent, the parameter name can
+ * also be specified as a <code>&lt;map:parameter&nbsp;.../&gt;</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);
+    }
+}

Mime
View raw message