Changeset 657
- Timestamp:
- 2008-10-02 14:35:05 (5 years ago)
- Location:
- trunk/universe/kauri-template/src
- Files:
-
- 4 edited
-
main/java/org/kauriproject/template/VariableBlock.java (modified) (5 diffs)
-
test/java/org/kauriproject/template/TemplateExecutionTest.java (modified) (1 diff)
-
test/resources/org/kauriproject/template/variable_from_src.xml (modified) (1 diff)
-
test/resources/org/kauriproject/template/variable_from_src_result.xml (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/universe/kauri-template/src/main/java/org/kauriproject/template/VariableBlock.java
r656 r657 34 34 public class VariableBlock extends TemplateBlock { 35 35 36 pr otectedstatic Log log = LogFactory.getLog(VariableBlock.class);36 private static Log log = LogFactory.getLog(VariableBlock.class); 37 37 38 38 // VARIABLE CONSTANTS 39 public static final String NAME = "name"; 40 public static final String VALUE = "value"; 41 public static final String SRC = "src"; 39 private static final String NAME = "name"; 40 private static final String VALUE = "value"; 41 private static final String SRC = "src"; 42 private static final String OVERWRITE = "overwrite"; 42 43 43 44 // typical block stuff … … 47 48 48 49 // EL 49 pr otectedELFacade elFacade;50 private ELFacade elFacade; 50 51 private String baseURI; 51 52 … … 75 76 private final Expression valueExpression; 76 77 private final Expression srcExpression; 78 private final boolean overwrite; 77 79 78 80 // compiledNext: first following instruction … … 96 98 srcExpression = null; 97 99 } 100 101 overwrite = !"false".equalsIgnoreCase(attributes.getValue(OVERWRITE)); 98 102 } 99 103 … … 101 105 public Step executeAndProceed(ExecutionContext context, TemplateResult result) throws SAXException { 102 106 TemplateContext tmplContext = context.getTemplateContext(); 103 if (valueExpression != null) { 104 final Object value = valueExpression.evaluate(context.getTemplateContext()); 105 tmplContext.put(name, value); 106 } else if (srcExpression != null) { 107 Object data = loadFromSrc(context); 108 tmplContext.put(name, data); 109 } else { 110 // use element body as value 111 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 112 KauriSaxHandler ksh = new KauriSaxHandler(bos, OutputFormat.TEXT, "UTF-8", true, true); 113 TemplateResult buffer = new TemplateResultImpl(ksh); 114 buffer.startDocument(); 115 Step next = this.getCompiledNext(); 116 while (next != endStep) { 117 next = next.executeAndProceed(context, buffer); 107 if (overwrite || !tmplContext.containsKey(name)) { 108 if (valueExpression != null) { 109 final Object value = valueExpression.evaluate(context.getTemplateContext()); 110 tmplContext.put(name, value); 111 } else if (srcExpression != null) { 112 Object data = loadFromSrc(context); 113 tmplContext.put(name, data); 114 } else { 115 // use element body as value 116 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 117 KauriSaxHandler ksh = new KauriSaxHandler(bos, OutputFormat.TEXT, "UTF-8", true, true); 118 TemplateResult buffer = new TemplateResultImpl(ksh); 119 buffer.startDocument(); 120 Step next = this.getCompiledNext(); 121 while (next != endStep) { 122 next = next.executeAndProceed(context, buffer); 123 } 124 buffer.endDocument(); 125 buffer.flush(); 126 String value = ""; 127 try { 128 bos.flush(); 129 value = bos.toString("UTF-8"); 130 } catch (IOException ex) { 131 log.error(ex); 132 } 133 134 tmplContext.put(name, value); 118 135 } 119 buffer.endDocument();120 buffer.flush();121 String value = "";122 try {123 bos.flush();124 value = bos.toString("UTF-8");125 } catch (IOException ex) {126 log.error(ex);127 }128 129 tmplContext.put(name, value);130 136 } 131 137 return endStep.getCompiledNext(); -
trunk/universe/kauri-template/src/test/java/org/kauriproject/template/TemplateExecutionTest.java
r656 r657 132 132 133 133 public void testVariableFromSrc() throws Exception { 134 testFlow("/org/kauriproject/template/variable_from_src.xml", true); 134 Map<String, Object> parameters = new HashMap<String, Object>(); 135 parameters.put("overwriteTest1", "overwriteTest1 - external value"); 136 parameters.put("overwriteTest2", "overwriteTest2 - external value"); 137 testFlow("/org/kauriproject/template/variable_from_src.xml", parameters, true); 135 138 } 136 139 -
trunk/universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src.xml
r656 r657 2 2 <root xmlns:ktl="http://kauriproject.org/template"> 3 3 <ktl:variable name="data" src="data.json"/> 4 ${data.message} 4 <ktl:variable name="overwriteTest1" value="local"/> 5 <ktl:variable name="overwriteTest2" value="local" overwrite="false"/> 6 7 ${data.message},${overwriteTest1},${overwriteTest2} 5 8 </root> -
trunk/universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src_result.xml
r656 r657 1 1 <?xml version="1.0" encoding="UTF-8"?> 2 2 <root xmlns:ktl="http://kauriproject.org/template"> 3 Hello world! 3 Hello world!,local,overwriteTest2 - external value 4 4 </root>
Note: See TracChangeset
for help on using the changeset viewer.