1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.renderkit.html; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.io.OutputStream; |
23 | |
import java.io.Writer; |
24 | |
import java.util.Map; |
25 | |
import java.util.concurrent.ConcurrentHashMap; |
26 | |
|
27 | |
import javax.faces.context.ResponseStream; |
28 | |
import javax.faces.context.ResponseWriter; |
29 | |
import javax.faces.render.RenderKit; |
30 | |
import javax.faces.render.Renderer; |
31 | |
import javax.faces.render.ResponseStateManager; |
32 | |
|
33 | |
import org.apache.commons.logging.Log; |
34 | |
import org.apache.commons.logging.LogFactory; |
35 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderKit; |
36 | |
import org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils; |
37 | |
import org.apache.myfaces.shared_impl.renderkit.html.HtmlResponseWriterImpl; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
@JSFRenderKit(renderKitId = "HTML_BASIC") |
45 | |
public class HtmlRenderKitImpl |
46 | |
extends RenderKit |
47 | |
{ |
48 | 0 | private static final Log log = LogFactory.getLog(HtmlRenderKitImpl.class); |
49 | |
|
50 | |
|
51 | |
|
52 | |
private Map<String, Map<String, Renderer>> _renderers; |
53 | |
private ResponseStateManager _responseStateManager; |
54 | |
|
55 | |
|
56 | |
|
57 | |
public HtmlRenderKitImpl() |
58 | 0 | { |
59 | 0 | _renderers = new ConcurrentHashMap<String, Map<String, Renderer>>(64, 0.75f, 1); |
60 | 0 | _responseStateManager = new HtmlResponseStateManager(); |
61 | 0 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
public Renderer getRenderer(String componentFamily, String rendererType) |
66 | |
{ |
67 | 0 | if(componentFamily == null) |
68 | |
{ |
69 | 0 | throw new NullPointerException("component family must not be null."); |
70 | |
} |
71 | 0 | if(rendererType == null) |
72 | |
{ |
73 | 0 | throw new NullPointerException("renderer type must not be null."); |
74 | |
} |
75 | 0 | Map <String,Renderer> familyRendererMap = _renderers.get(componentFamily); |
76 | 0 | Renderer renderer = null; |
77 | 0 | if (familyRendererMap != null) |
78 | |
{ |
79 | 0 | renderer = familyRendererMap.get(rendererType); |
80 | |
} |
81 | 0 | if (renderer == null) |
82 | |
{ |
83 | 0 | log.warn("Unsupported component-family/renderer-type: " + componentFamily + "/" + rendererType); |
84 | |
} |
85 | 0 | return renderer; |
86 | |
} |
87 | |
|
88 | |
public void addRenderer(String componentFamily, String rendererType, Renderer renderer) |
89 | |
{ |
90 | 0 | if(componentFamily == null) |
91 | |
{ |
92 | 0 | log.error("addRenderer: componentFamily = null is not allowed"); |
93 | 0 | throw new NullPointerException("component family must not be null."); |
94 | |
} |
95 | 0 | if(rendererType == null) |
96 | |
{ |
97 | 0 | log.error("addRenderer: rendererType = null is not allowed"); |
98 | 0 | throw new NullPointerException("renderer type must not be null."); |
99 | |
} |
100 | 0 | if(renderer == null) |
101 | |
{ |
102 | 0 | log.error("addRenderer: renderer = null is not allowed"); |
103 | 0 | throw new NullPointerException("renderer must not be null."); |
104 | |
} |
105 | |
|
106 | 0 | _put(componentFamily, rendererType, renderer); |
107 | |
|
108 | 0 | if (log.isTraceEnabled()) |
109 | 0 | log.trace("add Renderer family = " + componentFamily + |
110 | |
" rendererType = " + rendererType + |
111 | |
" renderer class = " + renderer.getClass().getName()); |
112 | 0 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
synchronized private void _put(String componentFamily, String rendererType, Renderer renderer) |
122 | |
{ |
123 | 0 | Map <String,Renderer> familyRendererMap = _renderers.get(componentFamily); |
124 | 0 | if (familyRendererMap == null) |
125 | |
{ |
126 | 0 | familyRendererMap = new ConcurrentHashMap<String, Renderer>(8, 0.75f, 1); |
127 | 0 | _renderers.put(componentFamily, familyRendererMap); |
128 | |
} |
129 | |
else |
130 | |
{ |
131 | 0 | if (familyRendererMap.get(rendererType) != null) { |
132 | |
|
133 | |
|
134 | |
|
135 | 0 | log.debug("Overwriting renderer with family = " + componentFamily + |
136 | |
" rendererType = " + rendererType + |
137 | |
" renderer class = " + renderer.getClass().getName()); |
138 | |
} |
139 | |
} |
140 | 0 | familyRendererMap.put(rendererType, renderer); |
141 | 0 | } |
142 | |
|
143 | |
public ResponseStateManager getResponseStateManager() |
144 | |
{ |
145 | 0 | return _responseStateManager; |
146 | |
} |
147 | |
|
148 | |
public ResponseWriter createResponseWriter(Writer writer, |
149 | |
String contentTypeListString, |
150 | |
String characterEncoding) |
151 | |
{ |
152 | 0 | String selectedContentType = HtmlRendererUtils.selectContentType(contentTypeListString); |
153 | |
|
154 | 0 | if(characterEncoding==null) |
155 | |
{ |
156 | 0 | characterEncoding = HtmlRendererUtils.DEFAULT_CHAR_ENCODING; |
157 | |
} |
158 | |
|
159 | 0 | return new HtmlResponseWriterImpl(writer, selectedContentType, characterEncoding); |
160 | |
} |
161 | |
|
162 | |
public ResponseStream createResponseStream(OutputStream outputStream) |
163 | |
{ |
164 | 0 | return new MyFacesResponseStream(outputStream); |
165 | |
} |
166 | |
|
167 | |
private static class MyFacesResponseStream extends ResponseStream { |
168 | |
private OutputStream output; |
169 | |
|
170 | 0 | public MyFacesResponseStream(OutputStream output) { |
171 | 0 | this.output = output; |
172 | 0 | } |
173 | |
|
174 | |
public void write(int b) throws IOException |
175 | |
{ |
176 | 0 | output.write(b); |
177 | 0 | } |
178 | |
|
179 | |
public void write(byte b[]) throws IOException |
180 | |
{ |
181 | 0 | output.write(b); |
182 | 0 | } |
183 | |
|
184 | |
public void write(byte b[], int off, int len) throws IOException |
185 | |
{ |
186 | 0 | output.write(b, off, len); |
187 | 0 | } |
188 | |
|
189 | |
public void flush() throws IOException |
190 | |
{ |
191 | 0 | output.flush(); |
192 | 0 | } |
193 | |
|
194 | |
public void close() throws IOException |
195 | |
{ |
196 | 0 | output.close(); |
197 | 0 | } |
198 | |
} |
199 | |
} |