View Javadoc

1   /*
2    * This file is part of Pease Plate Template Engine.
3    * 
4    * Pease Plate Template Engine is free software: you can redistribute
5    * it and/or modify it under the terms of the GNU Lesser General 
6    * Public License as published by the Free Software Foundation, 
7    * either version 3 of the License, or any later version.
8    * 
9    * Pease Plate Template Engine is distributed in the hope that it 
10   * will be useful, but WITHOUT ANY WARRANTY; without even the implied
11   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12   * See the GNU Lesser General Public License for more details.
13   * 
14   * You should have received a copy of the GNU Lesser General Public 
15   * License along with Pease Plate Template Engine. If not, see 
16   * <http://www.gnu.org/licenses/>.
17   * 
18   * Copyright (c) 2008 Manfred HANTSCHEL
19   */
20  package org.peaseplate.chunk;
21  
22  import java.io.IOException;
23  import java.io.Writer;
24  
25  import org.peaseplate.TemplateException;
26  import org.peaseplate.internal.BuildContext;
27  import org.peaseplate.locator.TemplateLocator;
28  
29  /**
30   * The abstract implementation of the block head. 
31   * 
32   * @author Manfred HANTSCHEL
33   */
34  public abstract class AbstractBlock extends AbstractChunk implements BlockHead {
35  
36  	private Chunk[] block = null;
37  	
38  	public AbstractBlock(TemplateLocator locator, int line, int column) {
39  	    super(locator, line, column);
40      }
41  
42  	/**
43  	 * @see org.peaseplate.chunk.BlockHead#getBlock()
44  	 */
45  	public Chunk[] getBlock() {
46  	    return block;
47      }
48  
49  	/**
50  	 * @see org.peaseplate.chunk.BlockHead#setBlock(org.peaseplate.chunk.Chunk[])
51  	 */
52  	public void setBlock(Chunk[] block) throws IllegalStateException {
53  		if ((this.block != null) && (block != null) && (this.block != block))
54  			throw new IllegalStateException("Block has already block");
55  		
56  		this.block = block;
57      }
58  	
59  	/**
60  	 * @see org.peaseplate.chunk.BlockHead#renderBlock(BuildContext, java.io.Writer)
61  	 */
62  	public void renderBlock(BuildContext context, Writer writer) throws TemplateException, IOException {
63  		for (Chunk chunk : getBlock())
64  			chunk.render(context, writer);
65      }
66  
67  	/**
68  	 * @see java.lang.Object#toString()
69  	 */
70  	@Override
71      public String toString() {
72  		StringBuilder builder = new StringBuilder("{\n");
73  		
74  		if (getBlock() != null)
75  			for (Chunk chunk : getBlock())
76  				builder.append("\t").append(
77  					chunk.toString().replace("\n", "\n\t")
78  				).append("\n");
79  
80  		builder.append("}");
81  		
82  		return builder.toString();
83      }
84  	
85  }