01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.forms.generation;
18:
19: import java.io.IOException;
20: import java.util.Locale;
21: import java.util.Map;
22:
23: import org.apache.avalon.framework.parameters.Parameters;
24: import org.apache.cocoon.ProcessingException;
25: import org.apache.cocoon.environment.SourceResolver;
26: import org.apache.cocoon.forms.FormsConstants;
27: import org.apache.cocoon.forms.formmodel.Form;
28: import org.apache.cocoon.forms.transformation.FormsPipelineConfig;
29: import org.apache.cocoon.generation.AbstractGenerator;
30: import org.xml.sax.Attributes;
31: import org.xml.sax.SAXException;
32:
33: /**
34: * A generator that streams an XML representation of a {@link Form}. This will
35: * recursively contain the XML for all widgets on the form. This can then be styled
36: * using an XSLT.
37: *
38: * <p>An alternative approach that requires less (or even none) XSLT work is offered by
39: * the {@link org.apache.cocoon.forms.transformation.FormsTemplateTransformer}.
40: *
41: * <p>The Form whose XML should be produced should reside either
42: * <ol><li> In a request attribute, whose name should be provided to this
43: * generator as a sitemap parameter called "attribute-name".</li>
44: * <li> Or else at its default-location in the flow context-object.</li>
45: * </ol>
46: *
47: * @version $Id: FormsGenerator.java 449149 2006-09-23 03:58:05Z crossley $
48: */
49: public class FormsGenerator extends AbstractGenerator {
50:
51: protected FormsPipelineConfig config;
52: private static final String FORM_GENERATED_EL = "form-generated";
53:
54: public void setup(SourceResolver resolver, Map objectModel,
55: String src, Parameters par) throws ProcessingException,
56: SAXException, IOException {
57: super .setup(resolver, objectModel, src, par);
58:
59: this .config = FormsPipelineConfig.createConfig(objectModel,
60: parameters);
61: // enforce a default POST method when using the generator.
62: if (this .config.getFormMethod() == null) {
63: this .config.setFormMethod("POST");
64: }
65: }
66:
67: public void recycle() {
68: super .recycle();
69: this .config = null;
70: }
71:
72: public void generate() throws IOException, SAXException,
73: ProcessingException {
74: contentHandler.startDocument();
75: contentHandler.startPrefixMapping(
76: FormsConstants.INSTANCE_PREFIX,
77: FormsConstants.INSTANCE_NS);
78: Attributes formAtts = this .config.getFormAttributes();
79:
80: contentHandler.startElement(FormsConstants.INSTANCE_NS,
81: FORM_GENERATED_EL, FormsConstants.INSTANCE_PREFIX_COLON
82: + FORM_GENERATED_EL, formAtts);
83: Form form = config.findForm();
84: form.generateSaxFragment(contentHandler, Locale.US);
85: contentHandler.endElement(FormsConstants.INSTANCE_NS,
86: FORM_GENERATED_EL, FormsConstants.INSTANCE_PREFIX_COLON
87: + FORM_GENERATED_EL);
88:
89: contentHandler.endPrefixMapping(FormsConstants.INSTANCE_PREFIX);
90: contentHandler.endDocument();
91: }
92: }
|