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