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 import org.apache.cocoon.environment.Session; 25 26 import java.util.Enumeration ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.SortedSet ; 32 import java.util.TreeSet ; 33 34 44 public class SessionAttributeModule extends AbstractInputModule implements ThreadSafe { 45 46 public Object getAttribute( String name, Configuration modeConf, Map objectModel ) 47 throws ConfigurationException { 48 49 String pname = (String ) this.settings.get("parameter", name); 50 if ( modeConf != null ) { 51 pname = modeConf.getAttribute( "parameter", pname ); 52 pname = modeConf.getChild("parameter").getValue(pname); 54 } 55 return ObjectModelHelper.getRequest(objectModel).getSession().getAttribute( pname ); 56 } 57 58 59 public Iterator getAttributeNames( Configuration modeConf, Map objectModel ) 60 throws ConfigurationException { 61 62 return new IteratorHelper(ObjectModelHelper.getRequest(objectModel).getSession().getAttributeNames()); 63 } 64 65 66 public Object [] getAttributeValues( String name, Configuration modeConf, Map objectModel ) 67 throws ConfigurationException { 68 69 Request request = ObjectModelHelper.getRequest(objectModel); 70 String wildcard = (String ) this.settings.get("parameter", name); 71 if ( modeConf != null ) { 72 wildcard = modeConf.getAttribute( "parameter", wildcard ); 73 wildcard = modeConf.getChild("parameter").getValue(wildcard); 75 } 76 int wildcardIndex = wildcard.indexOf( "*" ); 77 if ( wildcardIndex != -1 ) { 78 81 String prefix = wildcard.substring( 0, wildcardIndex ); 86 String suffix; 87 if ( wildcard.length() >= wildcardIndex + 1 ) { 88 suffix = wildcard.substring( wildcardIndex + 1 ); 89 } else { 90 suffix = ""; 91 } 92 SortedSet names = new TreeSet (); 93 Session session = request.getSession(); 94 Enumeration allNames = session.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( session.getAttribute(pname) ); 108 } 109 110 return values.toArray(); 111 112 } else { 113 116 Object value = request.getSession().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 | Popular Tags |