Index: universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src_result.xml =================================================================== --- universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src_result.xml (revision 1434) +++ universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src_result.xml (working copy) @@ -1,4 +1,4 @@ - Hello world!,local,overwriteTest2 - external value + Hello world!,Hello world!,local,overwriteTest2 - external value \ No newline at end of file Index: universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src.xml =================================================================== --- universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src.xml (revision 1434) +++ universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src.xml (working copy) @@ -1,8 +1,9 @@ - + + - ${data.message},${overwriteTest1},${overwriteTest2} + ${data.message},${data2.message},${overwriteTest1},${overwriteTest2} \ No newline at end of file Index: universe/kauri-template/src/main/java/org/kauriproject/template/VariableBlock.java =================================================================== --- universe/kauri-template/src/main/java/org/kauriproject/template/VariableBlock.java (revision 1434) +++ universe/kauri-template/src/main/java/org/kauriproject/template/VariableBlock.java (working copy) @@ -58,6 +58,8 @@ return new EndStep(locator); } + private static final Object NO_VALUE = new Object(); + class StartStep extends Step { // variable stuff @@ -100,16 +102,14 @@ overwrite = !"false".equalsIgnoreCase(attributes.getValue(OVERWRITE)); } + @Override public Step executeAndProceed(ExecutionContext context, TemplateResult result) throws SAXException { TemplateContext tmplContext = context.getTemplateContext(); if (overwrite || !tmplContext.containsKey(name)) { + Object value = NO_VALUE; if (valueExpression != null) { - final Object value = valueExpression.evaluate(context.getTemplateContext()); - tmplContext.put(name, value); - } else if (srcExpression != null) { - Object data = loadFromSrc(context); - tmplContext.put(name, data); + value = valueExpression.evaluate(context.getTemplateContext()); } else { // use element body as value ByteArrayOutputStream bos = new ByteArrayOutputStream(); @@ -122,21 +122,28 @@ } buffer.endDocument(); buffer.flush(); - String value; try { bos.flush(); - value = bos.toString("UTF-8"); + final String content = bos.toString("UTF-8"); + if (content != null && content.length() > 0) + value = content; } catch (IOException ex) { throw new TemplateException("Error building variable value.", ex); } + } - tmplContext.put(name, value); + // value retrieved from @value or body can be overwritten by loaded data from @src + if (srcExpression != null) { + value = loadFromSrc(context, value); } + + tmplContext.put(name, value); } + return endStep.getCompiledNext(); } - private Object loadFromSrc(ExecutionContext context) { + private Object loadFromSrc(ExecutionContext context, Object fallbackValue) { String src = (String)srcExpression.evaluate(context.getTemplateContext()); String accept = acceptExpression != null ? (String)acceptExpression.evaluate(context.getTemplateContext()) : null; Source source = null; @@ -151,6 +158,9 @@ return loadString(source); } } catch (Throwable e) { + if (fallbackValue != NO_VALUE) + return fallbackValue; + // else throw new TemplateException("Some error occured while loading data for variable " + name + " from URI " + src, e); } finally { if (source != null) {