KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > controller > FormContextFactory


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.controller;
25
26 import java.util.Locale JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
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 JavaDoc resourcePath = "/riot/resources";
59     
60     /**
61      * Sets the {@link AdvancedMessageCodesResolver} to be used. If not
62      * set, a {@link RiotMessageCodesResolver} will be used by default.
63      */

64     public void setMessageCodesResolver(AdvancedMessageCodesResolver resolver) {
65         this.messageCodesResolver = resolver;
66     }
67
68     /**
69      * Sets the {@link MessageSource} that is used to look up labels and
70      * error messages.
71      *
72      * @see MessageSourceAware
73      */

74     public void setMessageSource(MessageSource messageSource) {
75         this.messageSource = messageSource;
76     }
77
78     /**
79      * Sets the {@link ResourceLoader} that is used to load the FreeMarker
80      * templates.
81      *
82      * @see ResourceLoaderAware
83      */

84     public void setResourceLoader(ResourceLoader resourceLoader) {
85         this.resourceLoader = resourceLoader;
86     }
87     
88     public void setResourcePath(String JavaDoc 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 JavaDoc request,
110             HttpServletResponse JavaDoc response) {
111
112         Locale JavaDoc locale = RequestContextUtils.getLocale(request);
113         MessageResolver messageResolver = new MessageResolver(messageSource,
114                 messageCodesResolver, locale);
115         
116         String JavaDoc contextPath = request.getContextPath();
117         String JavaDoc formUrl = response.encodeURL(ServletUtils.getIncludeUri(request));
118         return createFormContext(messageResolver, contextPath, formUrl);
119     }
120     
121     public FormContext createFormContext(MessageResolver messageResolver,
122             String JavaDoc contextPath, String JavaDoc formUrl) {
123         
124         return new DefaultFormContext(messageResolver, templateRenderer,
125                 contextPath, resourcePath, formUrl);
126     }
127
128 }
129
Popular Tags