    001// Licensed to the Apache License, Version 2.0 (the "License");
    002// you may not use this file except in compliance with the License.
    003// You may obtain a copy of the License at
    004//
    005//     http://www.apache.org/licenses/LICENSE-2.0
    006//
    007// Unless required by applicable law or agreed to in writing, software
    008// distributed under the License is distributed on an "AS IS" BASIS,
    009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    010// See the License for the specific language governing permissions and
    011// limitations under the License.
    012
    013package org.apache.tapestry5.corelib.components;
    014
    015import org.apache.tapestry5.MarkupWriter;
    016import org.apache.tapestry5.annotations.Environmental;
    017import org.apache.tapestry5.dom.Element;
    018import org.apache.tapestry5.internal.RecursiveContext;
    019import org.apache.tapestry5.ioc.annotations.Inject;
    020import org.apache.tapestry5.services.Environment;
    021
    022/**
    023 * <p>
    024 * Component that marks the place in the template the
    025 * recursion should happen. It should only be used inside
    026 * {@link Recursive}, otherwise an exception will be
    027 * thrown.
    028 * </p>
    029 * <p>
    030 * This was contributed by <a href="https://www.pubfactory.com">KGL PubFactory</a>.
    031 * </p>
    032 * @since 5.9.0
    033 */
    034public class RecursiveBody
    035{
    036
    037    @Inject
    038    private Environment environment;
    039
    040    @Environmental
    041    private RecursiveContext context;
    042
    043    void beginRender(MarkupWriter writer)
    044    {
    045        final RecursiveContext recursiveContext = environment.peek(RecursiveContext.class);
    046        if (recursiveContext != null)
    047        {
    048            final Element placeholder = writer
    049                    .element(
    050                            Recursive.RECURSIVE_INSERTION_POINT_ELEMENT_NAME,
    051                            "id", recursiveContext.getId());
    052            context.registerPlaceholder(placeholder);
    053        }
    054
    055    }
    056
    057    boolean beginRenderTemplate(MarkupWriter writer)
    058    {
    059        return false; // throw away any body this component instance might have in its declaration
    060    }
    061
    062    void afterRender(MarkupWriter writer)
    063    {
    064        writer.end();
    065    }
    066
    067}