-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFreeMarkerAutoFragmentConfig.java
91 lines (75 loc) · 3.54 KB
/
FreeMarkerAutoFragmentConfig.java
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
package example.freemarker.fragments;
import java.io.IOException;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractUrlBasedView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
import freemarker.template.Template;
@Configuration
public class FreeMarkerAutoFragmentConfig {
private static final String VIEW_FRAGMENT_SEPARATOR = " :: "; // Same as used by Thymeleaf templating
private static final boolean TRANSLATE_MACRO_NAMES_ENABLED = false;
private static final FragmentTemplate FRAGMENT_TEMPLATE_BUILDER = new FragmentTemplate.FullyAutomatic();
private static final String DEFAULT_MACRO = null;
@Bean
FreeMarkerViewResolver freeMarkerViewResolver(FreeMarkerProperties properties) {
FreeMarkerViewResolver resolver = new CustomFreeMarkerViewResolver();
properties.applyToMvcViewResolver(resolver);
return resolver;
}
static class CustomFreeMarkerView extends FreeMarkerView {
String fragmentId;
String fragmentViewName;
@Override
protected Template getTemplate(String name, Locale locale) throws IOException {
Template template = super.getTemplate(name, locale);
String fragment = (fragmentId != null || DEFAULT_MACRO == null) ? fragmentId : DEFAULT_MACRO;
if (fragment != null) {
template = FRAGMENT_TEMPLATE_BUILDER.build(transformMacroName(fragment), fragmentViewName, template);
}
return template;
}
private static String transformMacroName(String fragmentId) {
if (TRANSLATE_MACRO_NAMES_ENABLED) { // e.g. "my-fragment" to "MyFragment"
// Will also capitalize when delimiter not found.
fragmentId = Stream.of(fragmentId.split("[-_]"))
.map(s -> Character.toUpperCase(s.charAt(0)) + s.substring(1))
.collect(Collectors.joining());
}
return fragmentId;
}
}
static class CustomFreeMarkerViewResolver extends FreeMarkerViewResolver {
@Override
protected View loadView(String viewName, Locale locale) throws Exception {
String fragmentId = null;
String originalViewName = viewName;
int index = viewName.indexOf(VIEW_FRAGMENT_SEPARATOR);
if (index != -1) {
fragmentId = viewName.substring(index + VIEW_FRAGMENT_SEPARATOR.length());
viewName = viewName.substring(0, index);
}
View view = super.loadView(viewName, locale);
if (view instanceof CustomFreeMarkerView) {
((CustomFreeMarkerView) view).fragmentId = fragmentId;
((CustomFreeMarkerView) view).fragmentViewName = originalViewName;
}
return view;
}
@Override
protected Class<?> requiredViewClass() {
return CustomFreeMarkerView.class;
}
@Override
protected AbstractUrlBasedView instantiateView() {
return getViewClass() == CustomFreeMarkerView.class
? new CustomFreeMarkerView() : super.instantiateView();
}
}
}