cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Christofer.Dutz@univativ.de" <christofer.d...@univativ.de>
Subject FormContentGenerator
Date Thu, 05 Oct 2006 08:22:05 GMT
Hi,

 

this generator expects a "continuation-id" parameter and uses this to lookup the associated
CForms form model. This is then serialized to xml where the ids of the widgets are transformed
to tag-names. I know this could be problematic with freaky ids but I didn't want to spend
the time modifying my version, if none finds it useful. I used this generator to generate
context-sensitive content (Suggestion-lists that's content depend on values of other form
fields) for my Ajax widgets (The base code was borrowed from the suggestion-list generator).
If you want I could modify it so it generates <widget id="blah">...</widget> instead
of <blah>...</blah>.

 

Regards,

    Chris

 

 

 

/*

 * Copyright 2005 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.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package org.apache.cocoon.generation;

 

import java.io.IOException;

import java.util.Iterator;

import java.util.Map;

 

import org.apache.avalon.framework.context.Context;

import org.apache.avalon.framework.context.ContextException;

import org.apache.avalon.framework.context.Contextualizable;

import org.apache.avalon.framework.parameters.Parameters;

import org.apache.avalon.framework.service.ServiceException;

import org.apache.avalon.framework.service.ServiceManager;

import org.apache.cocoon.ProcessingException;

import org.apache.cocoon.components.flow.ContinuationsManager;

import org.apache.cocoon.components.flow.InvalidContinuationException;

import org.apache.cocoon.components.flow.WebContinuation;

import org.apache.cocoon.environment.ObjectModelHelper;

import org.apache.cocoon.environment.Request;

import org.apache.cocoon.environment.SourceResolver;

import org.apache.cocoon.forms.FormsConstants;

import org.apache.cocoon.forms.formmodel.Action;

import org.apache.cocoon.forms.formmodel.ContainerWidget;

import org.apache.cocoon.forms.formmodel.Form;

import org.apache.cocoon.forms.formmodel.Group;

import org.apache.cocoon.forms.formmodel.Repeater;

import org.apache.cocoon.forms.formmodel.Widget;

import org.apache.cocoon.forms.formmodel.Repeater.RepeaterRow;

import org.apache.cocoon.generation.ServiceableGenerator;

import org.apache.cocoon.sitemap.SitemapParameters;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.AttributesImpl;

 

/**

 * A generator for cforms form content.

 * 

 * @version $Id: FormContentGenerator.java 385330 2006-03-12 18:25:06Z

 *          Christofer Dutz $

 */

public class FormContentGenerator extends ServiceableGenerator implements

                        Contextualizable {

            private AttributesImpl emptyAttr = new AttributesImpl();

 

            private ContinuationsManager contManager;

 

            private WebContinuation wk;

            

            private Form form;

 

            public void contextualize(Context ctx) throws ContextException {

            }

 

            public void service(ServiceManager manager) throws ServiceException {

                        super.service(manager);

                        this.contManager = (ContinuationsManager) manager

                                               .lookup(ContinuationsManager.ROLE);

            }

 

            public void setup(SourceResolver resolver, Map objectModel, String src,

                                   Parameters par) throws ProcessingException, SAXException,

                                   IOException {

                        super.setup(resolver, objectModel, src, par);

 

                        Request req = ObjectModelHelper.getRequest(objectModel);

 

                        String continuationId = par.getParameter("continuation-id", req

                                               .getParameter("continuation-id"));

 

                        // The interpreter id is the sitemap's URI

                        String interpreterId = SitemapParameters.getLocation(parameters)

                                               .getURI();

                        wk = this.contManager.lookupWebContinuation(continuationId,

                                               interpreterId);

                        if (wk == null || wk.disposed()) {

                                   throw new InvalidContinuationException(

                                                           "Cannot get continuation for specified
id");

                        }

 

                        form = (Form) wk.getAttribute("form");

                        if (form == null) {

                                   throw new ProcessingException(

                                                           "No form is attached to the continuation");

                        }

            }

 

            public void generate() throws IOException, SAXException,

                                   ProcessingException {

                        super.contentHandler.startDocument();

                        super.contentHandler.startPrefixMapping(FormsConstants.INSTANCE_PREFIX,

                                               FormsConstants.INSTANCE_NS);

 

                        // Recrusively output the form and all widgets

                        outputWidget(form);

 

                        super.contentHandler.endPrefixMapping(FormsConstants.INSTANCE_PREFIX);

                        super.contentHandler.endDocument();

            }

            

            protected void outputWidget(Widget widget) throws SAXException {

                        if(widget instanceof Form) {

                                   contentHandler.startElement("", "form", "form", emptyAttr);

                        } else {

                                   contentHandler.startElement("", widget.getId(), widget.getId(),
emptyAttr);

                        }

 

                        // The tag names are allways equal to the widgets ids

                        // If the widget is a container widget, then output

                        // it's children.

                        if(widget instanceof ContainerWidget) {

                                   ContainerWidget containerWidget = (ContainerWidget) widget;

                                   Iterator childIterator = containerWidget.getChildren();

                                   while(childIterator.hasNext()) {

                                               Widget child = (Widget) childIterator.next();

                                               outputWidget(child);

                                   }

                        } 

 

                        // Actions have no content and if we try to access the 

                        // value we get an exception

                        else if(widget instanceof Action) {

                        }

 

                        // if the current widget is a repeater then output

                        // a "repeater-row" element for each row and output

                        // that rows content within this

                        else if(widget instanceof Repeater) {

                                   Repeater repeater = (Repeater) widget;

                                   for(int i = 0; i < repeater.getSize(); i++) {

                                               RepeaterRow row = repeater.getRow(i);

                                               contentHandler.startElement("", "repeater-row",
"repeater-row", emptyAttr);

                                               Iterator childIterator = row.getChildren();

                                               while(childIterator.hasNext()) {

                                                           Widget child = (Widget) childIterator.next();

                                                           outputWidget(child);

                                               }

                                               contentHandler.endElement("", "repeater-row",
"repeater-row");

                                   }

                        }

                        

                        // if the widget is an ordinary widget, then output

                        // it's content.

                        else {

                                   Object value = widget.getValue();

                                   if((value != null) && (value.toString() != null))
{

                                               char charData[];

                                               charData = value.toString().toCharArray();

                                               contentHandler.characters(charData, 0, charData.length);

                                   }

                        }

 

                        if(widget instanceof Form) {

                                   contentHandler.endElement("", "form", "form");

                        } else {

                                   contentHandler.endElement("", widget.getId(), widget.getId());

                        }

            }

 

}

 

 

[ c h r i s t o f e r   d u t z ]

 

IT-Berater

univativ GmbH & Co. KG

Robert-Bosch-Str. 7, 64293 Darmstadt

 

fon: 0 61 51 / 66 717 - 21

fax: 0 61 51 / 66 717 - 29

email: christofer.dutz@univativ.de

http://www.univativ.de

 

Darmstadt, Stuttgart, Karlsruhe, Düsseldorf

 


Mime
View raw message