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.context.FacesContext;
024import javax.faces.context.ResponseWriter;
025
026/**
027 * Overrides JSF stylesheet renderer to allow specifying resources from the war using "src" attribute.
028 *
029 * @since 7.4
030 */
031public class NXStylesheetRenderer extends AbstractResourceRenderer {
032
033    @Override
034    protected void startElement(ResponseWriter writer, UIComponent component) throws IOException {
035        writer.startElement("style", component);
036        writer.writeAttribute("type", "text/css", "type");
037    }
038
039    @Override
040    protected void endElement(ResponseWriter writer) throws IOException {
041        writer.endElement("style");
042    }
043
044    @Override
045    protected String verifyTarget(String toVerify) {
046        return "head";
047    }
048
049    @Override
050    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
051        String url = resolveUrl(context, component);
052        if (url != null) {
053            ResponseWriter writer = context.getResponseWriter();
054            writer.startElement("link", component);
055            writer.writeAttribute("type", "text/css", "type");
056            writer.writeAttribute("rel", "stylesheet", "rel");
057            writer.writeURIAttribute("href", url, "href");
058            Map<String, Object> attributes = component.getAttributes();
059            String media = (String) attributes.get("media");
060            if (media != null) {
061                writer.writeAttribute("media", media, "media");
062            }
063            writer.endElement("link");
064        }
065        super.encodeEnd(context, component);
066    }
067
068    @Override
069    public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
070        encodeChildren(context, component, true);
071    }
072
073}