001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.renderkit.html;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils;
021: import org.apache.myfaces.shared_impl.renderkit.html.HtmlResponseWriterImpl;
022:
023: import javax.faces.context.ResponseStream;
024: import javax.faces.context.ResponseWriter;
025: import javax.faces.render.RenderKit;
026: import javax.faces.render.Renderer;
027: import javax.faces.render.ResponseStateManager;
028: import java.io.IOException;
029: import java.io.OutputStream;
030: import java.io.Writer;
031: import java.util.HashMap;
032: import java.util.Map;
033:
034: /**
035: * @author Manfred Geiler (latest modification by $Author: baranda $)
036: * @version $Revision: 542009 $ $Date: 2007-05-27 19:46:22 +0200 (So, 27 Mai 2007) $
037: */
038: public class HtmlRenderKitImpl extends RenderKit {
039: private static final Log log = LogFactory
040: .getLog(HtmlRenderKitImpl.class);
041:
042: //~ Instance fields ----------------------------------------------------------------------------
043:
044: private Map<String, Renderer> _renderers;
045: private ResponseStateManager _responseStateManager;
046:
047: //~ Constructors -------------------------------------------------------------------------------
048:
049: public HtmlRenderKitImpl() {
050: _renderers = new HashMap<String, Renderer>();
051: _responseStateManager = new HtmlResponseStateManager();
052: }
053:
054: //~ Methods ------------------------------------------------------------------------------------
055:
056: private String key(String componentFamily, String rendererType) {
057: return componentFamily + "." + rendererType;
058: }
059:
060: public Renderer getRenderer(String componentFamily,
061: String rendererType) {
062: if (componentFamily == null) {
063: throw new NullPointerException(
064: "component family must not be null.");
065: }
066: if (rendererType == null) {
067: throw new NullPointerException(
068: "renderer type must not be null.");
069: }
070: Renderer renderer = _renderers.get(key(componentFamily,
071: rendererType));
072: if (renderer == null) {
073: log.warn("Unsupported component-family/renderer-type: "
074: + componentFamily + "/" + rendererType);
075: }
076: return renderer;
077: }
078:
079: public void addRenderer(String componentFamily,
080: String rendererType, Renderer renderer) {
081: if (componentFamily == null) {
082: log
083: .error("addRenderer: componentFamily = null is not allowed");
084: throw new NullPointerException(
085: "component family must not be null.");
086: }
087: if (rendererType == null) {
088: log
089: .error("addRenderer: rendererType = null is not allowed");
090: throw new NullPointerException(
091: "renderer type must not be null.");
092: }
093: if (renderer == null) {
094: log.error("addRenderer: renderer = null is not allowed");
095: throw new NullPointerException("renderer must not be null.");
096: }
097:
098: String rendererKey = key(componentFamily, rendererType);
099: if (_renderers.get(rendererKey) != null) {
100: // this is not necessarily an error, but users do need to be
101: // very careful about jar processing order when overriding
102: // some component's renderer with an alternate renderer.
103: log.debug("Overwriting renderer with family = "
104: + componentFamily + " rendererType = "
105: + rendererType + " renderer class = "
106: + renderer.getClass().getName());
107: }
108:
109: _renderers.put(rendererKey, renderer);
110:
111: if (log.isTraceEnabled())
112: log.trace("add Renderer family = " + componentFamily
113: + " rendererType = " + rendererType
114: + " renderer class = "
115: + renderer.getClass().getName());
116: }
117:
118: public ResponseStateManager getResponseStateManager() {
119: return _responseStateManager;
120: }
121:
122: public ResponseWriter createResponseWriter(Writer writer,
123: String contentTypeListString, String characterEncoding) {
124: String selectedContentType = HtmlRendererUtils
125: .selectContentType(contentTypeListString);
126:
127: if (characterEncoding == null) {
128: characterEncoding = HtmlRendererUtils.DEFAULT_CHAR_ENCODING;
129: }
130:
131: return new HtmlResponseWriterImpl(writer, selectedContentType,
132: characterEncoding);
133: }
134:
135: public ResponseStream createResponseStream(OutputStream outputStream) {
136: final OutputStream output = outputStream;
137:
138: return new ResponseStream() {
139: public void write(int b) throws IOException {
140: output.write(b);
141: }
142:
143: public void write(byte b[]) throws IOException {
144: output.write(b);
145: }
146:
147: public void write(byte b[], int off, int len)
148: throws IOException {
149: output.write(b, off, len);
150: }
151:
152: public void flush() throws IOException {
153: output.flush();
154: }
155:
156: public void close() throws IOException {
157: output.close();
158: }
159: };
160: }
161: }
|