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