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.internal.model;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import org.peaseplate.chunk.OutputStrategy;
31  import org.peaseplate.chunk.strategy.DefaultOutputStrategy;
32  import org.peaseplate.internal.ResourceKey;
33  import org.peaseplate.internal.chunk.MacroBlock;
34  import org.peaseplate.locator.TemplateLocator;
35  
36  public class CompileContext {
37  
38  	private static class Variables {
39  		private final Variables parent;
40  		
41  		private Set<String> variables = null;
42  		
43  		public Variables(Variables parent) {
44  			super();
45  			
46  			this.parent = parent;
47  		}
48  		
49  		public Variables getParent() {
50          	return parent;
51          }
52  
53  		public void add(String name) {
54  			if (variables == null)
55  				variables = new HashSet<String>();
56  			
57  			variables.add(name);
58  		}
59  		
60  		public boolean contains(String name) {
61  			boolean result = (variables != null) && (variables.contains(name));
62  			
63  			if ((!result) && (parent != null))
64  				result = parent.contains(name);
65  			
66  			return result;
67  		}
68  		
69  		public String[] toArray() {
70  			return (variables != null) ? variables.toArray(new String[variables.size()]) : null;
71  		}
72  	}
73  	
74  	private final TemplateLocator locator;
75  	
76  	private List<ResourceKey> importedResourceKeys;
77  	private Map<String, MacroBlock> macroBlocks;
78  	private Variables variables;
79  	private OutputStrategy outputStrategy;
80  	
81  	public CompileContext(TemplateLocator locator) {
82  	    super();
83  	    
84  	    this.locator = locator;
85  	    
86  	    variables = new Variables(null);
87  	    outputStrategy = new DefaultOutputStrategy();
88      }
89  
90  	/**
91  	 * Returns the template locator
92  	 * @return the template locator
93  	 */
94  	public TemplateLocator getLocator() {
95      	return locator;
96  
97  	}
98  
99  	/**
100 	 * Adds an imported resource key
101 	 * @param descriptor the imported resource key
102 	 */
103 	public void addImportedResourceKey(ResourceKey descriptor) {
104 		if (importedResourceKeys == null)
105 			importedResourceKeys = new ArrayList<ResourceKey>();
106 		
107 		importedResourceKeys.add(descriptor);
108 	}
109 	
110 	/**
111 	 * Returns an unmodifiable list of imported resource descriptors
112 	 * @return an unmodifiable list of imported resource descriptors
113 	 */
114 	public List<ResourceKey> getImportedResourceKeys() {
115 		return (importedResourceKeys != null) ? Collections.unmodifiableList(importedResourceKeys) : null;
116 	}
117 	
118 	/**
119 	 * Adds a macro block
120 	 * @param macroBlock a macro block
121 	 */
122 	public void addMacroBlock(MacroBlock macroBlock) {
123 		if (macroBlocks == null)
124 			macroBlocks = new HashMap<String, MacroBlock>();
125 		
126 		macroBlocks.put(macroBlock.getQualifiedName(), macroBlock);
127 	}
128 	
129 	/**
130 	 * Returns an unmodifiable map of macro blocks
131 	 * @return an unmodifiable map of macro blocks
132 	 */
133 	public Map<String, MacroBlock> getMacroBlocks() {
134 		return (macroBlocks != null) ? Collections.unmodifiableMap(macroBlocks) : null;
135 	}
136 	
137 	public void addVariable(String... names) {
138 		if (names != null) 
139 			for (String name : names)
140 				variables.add(name);
141 	}
142 	
143 	public boolean containsVariable(String name) {
144 		return variables.contains(name);
145 	}
146 	
147 	public void pushVariables() {
148 		variables = new Variables(variables);
149 	}
150 	
151 	public String[] popVariables() {
152 		String[] result = variables.toArray(); 
153 		
154 		variables = variables.getParent();
155 		
156 		return result;
157 	}
158 
159 	public OutputStrategy getOutputStrategy() {
160 		return outputStrategy;
161 	}
162 
163 	@Directive("outputStrategy")
164 	public void setOutputStrategy(Class<? extends OutputStrategy> outputStrategyClass) throws IllegalArgumentException {
165 		try {
166 			this.outputStrategy = outputStrategyClass.newInstance();
167 		}
168 		catch (InstantiationException e) {
169 			throw new IllegalArgumentException("Cannot instantiate output strategy " + outputStrategyClass, e);
170 		}
171 		catch (IllegalAccessException e) {
172 			throw new IllegalArgumentException("Cannot access output strategy " + outputStrategyClass, e);
173 		}
174 	}
175 	
176 }