Code example for StringWriter

Methods: toString, write

        if (root == null) {
            throw new IOException("Parsing XML source failed: root node is null.");
        StringWriter writer = new StringWriter(1024);
        writer.write("/* Generated by XmlTemplateEngine */\n");
        new GspPrinter(new PrintWriter(writer), indentation).print(root);
        Script script;
        try { 
            script = groovyShell.parse(writer.toString(), "XmlTemplateScript" + counter++ + ".groovy");
        } catch (Exception e) {
            throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
        return new XmlTemplate(script);
    public String getIndentation() {
        return indentation;