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;
016
017import org.apache.tapestry5.MarkupWriter;
018import org.apache.tapestry5.dom.Document;
019import org.apache.tapestry5.dom.Element;
020import org.apache.tapestry5.services.MarkupRenderer;
021import org.apache.tapestry5.services.MarkupRendererFilter;
022import org.apache.tapestry5.services.RequestGlobals;
023
024/**
025 * Injects a {@code <meta/>} element into the {@code <head/>} to identify the Tapestry page name.
026 * This filter is only active during development, not production.
027 *
028 * @since 5.4
029 */
030public class PageNameMetaInjector implements MarkupRendererFilter
031{
032    private final RequestGlobals globals;
033
034    public PageNameMetaInjector(RequestGlobals globals)
035    {
036        this.globals = globals;
037    }
038
039    public void renderMarkup(MarkupWriter writer, MarkupRenderer delegate)
040    {
041        delegate.renderMarkup(writer);
042
043        String pageName = globals.getActivePageName();
044
045        Document document = writer.getDocument();
046
047        Element element = document.find("html/head");
048
049        if (element != null)
050        {
051            element.element("meta",
052                    "name", "tapestry-page-name",
053                    "content", pageName);
054        }
055
056    }
057}