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.woody.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.generation.AbstractGenerator;
27: import org.apache.cocoon.woody.Constants;
28: import org.apache.cocoon.woody.formmodel.Form;
29: import org.apache.cocoon.woody.transformation.WoodyPipelineConfig;
30: import org.xml.sax.Attributes;
31: import org.xml.sax.SAXException;
32:
33: /**
34: * A generator that streams an XML representation of a Woody {@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.woody.transformation.WoodyTemplateTransformer WoodyTemplateTransformer}.
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: WoodyGenerator.java 433543 2006-08-22 06:22:54Z crossley $
48: */
49: public class WoodyGenerator extends AbstractGenerator {
50:
51: protected WoodyPipelineConfig 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 = WoodyPipelineConfig.createConfig(objectModel,
60: parameters);
61: }
62:
63: public void recycle() {
64: super .recycle();
65: this .config = null;
66: }
67:
68: public void generate() throws IOException, SAXException,
69: ProcessingException {
70: contentHandler.startDocument();
71: contentHandler.startPrefixMapping(Constants.WI_PREFIX,
72: Constants.WI_NS);
73: Attributes formAtts = this .config.getFormAttributes();
74:
75: contentHandler
76: .startElement(Constants.WI_NS, FORM_GENERATED_EL,
77: Constants.WI_PREFIX_COLON + FORM_GENERATED_EL,
78: formAtts);
79: Form form = config.findForm();
80: form.generateSaxFragment(contentHandler, Locale.US);
81: contentHandler.endElement(Constants.WI_NS, FORM_GENERATED_EL,
82: Constants.WI_PREFIX_COLON + FORM_GENERATED_EL);
83:
84: contentHandler.endPrefixMapping(Constants.WI_PREFIX);
85: contentHandler.endDocument();
86: }
87: }
|