1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.mc.test.core.mock; |
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 | 0 | public class MockMyFacesFaceletViewDeclarationLanguageStrategy implements ViewDeclarationLanguageStrategy |
35 | |
{ |
36 | |
private Pattern _acceptPatterns; |
37 | |
private String _extension; |
38 | |
|
39 | |
private ViewDeclarationLanguage _language; |
40 | |
|
41 | |
public MockMyFacesFaceletViewDeclarationLanguageStrategy() |
42 | 0 | { |
43 | 0 | FacesContext context = FacesContext.getCurrentInstance(); |
44 | 0 | ExternalContext eContext = context.getExternalContext(); |
45 | |
|
46 | 0 | _acceptPatterns = loadAcceptPattern(eContext); |
47 | |
|
48 | 0 | _extension = loadFaceletExtension(eContext); |
49 | |
|
50 | 0 | _language = new MockMyFacesFaceletViewDeclarationLanguage(context, this); |
51 | 0 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public ViewDeclarationLanguage getViewDeclarationLanguage() |
57 | |
{ |
58 | 0 | return _language; |
59 | |
} |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public boolean handles(String viewId) |
65 | |
{ |
66 | 0 | if (viewId == null) |
67 | |
{ |
68 | 0 | return false; |
69 | |
} |
70 | |
|
71 | 0 | if (viewId.endsWith(_extension)) |
72 | |
{ |
73 | |
|
74 | 0 | return true; |
75 | |
} |
76 | |
|
77 | |
|
78 | 0 | return _acceptPatterns != null && _acceptPatterns.matcher(viewId).matches(); |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
private Pattern loadAcceptPattern(ExternalContext context) |
90 | |
{ |
91 | 0 | assert context != null; |
92 | |
|
93 | 0 | String mappings = context.getInitParameter(ViewHandler.FACELETS_VIEW_MAPPINGS_PARAM_NAME); |
94 | 0 | if(mappings == null) |
95 | |
{ |
96 | 0 | mappings = context.getInitParameter("facelets.VIEW_MAPPINGS"); |
97 | |
} |
98 | 0 | if (mappings == null) |
99 | |
{ |
100 | 0 | return null; |
101 | |
} |
102 | |
|
103 | |
|
104 | 0 | mappings = mappings.trim(); |
105 | 0 | if (mappings.length() == 0) |
106 | |
{ |
107 | 0 | return null; |
108 | |
} |
109 | |
|
110 | 0 | return Pattern.compile(toRegex(mappings)); |
111 | |
} |
112 | |
|
113 | |
private String loadFaceletExtension(ExternalContext context) |
114 | |
{ |
115 | 0 | assert context != null; |
116 | |
|
117 | 0 | String suffix = context.getInitParameter(ViewHandler.FACELETS_SUFFIX_PARAM_NAME); |
118 | 0 | if (suffix == null) |
119 | |
{ |
120 | 0 | suffix = ViewHandler.DEFAULT_FACELETS_SUFFIX; |
121 | |
} |
122 | |
else |
123 | |
{ |
124 | 0 | suffix = suffix.trim(); |
125 | 0 | if (suffix.length() == 0) |
126 | |
{ |
127 | 0 | suffix = ViewHandler.DEFAULT_FACELETS_SUFFIX; |
128 | |
} |
129 | |
} |
130 | |
|
131 | 0 | return suffix; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
private String toRegex(String mappings) |
143 | |
{ |
144 | 0 | assert mappings != null; |
145 | |
|
146 | |
|
147 | 0 | mappings = mappings.replaceAll("\\s", ""); |
148 | |
|
149 | |
|
150 | 0 | mappings = mappings.replaceAll("\\.", "\\\\."); |
151 | |
|
152 | |
|
153 | 0 | mappings = mappings.replaceAll("\\*", ".*"); |
154 | |
|
155 | |
|
156 | 0 | mappings = mappings.replaceAll(";", "|"); |
157 | |
|
158 | 0 | return mappings; |
159 | |
} |
160 | |
} |