/*
 * Copyright (c) 2004-2010, Kohsuke Kawaguchi
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
 * that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice, this list of
 *       conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.kohsuke.stapler.jelly;

import org.apache.commons.jelly.JellyTagException;
import org.apache.commons.jelly.XMLOutput;
import org.jvnet.maven.jellydoc.annotation.NoContent;
import org.jvnet.maven.jellydoc.annotation.Required;
import org.kohsuke.stapler.WebApp;
import org.kohsuke.stapler.bind.Bound;
import org.xml.sax.SAXException;

/**
 * Binds a server-side object to client side so that JavaScript can call into server.
 * This tag evaluates to a &lt;script> tag.
 * 
 * @author Kohsuke Kawaguchi
 */
@NoContent
public class BindTag extends AbstractStaplerTag {
    private String varName;
    private Object javaObject;

    /**
     * JavaScript variable name to set the proxy to.
     *
     * This name can be arbitrary left hand side expression,
     * such as "a[0]" or "a.b.c".
     */
    @Required
    public void setVar(String varName) {
        this.varName = varName;
    }

    @Required
    public void setValue(Object o) {
        this.javaObject = o;
    }

    public void doTag(XMLOutput out) throws JellyTagException {
        // make sure we get the supporting script in place
        AdjunctTag a = new AdjunctTag();
        a.setContext(getContext());
        a.setIncludes("org.kohsuke.stapler.bind");
        a.doTag(out);

        try {
            out.startElement("script");
            if (javaObject==null) {
                out.write(varName+"=null;");
            } else {
                Bound h = WebApp.getCurrent().boundObjectTable.bind(javaObject);
                out.write(varName+'='+h.getProxyScript()+';');
            }
            out.endElement("script");
        } catch (SAXException e) {
            throw new JellyTagException(e);
        }
    }
}

@@ -2085,16 +2085,20 @@
 .%0A     *
+ %3Cp%3E
 %0A     * 
@@ -2195,23 +2195,114 @@
    *
-/
 %0A    
-@Required
+ * If this value is unspecified, the tag generates a JavaScript expression to create a proxy.%0A     */
 %0A   
@@ -2753,16 +2753,332 @@
   try %7B%0A
+            if (varName==null) %7B%0A                if (javaObject==null) %7B%0A                    out.write(%22null%22);%0A                %7D else %7B%0A                    Bound h = WebApp.getCurrent().boundObjectTable.bind(javaObject);%0A                    out.write(h.getProxyScript());%0A                %7D%0A            %7D else %7B%0A    
         
@@ -3101,32 +3101,36 @@
 ment(%22script%22);%0A
+    
             if (
@@ -3157,32 +3157,36 @@
                 
+    
 out.write(varNam
@@ -3202,32 +3202,36 @@
 %22);%0A            
+ 
+   
 %7D else %7B%0A       
@@ -3215,32 +3215,36 @@
        %7D else %7B%0A
+    
                 
@@ -3300,32 +3300,36 @@
 nd(javaObject);%0A
+    
                 
@@ -3379,34 +3379,42 @@
 ');%0A            
-%7D%0A
+    %7D%0A    
             out.
@@ -3427,32 +3427,46 @@
 ment(%22script%22);%0A
+            %7D%0A
         %7D catch 

allow varName==null. In fact, this is probably more intuitive