    001// Copyright 2012 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007// http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.internal.services.javascript;
    016
    017import org.apache.tapestry5.MarkupWriter;
    018import org.apache.tapestry5.SymbolConstants;
    019import org.apache.tapestry5.dom.Element;
    020import org.apache.tapestry5.ioc.annotations.Symbol;
    021import org.apache.tapestry5.ioc.services.ThreadLocale;
    022import org.apache.tapestry5.services.MarkupRenderer;
    023import org.apache.tapestry5.services.MarkupRendererFilter;
    024
    025/**
    026 * Responsible for writing attributes needed to configure the client, into the
    027 * HTML element.
    028 *
    029 * @since 5.4
    030 */
    031public class ConfigureHTMLElementFilter implements MarkupRendererFilter
    032{
    033    private final ThreadLocale threadLocale;
    034
    035    private final boolean debugEnabled;
    036
    037    public ConfigureHTMLElementFilter(ThreadLocale threadLocale, @Symbol(SymbolConstants.PRODUCTION_MODE) boolean productionMode)
    038    {
    039        this.threadLocale = threadLocale;
    040        this.debugEnabled = !productionMode;
    041    }
    042
    043    public void renderMarkup(MarkupWriter writer, MarkupRenderer renderer)
    044    {
    045        renderer.renderMarkup(writer);
    046
    047        // After that's done (i.e., pretty much all rendering), touch it up a little.
    048
    049        Element html = writer.getDocument().find("html");
    050
    051        // If it is an HTML document, with a root HTML node, add attributes
    052        // to describe locale, and if debug is enabled.
    053        if (html != null)
    054        {
    055            html.attributes("data-locale", threadLocale.getLocale().toString());
    056
    057            if (debugEnabled)
    058            {
    059                html.attributes("data-debug-enabled", "true");
    060            }
    061
    062        }
    063    }
    064}