Changeset 816
- Timestamp:
- 2008-11-25 14:48:03 (4 years ago)
- Location:
- trunk/universe/kauri-template/src
- Files:
-
- 8 edited
-
main/java/org/kauriproject/template/DefaultTemplateExecutor.java (modified) (1 diff)
-
main/java/org/kauriproject/template/DefaultTemplateService.java (modified) (1 diff)
-
main/java/org/kauriproject/template/DocumentBlock.java (modified) (1 diff)
-
main/java/org/kauriproject/template/ExecutionContext.java (modified) (5 diffs)
-
main/java/org/kauriproject/template/OutputBlock.java (modified) (1 diff)
-
main/java/org/kauriproject/template/SourceContextSwitchBlock.java (modified) (2 diffs)
-
main/java/org/kauriproject/template/TemplateExecutor.java (modified) (1 diff)
-
test/java/org/kauriproject/template/TemplateTestBase.java (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/DefaultTemplateExecutor.java
r815 r816 16 16 package org.kauriproject.template; 17 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 18 import org.xml.sax.SAXException; 20 19 21 20 /** 22 * Default implementation. 23 * 24 * NOTE: We could remove the instance variable {@link #result} and instead make it a required parameter of the 25 * {@link #execute(CompiledTemplate)} method. 21 * The template executor is responsible for the execution of a template. 26 22 */ 27 23 public class DefaultTemplateExecutor implements TemplateExecutor { 28 24 29 private ExecutionContext context; 30 private TemplateResult result; 25 public void execute(Step startStep, Step endStep, ExecutionContext context, TemplateResult result) throws SAXException { 26 if (context.getExecutor() == null) { 27 context.setExecutor(this); 28 } 31 29 32 public DefaultTemplateExecutor(ExecutionContext context, TemplateResult result) { 33 this.context = context; 34 this.result = result; 35 } 36 37 // TODO: Handle errors: failfast strategy ? (issue #41) 38 public void execute(CompiledTemplate compiledTemplate) { 39 try { 40 Step currentStep = compiledTemplate; 41 while (currentStep != null) { 42 currentStep = currentStep.executeAndProceed(context, result); 43 } 44 } catch (Exception ex) { 45 // TODO (bruno) I've changed this log by a throw -- need to look closer at why 46 // it was done this way 47 // log.error("An error occurred when executing the compiled template: " + ex); 48 throw new RuntimeException("An error occured executing the template.", ex); 30 Step currentStep = startStep; 31 while (currentStep != endStep) { 32 currentStep = currentStep.executeAndProceed(context, result); 49 33 } 50 34 } -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/DefaultTemplateService.java
r656 r816 79 79 } 80 80 CompiledTemplate compiledTemplate = buildTemplate(templateLocation, executionContext); 81 DefaultTemplateExecutor executor = new DefaultTemplateExecutor( executionContext, result);82 executor.execute(compiledTemplate );81 DefaultTemplateExecutor executor = new DefaultTemplateExecutor(); 82 executor.execute(compiledTemplate, null, executionContext, result); 83 83 result.flush(); 84 84 } -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/DocumentBlock.java
r686 r816 100 100 } 101 101 102 result.endDocument(); 102 if (!context.isInherited()) { 103 result.endDocument(); 104 } 103 105 return super.executeAndProceed(context, result); 104 106 } -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/ExecutionContext.java
r808 r816 30 30 31 31 private SourceResolver sourceResolver; 32 33 32 private TemplateContext templateContext; 34 35 33 private FunctionRegistry functionRegistry; 36 37 private Stack<Object> blockState; 34 private TemplateExecutor executor; 38 35 39 36 private Map<String, MacroBlock> macroRegistry; … … 43 40 private Stack<SuperBlock> superBlockStack; 44 41 45 private boolean inherited = false;42 private int inherited = 0; 46 43 47 44 public ExecutionContext() { … … 54 51 this.templateContext = templateContext; 55 52 this.functionRegistry = functionRegistry; 56 this.blockState = new Stack();57 53 this.macroRegistry = new HashMap<String, MacroBlock>(); 58 54 this.callerRegistry = new HashMap<String, CallMacroBlock>(); … … 86 82 } 87 83 88 public void setInherited(boolean inherited) { 89 this.inherited = inherited; 84 public void inheritInc() { 85 this.inherited++; 86 } 87 88 public void inheritDecr() { 89 this.inherited--; 90 90 } 91 91 92 92 public boolean isInherited() { 93 return inherited ;93 return inherited > 0; 94 94 } 95 95 … … 114 114 } 115 115 116 public Stack<Object> getBlockState() { 117 return blockState; 116 public TemplateExecutor getExecutor() { 117 return executor; 118 } 119 120 public void setExecutor(TemplateExecutor executor) { 121 this.executor = executor; 118 122 } 119 123 } -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/OutputBlock.java
r812 r816 119 119 } 120 120 121 context.setInherited(true); 122 return baseTemplate.executeAndProceed(context, result); 121 context.inheritInc(); 122 context.getExecutor().execute(baseTemplate, null, context, result); 123 context.inheritDecr(); 124 125 return getEndStep().getCompiledNext(); 123 126 } 124 127 } -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/SourceContextSwitchBlock.java
r809 r816 39 39 @Override 40 40 public Step executeAndProceed(ExecutionContext context, TemplateResult result) throws SAXException { 41 42 Object sc = SourceResolver.ACTIVE_SOURCE_CONTEXT.get(); 43 context.getBlockState().push(sc); 44 45 SourceResolver.ACTIVE_SOURCE_CONTEXT.set(sourceContext); 46 47 return super.executeAndProceed(context, result); 41 Object oldSourceContext = SourceResolver.ACTIVE_SOURCE_CONTEXT.get(); 42 try { 43 SourceResolver.ACTIVE_SOURCE_CONTEXT.set(sourceContext); 44 context.getExecutor().execute(getCompiledNext(), getEndStep(), context, result); 45 } finally { 46 SourceResolver.ACTIVE_SOURCE_CONTEXT.set(oldSourceContext); 47 } 48 return getEndStep().getCompiledNext(); 48 49 } 49 50 } … … 56 57 @Override 57 58 public Step executeAndProceed(ExecutionContext context, TemplateResult result) throws SAXException { 58 Object sc = context.getBlockState().pop(); 59 60 if (sc != null) { 61 SourceResolver.ACTIVE_SOURCE_CONTEXT.set(sc); 62 } else { 63 SourceResolver.ACTIVE_SOURCE_CONTEXT.remove(); 64 } 65 66 return super.executeAndProceed(context, result); 59 return getCompiledNext(); 67 60 } 68 61 } -
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/TemplateExecutor.java
r38 r816 1 /*2 * Copyright 2008 Outerthought bvba and Schaubroeck nv3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16 1 package org.kauriproject.template; 17 2 18 /** 19 * A template executor is responsible for the execution of a specified compiled template. 20 */ 3 import org.xml.sax.SAXException; 4 21 5 public interface TemplateExecutor { 22 23 public void execute(CompiledTemplate compiledTemplate); 6 void execute(Step startStep, Step endStep, ExecutionContext context, TemplateResult result) throws SAXException; 24 7 } -
trunk/universe/kauri-template/src/test/java/org/kauriproject/template/TemplateTestBase.java
r656 r816 35 35 import org.xml.sax.InputSource; 36 36 import org.xml.sax.XMLReader; 37 import org.xml.sax.SAXException; 37 38 38 39 public class TemplateTestBase extends TestCase { … … 86 87 */ 87 88 protected void executeTemplate(CompiledTemplate compiledTemplate, TemplateResult result, 88 Map<String, Object> variables, double treshold) {89 Map<String, Object> variables, double treshold) throws SAXException { 89 90 // setup 90 91 TemplateContext context = new DefaultTemplateContext(); … … 95 96 execContext.setTemplateContext(context); 96 97 execContext.setSourceResolver(sourceResolver); 97 DefaultTemplateExecutor executor = new DefaultTemplateExecutor( execContext, result);98 DefaultTemplateExecutor executor = new DefaultTemplateExecutor(); 98 99 99 100 // monitor duration 100 101 long start = new Date().getTime(); 101 102 // execute template 102 executor.execute(compiledTemplate );103 executor.execute(compiledTemplate, null, execContext, result); 103 104 long end = new Date().getTime(); 104 105
Note: See TracChangeset
for help on using the changeset viewer.