001 // Copyright 2007, 2008 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 015 package org.apache.tapestry5.internal.structure; 016 017 import org.apache.tapestry5.Block; 018 import org.apache.tapestry5.MarkupWriter; 019 import org.apache.tapestry5.ioc.BaseLocatable; 020 import org.apache.tapestry5.ioc.Location; 021 import org.apache.tapestry5.ioc.internal.util.CollectionFactory; 022 import org.apache.tapestry5.runtime.RenderCommand; 023 import org.apache.tapestry5.runtime.RenderQueue; 024 025 import java.util.List; 026 027 public class BlockImpl extends BaseLocatable implements Block, BodyPageElement, RenderCommand 028 { 029 // We could lazily create this, but for (parameter) block elements the case 030 // for an empty block is extremely rare. 031 032 private final List<RenderCommand> elements = CollectionFactory.newList(); 033 034 private final String description; 035 036 public BlockImpl(Location location, String description) 037 { 038 super(location); 039 040 this.description = description; 041 } 042 043 public void addToBody(RenderCommand element) 044 { 045 elements.add(element); 046 } 047 048 /** 049 * Pushes all the elements of the body of this block onto the queue in appropriate order. 050 */ 051 public void render(MarkupWriter writer, RenderQueue queue) 052 { 053 int count = elements.size(); 054 for (int i = count - 1; i >= 0; i--) 055 queue.push(elements.get(i)); 056 } 057 058 @Override 059 public String toString() 060 { 061 return String.format("Block[%s, at %s]", description, getLocation()); 062 } 063 }