001    package org.apache.myfaces.tobago.renderkit;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.apache.myfaces.tobago.context.ResourceManager;
023    import org.apache.myfaces.tobago.context.ResourceManagerFactory;
024    import org.apache.myfaces.tobago.webapp.TobagoResponseWriterImpl;
025    
026    import javax.faces.FactoryFinder;
027    import javax.faces.context.FacesContext;
028    import javax.faces.context.ResponseStream;
029    import javax.faces.context.ResponseWriter;
030    import javax.faces.render.RenderKit;
031    import javax.faces.render.RenderKitFactory;
032    import javax.faces.render.Renderer;
033    import javax.faces.render.ResponseStateManager;
034    import java.io.OutputStream;
035    import java.io.Writer;
036    
037    public class TobagoRenderKit extends RenderKit {
038    
039      private static final Log LOG = LogFactory.getLog(TobagoRenderKit.class);
040    
041      public static final String RENDER_KIT_ID = "tobago";
042    
043      private ResourceManager resources;
044    
045      private ResponseStateManager responseStateManager;
046    
047      public TobagoRenderKit() {
048        responseStateManager = new TobagoResponseStateManager();
049      }
050    
051      // FIXME: use family
052      @Override
053      public Renderer getRenderer(String family, String rendererType) {
054        if (LOG.isDebugEnabled()) {
055          LOG.debug("family = '" + family + "'");
056        }
057        Renderer renderer = null;
058        FacesContext facesContext = FacesContext.getCurrentInstance();
059        if (!"facelets".equals(family)) {
060          if (rendererType != null) {
061            if (resources == null) {
062              resources = ResourceManagerFactory.getResourceManager(facesContext);
063            }
064            renderer = resources.getRenderer(facesContext.getViewRoot(), rendererType);
065          }
066        }
067    
068        if (renderer == null) {
069          RenderKitFactory rkFactory = (RenderKitFactory)
070              FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
071          RenderKit renderKit = rkFactory.getRenderKit(facesContext, RenderKitFactory.HTML_BASIC_RENDER_KIT);
072          renderer = renderKit.getRenderer(family, rendererType);
073          if (renderer != null) {
074            renderer = new RendererBaseWrapper(renderer);
075          }
076        }
077    
078        if (renderer == null) {
079          LOG.error("The class which was found by the ResourceManager cannot be "
080              + "found or instantiated: classname='" + rendererType + "'");
081        }
082    
083        return renderer;
084      }
085    
086      @Override
087      public ResponseWriter createResponseWriter(
088          Writer writer, String contentTypeList, String characterEncoding) {
089        String contentType;
090        if (contentTypeList == null) {
091          contentType = "text/html";
092        } else if (contentTypeList.indexOf("text/html") > -1) {
093          contentType = "text/html";
094          LOG.warn("patching content type from " + contentTypeList + " to " + contentType + "'");
095        } else if (contentTypeList.indexOf("text/fo") > -1) {
096          contentType = "text/fo";
097          LOG.warn("patching content type from " + contentTypeList + " to " + contentType + "'");
098        } else {
099          contentType = "text/html";
100          LOG.warn("Content-Type '" + contentTypeList + "' not supported!"
101              + " Using text/html", new Exception());
102        }
103    
104        return new TobagoResponseWriterImpl(writer, contentType, characterEncoding);
105      }
106    
107    // ///////////////////////////////////////////// TODO
108    
109      @Override
110      public void addRenderer(String family, String rendererType, Renderer renderer) {
111    //    synchronized(renderers) {
112    //      renderers.put(family + SEP + rendererType, renderer);
113    //    }
114        if (LOG.isDebugEnabled()) {
115          LOG.debug("addRenderer family='" + family
116              + "' rendererType='" + rendererType + "'");
117        }
118        LOG.error(
119            "This method isn't implemented yet, and should not be called: "
120                + new Exception().getStackTrace()[0].getMethodName()); //FIXME jsf1.0
121      }
122    
123      @Override
124      public ResponseStateManager getResponseStateManager() {
125        return responseStateManager;
126      }
127    
128      @Override
129      public ResponseStream createResponseStream(OutputStream outputstream) {
130        LOG.error(
131            "This method isn't implemented yet, and should not be called: "
132                + new Exception().getStackTrace()[0].getMethodName()); //FIXME jsfbeta
133        return null;
134      }
135    }