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.component;
018
019import java.io.IOException;
020
021import javax.faces.component.UIComponentBase;
022import javax.faces.component.UIOutput;
023import javax.faces.context.FacesContext;
024import javax.faces.context.ResponseWriter;
025
026import org.apache.commons.lang.StringUtils;
027
028import com.sun.faces.config.FaceletsConfiguration;
029import com.sun.faces.config.WebConfiguration;
030
031/**
032 * Component rendering a "meta" HTML tag.
033 *
034 * @since 7.4
035 */
036public class UIMeta extends UIComponentBase {
037
038    public static final String COMPONENT_TYPE = UIMeta.class.getName();
039
040    public static enum PropertyKeys {
041        charset, content, httpequiv, name
042    }
043
044    @Override
045    public String getFamily() {
046        return UIOutput.COMPONENT_FAMILY;
047    }
048
049    public String getCharset() {
050        return (String) getStateHelper().eval(PropertyKeys.charset);
051    }
052
053    public void setCharset(String charset) {
054        getStateHelper().put(PropertyKeys.charset, charset);
055    }
056
057    public String getContent() {
058        return (String) getStateHelper().eval(PropertyKeys.content);
059    }
060
061    public void setContent(String content) {
062        getStateHelper().put(PropertyKeys.content, content);
063    }
064
065    public String getHttpequiv() {
066        return (String) getStateHelper().eval(PropertyKeys.httpequiv);
067    }
068
069    public void setHttpequiv(String httpequiv) {
070        getStateHelper().put(PropertyKeys.httpequiv, httpequiv);
071    }
072
073    public String getName() {
074        return (String) getStateHelper().eval(PropertyKeys.name);
075    }
076
077    public void setName(String name) {
078        getStateHelper().put(PropertyKeys.name, name);
079    }
080
081    public String getRendererType() {
082        return null;
083    }
084
085    @Override
086    public void encodeEnd(FacesContext context) throws IOException {
087        if (context == null) {
088            throw new NullPointerException();
089        }
090        if (!isRendered()) {
091            popComponentFromEL(context);
092            return;
093        }
094
095        ResponseWriter writer = context.getResponseWriter();
096        writer.startElement("meta", this);
097        WebConfiguration webConfig = WebConfiguration.getInstance(context.getExternalContext());
098        FaceletsConfiguration faceletsConfig = webConfig.getFaceletsConfiguration();
099        if (faceletsConfig.isOutputHtml5Doctype(context.getViewRoot().getViewId())) {
100            String charset = getCharset();
101            if (!StringUtils.isBlank(charset)) {
102                writer.writeAttribute("charset", charset, "charset");
103            }
104            String httpEquiv = getHttpequiv();
105            if (!StringUtils.isBlank(httpEquiv)) {
106                writer.writeAttribute("http-equiv", httpEquiv, "http-equiv");
107            }
108            String content = getContent();
109            if (!StringUtils.isBlank(content)) {
110                writer.writeAttribute("content", content, "content");
111            }
112        } else {
113            writer.writeAttribute("http-equiv", "Content-Type", "http-equiv");
114            writer.writeAttribute("content", String.format("text/html;charset=%s", getCharset()), "content");
115        }
116        String name = getName();
117        if (!StringUtils.isBlank(name)) {
118            writer.writeAttribute("name", name, "name");
119        }
120        writer.endElement("meta");
121
122        popComponentFromEL(context);
123    }
124
125}