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