forked from grails-plugins/grails-freemarker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FreemarkerGrailsPlugin.groovy
205 lines (177 loc) · 8.67 KB
/
FreemarkerGrailsPlugin.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* Copyright 2009 Jeff Brown
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import grails.plugin.freemarker.AbstractTagLibAwareConfigurer
import grails.plugin.freemarker.GrailsFreeMarkerViewResolver
import grails.plugin.freemarker.GrailsTemplateLoader
import grails.plugin.freemarker.TagLibAwareConfigurer
import grails.plugin.freemarker.TagLibPostProcessor
import grails.plugin.viewtools.ViewResourceLocator
import org.codehaus.groovy.grails.commons.GrailsClass
import org.codehaus.groovy.grails.commons.TagLibArtefactHandler
import org.codehaus.groovy.grails.web.util.WebUtils
import org.springframework.context.ApplicationContext
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer
import org.codehaus.groovy.grails.web.pages.discovery.DefaultGroovyPageLocator
/**
* @author Jeff Brown
* @author Daniel Henrique Alves Lima
* @author Joshua Burnett
*/
class FreemarkerGrailsPlugin {
def version = "2.0.1"
def grailsVersion = "2.3 > *"
def pluginExcludes = [
"grails-app/views/**/*",
"grails-app/controllers/**/*",
"grails-app/services/grails/plugin/freemarker/test/**/*",
"src/groovy/grails/plugin/freemarker/test/**/*",
"src/docs/**/*",
"grails-app/i18n/*",
'grails-app/taglib/**/test/**/*',
'scripts/**/Eclipse.groovy',
"test-plugins/**/*",
"web-app/**/*"
]
def title = "FreeMarker Views Plugin"
def description = 'The Grails FreeMarker plugin provides support for rendering FreeMarker templates as views.'
def documentation = "https://github.com/grails-plugins/grails-freemarker"
def license = "APACHE"
def developers = [
[name: 'Jeff Brown', email: "[email protected]"],
[name: 'Joshua Burnett', email: '[email protected]'],
[name: 'Daniel Henrique Alves Lima', email: 'xxx']
]
def scm = [ url: "https://github.com/grails-plugins/grails-freemarker" ]
def issueManagement = [ system: "GITHUB", url: "http://github.com/grails-plugins/grails-freemarker" ]
def observe = ["controllers", 'groovyPages']
def loadAfter = ['controllers', 'groovyPages','pluginConfig']
def author = "Jeff Brown"
def authorEmail = "[email protected]"
def doWithSpring = {
def freeconfig = application.mergedConfig.asMap().grails.plugin.freemarker
String ftlSuffix = '.ftl'
freeMarkerViewResourceLocator(grails.plugin.viewtools.ViewResourceLocator) { bean ->
searchBinaryPlugins = false //whether to look in binary plugins, does not work in grails2
//initial searchLocations
searchPaths = freeconfig.viewResourceLocator.searchLocations
//resourceLoaders to use right after searchLocations above are scanned
//searchLoaders = [ref('tenantViewResourceLoader')]
// in dev mode there will be a groovyPageResourceLoader with base dir set to the running project
//if(Environment.isDevelopmentEnvironmentAvailable()) <- better for Grails 3
if(!application.warDeployed){ // <- grails2
grailsViewPaths = ["/grails-app/views"]
webInfPrefix = ""
}
}
freeMarkerGrailsTemplateLoader(GrailsTemplateLoader) { bean ->
bean.autowire = "byName"
viewResourceLocator = ref('freeMarkerViewResourceLocator')
}
def resolveLoaders = {List loaders ->
return loaders.collect{ (it instanceof CharSequence) ? ref(it.toString()) : it }
}
Class configClass = freeconfig.tags.enabled == true ? TagLibAwareConfigurer : FreeMarkerConfigurer
/* factory to return FreeMarkerConfig
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerConfig */
freeMarkerConfigurer(configClass) {
if (freeconfig.templateLoaders) {
// default config has the 'freeMarkerGrailsTemplateLoader'
postTemplateLoaders = resolveLoaders(freeconfig.templateLoaders)
}
freemarkerSettings = application.mergedConfig.grails.plugin.freemarker.configSettings?.toProperties()
}
//the key bean that kicks it all off using Spring/Grails ViewResolver concepts
freeMarkerViewResolver(GrailsFreeMarkerViewResolver) {
//viewLocator = ref("viewLocator")
viewResourceLocator = ref("freeMarkerViewResourceLocator")
freeMarkerConfigurer = ref('freeMarkerConfigurer')
prefix = ''
suffix = ftlSuffix
requireViewSuffix = freeconfig.requireViewSuffix
//hideException = freeconfig.viewResolver.hideException
order = 10
}
if (freeconfig.tags.enabled == true) {
// Now go through tag libraries and configure them in spring too. With AOP proxies and so on
for (taglib in application.tagLibClasses) {
"${taglib.fullName}_fm"(taglib.clazz) { bean ->
bean.autowire = true
bean.lazyInit = true
// Taglib scoping support could be easily added here. Scope could be based on a static field in the taglib class.
//bean.scope = 'request'
}
}
"${TagLibPostProcessor.name}"(TagLibPostProcessor) {
grailsApplication = ref('grailsApplication')
}
}
}
def doWithDynamicMethods = { ctx ->
// for (controller in application.controllerClasses) {
// modRenderMethod(application, controller)
// }
/** Fremarker configuration mods **/
def configLocalizedLookup = application.config.grails.plugin.freemarker.localizedLookup
//turn off the localizedLookup by default
if (!configLocalizedLookup) {
ctx.freeMarkerConfigurer.configuration.localizedLookup = false
}
}
def onChange = { event ->
def freeconfig = application.mergedConfig.asMap(true).grails.plugin.freemarker
// if (application.isControllerClass(event.source) ) {
// modRenderMethod(application, event.source)
// }
if (freeconfig.tags.enabled == true && application.isArtefactOfType(TagLibArtefactHandler.TYPE, event.source)) {
GrailsClass taglibClass = application.addArtefact(TagLibArtefactHandler.TYPE, event.source)
if (taglibClass) {
// replace tag library bean
def beanName = taglibClass.fullName
def beans = beans {
"${beanName}_fm"(taglibClass.clazz) { bean ->
bean.autowire = true
//bean.scope = 'request'
}
"${TagLibPostProcessor.name}"(TagLibPostProcessor) {
grailsApplication = ref('grailsApplication')
}
}
beans.registerBeans(event.ctx)
//event.manager?.getGrailsPlugin('groovyPages')?.doWithDynamicMethods(event.ctx)
def ApplicationContext springContext = application.mainContext
for (configurerBeanName in springContext.getBeanNamesForType(AbstractTagLibAwareConfigurer)) {
def configurer = springContext.getBean(configurerBeanName)
configurer.reconfigure()
}
}
}
}
//
// def modRenderMethod(application, controller) {
// def original = controller.metaClass.getMetaMethod("render", [Map] as Class[])
//
// controller.metaClass.render = {Map args ->
// //if args has a pluginName then set it in a threadlocal so we can get to it from the freemarkerViewResolver
// def request = WebUtils.retrieveGrailsWebRequest()?.getCurrentRequest()
// if (args.plugin && request) {
// request.setAttribute(GrailsTemplateLoader.PLUGIN_NAME_ATTRIBUTE,args.plugin)
// }
// if (args.loaderAttribute && request) {
// request.setAttribute("loaderAttribute",args.loaderAttribute)
// }
// original.invoke(delegate, [args] as Object[])
// }
// }
}