1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets;
20
21 import java.util.regex.Pattern;
22
23 import javax.faces.application.ViewHandler;
24 import javax.faces.context.ExternalContext;
25 import javax.faces.context.FacesContext;
26 import javax.faces.view.ViewDeclarationLanguage;
27
28 import org.apache.myfaces.view.ViewDeclarationLanguageStrategy;
29
30
31
32
33
34
35
36 public class FaceletViewDeclarationLanguageStrategy implements ViewDeclarationLanguageStrategy
37 {
38 private Pattern _acceptPatterns;
39 private String _extension;
40
41 private ViewDeclarationLanguage _language;
42
43 public FaceletViewDeclarationLanguageStrategy()
44 {
45 FacesContext context = FacesContext.getCurrentInstance();
46 ExternalContext eContext = context.getExternalContext();
47
48 _acceptPatterns = loadAcceptPattern(eContext);
49
50 _extension = loadFaceletExtension(eContext);
51
52 _language = new FaceletViewDeclarationLanguage(context, this);
53 }
54
55
56
57
58 public ViewDeclarationLanguage getViewDeclarationLanguage()
59 {
60 return _language;
61 }
62
63
64
65
66 public boolean handles(String viewId)
67 {
68 if (viewId == null)
69 {
70 return false;
71 }
72
73 if (viewId.endsWith(_extension))
74 {
75
76 return true;
77 }
78
79
80 return _acceptPatterns != null && _acceptPatterns.matcher(viewId).matches();
81 }
82
83
84
85
86
87
88
89
90
91 private Pattern loadAcceptPattern(ExternalContext context)
92 {
93 assert context != null;
94
95 String mappings = context.getInitParameter(ViewHandler.FACELETS_VIEW_MAPPINGS_PARAM_NAME);
96 if(mappings == null)
97 {
98 mappings = context.getInitParameter("facelets.VIEW_MAPPINGS");
99 }
100 if (mappings == null)
101 {
102 return null;
103 }
104
105
106 mappings = mappings.trim();
107 if (mappings.length() == 0)
108 {
109 return null;
110 }
111
112 return Pattern.compile(toRegex(mappings));
113 }
114
115 private String loadFaceletExtension(ExternalContext context)
116 {
117 assert context != null;
118
119 String suffix = context.getInitParameter(ViewHandler.FACELETS_SUFFIX_PARAM_NAME);
120 if (suffix == null)
121 {
122 suffix = ViewHandler.DEFAULT_FACELETS_SUFFIX;
123 }
124 else
125 {
126 suffix = suffix.trim();
127 if (suffix.length() == 0)
128 {
129 suffix = ViewHandler.DEFAULT_FACELETS_SUFFIX;
130 }
131 }
132
133 return suffix;
134 }
135
136
137
138
139
140
141
142
143
144 private String toRegex(String mappings)
145 {
146 assert mappings != null;
147
148
149 mappings = mappings.replaceAll("\\s", "");
150
151
152 mappings = mappings.replaceAll("\\.", "\\\\.");
153
154
155 mappings = mappings.replaceAll("\\*", ".*");
156
157
158 mappings = mappings.replaceAll(";", "|");
159
160 return mappings;
161 }
162
163 @Override
164 public String getMinimalImplicitOutcome(String viewId)
165 {
166 if (viewId != null && viewId.endsWith(_extension))
167 {
168 return viewId.substring(0, viewId.length()-_extension.length());
169 }
170 else
171 {
172 return viewId;
173 }
174 }
175 }