Ignore:
Timestamp:
2010-02-16 09:06:52 (2 years ago)
Author:
mpo
Message:

fix for #191

File:
1 edited

Legend:

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

    r1350 r1436  
    5959    } 
    6060 
     61    private static final Object NO_VALUE = new Object(); 
     62 
    6163    class StartStep extends Step { 
    6264 
     
    101103        } 
    102104 
     105         
    103106        @Override 
    104107        public Step executeAndProceed(ExecutionContext context, TemplateResult result) throws SAXException { 
    105108            TemplateContext tmplContext = context.getTemplateContext(); 
    106109            if (overwrite || !tmplContext.containsKey(name)) { 
     110                Object value = NO_VALUE; 
    107111                if (valueExpression != null) { 
    108                     final Object value = valueExpression.evaluate(context.getTemplateContext()); 
    109                     tmplContext.put(name, value); 
    110                 } else if (srcExpression != null) { 
    111                     Object data = loadFromSrc(context); 
    112                     tmplContext.put(name, data); 
     112                    value = valueExpression.evaluate(context.getTemplateContext()); 
    113113                } else { 
    114114                    // use element body as value 
     
    123123                    buffer.endDocument(); 
    124124                    buffer.flush(); 
    125                     String value; 
    126125                    try { 
    127126                        bos.flush(); 
    128                         value = bos.toString("UTF-8"); 
     127                        final String content = bos.toString("UTF-8"); 
     128                        if (content != null && content.length() > 0) 
     129                            value = content;  
    129130                    } catch (IOException ex) { 
    130131                        throw new TemplateException("Error building variable value.", ex); 
    131132                    } 
    132  
    133                     tmplContext.put(name, value); 
    134                 } 
    135             } 
     133                } 
     134 
     135                // value retrieved from @value or body can be overwritten by loaded data from @src 
     136                if (srcExpression != null) { 
     137                    value = loadFromSrc(context, value); 
     138                } 
     139 
     140                tmplContext.put(name, value); 
     141            } 
     142 
    136143            return endStep.getCompiledNext(); 
    137144        } 
    138145 
    139         private Object loadFromSrc(ExecutionContext context) { 
     146        private Object loadFromSrc(ExecutionContext context, Object fallbackValue) { 
    140147            String src = (String)srcExpression.evaluate(context.getTemplateContext()); 
    141148            String accept = acceptExpression != null ? (String)acceptExpression.evaluate(context.getTemplateContext()) : null; 
     
    152159                } 
    153160            } catch (Throwable e) { 
     161                if (fallbackValue != NO_VALUE) 
     162                    return fallbackValue; 
     163                // else 
    154164                throw new TemplateException("Some error occured while loading data for variable " + name + " from URI " + src, e); 
    155165            } finally { 
Note: See TracChangeset for help on using the changeset viewer.