Changeset 657


Ignore:
Timestamp:
2008-10-02 14:35:05 (5 years ago)
Author:
bruno
Message:

Templates: variables: introduced a new boolean attribute 'overwrite' which can be used to indicate that a variable should only be set if there was no value already associated with the variable name. This is useful for prototyping.

Location:
trunk/universe/kauri-template/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/VariableBlock.java

    r656 r657  
    3434public class VariableBlock extends TemplateBlock { 
    3535 
    36     protected static Log log = LogFactory.getLog(VariableBlock.class); 
     36    private static Log log = LogFactory.getLog(VariableBlock.class); 
    3737 
    3838    // 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"; 
    4243 
    4344    // typical block stuff 
     
    4748 
    4849    // EL 
    49     protected ELFacade elFacade; 
     50    private ELFacade elFacade; 
    5051    private String baseURI; 
    5152 
     
    7576        private final Expression valueExpression; 
    7677        private final Expression srcExpression; 
     78        private final boolean overwrite; 
    7779 
    7880        // compiledNext: first following instruction 
     
    9698                srcExpression = null; 
    9799            } 
     100 
     101            overwrite = !"false".equalsIgnoreCase(attributes.getValue(OVERWRITE)); 
    98102        } 
    99103 
     
    101105        public Step executeAndProceed(ExecutionContext context, TemplateResult result) throws SAXException { 
    102106            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); 
    118135                } 
    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); 
    130136            } 
    131137            return endStep.getCompiledNext(); 
  • trunk/universe/kauri-template/src/test/java/org/kauriproject/template/TemplateExecutionTest.java

    r656 r657  
    132132 
    133133    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); 
    135138    } 
    136139 
  • trunk/universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src.xml

    r656 r657  
    22<root xmlns:ktl="http://kauriproject.org/template"> 
    33    <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} 
    58</root> 
  • trunk/universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src_result.xml

    r656 r657  
    11<?xml version="1.0" encoding="UTF-8"?> 
    22<root xmlns:ktl="http://kauriproject.org/template"> 
    3   Hello world! 
     3  Hello world!,local,overwriteTest2 - external value 
    44</root> 
Note: See TracChangeset for help on using the changeset viewer.