1 16 package org.apache.cocoon.forms.generation; 17 18 import java.io.IOException ; 19 import java.util.Locale ; 20 import java.util.Map ; 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 ; 39 import org.xml.sax.SAXException ; 40 41 47 public class SuggestionListGenerator extends ServiceableGenerator { 48 49 private ContinuationsManager contManager; 50 private WebContinuation wk; 51 private SelectionList list; 52 private String filter; 53 private Locale 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 objectModel, String src, Parameters par) throws ProcessingException, SAXException , IOException { 61 super.setup(resolver, objectModel, src, par); 62 63 Request req = ObjectModelHelper.getRequest(objectModel); 64 65 String continuationId = par.getParameter("continuation-id", req.getParameter("continuation-id")); 66 String widgetPath = par.getParameter("widget-id", req.getParameter("widget-id")).replace('.', '/'); 67 String widgetId = widgetPath.replace('/', '.'); 68 this.filter = par.getParameter("filter", req.getParameter(widgetId)); 69 70 String 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 , SAXException , ProcessingException { 92 super.contentHandler.startDocument(); 93 super.contentHandler.startPrefixMapping(FormsConstants.INSTANCE_PREFIX, FormsConstants.INSTANCE_NS); 94 ContentHandler 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 |