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 | 0 | public class FaceletViewDeclarationLanguageStrategy implements ViewDeclarationLanguageStrategy |
37 | |
{ |
38 | |
private Pattern _acceptPatterns; |
39 | |
private String _extension; |
40 | |
|
41 | |
private ViewDeclarationLanguage _language; |
42 | |
|
43 | |
public FaceletViewDeclarationLanguageStrategy() |
44 | 0 | { |
45 | 0 | FacesContext context = FacesContext.getCurrentInstance(); |
46 | 0 | ExternalContext eContext = context.getExternalContext(); |
47 | |
|
48 | 0 | _acceptPatterns = loadAcceptPattern(eContext); |
49 | |
|
50 | 0 | _extension = loadFaceletExtension(eContext); |
51 | |
|
52 | 0 | _language = new FaceletViewDeclarationLanguage(context, this); |
53 | 0 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public ViewDeclarationLanguage getViewDeclarationLanguage() |
59 | |
{ |
60 | 0 | return _language; |
61 | |
} |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public boolean handles(String viewId) |
67 | |
{ |
68 | 0 | if (viewId == null) |
69 | |
{ |
70 | 0 | return false; |
71 | |
} |
72 | |
|
73 | 0 | if (viewId.endsWith(_extension)) |
74 | |
{ |
75 | |
|
76 | 0 | return true; |
77 | |
} |
78 | |
|
79 | |
|
80 | 0 | 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 | 0 | assert context != null; |
94 | |
|
95 | 0 | String mappings = context.getInitParameter(ViewHandler.FACELETS_VIEW_MAPPINGS_PARAM_NAME); |
96 | 0 | if(mappings == null) |
97 | |
{ |
98 | 0 | mappings = context.getInitParameter("facelets.VIEW_MAPPINGS"); |
99 | |
} |
100 | 0 | if (mappings == null) |
101 | |
{ |
102 | 0 | return null; |
103 | |
} |
104 | |
|
105 | |
|
106 | 0 | mappings = mappings.trim(); |
107 | 0 | if (mappings.length() == 0) |
108 | |
{ |
109 | 0 | return null; |
110 | |
} |
111 | |
|
112 | 0 | return Pattern.compile(toRegex(mappings)); |
113 | |
} |
114 | |
|
115 | |
private String loadFaceletExtension(ExternalContext context) |
116 | |
{ |
117 | 0 | assert context != null; |
118 | |
|
119 | 0 | String suffix = context.getInitParameter(ViewHandler.FACELETS_SUFFIX_PARAM_NAME); |
120 | 0 | if (suffix == null) |
121 | |
{ |
122 | 0 | suffix = ViewHandler.DEFAULT_FACELETS_SUFFIX; |
123 | |
} |
124 | |
else |
125 | |
{ |
126 | 0 | suffix = suffix.trim(); |
127 | 0 | if (suffix.length() == 0) |
128 | |
{ |
129 | 0 | suffix = ViewHandler.DEFAULT_FACELETS_SUFFIX; |
130 | |
} |
131 | |
} |
132 | |
|
133 | 0 | return suffix; |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
private String toRegex(String mappings) |
145 | |
{ |
146 | 0 | assert mappings != null; |
147 | |
|
148 | |
|
149 | 0 | mappings = mappings.replaceAll("\\s", ""); |
150 | |
|
151 | |
|
152 | 0 | mappings = mappings.replaceAll("\\.", "\\\\."); |
153 | |
|
154 | |
|
155 | 0 | mappings = mappings.replaceAll("\\*", ".*"); |
156 | |
|
157 | |
|
158 | 0 | mappings = mappings.replaceAll(";", "|"); |
159 | |
|
160 | 0 | return mappings; |
161 | |
} |
162 | |
} |