// Path: src/main/java/org/haplo/template/html/Context.java
// public enum Context {
//     UNSAFE,     // no escaping
//     FUNCTION_ARGUMENTS,
//     TEXT,
//     TAG,
//     ATTRIBUTE_VALUE,
//     COMMENT,
//     URL,
//     URL_PATH
// }
// 
// Path: src/main/java/org/haplo/template/html/RenderException.java
// public class RenderException extends Exception {
//     private Driver driver;
// 
//     public RenderException(Driver driver, String message) {
//         super(message);
//         this.driver = driver;
//     }
// 
//     public String getMessage() {
//         String templateName = "(no template)";
//         if(this.driver != null) {
//             Template template = driver.getLastTemplate();
//             if(template != null) {
//                 templateName = template.getName();
//             }
//         }
//         return "When rendering template '"+templateName+"': "+super.getMessage();
//     }
// }


import org.haplo.template.html.Context;
import org.haplo.template.html.DeferredRender;
import org.haplo.template.html.RenderException;
import org.mozilla.javascript.ScriptableObject;
/* Haplo Safe View Templates                          http://haplo.org
 * (c) Haplo Services Ltd 2015 - 2016    http://www.haplo-services.com
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.         */

package org.haplo.template.driver.rhinojs;



public class HaploTemplateDeferredRender extends ScriptableObject implements DeferredRender {
    private DeferredRender deferredRender;

    public String getClassName() {
        return "$HaploTemplateDeferredRender";
    }

    public void setDeferredRender(DeferredRender deferredRender) {
        this.deferredRender = deferredRender;
    }

    private String _renderToString() throws RenderException {
        StringBuilder builder = new StringBuilder();
        if(this.deferredRender != null) {
            this.deferredRender.renderDeferred(builder, Context.TEXT);
        }
        return builder.toString();
    }

    public String jsFunction_toString() throws RenderException {
        return this._renderToString();
    }

    public void renderDeferred(StringBuilder builder, Context context) throws RenderException {
        if(this.deferredRender != null) {
            this.deferredRender.renderDeferred(builder, context);
        }
    }

    // ----------------------------------------------------------------------

    // immediate() returns another 'deferred' render which has been pre-rendered
    public HaploTemplateDeferredRender jsFunction_immediate() throws RenderException {
        // Generate a new deferred containing the rendered text
        Immediate immediate = new Immediate(this._renderToString());

        HaploTemplateDeferredRender deferred =
// Path: src/main/java/org/haplo/template/html/Context.java
// public enum Context {
//     UNSAFE,     // no escaping
//     FUNCTION_ARGUMENTS,
//     TEXT,
//     TAG,
//     ATTRIBUTE_VALUE,
//     COMMENT,
//     URL,
//     URL_PATH
// }
// 
// Path: src/main/java/org/haplo/template/html/RenderException.java
// public class RenderException extends Exception {
//     private Driver driver;
// 
//     public RenderException(Driver driver, String message) {
//         super(message);
//         this.driver = driver;
//     }
// 
//     public String getMessage() {
//         String templateName = "(no template)";
//         if(this.driver != null) {
//             Template template = driver.getLastTemplate();
//             if(template != null) {
//                 templateName = template.getName();
//             }
//         }
//         return "When rendering template '"+templateName+"': "+super.getMessage();
//     }
// }


// Path: src/main/java/org/haplo/template/driver/rhinojs/HaploTemplateDeferredRender.java
import org.haplo.template.html.Context;
import org.haplo.template.html.DeferredRender;
import org.haplo.template.html.RenderException;
import org.mozilla.javascript.ScriptableObject;

/* Haplo Safe View Templates                          http://haplo.org
 * (c) Haplo Services Ltd 2015 - 2016    http://www.haplo-services.com
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.         */

package org.haplo.template.driver.rhinojs;



public class HaploTemplateDeferredRender extends ScriptableObject implements DeferredRender {
    private DeferredRender deferredRender;

    public String getClassName() {
        return "$HaploTemplateDeferredRender";
    }

    public void setDeferredRender(DeferredRender deferredRender) {
        this.deferredRender = deferredRender;
    }

    private String _renderToString() throws RenderException {
        StringBuilder builder = new StringBuilder();
        if(this.deferredRender != null) {
            this.deferredRender.renderDeferred(builder, Context.TEXT);
        }
        return builder.toString();
    }

    public String jsFunction_toString() throws RenderException {
        return this._renderToString();
    }

    public void renderDeferred(StringBuilder builder, Context context) throws RenderException {
        if(this.deferredRender != null) {
            this.deferredRender.renderDeferred(builder, context);
        }
    }

    // ----------------------------------------------------------------------

    // immediate() returns another 'deferred' render which has been pre-rendered
    public HaploTemplateDeferredRender jsFunction_immediate() throws RenderException {
        // Generate a new deferred containing the rendered text
        Immediate immediate = new Immediate(this._renderToString());

        HaploTemplateDeferredRender deferred =
            (HaploTemplateDeferredRender)org.mozilla.javascript.Context.getCurrentContext().