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