You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by br...@apache.org on 2003/05/14 13:33:38 UTC
cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/acting HandleFormSubmitAction.java
bruno 2003/05/14 04:33:38
Modified: src/blocks/woody/java/org/apache/cocoon/woody/formmodel
BooleanField.java Field.java Form.java
MultiValueField.java Repeater.java Widget.java
src/blocks/woody/java/org/apache/cocoon/woody/samples
InitForm1Action.java
src/blocks/woody/java/org/apache/cocoon/woody/acting
HandleFormSubmitAction.java
Added: src/blocks/woody/java/org/apache/cocoon/woody
FormContext.java
Log:
introduced a FormContext
Revision Changes Path
1.1 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/FormContext.java
Index: FormContext.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.woody;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.woody.event.ActionEvent;
import java.util.Locale;
/**
* Holds data needed during the processing of a form submit.
*/
public class FormContext {
private Request request;
private Locale locale;
private ActionEvent actionEvent;
private FormHandler formHandler;
private boolean doValidation;
public FormContext(Request request, Locale locale) {
this(request, locale, null);
}
public FormContext(Request request, Locale locale, FormHandler formHandler) {
this.request = request;
this.locale = locale;
this.formHandler = formHandler;
doValidation = true;
}
/**
* Sets the current ActionEvent. An ActionEvent is the result of a certain user
* action that caused a form submit. For example, pressing a button.
*
* <p>This method will be called by the widget that detected an action has been
* performed on it. The Event will then be performed after all widgets have been
* through the "readFromRequest" stage.
*
* <p>If an action event is set, validation is automatically disabled.
*/
public void setActionEvent(ActionEvent actionEvent) {
if (this.actionEvent != null)
throw new RuntimeException("There is already an actionEvent set on this formContext!");
doValidation = false;
this.actionEvent = actionEvent;
}
public ActionEvent getActionEvent() {
return actionEvent;
}
public Request getRequest() {
return request;
}
public Locale getLocale() {
return locale;
}
public boolean doValidation() {
return doValidation;
}
public FormHandler getFormHandler() {
return formHandler;
}
}
1.2 +4 -4 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/BooleanField.java
Index: BooleanField.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/BooleanField.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BooleanField.java 22 Apr 2003 12:04:19 -0000 1.1
+++ BooleanField.java 14 May 2003 11:33:37 -0000 1.2
@@ -50,8 +50,8 @@
*/
package org.apache.cocoon.woody.formmodel;
-import org.apache.cocoon.environment.Request;
import org.apache.cocoon.woody.Constants;
+import org.apache.cocoon.woody.FormContext;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
@@ -81,15 +81,15 @@
return definition.getId();
}
- public void readFromRequest(Request request, Locale locale) {
- String param = request.getParameter(getFullyQualifiedId());
+ public void readFromRequest(FormContext formContext) {
+ String param = formContext.getRequest().getParameter(getFullyQualifiedId());
if (param != null && param.equalsIgnoreCase("true"))
value = Boolean.TRUE;
else
value = Boolean.FALSE;
}
- public boolean validate(Locale locale) {
+ public boolean validate(FormContext formContext) {
// a boolean field is always valid
return true;
}
1.2 +5 -5 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Field.java
Index: Field.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Field.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Field.java 22 Apr 2003 12:04:19 -0000 1.1
+++ Field.java 14 May 2003 11:33:37 -0000 1.2
@@ -50,8 +50,8 @@
*/
package org.apache.cocoon.woody.formmodel;
-import org.apache.cocoon.environment.Request;
import org.apache.cocoon.woody.Constants;
+import org.apache.cocoon.woody.FormContext;
import org.apache.cocoon.woody.datatype.ValidationError;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
@@ -96,8 +96,8 @@
conversionFailed = false;
}
- public void readFromRequest(Request request, Locale locale) {
- enteredValue = request.getParameter(getFullyQualifiedId());
+ public void readFromRequest(FormContext formContext) {
+ enteredValue = formContext.getRequest().getParameter(getFullyQualifiedId());
validationError = null;
conversionFailed = false;
@@ -112,14 +112,14 @@
// try to convert entered string to the field's native datatype
if (enteredValue != null) {
- value = definition.getDatatype().convertFromStringLocalized(enteredValue, locale);
+ value = definition.getDatatype().convertFromStringLocalized(enteredValue, formContext.getLocale());
if (value == null)
conversionFailed = true;
} else
value = null;
}
- public boolean validate(Locale locale) {
+ public boolean validate(FormContext formContext) {
if (value != null)
validationError = definition.getDatatype().validate(value, new ExpressionContextImpl(this));
else if (conversionFailed)
1.2 +28 -5 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Form.java
Index: Form.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Form.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Form.java 22 Apr 2003 12:04:19 -0000 1.1
+++ Form.java 14 May 2003 11:33:37 -0000 1.2
@@ -50,8 +50,8 @@
*/
package org.apache.cocoon.woody.formmodel;
-import org.apache.cocoon.environment.Request;
import org.apache.cocoon.woody.Constants;
+import org.apache.cocoon.woody.FormContext;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
@@ -79,21 +79,44 @@
widgetsById.put(widget.getId(), widget);
}
- public void readFromRequest(Request request, Locale locale) {
+ /**
+ * Processes a form submit. This consists of multiple steps:
+ * <ul>
+ * <li>all widgets read their value from the request (i.e. {@link #readFromRequest} is called recursively on
+ * the whole widget tree)
+ * <li>if there is an action event, execute it
+ * <li>perform validation, if {@link FormContext#doValidation} returns true (which is true by default,
+ * but false by default if there is an action event).
+ * </ul>
+ *
+ * If the form is finished, i.e. validation was succesful and the form should not be redisplayed to the user,
+ * then this method returns true, otherwise it returns false.
+ */
+ public boolean process(FormContext formContext) {
+ readFromRequest(formContext);
+ if (formContext.getActionEvent() != null && formContext.getFormHandler() != null) {
+ formContext.getFormHandler().handleActionEvent(formContext.getActionEvent());
+ }
+ if (formContext.doValidation())
+ return validate(formContext);
+ return false;
+ }
+
+ public void readFromRequest(FormContext formContext) {
// let all individual widgets read their value from the request object
Iterator widgetIt = widgets.iterator();
while (widgetIt.hasNext()) {
Widget widget = (Widget)widgetIt.next();
- widget.readFromRequest(request, locale);
+ widget.readFromRequest(formContext);
}
}
- public boolean validate(Locale locale) {
+ public boolean validate(FormContext formContext) {
boolean allValid = true;
Iterator widgetIt = widgets.iterator();
while (widgetIt.hasNext()) {
Widget widget = (Widget)widgetIt.next();
- allValid = allValid & widget.validate(locale);
+ allValid = allValid & widget.validate(formContext);
}
return allValid;
}
1.2 +5 -5 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/MultiValueField.java
Index: MultiValueField.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/MultiValueField.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MultiValueField.java 22 Apr 2003 12:04:19 -0000 1.1
+++ MultiValueField.java 14 May 2003 11:33:37 -0000 1.2
@@ -50,9 +50,9 @@
*/
package org.apache.cocoon.woody.formmodel;
-import org.apache.cocoon.environment.Request;
import org.apache.cocoon.woody.datatype.ValidationError;
import org.apache.cocoon.woody.Constants;
+import org.apache.cocoon.woody.FormContext;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
@@ -88,8 +88,8 @@
return definition.getId();
}
- public void readFromRequest(Request request, Locale locale) {
- enteredValues = request.getParameterValues(getFullyQualifiedId());
+ public void readFromRequest(FormContext formContext) {
+ enteredValues = formContext.getRequest().getParameterValues(getFullyQualifiedId());
validationError = null;
values = null;
@@ -103,7 +103,7 @@
Object[] tempValues = new Object[enteredValues.length];
for (int i = 0; i < enteredValues.length; i++) {
String param = enteredValues[i];
- tempValues[i] = definition.getDatatype().convertFromStringLocalized(param, locale);
+ tempValues[i] = definition.getDatatype().convertFromStringLocalized(param, formContext.getLocale());
if (tempValues[i] == null) {
conversionFailed = true;
break;
@@ -119,7 +119,7 @@
}
}
- public boolean validate(Locale locale) {
+ public boolean validate(FormContext formContext) {
if (values != null)
validationError = definition.getDatatype().validate(values, new ExpressionContextImpl(this));
else
1.2 +17 -10 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Repeater.java
Index: Repeater.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Repeater.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Repeater.java 22 Apr 2003 12:04:19 -0000 1.1
+++ Repeater.java 14 May 2003 11:33:37 -0000 1.2
@@ -50,8 +50,8 @@
*/
package org.apache.cocoon.woody.formmodel;
-import org.apache.cocoon.environment.Request;
import org.apache.cocoon.woody.Constants;
+import org.apache.cocoon.woody.FormContext;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
@@ -94,6 +94,13 @@
}
/**
+ * @throws IndexOutOfBoundsException if the the index is outside the range of existing rows.
+ */
+ public void removeRow(int index) {
+ rows.remove(index);
+ }
+
+ /**
* Gets a widget on a certain row.
* @param rowIndex startin from 0
* @param id a widget id
@@ -110,9 +117,9 @@
return (RepeaterRow)rows.get(row);
}
- public void readFromRequest(Request request, Locale locale) {
+ public void readFromRequest(FormContext formContext) {
// read number of rows from request, and make an according number of rows
- String sizeParameter = request.getParameter(getFullyQualifiedId() + ".size");
+ String sizeParameter = formContext.getRequest().getParameter(getFullyQualifiedId() + ".size");
if (sizeParameter != null) {
int size = 0;
try {
@@ -133,16 +140,16 @@
Iterator rowIt = rows.iterator();
while (rowIt.hasNext()) {
RepeaterRow row = (RepeaterRow)rowIt.next();
- row.readFromRequest(request, locale);
+ row.readFromRequest(formContext);
}
}
- public boolean validate(Locale locale) {
+ public boolean validate(FormContext formContext) {
boolean valid = true;
Iterator rowIt = rows.iterator();
while (rowIt.hasNext()) {
RepeaterRow row = (RepeaterRow)rowIt.next();
- valid = valid & row.validate(locale);
+ valid = valid & row.validate(formContext);
}
return valid;
}
@@ -253,20 +260,20 @@
return (Widget)widgetsById.get(id);
}
- public void readFromRequest(Request request, Locale locale) {
+ public void readFromRequest(FormContext formContext) {
Iterator widgetIt = widgets.iterator();
while (widgetIt.hasNext()) {
Widget widget = (Widget)widgetIt.next();
- widget.readFromRequest(request, locale);
+ widget.readFromRequest(formContext);
}
}
- public boolean validate(Locale locale) {
+ public boolean validate(FormContext formContext) {
boolean valid = true;
Iterator widgetIt = widgets.iterator();
while (widgetIt.hasNext()) {
Widget widget = (Widget)widgetIt.next();
- valid = valid & widget.validate(locale);
+ valid = valid & widget.validate(formContext);
}
return valid;
}
1.2 +3 -3 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Widget.java
Index: Widget.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Widget.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Widget.java 22 Apr 2003 12:04:19 -0000 1.1
+++ Widget.java 14 May 2003 11:33:38 -0000 1.2
@@ -50,7 +50,7 @@
*/
package org.apache.cocoon.woody.formmodel;
-import org.apache.cocoon.environment.Request;
+import org.apache.cocoon.woody.FormContext;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
@@ -106,14 +106,14 @@
* may try to convert the request parameter to its native datatype (if it
* is not a string), but it should not yet generate any validation errors.
*/
- public void readFromRequest(Request request, Locale locale);
+ public void readFromRequest(FormContext formContext);
/**
* Validates this widget and returns the outcome. Possible error messages are
* remembered by the widget itself and will be part of the XML produced by
* this widget in its {@link #generateSaxFragment} method.
*/
- public boolean validate(Locale locale);
+ public boolean validate(FormContext formContext);
/**
* Generates an XML representation of this widget. The startDocument and endDocument
1.3 +0 -1 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/samples/InitForm1Action.java
Index: InitForm1Action.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/samples/InitForm1Action.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- InitForm1Action.java 13 May 2003 11:59:00 -0000 1.2
+++ InitForm1Action.java 14 May 2003 11:33:38 -0000 1.3
@@ -51,7 +51,6 @@
package org.apache.cocoon.woody.samples;
import org.apache.cocoon.woody.acting.AbstractWoodyAction;
-import org.apache.cocoon.woody.formmodel.FormDefinition;
import org.apache.cocoon.woody.formmodel.Form;
import org.apache.cocoon.woody.formmodel.Repeater;
import org.apache.cocoon.woody.formmodel.Field;
1.3 +20 -7 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/acting/HandleFormSubmitAction.java
Index: HandleFormSubmitAction.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/acting/HandleFormSubmitAction.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- HandleFormSubmitAction.java 13 May 2003 11:59:00 -0000 1.2
+++ HandleFormSubmitAction.java 14 May 2003 11:33:38 -0000 1.3
@@ -52,16 +52,14 @@
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.avalon.framework.parameters.Parameters;
-import org.apache.avalon.framework.component.ComponentManager;
-import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.Composable;
import org.apache.cocoon.acting.Action;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.ObjectModelHelper;
-import org.apache.cocoon.woody.FormManager;
-import org.apache.cocoon.woody.formmodel.FormDefinition;
+import org.apache.cocoon.woody.FormContext;
+import org.apache.cocoon.woody.FormHandler;
import org.apache.cocoon.woody.formmodel.Form;
import java.util.Map;
@@ -86,15 +84,30 @@
throws Exception {
String formSource = parameters.getParameter("form-definition");
String formAttribute = parameters.getParameter("attribute-name");
+ String formHandlerClassName = parameters.getParameter("formhandler", null);
Form form = formManager.createForm(resolver.resolveURI(formSource));
Request request = ObjectModelHelper.getRequest(objectModel);
- form.readFromRequest(request, Locale.US);
- boolean validationSuccess = form.validate(Locale.US);
+ FormHandler formHandler = null;
+
+ if (formHandlerClassName != null) {
+ // TODO cache these classes
+ Class clazz = Class.forName(formHandlerClassName);
+ formHandler = (FormHandler)clazz.newInstance();
+ formHandler.setup(form);
+ }
+
+ FormContext formContext;
+ if (formHandler == null)
+ formContext = new FormContext(request, Locale.US);
+ else
+ formContext = new FormContext(request, Locale.US, formHandler);
+
+ boolean finished = form.process(formContext);
request.setAttribute(formAttribute, form);
- if (validationSuccess)
+ if (finished)
return Collections.EMPTY_MAP;
else
return null;