| 1 | Index: universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src_result.xml |
|---|
| 2 | =================================================================== |
|---|
| 3 | --- universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src_result.xml (revision 1434) |
|---|
| 4 | +++ universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src_result.xml (working copy) |
|---|
| 5 | @@ -1,4 +1,4 @@ |
|---|
| 6 | <?xml version="1.0" encoding="UTF-8"?> |
|---|
| 7 | <root xmlns:ktl="http://kauriproject.org/template"> |
|---|
| 8 | - Hello world!,local,overwriteTest2 - external value |
|---|
| 9 | + Hello world!,Hello world!,local,overwriteTest2 - external value |
|---|
| 10 | </root> |
|---|
| 11 | \ No newline at end of file |
|---|
| 12 | Index: universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src.xml |
|---|
| 13 | =================================================================== |
|---|
| 14 | --- universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src.xml (revision 1434) |
|---|
| 15 | +++ universe/kauri-template/src/test/resources/org/kauriproject/template/variable_from_src.xml (working copy) |
|---|
| 16 | @@ -1,8 +1,9 @@ |
|---|
| 17 | <?xml version="1.0" encoding="UTF-8"?> |
|---|
| 18 | <root xmlns:ktl="http://kauriproject.org/template"> |
|---|
| 19 | - <ktl:variable name="data" src="data.json"/> |
|---|
| 20 | + <ktl:variable name="data" src="data.json" value="ignore"/> |
|---|
| 21 | + <ktl:variable name="data2" src="nodata.json" value="${data}"/> |
|---|
| 22 | <ktl:variable name="overwriteTest1" value="local"/> |
|---|
| 23 | <ktl:variable name="overwriteTest2" value="local" overwrite="false"/> |
|---|
| 24 | |
|---|
| 25 | - ${data.message},${overwriteTest1},${overwriteTest2} |
|---|
| 26 | + ${data.message},${data2.message},${overwriteTest1},${overwriteTest2} |
|---|
| 27 | </root> |
|---|
| 28 | \ No newline at end of file |
|---|
| 29 | Index: universe/kauri-template/src/main/java/org/kauriproject/template/VariableBlock.java |
|---|
| 30 | =================================================================== |
|---|
| 31 | --- universe/kauri-template/src/main/java/org/kauriproject/template/VariableBlock.java (revision 1434) |
|---|
| 32 | +++ universe/kauri-template/src/main/java/org/kauriproject/template/VariableBlock.java (working copy) |
|---|
| 33 | @@ -58,6 +58,8 @@ |
|---|
| 34 | return new EndStep(locator); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | + private static final Object NO_VALUE = new Object(); |
|---|
| 38 | + |
|---|
| 39 | class StartStep extends Step { |
|---|
| 40 | |
|---|
| 41 | // variable stuff |
|---|
| 42 | @@ -100,16 +102,14 @@ |
|---|
| 43 | overwrite = !"false".equalsIgnoreCase(attributes.getValue(OVERWRITE)); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | + |
|---|
| 47 | @Override |
|---|
| 48 | public Step executeAndProceed(ExecutionContext context, TemplateResult result) throws SAXException { |
|---|
| 49 | TemplateContext tmplContext = context.getTemplateContext(); |
|---|
| 50 | if (overwrite || !tmplContext.containsKey(name)) { |
|---|
| 51 | + Object value = NO_VALUE; |
|---|
| 52 | if (valueExpression != null) { |
|---|
| 53 | - final Object value = valueExpression.evaluate(context.getTemplateContext()); |
|---|
| 54 | - tmplContext.put(name, value); |
|---|
| 55 | - } else if (srcExpression != null) { |
|---|
| 56 | - Object data = loadFromSrc(context); |
|---|
| 57 | - tmplContext.put(name, data); |
|---|
| 58 | + value = valueExpression.evaluate(context.getTemplateContext()); |
|---|
| 59 | } else { |
|---|
| 60 | // use element body as value |
|---|
| 61 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
|---|
| 62 | @@ -122,21 +122,28 @@ |
|---|
| 63 | } |
|---|
| 64 | buffer.endDocument(); |
|---|
| 65 | buffer.flush(); |
|---|
| 66 | - String value; |
|---|
| 67 | try { |
|---|
| 68 | bos.flush(); |
|---|
| 69 | - value = bos.toString("UTF-8"); |
|---|
| 70 | + final String content = bos.toString("UTF-8"); |
|---|
| 71 | + if (content != null && content.length() > 0) |
|---|
| 72 | + value = content; |
|---|
| 73 | } catch (IOException ex) { |
|---|
| 74 | throw new TemplateException("Error building variable value.", ex); |
|---|
| 75 | } |
|---|
| 76 | + } |
|---|
| 77 | |
|---|
| 78 | - tmplContext.put(name, value); |
|---|
| 79 | + // value retrieved from @value or body can be overwritten by loaded data from @src |
|---|
| 80 | + if (srcExpression != null) { |
|---|
| 81 | + value = loadFromSrc(context, value); |
|---|
| 82 | } |
|---|
| 83 | + |
|---|
| 84 | + tmplContext.put(name, value); |
|---|
| 85 | } |
|---|
| 86 | + |
|---|
| 87 | return endStep.getCompiledNext(); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | - private Object loadFromSrc(ExecutionContext context) { |
|---|
| 91 | + private Object loadFromSrc(ExecutionContext context, Object fallbackValue) { |
|---|
| 92 | String src = (String)srcExpression.evaluate(context.getTemplateContext()); |
|---|
| 93 | String accept = acceptExpression != null ? (String)acceptExpression.evaluate(context.getTemplateContext()) : null; |
|---|
| 94 | Source source = null; |
|---|
| 95 | @@ -151,6 +158,9 @@ |
|---|
| 96 | return loadString(source); |
|---|
| 97 | } |
|---|
| 98 | } catch (Throwable e) { |
|---|
| 99 | + if (fallbackValue != NO_VALUE) |
|---|
| 100 | + return fallbackValue; |
|---|
| 101 | + // else |
|---|
| 102 | throw new TemplateException("Some error occured while loading data for variable " + name + " from URI " + src, e); |
|---|
| 103 | } finally { |
|---|
| 104 | if (source != null) { |
|---|