You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2004/07/11 19:18:26 UTC

cvs commit: cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/formmodel Field.java Form.java

vgritsenko    2004/07/11 10:18:26

  Modified:    src/blocks/forms/java/org/apache/cocoon/forms/formmodel
                        Field.java Form.java
  Log:
  Remove doValidate(), move validation code from process() back into the validate().
  http://marc.theaimsgroup.com/?l=xml-cocoon-dev&m=108860790208866
  
  Revision  Changes    Path
  1.19      +33 -33    cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Field.java
  
  Index: Field.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Field.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- Field.java	29 Jun 2004 13:06:04 -0000	1.18
  +++ Field.java	11 Jul 2004 17:18:26 -0000	1.19
  @@ -1,12 +1,12 @@
   /*
    * Copyright 1999-2004 The Apache Software Foundation.
  - * 
  + *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
  - * 
  + *
    *      http://www.apache.org/licenses/LICENSE-2.0
  - * 
  + *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  @@ -55,55 +55,55 @@
   
       protected String enteredValue;
       protected Object value;
  -    
  +
       /**
        * Value state indicating that a new value has been read from the request,
        * but has not yet been parsed.
        */
       protected final static int VALUE_UNPARSED = 0;
  -    
  +
       /**
        * Value state indicating that a value has been parsed, but needs to be
        * validated (that must occur before the value is given to the application)
        */
       protected final static int VALUE_PARSED = 1;
  -    
  +
       /**
        * Value state indicating that a parse error was encountered but should not
        * yet be displayed.
        */
       protected final static int VALUE_PARSE_ERROR = 2;
  -    
  +
       /**
        * Value state indicating that validate() has been called when state was
        * VALUE_PARSE_ERROR. This makes the error visible on output.
        */
       protected final static int VALUE_DISPLAY_PARSE_ERROR = 3;
  -    
  +
       /**
        * Transient value state indicating that validation is going on.
  -     * 
  +     *
        * @see #validate()
        */
       protected final static int VALUE_VALIDATING = 4;
  -    
  +
       /**
        * Value state indicating that validation has occured, but that any error should not
        * yet be displayed.
        */
       protected final static int VALUE_VALIDATED = 5;
  -    
  +
       /**
        * Value state indicating that value validation has occured, and the
        * validation error, if any, should be displayed.
        */
       protected final static int VALUE_DISPLAY_VALIDATION = 6;
  -    
  +
       // At startup, we have no value to parse (both enteredValue and value are null),
       // but need to validate (e.g. error if field is required)
       protected int valueState = VALUE_PARSED;
   
  -    
  +
       /**
        * Transient widget processing state indicating that the widget is currently validating
        * (used to avoid endless loops when a validator calls getValue)
  @@ -129,7 +129,7 @@
           if (this.valueState == VALUE_VALIDATING) {
               return this.value;
           }
  -        
  +
           // Parse the value
           if (this.valueState == VALUE_UNPARSED) {
               doParse();
  @@ -176,7 +176,7 @@
           //if (newEnteredValue != null) {
               readFromRequest(newEnteredValue);
           //}
  -        
  +
       }
   
       protected void readFromRequest(String newEnteredValue) {
  @@ -196,7 +196,7 @@
               validationError = null;
               value = null;
               this.valueState = VALUE_UNPARSED;
  -            
  +
               if (hasValueChangedListeners()) {
   	        	    // Throw an event that will parse the value only if needed.
   	    	        getForm().addWidgetEvent(new DeferredValueChangedEvent(this, value));
  @@ -208,22 +208,22 @@
           if (this.valueState == VALUE_UNPARSED) {
               doParse();
           }
  -        
  +
           // Force validation on already validated values (but keep invalid parsings)
           if (this.valueState >= VALUE_VALIDATED) {
               this.valueState = VALUE_PARSED;
           }
  -        
  +
           if (this.valueState == VALUE_PARSED) {
               doValidate();
               this.valueState = VALUE_DISPLAY_VALIDATION;
           } else if (this.valueState == VALUE_PARSE_ERROR) {
               this.valueState = VALUE_DISPLAY_PARSE_ERROR;
           }
  -        
  +
           return this.validationError == null;
       }
  -    
  +
       /**
        * Parse the value that has been read from the request.
        * Should be called when valueState is VALUE_UNPARSED.
  @@ -247,17 +247,17 @@
                   this.value = conversionResult.getResult();
                   this.valueState = VALUE_PARSED;
               } else {
  -                   // Conversion failed
  +                // Conversion failed
                   this.validationError = conversionResult.getValidationError();
                   // No need for further validation (and need to keep the above error)
                   this.valueState = VALUE_PARSE_ERROR;
               }
           } else {
  -               // No value: needs to be validated
  -               this.valueState = VALUE_PARSED;
  +            // No value: needs to be validated
  +            this.valueState = VALUE_PARSED;
           }
       }
  -    
  +
       /**
        * Validate the value once it has been parsed.
        * Should be called when valueState is VALUE_PARSED.
  @@ -268,10 +268,10 @@
           if (this.valueState != VALUE_PARSED) {
               throw new IllegalStateException("Field is not in PARSED state (" + this.valueState + ")");
           }
  -        
  +
           // Go to transient validating state
           this.valueState = VALUE_VALIDATING;
  -        
  +
           try {
               if (this.value == null && getFieldDefinition().isRequired()) {
                   // Field is required
  @@ -279,7 +279,7 @@
               } else {
                   if (super.validate()) {
                       // New-style validators were successful. Check the old-style ones.
  -                    this.validationError = getDatatype().validate(value, new ExpressionContextImpl(this));                
  +                    this.validationError = getDatatype().validate(value, new ExpressionContextImpl(this));
                   }
               }
           } finally {
  @@ -317,7 +317,7 @@
       private static final String FIELD_EL = "field";
       private static final String VALUE_EL = "value";
       private static final String VALIDATION_MSG_EL = "validation-message";
  -    
  +
   
       /**
        * @return "field"
  @@ -325,7 +325,7 @@
       public String getXMLElementName() {
           return FIELD_EL;
       }
  -    
  +
       /**
        * Adds the @required attribute
        */
  @@ -333,8 +333,8 @@
           AttributesImpl attrs = super.getXMLElementAttributes();
           attrs.addCDATAAttribute("required", String.valueOf(isRequired()));
           return attrs;
  -    }    
  -    
  +    }
  +
       public void generateItemSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
           if (enteredValue != null || value != null) {
               contentHandler.startElement(Constants.INSTANCE_NS, VALUE_EL, Constants.INSTANCE_PREFIX_COLON + VALUE_EL, XMLUtils.EMPTY_ATTRIBUTES);
  @@ -429,7 +429,7 @@
       public void removeValueChangedListener(ValueChangedListener listener) {
           this.listener = WidgetEventMulticaster.remove(this.listener, listener);
       }
  -    
  +
       private boolean hasValueChangedListeners() {
           return this.listener != null || this.fieldDefinition.hasValueChangedListeners();
       }
  
  
  
  1.16      +56 -59    cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Form.java
  
  Index: Form.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/formmodel/Form.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- Form.java	7 May 2004 16:43:42 -0000	1.15
  +++ Form.java	11 Jul 2004 17:18:26 -0000	1.16
  @@ -1,12 +1,12 @@
   /*
    * Copyright 1999-2004 The Apache Software Foundation.
  - * 
  + *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
  - * 
  + *
    *      http://www.apache.org/licenses/LICENSE-2.0
  - * 
  + *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  @@ -30,15 +30,15 @@
   /**
    * A widget that serves as a container for other widgets, the top-level widget in
    * a form description file.
  - * 
  + *
    * @author Bruno Dumon
    * @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
    * @version CVS $Id$
    */
   public class Form extends AbstractContainerWidget {
  -    
  +
       private final FormDefinition definition;
  -    
  +
       private Boolean endProcessing;
       private Locale locale = Locale.getDefault();
       private FormHandler formHandler;
  @@ -59,20 +59,20 @@
       protected WidgetDefinition getDefinition() {
           return this.definition;
       }
  -    
  +
       /**
        * Events produced by child widgets should not be fired immediately, but queued in order to ensure
        * an overall consistency of the widget tree before being handled.
  -     * 
  +     *
        * @param event the event to queue
        */
       public void addWidgetEvent(WidgetEvent event) {
  -        
  +
           if (this.bufferEvents) {
               if (this.events == null) {
                   this.events = new CursorableLinkedList();
               }
  -            
  +
               // FIXME: limit the number of events to detect recursive event loops ?
               this.events.add(event);
           } else {
  @@ -80,7 +80,7 @@
               event.getSourceWidget().broadcastEvent(event);
           }
       }
  -    
  +
       /**
        * Fire the widget events that have been queued. Note that event handling can fire new
        * events.
  @@ -95,14 +95,14 @@
                       formHandler.handleEvent(event);
               }
               cursor.close();
  -        
  +
               this.events.clear();
           }
       }
   
       /**
        * Get the locale to be used to process this form.
  -     * 
  +     *
        * @return the form's locale.
        */
       public Locale getLocale() {
  @@ -112,16 +112,16 @@
       /**
        * Get the widget that triggered the current processing. Note that it can be any widget, and
        * not necessarily an action or a submit.
  -     * 
  +     *
        * @return the widget that submitted this form.
        */
       public Widget getSubmitWidget() {
           return this.submitWidget;
       }
  -    
  +
       /**
        * Set the widget that triggered the current form processing.
  -     * 
  +     *
        * @param widget the widget
        */
       public void setSubmitWidget(Widget widget) {
  @@ -151,7 +151,7 @@
   //        if (this.phase != ProcessingPhase.VALIDATE) {
   //            throw new IllegalStateException("Cannot save model in phase " + this.phase);
   //        }
  -//        
  +//
   //        if (!isValid()) {
   //            throw new IllegalStateException("Cannot save an invalid form.");
   //        }
  @@ -162,7 +162,7 @@
       public void addProcessingPhaseListener(ProcessingPhaseListener listener) {
           this.listener = WidgetEventMulticaster.add(this.listener, listener);
       }
  -    
  +
       public void removeProcessingPhaseListener(ProcessingPhaseListener listener) {
           this.listener = WidgetEventMulticaster.remove(this.listener, listener);
       }
  @@ -184,21 +184,21 @@
        * {@link #endProcessing(boolean)}.
        */
       public boolean process(FormContext formContext) {
  -        
  +
           // Fire the binding phase events
           fireWidgetEvents();
  -        
  +
           // setup processing
           this.submitWidget = null;
           this.locale = formContext.getLocale();
           this.endProcessing = null;
           this.isValid = false;
  -        
  +
           // Notify the end of the current phase
           if (this.listener != null) {
               this.listener.phaseEnded(new ProcessingPhaseEvent(this, this.phase));
           }
  -        
  +
           this.phase = ProcessingPhase.READ_FROM_REQUEST;
           // Find the submit widget, if not an action
           this.submitWidget = null;
  @@ -212,16 +212,16 @@
                       throw new IllegalArgumentException("Invalid submit id (no such widget): " + submitId);
                   }
               }
  -            
  +
               setSubmitWidget(submit);
           }
  -        
  +
           try {
               // Start buffering events
               this.bufferEvents = true;
  -            
  +
               doReadFromRequest(formContext);
  -            
  +
               // Fire events, still buffering them: this ensures they will be handled in the same
               // order as they were added.
               fireWidgetEvents();
  @@ -229,51 +229,30 @@
               // No need for buffering in the following phases
               this.bufferEvents = false;
           }
  -        
  -        // Notify the end of the current phase
  -        if (this.listener != null) {
  -            this.listener.phaseEnded(new ProcessingPhaseEvent(this, this.phase));
  -        }
   
  -        if (this.endProcessing != null) {
  -            return this.endProcessing.booleanValue();
  -        }
  -
  -        // Validate the form
  -        this.phase = ProcessingPhase.VALIDATE;
  -        this.isValid = doValidate();
  -
  -        if (this.endProcessing != null) {
  -            return this.endProcessing.booleanValue();
  -        }
  -        
           // Notify the end of the current phase
           if (this.listener != null) {
               this.listener.phaseEnded(new ProcessingPhaseEvent(this, this.phase));
           }
  -        
           if (this.endProcessing != null) {
  -            // De-validate the form if one of the listeners asked to end the processing
  -            // This allows for additional application-level validation.
  -            this.isValid = false;
               return this.endProcessing.booleanValue();
           }
   
  -        return this.isValid;
  +        return validate();
       }
  -    
  +
       /**
        * End the current form processing after the current phase.
  -     * 
  +     *
        * @param redisplayForm indicates if the form should be redisplayed to the user.
        */
       public void endProcessing(boolean redisplayForm) {
           this.endProcessing = new Boolean(!redisplayForm);
       }
  -    
  +
       /**
        * Was form validation successful ?
  -     * 
  +     *
        * @return <code>true</code> if the form was successfully validated.
        */
       public boolean isValid() {
  @@ -286,15 +265,34 @@
   
       private void doReadFromRequest(FormContext formContext) {
           // let all individual widgets read their value from the request object
  -        super.readFromRequest(formContext); 
  +        super.readFromRequest(formContext);
       }
   
  +    /**
  +     * Performs validation phase of form processing.
  +     */
       public boolean validate() {
  -        throw new UnsupportedOperationException("Please use Form.process()");
  -    }
  +        // Validate the form
  +        this.phase = ProcessingPhase.VALIDATE;
  +        this.isValid = super.validate();
  +
  +        // FIXME: Is this check needed, before invoking the listener?
  +        if (this.endProcessing != null) {
  +            return this.endProcessing.booleanValue();
  +        }
   
  -    public boolean doValidate() {
  -        return super.validate();
  +        // Notify the end of the current phase
  +        if (this.listener != null) {
  +            this.listener.phaseEnded(new ProcessingPhaseEvent(this, this.phase));
  +        }
  +        if (this.endProcessing != null) {
  +            // De-validate the form if one of the listeners asked to end the processing
  +            // This allows for additional application-level validation.
  +            this.isValid = false;
  +            return this.endProcessing.booleanValue();
  +        }
  +
  +        return this.isValid;
       }
   
       private static final String FORM_EL = "form";
  @@ -302,5 +300,4 @@
       public String getXMLElementName() {
           return FORM_EL;
       }
  -    
   }