1 16 package org.apache.cocoon.acting; 17 18 import java.util.Map ; 19 20 import org.apache.avalon.framework.parameters.ParameterException; 21 import org.apache.avalon.framework.parameters.Parameterizable; 22 import org.apache.avalon.framework.parameters.Parameters; 23 import org.apache.avalon.framework.thread.ThreadSafe; 24 import org.apache.cocoon.environment.ObjectModelHelper; 25 import org.apache.cocoon.environment.Redirector; 26 import org.apache.cocoon.environment.SourceResolver; 27 28 37 public class SetterAction 38 extends AbstractAction 39 implements Parameterizable, ThreadSafe { 40 41 public static final int MODE_OBJECT_MODEL = 1; 42 public static final int MODE_REQUEST_ATTR = 2; 43 public static final int MODE_SESSION_ATTR = 3; 44 45 public static final String MODEDEF_OBJECT_MODEL = "object-model"; 46 public static final String MODEDEF_REQUEST_ATTR = "request-attribute"; 47 public static final String MODEDEF_SESSION_ATTR = "session-attribute"; 48 49 protected int mode = MODE_OBJECT_MODEL; 50 51 55 public void parameterize(Parameters params) 56 throws ParameterException { 57 String modeDef = params.getParameter("mode", null); 58 if ( modeDef != null ) { 59 if ( MODEDEF_OBJECT_MODEL.equals(modeDef) ) { 60 this.mode = MODE_OBJECT_MODEL; 61 } else if ( MODEDEF_REQUEST_ATTR.equals(modeDef) ) { 62 this.mode = MODE_REQUEST_ATTR; 63 } else if ( MODEDEF_SESSION_ATTR.equals(modeDef) ) { 64 this.mode = MODE_SESSION_ATTR; 65 } else { 66 throw new ParameterException("Unknown mode: " + this.mode); 67 } 68 } 69 } 70 71 74 public Map act(Redirector redirector, 75 SourceResolver resolver, 76 Map objectModel, 77 String source, 78 Parameters parameters) 79 throws Exception { 80 final String [] names = parameters.getNames(); 81 for(int i = 0; i < names.length; i++) { 82 final String name = names[i]; 83 if ( this.mode == MODE_OBJECT_MODEL ) { 84 objectModel.put(name, parameters.getParameter(name)); 85 } else if ( this.mode == MODE_REQUEST_ATTR ) { 86 ObjectModelHelper.getRequest(objectModel).setAttribute(name, parameters.getParameter(name)); 87 } else if ( this.mode == MODE_SESSION_ATTR ) { 88 ObjectModelHelper.getRequest(objectModel).getSession().setAttribute(name, parameters.getParameter(name)); 89 } 90 } 91 return EMPTY_MAP; 92 } 93 } 94 | Popular Tags |