1 24 package org.riotfamily.forms.controller; 25 26 import java.util.Locale ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 import org.riotfamily.common.i18n.AdvancedMessageCodesResolver; 32 import org.riotfamily.common.i18n.MessageResolver; 33 import org.riotfamily.common.i18n.RiotMessageCodesResolver; 34 import org.riotfamily.common.web.util.ServletUtils; 35 import org.riotfamily.common.web.view.freemarker.ResourceTemplateLoader; 36 import org.riotfamily.forms.FormContext; 37 import org.riotfamily.forms.TemplateRenderer; 38 import org.springframework.beans.factory.InitializingBean; 39 import org.springframework.context.MessageSource; 40 import org.springframework.context.MessageSourceAware; 41 import org.springframework.context.ResourceLoaderAware; 42 import org.springframework.core.io.ResourceLoader; 43 import org.springframework.web.servlet.support.RequestContextUtils; 44 45 import freemarker.template.Configuration; 46 47 public final class FormContextFactory implements MessageSourceAware, 48 ResourceLoaderAware, InitializingBean { 49 50 private MessageSource messageSource; 51 52 private ResourceLoader resourceLoader; 53 54 private AdvancedMessageCodesResolver messageCodesResolver; 55 56 private TemplateRenderer templateRenderer; 57 58 private String resourcePath = "/riot/resources"; 59 60 64 public void setMessageCodesResolver(AdvancedMessageCodesResolver resolver) { 65 this.messageCodesResolver = resolver; 66 } 67 68 74 public void setMessageSource(MessageSource messageSource) { 75 this.messageSource = messageSource; 76 } 77 78 84 public void setResourceLoader(ResourceLoader resourceLoader) { 85 this.resourceLoader = resourceLoader; 86 } 87 88 public void setResourcePath(String resourcePath) { 89 this.resourcePath = resourcePath; 90 } 91 92 public void setTemplateRenderer(TemplateRenderer templateRenderer) { 93 this.templateRenderer = templateRenderer; 94 } 95 96 public void afterPropertiesSet() { 97 if (templateRenderer == null) { 98 Configuration configuration = new Configuration(); 99 ResourceTemplateLoader loader = new ResourceTemplateLoader(); 100 loader.setResourceLoader(resourceLoader); 101 configuration.setTemplateLoader(loader); 102 templateRenderer = new TemplateRenderer(configuration); 103 } 104 if (messageCodesResolver == null) { 105 messageCodesResolver = new RiotMessageCodesResolver(); 106 } 107 } 108 109 public FormContext createFormContext(HttpServletRequest request, 110 HttpServletResponse response) { 111 112 Locale locale = RequestContextUtils.getLocale(request); 113 MessageResolver messageResolver = new MessageResolver(messageSource, 114 messageCodesResolver, locale); 115 116 String contextPath = request.getContextPath(); 117 String formUrl = response.encodeURL(ServletUtils.getIncludeUri(request)); 118 return createFormContext(messageResolver, contextPath, formUrl); 119 } 120 121 public FormContext createFormContext(MessageResolver messageResolver, 122 String contextPath, String formUrl) { 123 124 return new DefaultFormContext(messageResolver, templateRenderer, 125 contextPath, resourcePath, formUrl); 126 } 127 128 } 129 | Popular Tags |