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;
020
021import javax.faces.component.UIComponent;
022import javax.faces.context.FacesContext;
023import javax.faces.context.ResponseWriter;
024
025/**
026 * Overrides JSF script renderer to allow specifying resources from the war using "src" attribute.
027 *
028 * @since 7.4
029 */
030public class NXScriptRenderer extends AbstractResourceRenderer {
031
032    @Override
033    protected void startElement(ResponseWriter writer, UIComponent component) throws IOException {
034        writer.startElement("script", component);
035        writer.writeAttribute("type", "text/javascript", "type");
036    }
037
038    @Override
039    protected void endElement(ResponseWriter writer) throws IOException {
040        writer.endElement("script");
041    }
042
043    @Override
044    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
045        String url = resolveUrl(context, component);
046        if (url != null) {
047            ResponseWriter writer = context.getResponseWriter();
048            startElement(writer, component);
049            writer.writeURIAttribute("src", url, "src");
050            endElement(writer);
051        }
052        super.encodeEnd(context, component);
053    }
054
055    @Override
056    public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
057        encodeChildren(context, component, true);
058    }
059
060}