ivelin 2002/10/27 00:25:19 Modified: src/java/org/apache/cocoon/transformation XMLFormTransformer.java Log: Implement automated population with default values of unchecked check-boxes Revision Changes Path 1.15 +94 -11 xml-cocoon2/src/java/org/apache/cocoon/transformation/XMLFormTransformer.java Index: XMLFormTransformer.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/transformation/XMLFormTransformer.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- XMLFormTransformer.java 28 Sep 2002 19:42:05 -0000 1.14 +++ XMLFormTransformer.java 27 Oct 2002 07:25:19 -0000 1.15 @@ -122,6 +122,7 @@ */ public final static String TAG_FORM = "form"; public final static String TAG_FORM_ATTR_ID = "id"; + public final static String TAG_FORM_ATTR_VIEW = "view"; /** * the only tag which can be used outside of the form tag @@ -284,13 +285,21 @@ public final static String TAG_COMMON_ATTR_REF = "ref"; - // the stack of nested forms. - // Although nested form tags are not allowed, it is possible - // that an output tag (with reference to another form) might be nested within a form tag. - // In this case elements under the output tag (like caption) can reference properties - // of the form of the enclosing output tag. + /** the stack of nested forms. + * Although nested form tags are not allowed, it is possible + * that an output tag (with reference to another form) might be nested within a form tag. + * In this case elements under the output tag (like caption) can reference properties + * of the form of the enclosing output tag. + */ private Stack formStack = null; + + + /** + * Since form elements cannot be nested, + * at most one possible value for the current form view is available. + */ + private String currentFormView = null; private Object value_; @@ -517,7 +526,7 @@ TAG_SELECTMANY.equals(name) ) { - startElementSimpleField( uri, name, raw, attributes ); + startElementInputField( uri, name, raw, attributes ); } else if ( TAG_CAPTION.equals(name) || @@ -543,7 +552,7 @@ // since there are intricacies in // handling the value sub-element isHiddenTag = true; - startElementSimpleField( uri, name, raw, attributes ); + startElementInputField( uri, name, raw, attributes ); } else { @@ -571,6 +580,7 @@ { String id = attributes.getValue(TAG_FORM_ATTR_ID); + // currently form elements cannot be nested if ( !formStack.isEmpty() ) { String error = "Form nodes should not be nested ! Current form [id=" + formStack.peek() + "], nested form [id=" + String.valueOf(id) + "]"; @@ -582,7 +592,7 @@ // load up the referenced form Form currentForm = Form.lookup( objectModel, id ); - + // if the form wasn't found, we're in trouble if (currentForm == null) { @@ -591,7 +601,16 @@ throw new IllegalStateException( error ); }; + formStack.push( currentForm ); + + // memorize the current form view + // it will be needed when saving expected references to properties + currentFormView = attributes.getValue(TAG_FORM_ATTR_VIEW); + + // clear previously saved form state for this view + resetSavedModelReferences(); + } // end of startElementForm @@ -711,6 +730,27 @@ } // end of startElementOutput + /** + * + * Renders elements, which are used for input + * + * TAG_TEXTBOX, TAG_TEXTAREA, TAG_PASSWORD, TAG_SELECTBOOLEAN, + * TAG_SELECTONE, TAG_SELECTMANY + * + */ + protected void startElementInputField(String uri, String name, String raw, Attributes attributes ) + throws SAXException + { + startElementSimpleField( uri, name, raw, attributes ); + + String ref = attributes.getValue(TAG_COMMON_ATTR_REF); + if (ref == null) + { + throw new SAXException( name + " element should provide a '" + TAG_COMMON_ATTR_REF + "' attribute" ); + } + saveModelReferenceForFormView( ref, name ); + } + protected void startElementSimpleField(String uri, String name, String raw, Attributes attributes ) throws SAXException @@ -727,10 +767,11 @@ throw new SAXException( name + " element should be either nested within a form tag or provide a form attribute" ); } - Form form = (Form) formStack.peek(); + Form form = getCurrentForm(); getLogger().debug("[" + String.valueOf( name ) + "] getting value from form [id=" + form.getId() + ", ref=" + String.valueOf(ref) + "]"); + // retrieve current value of referenced property value_ = form.getValue( ref ); // we will only forward the SAX event once we know @@ -742,10 +783,49 @@ // Only render value sub-elements // at this point // if this is not a xf:hidden element. - if(! isHiddenTag) renderValueSubElements(); + if( !isHiddenTag ) renderValueSubElements(); } // end of startElementSimpleField + /** + * Let the form wrapper know that this reference should be expected + * when data is submitted by the client for the current form view. + * The name of the XML tag is also saved to help the form populator + * find an appropriate default value when one is not provided in the http request. + */ + protected void saveModelReferenceForFormView( String ref, String name ) + { + // the xf:form/@view attribute is not mandatory + // although it is strongly recommended + if (currentFormView != null) + { + Form form = getCurrentForm(); + form.saveExpectedModelReferenceForView( currentFormView, ref, name ); + } + } + + /** + * When the transformer starts rendering a new form element + * It needs to reset previously saved references for another + * transformation of the same view. + */ + protected void resetSavedModelReferences() + { + if ( currentFormView != null ) + { + Form form = getCurrentForm(); + form.clearSavedModelReferences( currentFormView ); + } + + } + + /** + * Used for elements which are not two directional. + * They are displayed but cannot be used for submitting new values + * + * TAG_CAPTION, TAG_HINT, TAG_HELP, TAG_VALUE + * + */ protected void startElementWithOptionalRefAndSimpleContent(String uri, String name, String raw, Attributes attributes ) throws SAXException { @@ -1088,7 +1168,10 @@ } // unrollItemSetTag - + protected Form getCurrentForm() + { + return (Form) formStack.peek(); + } /**
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]