001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.web.resources.jsf;
018
019import java.io.IOException;
020import java.util.Map;
021
022import javax.faces.component.UIComponent;
023import javax.faces.component.UIViewRoot;
024import javax.faces.context.FacesContext;
025import javax.faces.render.Renderer;
026
027import com.sun.faces.renderkit.html_basic.HeadRenderer;
028
029/**
030 * Renderer for resources added as component resources to the {@link UIViewRoot} for given target.
031 * <p>
032 * Plays a role similar role to the {@link HeadRenderer} when JSF resources are declared with target "head", useful to
033 * place js resources at the end of the page, for instance.
034 *
035 * @since 7.4
036 */
037public class ResourceInsertRenderer extends Renderer {
038
039    public static final String RENDERER_TYPE = "org.nuxeo.ecm.web.resources.jsf.ResourceInsert";
040
041    @Override
042    public boolean getRendersChildren() {
043        return true;
044    }
045
046    @Override
047    public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
048        if (context == null || component == null) {
049            throw new NullPointerException();
050        }
051        Map<String, Object> attributes = component.getAttributes();
052        String target = (String) attributes.get("target");
053        UIViewRoot viewRoot = context.getViewRoot();
054        for (UIComponent resource : viewRoot.getComponentResources(context, target)) {
055            resource.encodeAll(context);
056        }
057    }
058
059    @Override
060    public void decode(FacesContext context, UIComponent component) {
061        // NOOP
062    }
063
064    @Override
065    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
066        // NOOP
067    }
068
069    @Override
070    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
071        // NOOP
072    }
073
074}