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