KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > generation > FormsGenerator


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.generation;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.environment.SourceResolver;
25 import org.apache.cocoon.forms.FormsConstants;
26 import org.apache.cocoon.forms.formmodel.Form;
27 import org.apache.cocoon.forms.transformation.FormsPipelineConfig;
28 import org.apache.cocoon.generation.AbstractGenerator;
29 import org.xml.sax.Attributes JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31
32 /**
33  * A generator that streams an XML representation of a {@link Form}. This will
34  * recursively contain the XML for all widgets on the form. This can then be styled
35  * using an XSLT.
36  *
37  * <p>An alternative approach that requires less (or even none) XSLT work is offered by
38  * the {@link org.apache.cocoon.forms.transformation.FormsTemplateTransformer}.
39  *
40  * <p>The Form whose XML should be produced should reside either
41  * <ol><li> In a request attribute, whose name should be provided to this
42  * generator as a sitemap parameter called "attribute-name".</li>
43  * <li> Or else at its default-location in the flow context-object.</li>
44  * </ol>
45  *
46  * @version $Id: FormsGenerator.java 326838 2005-10-20 06:26:53Z sylvain $
47  */

48 public class FormsGenerator extends AbstractGenerator {
49     
50     protected FormsPipelineConfig config;
51     private static final String JavaDoc FORM_GENERATED_EL = "form-generated";
52
53     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters par)
54             throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
55         super.setup(resolver, objectModel, src, par);
56         
57         this.config = FormsPipelineConfig.createConfig(objectModel, parameters);
58         // enforce a default POST method when using the generator.
59
if (this.config.getFormMethod() == null) {
60             this.config.setFormMethod("POST");
61         }
62     }
63
64     public void recycle() {
65         super.recycle();
66         this.config = null;
67     }
68
69     public void generate() throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
70         contentHandler.startDocument();
71         contentHandler.startPrefixMapping(FormsConstants.INSTANCE_PREFIX, FormsConstants.INSTANCE_NS);
72         Attributes JavaDoc formAtts = this.config.getFormAttributes();
73         
74         contentHandler.startElement(FormsConstants.INSTANCE_NS, FORM_GENERATED_EL, FormsConstants.INSTANCE_PREFIX_COLON +FORM_GENERATED_EL, formAtts);
75         Form form = config.findForm();
76         form.generateSaxFragment(contentHandler, Locale.US);
77         contentHandler.endElement(FormsConstants.INSTANCE_NS, FORM_GENERATED_EL, FormsConstants.INSTANCE_PREFIX_COLON +FORM_GENERATED_EL);
78         
79         contentHandler.endPrefixMapping(FormsConstants.INSTANCE_PREFIX);
80         contentHandler.endDocument();
81     }
82 }
83
Popular Tags