1 16 17 package org.apache.cocoon.components.modules.input; 18 19 import org.apache.avalon.framework.configuration.Configuration; 20 import org.apache.avalon.framework.configuration.ConfigurationException; 21 import org.apache.avalon.framework.thread.ThreadSafe; 22 import org.apache.cocoon.environment.ObjectModelHelper; 23 import org.apache.cocoon.environment.Request; 24 25 import java.util.Enumeration ; 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.SortedSet ; 31 import java.util.TreeSet ; 32 33 43 public class RequestAttributeModule extends AbstractInputModule implements ThreadSafe { 44 45 public Object getAttribute( String name, Configuration modeConf, Map objectModel ) 46 throws ConfigurationException { 47 48 String pname = (String ) this.settings.get("parameter", name); 49 if ( modeConf != null ) { 50 pname = modeConf.getAttribute( "parameter", pname ); 51 pname = modeConf.getChild("parameter").getValue(pname); 53 } 54 return ObjectModelHelper.getRequest(objectModel).getAttribute( pname ); 55 } 56 57 58 61 public Iterator getAttributeNames( Configuration modeConf, Map objectModel ) 62 throws ConfigurationException { 63 return new IteratorHelper(ObjectModelHelper.getRequest(objectModel).getAttributeNames()); 64 } 65 66 67 public Object [] getAttributeValues( String name, Configuration modeConf, Map objectModel ) 68 throws ConfigurationException { 69 final Request request = ObjectModelHelper.getRequest(objectModel); 70 71 String wildcard = (String ) this.settings.get("parameter",name); 72 if ( modeConf != null ) { 73 wildcard = modeConf.getAttribute( "parameter", wildcard ); 74 wildcard = modeConf.getChild("parameter").getValue(wildcard); 76 } 77 int wildcardIndex = wildcard.indexOf( "*" ); 78 if ( wildcardIndex != -1 ) { 79 82 String prefix = wildcard.substring( 0, wildcardIndex ); 87 String suffix; 88 if ( wildcard.length() >= wildcardIndex + 1 ) { 89 suffix = wildcard.substring( wildcardIndex + 1 ); 90 } else { 91 suffix = ""; 92 } 93 SortedSet names = new TreeSet (); 94 Enumeration allNames = request.getAttributeNames(); 95 96 while (allNames.hasMoreElements()) { 97 String pname = (String ) allNames.nextElement(); 98 if ( pname.startsWith( prefix ) && pname.endsWith( suffix ) ) { 99 names.add(pname); 100 } 101 } 102 103 List values = new LinkedList (); 104 Iterator j = names.iterator(); 105 while (j.hasNext()){ 106 String pname = (String ) j.next(); 107 values.add( request.getAttribute( pname ) ); 108 } 109 110 return values.toArray(); 111 112 } else { 113 116 Object value = request.getAttribute( wildcard ); 117 if ( value != null && !value.getClass().isArray() ) { 118 Object [] values = new Object [1]; 119 values[0] = value; 120 return values; 121 } else { 122 return (Object []) value; 123 } 124 125 } 126 127 } 128 129 130 131 } 132 | Popular Tags |