Would be good to avoid duplication - looks similar to code in AbstractSoftwareProcessDriver.processTemplateContents.
Perhaps we should move this to a utility class in utils/common/src/main/java/brooklyn/util/text/. But that would involve adding freemarker as a dependency there. So perhaps a utility class in core:brooklyn.util.text.

Feel free to ignore for this pull request...