KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2005 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.avalon.framework.service.ServiceException;
24 import org.apache.avalon.framework.service.ServiceManager;
25 import org.apache.cocoon.ProcessingException;
26 import org.apache.cocoon.components.flow.ContinuationsManager;
27 import org.apache.cocoon.components.flow.InvalidContinuationException;
28 import org.apache.cocoon.components.flow.WebContinuation;
29 import org.apache.cocoon.environment.ObjectModelHelper;
30 import org.apache.cocoon.environment.Request;
31 import org.apache.cocoon.environment.SourceResolver;
32 import org.apache.cocoon.forms.FormsConstants;
33 import org.apache.cocoon.forms.datatype.SelectionList;
34 import org.apache.cocoon.forms.formmodel.Field;
35 import org.apache.cocoon.forms.formmodel.Form;
36 import org.apache.cocoon.generation.ServiceableGenerator;
37 import org.apache.cocoon.sitemap.SitemapParameters;
38 import org.xml.sax.ContentHandler JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41 /**
42  * A generator for suggestion lists.
43  *
44  * @since 2.1.8
45  * @version $Id: SuggestionListGenerator.java 326838 2005-10-20 06:26:53Z sylvain $
46  */

47 public class SuggestionListGenerator extends ServiceableGenerator {
48
49     private ContinuationsManager contManager;
50     private WebContinuation wk;
51     private SelectionList list;
52     private String JavaDoc filter;
53     private Locale JavaDoc locale;
54
55     public void service(ServiceManager manager) throws ServiceException {
56         super.service(manager);
57         this.contManager = (ContinuationsManager)manager.lookup(ContinuationsManager.ROLE);
58     }
59
60     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters par) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
61         super.setup(resolver, objectModel, src, par);
62         
63         Request req = ObjectModelHelper.getRequest(objectModel);
64         
65         String JavaDoc continuationId = par.getParameter("continuation-id", req.getParameter("continuation-id"));
66         String JavaDoc widgetPath = par.getParameter("widget-id", req.getParameter("widget-id")).replace('.', '/');
67         String JavaDoc widgetId = widgetPath.replace('/', '.');
68         this.filter = par.getParameter("filter", req.getParameter(widgetId));
69         
70         // The interpreter id is the sitemap's URI
71
String JavaDoc interpreterId = SitemapParameters.getLocation(parameters).getURI();
72         wk = this.contManager.lookupWebContinuation(continuationId, interpreterId);
73         if (wk == null || wk.disposed()) {
74             throw new InvalidContinuationException("Cannot get continuation for suggestion list");
75         }
76         
77         Form form = (Form)wk.getAttribute("form");
78         if (form == null) {
79             throw new ProcessingException("No form is attached to the continuation");
80         }
81         
82         this.locale = form.getLocale();
83         
84         Field field = (Field)form.lookupWidget(widgetPath);
85         list = field.getSuggestionList();
86         if (list == null) {
87             throw new ProcessingException(field + " has no suggestion list");
88         }
89     }
90     
91     public void generate() throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
92         super.contentHandler.startDocument();
93         super.contentHandler.startPrefixMapping(FormsConstants.INSTANCE_PREFIX, FormsConstants.INSTANCE_NS);
94         ContentHandler JavaDoc handler;
95         if (filter == null || filter.length() == 0) {
96             handler = super.contentHandler;
97         } else {
98             handler = new SelectionListFilter(filter, super.contentHandler);
99         }
100         list.generateSaxFragment(handler, this.locale);
101         
102         super.contentHandler.endPrefixMapping(FormsConstants.INSTANCE_PREFIX);
103         super.contentHandler.endDocument();
104     }
105
106 }
107
Popular Tags