1
|
package eu.dnetlib.springutils.stringtemplate;
|
2
|
|
3
|
import java.util.Locale;
|
4
|
|
5
|
import org.antlr.stringtemplate.StringTemplateGroup;
|
6
|
import org.springframework.beans.factory.BeanFactoryUtils;
|
7
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
8
|
import org.springframework.context.ApplicationContextException;
|
9
|
import org.springframework.web.servlet.view.AbstractUrlBasedView;
|
10
|
import org.springframework.web.servlet.view.UrlBasedViewResolver;
|
11
|
|
12
|
/**
|
13
|
* Spring MVC view resolver based on string templates. You can parameterize this resolver with a java package name under
|
14
|
* the templates will be searched.
|
15
|
*
|
16
|
* <p>
|
17
|
* requires a StringTemplateGroup bean
|
18
|
* </p>
|
19
|
*
|
20
|
* <p>
|
21
|
* This code is based to a public domain example taken from <a
|
22
|
* href="http://jira.springframework.org/browse/SPR-3266">here</a>
|
23
|
* </p>
|
24
|
*
|
25
|
* @author marko
|
26
|
* @author Brian Lewis
|
27
|
*
|
28
|
*/
|
29
|
public class StringTemplateViewResolver extends UrlBasedViewResolver {
|
30
|
|
31
|
/**
|
32
|
* template group.
|
33
|
*/
|
34
|
private StringTemplateGroup templateGroup;
|
35
|
|
36
|
public void setTemplateGroup(final StringTemplateGroup value) {
|
37
|
templateGroup = value;
|
38
|
}
|
39
|
|
40
|
public StringTemplateGroup getTemplateGroup() {
|
41
|
return templateGroup;
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* Sets default viewClass to <code>requiredViewClass</code>.
|
46
|
*
|
47
|
* @see #setViewClass
|
48
|
* @see #requiredViewClass
|
49
|
*/
|
50
|
public StringTemplateViewResolver() {
|
51
|
setViewClass(requiredViewClass()); // NOPMD
|
52
|
// XXX bad but we have to make assumptions later
|
53
|
setRequestContextAttribute("rc");
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Requires StringTemplateView.
|
58
|
*
|
59
|
* @see StringTemplateView
|
60
|
* @return view class
|
61
|
*/
|
62
|
@Override
|
63
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
64
|
protected Class requiredViewClass() {
|
65
|
return StringTemplateView.class;
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Invoked on startup. Looks for a single StringTemplateGroup bean to find the relevant StringTemplate for this
|
70
|
* factory.
|
71
|
*/
|
72
|
@Override
|
73
|
protected void initApplicationContext() {
|
74
|
super.initApplicationContext();
|
75
|
|
76
|
if (getTemplateGroup() == null) {
|
77
|
// No explicit StringTemplateGroup: try to autodetect one.
|
78
|
setTemplateGroup(autodetectMyStringTemplateGroup());
|
79
|
}
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Autodetect a StringTemplateGroup via the ApplicationContext. Called if no explicit StringTemplateGroup has been
|
84
|
* specified.
|
85
|
*
|
86
|
* @return the StringTemplateGroup to use for StringTemplateViews
|
87
|
* @see #getApplicationContext
|
88
|
* @see #setMyStringTemplateGroup
|
89
|
*/
|
90
|
protected StringTemplateGroup autodetectMyStringTemplateGroup() {
|
91
|
try {
|
92
|
final StringTemplateGroup group = (StringTemplateGroup) BeanFactoryUtils.beanOfTypeIncludingAncestors(getApplicationContext(),
|
93
|
StringTemplateGroup.class, true, false);
|
94
|
return group;
|
95
|
} catch (NoSuchBeanDefinitionException ex) {
|
96
|
throw new ApplicationContextException("Must define a single StringTemplateGroup bean in this web application context"
|
97
|
+ " (may be inherited). StringTemplateGroup is the usual implementation." + " This bean may be given any name.", ex);
|
98
|
}
|
99
|
}
|
100
|
|
101
|
/**
|
102
|
* {@inheritDoc}
|
103
|
*
|
104
|
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#buildView(java.lang.String)
|
105
|
*/
|
106
|
@Override
|
107
|
protected AbstractUrlBasedView buildView(final String viewName) throws Exception { // NOPMD
|
108
|
if (logger.isDebugEnabled())
|
109
|
logger.debug("looking up by view name: '" + viewName + "' based in '" + getPrefix() + "'");
|
110
|
|
111
|
final StringTemplateView view = (StringTemplateView) super.buildView(getPrefix() + viewName);
|
112
|
|
113
|
view.setStringTemplate(templateGroup.getInstanceOf(getPrefix() + viewName));
|
114
|
|
115
|
return view;
|
116
|
}
|
117
|
|
118
|
@Override
|
119
|
protected boolean canHandle(String viewName, Locale locale) {
|
120
|
if(viewName.startsWith("redirect:"))
|
121
|
return true;
|
122
|
|
123
|
try {
|
124
|
templateGroup.getInstanceOf(getPrefix() + viewName);
|
125
|
} catch (IllegalArgumentException e) {
|
126
|
return false;
|
127
|
}
|
128
|
|
129
|
if (super.canHandle(viewName, locale))
|
130
|
return true;
|
131
|
|
132
|
return true;
|
133
|
}
|
134
|
|
135
|
}
|