cziegeler 2004/07/22 23:56:37
Modified: src/java/org/apache/cocoon/components/modules/input
RequestAttributeModule.java
src/webapp/WEB-INF cocoon.xconf
Added: src/java/org/apache/cocoon/components/modules/input
RequestScopedAttributeModule.java
Log:
Module to get scoped attributes
Revision Changes Path
1.4 +42 -26
cocoon-2.1/src/java/org/apache/cocoon/components/modules/input/RequestAttributeModule.java
Index: RequestAttributeModule.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/modules/input/RequestAttributeModule.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- RequestAttributeModule.java 5 Mar 2004 13:02:48 -0000 1.3
+++ RequestAttributeModule.java 23 Jul 2004 06:56:37 -0000 1.4
@@ -42,30 +42,39 @@
*/
public class RequestAttributeModule extends AbstractInputModule implements
ThreadSafe {
- public Object getAttribute( String name, Configuration modeConf, Map
objectModel )
- throws ConfigurationException {
-
+ protected Object getAttribute( String name, Configuration modeConf, Map
objectModel, int scope)
+ throws ConfigurationException {
String pname = (String) this.settings.get("parameter", name);
if ( modeConf != null ) {
pname = modeConf.getAttribute( "parameter", pname );
// preferred
pname = modeConf.getChild("parameter").getValue(pname);
}
- return ObjectModelHelper.getRequest(objectModel).getAttribute( pname
);
+ return ObjectModelHelper.getRequest(objectModel).getAttribute(
pname, scope );
+ }
+
+ /* (non-Javadoc)
+ * @see
org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
org.apache.avalon.framework.configuration.Configuration, java.util.Map)
+ */
+ public Object getAttribute( String name, Configuration modeConf, Map
objectModel )
+ throws ConfigurationException {
+ return this.getAttribute(name, modeConf, objectModel,
Request.GLOBAL_SCOPE);
}
+ /* (non-Javadoc)
+ * @see
org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration,
java.util.Map)
+ */
public Iterator getAttributeNames( Configuration modeConf, Map
objectModel )
- throws ConfigurationException {
-
+ throws ConfigurationException {
return new
IteratorHelper(ObjectModelHelper.getRequest(objectModel).getAttributeNames());
}
- public Object[] getAttributeValues( String name, Configuration modeConf,
Map objectModel )
- throws ConfigurationException {
-
- Request request = ObjectModelHelper.getRequest(objectModel);
+ protected Object[] getAttributeValues( String name, Configuration
modeConf, Map objectModel, int scope )
+ throws ConfigurationException {
+ final Request request = ObjectModelHelper.getRequest(objectModel);
+
String wildcard = (String) this.settings.get("parameter",name);
if ( modeConf != null ) {
wildcard = modeConf.getAttribute( "parameter", wildcard );
@@ -89,21 +98,21 @@
suffix = "";
}
SortedSet names = new TreeSet();
- Enumeration allNames = request.getAttributeNames();
+ Enumeration allNames = request.getAttributeNames( scope );
+
+ while (allNames.hasMoreElements()) {
+ String pname = (String) allNames.nextElement();
+ if ( pname.startsWith( prefix ) && pname.endsWith( suffix )
) {
+ names.add(pname);
+ }
+ }
- while (allNames.hasMoreElements()) {
- String pname = (String) allNames.nextElement();
- if ( pname.startsWith( prefix ) && pname.endsWith( suffix ) )
{
- names.add(pname);
- }
- }
-
- List values = new LinkedList();
- Iterator j = names.iterator();
- while (j.hasNext()){
- String pname = (String) j.next();
- values.add( request.getAttribute( pname ) );
- }
+ List values = new LinkedList();
+ Iterator j = names.iterator();
+ while (j.hasNext()){
+ String pname = (String) j.next();
+ values.add( request.getAttribute( pname ) );
+ }
return values.toArray();
@@ -111,7 +120,7 @@
// no "*" in attribute name => just return all values of
// this one attribute. Make sure, it's an array.
- Object value = request.getAttribute( wildcard );
+ Object value = request.getAttribute( wildcard, scope );
if ( value != null && !value.getClass().isArray() ) {
Object[] values = new Object[1];
values[0] = value;
@@ -124,6 +133,13 @@
}
+ /* (non-Javadoc)
+ * @see
org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String,
org.apache.avalon.framework.configuration.Configuration, java.util.Map)
+ */
+ public Object[] getAttributeValues( String name, Configuration modeConf,
Map objectModel )
+ throws ConfigurationException {
+ return this.getAttributeValues(name, modeConf, objectModel,
Request.GLOBAL_SCOPE );
+ }
}
1.1
cocoon-2.1/src/java/org/apache/cocoon/components/modules/input/RequestScopedAttributeModule.java
Index: RequestScopedAttributeModule.java
===================================================================
/*
* 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.components.modules.input;
import java.util.Map;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.cocoon.environment.Request;
/**
* This is an extension of the [EMAIL PROTECTED] RequestAttributeModule}. It
has the same
* features but requires to define the scope of an attribute: either "global"
or
* "request"; so the name follows this form: SCOPE:KEY.
*
* @since 2.2
* @version CVS $Id: RequestScopedAttributeModule.java,v 1.1 2004/07/23
06:56:37 cziegeler Exp $
*/
public class RequestScopedAttributeModule extends RequestAttributeModule {
private static final class KeyInfo {
public final int scope;
public final String key;
public KeyInfo(String name) throws ConfigurationException {
final int pos = name.indexOf(':');
if ( pos == -1 ) {
throw new ConfigurationException("Scope is missing in '" +
name + '.');
}
final String scopeValue = name.substring(0, pos);
this.key = name.substring(pos + 1);
if ( "global".equalsIgnoreCase(scopeValue) ) {
this.scope = Request.GLOBAL_SCOPE;
} else if ("request".equalsIgnoreCase(scopeValue)) {
this.scope = Request.REQUEST_SCOPE;
} else {
throw new ConfigurationException("Unknown value for scope: "
+ scopeValue);
}
}
}
/* (non-Javadoc)
* @see
org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
org.apache.avalon.framework.configuration.Configuration, java.util.Map)
*/
public Object getAttribute( String name, Configuration modeConf, Map
objectModel )
throws ConfigurationException {
final KeyInfo info = new KeyInfo(name);
return this.getAttribute(info.key, modeConf, objectModel, info.scope);
}
/* (non-Javadoc)
* @see
org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String,
org.apache.avalon.framework.configuration.Configuration, java.util.Map)
*/
public Object[] getAttributeValues( String name, Configuration modeConf,
Map objectModel )
throws ConfigurationException {
final KeyInfo info = new KeyInfo(name);
return this.getAttributeValues(info.key, modeConf, objectModel,
info.scope );
}
}
1.55 +2 -1 cocoon-2.1/src/webapp/WEB-INF/cocoon.xconf
Index: cocoon.xconf
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/webapp/WEB-INF/cocoon.xconf,v
retrieving revision 1.54
retrieving revision 1.55
diff -u -r1.54 -r1.55
--- cocoon.xconf 23 Jul 2004 02:42:07 -0000 1.54
+++ cocoon.xconf 23 Jul 2004 06:56:37 -0000 1.55
@@ -158,6 +158,7 @@
<component-instance logger="core.modules.input" name="request-param"
class="org.apache.cocoon.components.modules.input.RequestParameterModule"/>
<component-instance logger="core.modules.input" name="raw-request-param"
class="org.apache.cocoon.components.modules.input.RawRequestParameterModule"/>
<component-instance logger="core.modules.input" name="request-attr"
class="org.apache.cocoon.components.modules.input.RequestAttributeModule"/>
+ <component-instance logger="core.modules.input"
name="request-scoped-attr"
class="org.apache.cocoon.components.modules.input.RequestScopedAttributeModule"/>
<component-instance logger="core.modules.input" name="request-header"
class="org.apache.cocoon.components.modules.input.HeaderAttributeModule"/>
<component-instance logger="core.modules.input" name="session-attr"
class="org.apache.cocoon.components.modules.input.SessionAttributeModule"/>
<component-instance logger="core.modules.input" name="system-property"
class="org.apache.cocoon.components.modules.input.SystemPropertyModule"/>