View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.renderkit.html;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils;
24  import org.apache.myfaces.shared_impl.renderkit.html.HtmlResponseWriterImpl;
25  
26  import javax.faces.context.ResponseStream;
27  import javax.faces.context.ResponseWriter;
28  import javax.faces.render.RenderKit;
29  import javax.faces.render.Renderer;
30  import javax.faces.render.ResponseStateManager;
31  import java.io.OutputStream;
32  import java.io.Writer;
33  import java.io.IOException;
34  import java.util.*;
35  
36  
37  /**
38   * @JSFRenderKit
39   *   renderKitId = "HTML_BASIC"
40   * 
41   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
42   * @version $Revision: 659857 $ $Date: 2008-05-24 13:17:47 -0500 (Sat, 24 May 2008) $
43   */
44  public class HtmlRenderKitImpl
45      extends RenderKit
46  {
47      private static final Log log = LogFactory.getLog(HtmlRenderKitImpl.class);
48  
49      //~ Instance fields ----------------------------------------------------------------------------
50  
51      private Map _renderers;
52      private ResponseStateManager _responseStateManager;
53  
54      //~ Constructors -------------------------------------------------------------------------------
55  
56      public HtmlRenderKitImpl()
57      {
58          _renderers = new HashMap();
59          _responseStateManager = new HtmlResponseStateManager();
60      }
61  
62      //~ Methods ------------------------------------------------------------------------------------
63  
64      private String key(String componentFamily, String rendererType)
65      {
66          return componentFamily + "." + rendererType;
67      }
68  
69      public Renderer getRenderer(String componentFamily, String rendererType)
70      {
71          if(componentFamily == null)
72          {
73              throw new NullPointerException("component family must not be null.");
74          }
75          if(rendererType == null)
76          {
77              throw new NullPointerException("renderer type must not be null.");
78          }
79          Renderer renderer = (Renderer) _renderers.get(key(componentFamily, rendererType));
80          if (renderer == null)
81          {
82              log.warn("Unsupported component-family/renderer-type: " + componentFamily + "/" + rendererType);
83          }
84          return renderer;
85      }
86  
87      public void addRenderer(String componentFamily, String rendererType, Renderer renderer)
88      {
89          if(componentFamily == null)
90          {
91              log.error("addRenderer: componentFamily = null is not allowed");
92              throw new NullPointerException("component family must not be null.");
93          }
94          if(rendererType == null)
95          {
96              log.error("addRenderer: rendererType = null is not allowed");
97              throw new NullPointerException("renderer type must not be null.");
98          }
99          if(renderer == null)
100         {
101             log.error("addRenderer: renderer = null is not allowed");
102             throw new NullPointerException("renderer must not be null.");
103         }
104 
105         String rendererKey = key(componentFamily, rendererType);
106         if (_renderers.get(rendererKey) != null) {
107             // this is not necessarily an error, but users do need to be
108             // very careful about jar processing order when overriding
109             // some component's renderer with an alternate renderer.
110             log.info("Overwriting renderer with family = " + componentFamily +
111                " rendererType = " + rendererType +
112                " renderer class = " + renderer.getClass().getName());
113         }
114 
115         _renderers.put(rendererKey, renderer);
116 
117         if (log.isTraceEnabled()) 
118             log.trace("add Renderer family = " + componentFamily +
119                 " rendererType = " + rendererType +
120                 " renderer class = " + renderer.getClass().getName());
121     }
122 
123     public ResponseStateManager getResponseStateManager()
124     {
125         return _responseStateManager;
126     }
127 
128     public ResponseWriter createResponseWriter(Writer writer,
129                                                String contentTypeListString,
130                                                String characterEncoding)
131     {
132         String selectedContentType = HtmlRendererUtils.selectContentType(contentTypeListString);
133 
134         if(characterEncoding==null)
135         {
136             characterEncoding = HtmlRendererUtils.DEFAULT_CHAR_ENCODING;
137         }
138 
139         return new HtmlResponseWriterImpl(writer, selectedContentType, characterEncoding);
140     }
141 
142     public ResponseStream createResponseStream(OutputStream outputStream)
143     {
144         final OutputStream output = outputStream;
145 
146         return new ResponseStream()
147         {
148             public void write(int b) throws IOException
149             {
150                 output.write(b);
151             }
152 
153 
154             public void write(byte b[]) throws IOException
155             {
156                 output.write(b);
157             }
158 
159 
160             public void write(byte b[], int off, int len) throws IOException
161             {
162                 output.write(b, off, len);
163             }
164 
165 
166             public void flush() throws IOException
167             {
168                 output.flush();
169             }
170 
171 
172             public void close() throws IOException
173             {
174                 output.close();
175             }
176         };
177     }
178 }