Coverage Report - org.apache.myfaces.renderkit.html.HtmlRenderKitImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlRenderKitImpl
0%
0/43
0%
0/22
0
HtmlRenderKitImpl$MyFacesResponseStream
0%
0/13
N/A
0
 
 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 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  
  * @author Manfred Geiler (latest modification by $Author: lu4242 $)
 42  
  * @version $Revision: 779759 $ $Date: 2009-05-28 15:22:15 -0500 (Thu, 28 May 2009) $
 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  
     //~ Instance fields ----------------------------------------------------------------------------
 51  
 
 52  
     private Map<String, Map<String, Renderer>> _renderers;
 53  
     private ResponseStateManager _responseStateManager;
 54  
 
 55  
     //~ Constructors -------------------------------------------------------------------------------
 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  
     //~ Methods ------------------------------------------------------------------------------------
 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  
      * Put the renderer on the double map
 116  
      * 
 117  
      * @param componentFamily
 118  
      * @param rendererType
 119  
      * @param renderer
 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  
                 // this is not necessarily an error, but users do need to be
 133  
                 // very careful about jar processing order when overriding
 134  
                 // some component's renderer with an alternate renderer.
 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  
 }