1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package javax.faces.internal; |
17 | |
|
18 | |
import java.util.List; |
19 | |
|
20 | |
import javax.faces.FacesException; |
21 | |
import javax.faces.FactoryFinder; |
22 | |
import javax.faces.application.Application; |
23 | |
import javax.faces.component.UIComponent; |
24 | |
import javax.faces.context.FacesContext; |
25 | |
import javax.faces.context.ResponseWriter; |
26 | |
import javax.faces.convert.Converter; |
27 | |
import javax.faces.el.ValueBinding; |
28 | |
import javax.faces.lifecycle.LifecycleFactory; |
29 | |
import javax.faces.render.RenderKit; |
30 | |
import javax.faces.render.RenderKitFactory; |
31 | |
import javax.faces.validator.Validator; |
32 | |
import javax.faces.webapp.FacesServlet; |
33 | |
import javax.servlet.ServletConfig; |
34 | |
import javax.servlet.ServletException; |
35 | |
import javax.servlet.jsp.PageContext; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class WebAppUtil { |
44 | |
|
45 | 0 | private WebAppUtil() { |
46 | 0 | } |
47 | |
|
48 | |
public static UIComponent createComponent(FacesContext context, |
49 | |
String binding, String componentType) { |
50 | 0 | UIComponent component = null; |
51 | 0 | Application application = context.getApplication(); |
52 | 0 | if (binding != null) { |
53 | 0 | ValueBinding vb = application.createValueBinding(binding); |
54 | 0 | component = application.createComponent(vb, context, componentType); |
55 | 0 | component.setValueBinding("binding", vb); |
56 | |
} else { |
57 | 0 | component = application.createComponent(componentType); |
58 | |
} |
59 | 0 | return component; |
60 | |
} |
61 | |
|
62 | |
public static List getCreatedComponentIds(UIComponent component) { |
63 | 3 | return (List) component.getAttributes().get( |
64 | |
InternalConstants.JSP_CREATED_COMPONENT_IDS); |
65 | |
} |
66 | |
|
67 | |
public static void setCreatedComponentIds(UIComponent component, |
68 | |
List createdComponents) { |
69 | 0 | component.getAttributes().put( |
70 | |
InternalConstants.JSP_CREATED_COMPONENT_IDS, createdComponents); |
71 | 0 | } |
72 | |
|
73 | |
public static void removeCreatedComponentIds(UIComponent component) { |
74 | 3 | component.getAttributes().remove( |
75 | |
InternalConstants.JSP_CREATED_COMPONENT_IDS); |
76 | 3 | } |
77 | |
|
78 | |
public static List getCreatedFacetNames(UIComponent component) { |
79 | 3 | return (List) component.getAttributes().get( |
80 | |
InternalConstants.JSP_CREATED_FACET_NAMES); |
81 | |
} |
82 | |
|
83 | |
public static void setCreatedFacetNames(UIComponent component, |
84 | |
List createdComponents) { |
85 | 0 | component.getAttributes().put( |
86 | |
InternalConstants.JSP_CREATED_FACET_NAMES, createdComponents); |
87 | 0 | } |
88 | |
|
89 | |
public static void removeCreatedFacetNames(UIComponent component) { |
90 | 3 | component.getAttributes().remove( |
91 | |
InternalConstants.JSP_CREATED_FACET_NAMES); |
92 | 3 | } |
93 | |
|
94 | |
public static ResponseWriter buildResponseWriter(FacesContext facesContext, |
95 | |
PageContext pageContext) { |
96 | 0 | RenderKitFactory factory = (RenderKitFactory) FactoryFinder |
97 | |
.getFactory(FactoryFinder.RENDER_KIT_FACTORY); |
98 | 0 | RenderKit renderKit = factory.getRenderKit(facesContext, facesContext |
99 | |
.getViewRoot().getRenderKitId()); |
100 | |
|
101 | 0 | PageContextOutWriter writer = new PageContextOutWriter(pageContext); |
102 | 0 | String encoding = pageContext.getRequest().getCharacterEncoding(); |
103 | 0 | FacesContext context = FacesContext.getCurrentInstance(); |
104 | 0 | String acceptContentTypes = getAcceptHeader(context); |
105 | 0 | return renderKit.createResponseWriter(writer, acceptContentTypes, |
106 | |
encoding); |
107 | |
} |
108 | |
|
109 | |
public static String getAcceptHeader(FacesContext context) { |
110 | 7 | return (String) context.getExternalContext().getRequestHeaderMap().get( |
111 | |
"accept"); |
112 | |
} |
113 | |
|
114 | |
public static Object getValueFromCreatedValueBinding(String buindName) { |
115 | 0 | FacesContext context = getFacesContext(); |
116 | 0 | ValueBinding vb = context.getApplication() |
117 | |
.createValueBinding(buindName); |
118 | 0 | return vb.getValue(context); |
119 | |
} |
120 | |
|
121 | |
public static Converter createConverter(String converterId) { |
122 | 4 | FacesContext context = getFacesContext(); |
123 | 4 | return context.getApplication().createConverter(converterId); |
124 | |
} |
125 | |
|
126 | |
public static Converter createConverter(Class clazz) { |
127 | 0 | FacesContext context = getFacesContext(); |
128 | 0 | return context.getApplication().createConverter(clazz); |
129 | |
} |
130 | |
|
131 | |
public static Validator createValidator(String validatorId) { |
132 | 21 | FacesContext context = getFacesContext(); |
133 | 21 | return context.getApplication().createValidator(validatorId); |
134 | |
} |
135 | |
|
136 | |
public static FacesContext getFacesContext() { |
137 | 25 | return FacesContext.getCurrentInstance(); |
138 | |
} |
139 | |
|
140 | |
public static Object getFactory(String factoryName) throws ServletException { |
141 | |
try { |
142 | 0 | return FactoryFinder.getFactory(factoryName); |
143 | 0 | } catch (FacesException e) { |
144 | 0 | Throwable rootCause = e.getCause(); |
145 | 0 | if (rootCause == null) { |
146 | 0 | throw e; |
147 | |
} else { |
148 | 0 | throw new ServletException(e.getMessage(), rootCause); |
149 | |
} |
150 | |
} |
151 | |
} |
152 | |
|
153 | |
public static String getLifecycleId(ServletConfig config) { |
154 | 0 | String lifecycleId = config.getServletContext().getInitParameter( |
155 | |
FacesServlet.LIFECYCLE_ID_ATTR); |
156 | 0 | if (lifecycleId == null) { |
157 | 0 | lifecycleId = LifecycleFactory.DEFAULT_LIFECYCLE; |
158 | |
} |
159 | 0 | return lifecycleId; |
160 | |
} |
161 | |
} |