001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.web.resources.jsf;
020
021import java.io.IOException;
022import java.util.Map;
023
024import javax.faces.component.UIComponent;
025import javax.faces.component.UIViewRoot;
026import javax.faces.context.FacesContext;
027import javax.faces.render.Renderer;
028
029import com.sun.faces.renderkit.html_basic.HeadRenderer;
030
031/**
032 * Renderer for resources added as component resources to the {@link UIViewRoot} for given target.
033 * <p>
034 * Plays a role similar role to the {@link HeadRenderer} when JSF resources are declared with target "head", useful to
035 * place js resources at the end of the page, for instance.
036 *
037 * @since 7.4
038 */
039public class ResourceInsertRenderer extends Renderer {
040
041    public static final String RENDERER_TYPE = "org.nuxeo.ecm.web.resources.jsf.ResourceInsert";
042
043    @Override
044    public boolean getRendersChildren() {
045        return true;
046    }
047
048    @Override
049    public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
050        if (context == null || component == null) {
051            throw new NullPointerException();
052        }
053        Map<String, Object> attributes = component.getAttributes();
054        String target = (String) attributes.get("target");
055        UIViewRoot viewRoot = context.getViewRoot();
056        for (UIComponent resource : viewRoot.getComponentResources(context, target)) {
057            resource.encodeAll(context);
058        }
059    }
060
061    @Override
062    public void decode(FacesContext context, UIComponent component) {
063        // NOOP
064    }
065
066    @Override
067    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
068        // NOOP
069    }
070
071    @Override
072    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
073        // NOOP
074    }
075
076}