001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.components.modules.input;
019:
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.avalon.framework.thread.ThreadSafe;
023: import org.apache.cocoon.environment.ObjectModelHelper;
024: import org.apache.cocoon.environment.Request;
025:
026: import java.util.Enumeration;
027: import java.util.Iterator;
028: import java.util.LinkedList;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.SortedSet;
032: import java.util.TreeSet;
033:
034: /**
035: * RequestAttributeModule accesses request attributes. If the
036: * attribute name contains an askerisk "*" this is considered a
037: * wildcard and all attributes that would match this wildcard are
038: * considered to be part of an array of that name for
039: * getAttributeValues. Only one "*" is allowed.
040: *
041: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
042: * @version $Id: RequestAttributeModule.java 433543 2006-08-22 06:22:54Z crossley $
043: */
044: public class RequestAttributeModule extends AbstractInputModule
045: implements ThreadSafe {
046:
047: public Object getAttribute(String name, Configuration modeConf,
048: Map objectModel) throws ConfigurationException {
049:
050: String pname = (String) this .settings.get("parameter", name);
051: if (modeConf != null) {
052: pname = modeConf.getAttribute("parameter", pname);
053: // preferred
054: pname = modeConf.getChild("parameter").getValue(pname);
055: }
056: return ObjectModelHelper.getRequest(objectModel).getAttribute(
057: pname);
058: }
059:
060: /* (non-Javadoc)
061: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration, java.util.Map)
062: */
063: public Iterator getAttributeNames(Configuration modeConf,
064: Map objectModel) throws ConfigurationException {
065: return new IteratorHelper(ObjectModelHelper.getRequest(
066: objectModel).getAttributeNames());
067: }
068:
069: public Object[] getAttributeValues(String name,
070: Configuration modeConf, Map objectModel)
071: throws ConfigurationException {
072: final Request request = ObjectModelHelper
073: .getRequest(objectModel);
074:
075: String wildcard = (String) this .settings.get("parameter", name);
076: if (modeConf != null) {
077: wildcard = modeConf.getAttribute("parameter", wildcard);
078: // preferred
079: wildcard = modeConf.getChild("parameter")
080: .getValue(wildcard);
081: }
082: int wildcardIndex = wildcard.indexOf("*");
083: if (wildcardIndex != -1) {
084: // "*" contained in attribute name => combine all
085: // attributes' values that match prefix, suffix
086:
087: // split the attribute's name so that the "*" could be
088: // determined by looking at the attributes' names that
089: // start with the prefix and end with the suffix
090: //
091: String prefix = wildcard.substring(0, wildcardIndex);
092: String suffix;
093: if (wildcard.length() >= wildcardIndex + 1) {
094: suffix = wildcard.substring(wildcardIndex + 1);
095: } else {
096: suffix = "";
097: }
098: SortedSet names = new TreeSet();
099: Enumeration allNames = request.getAttributeNames();
100:
101: while (allNames.hasMoreElements()) {
102: String pname = (String) allNames.nextElement();
103: if (pname.startsWith(prefix) && pname.endsWith(suffix)) {
104: names.add(pname);
105: }
106: }
107:
108: List values = new LinkedList();
109: Iterator j = names.iterator();
110: while (j.hasNext()) {
111: String pname = (String) j.next();
112: values.add(request.getAttribute(pname));
113: }
114:
115: return values.toArray();
116:
117: } else {
118: // no "*" in attribute name => just return all values of
119: // this one attribute. Make sure, it's an array.
120:
121: Object value = request.getAttribute(wildcard);
122: if (value != null && !value.getClass().isArray()) {
123: Object[] values = new Object[1];
124: values[0] = value;
125: return values;
126: } else {
127: return (Object[]) value;
128: }
129:
130: }
131:
132: }
133:
134: }
|