1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
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  
92  
93  
94  	public TemplateLocator getLocator() {
95      	return locator;
96  
97  	}
98  
99  	
100 
101 
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 
112 
113 
114 	public List<ResourceKey> getImportedResourceKeys() {
115 		return (importedResourceKeys != null) ? Collections.unmodifiableList(importedResourceKeys) : null;
116 	}
117 	
118 	
119 
120 
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 
131 
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 }