Update of /cvsroot/xdoclet/xdoclet/modules/apache/src/xdoclet/modules/apache/bsf
In directory sc8-pr-cvs1:/tmp/cvs-serv553/bsf

Added Files:
        BsfEngineTagHandler.java BsfSubTemplateEngine.java 
Log Message:
add BSF, refactoring common pieces with Velocity tag handler

--- NEW FILE: BsfEngineTagHandler.java ---
/*
 * Copyright (c) 2001, 2002 The XDoclet team
 * All rights reserved.
 */
package xdoclet.modules.apache.bsf;

import java.util.Properties;

import org.apache.commons.logging.Log;
import xdoclet.XDocletException;
import xdoclet.modules.apache.ScriptEngineTagHandler;
import xdoclet.modules.apache.SubTemplateEngine;

import xdoclet.tagshandler.AbstractProgramElementTagsHandler;
import xdoclet.template.TemplateEngine;
import xdoclet.template.TemplateException;
import xdoclet.util.LogUtil;

/**
 * This is a tag handler able to execute BSF engine on a block!
 *
 * @author               zluspai
 * @created              July 16, 2003
 * @xdoclet.taghandler   namespace="Bsf"
 */
public class BsfEngineTagHandler extends ScriptEngineTagHandler
{
    private BsfSubTemplateEngine bsfEngine;

    /**
     * Get a value of a BSF variable
     *
     * @param attributes
     * @return
     * @exception XDocletException
     * @doc.tag                     type="content"
     */
    public String getVariable(Properties attributes) throws XDocletException
    {
        return getSubTemplateVariable(getBsfEngine(), attributes);
    }

    /**
     * Evaluates the body block with the BSF script If the silent="yes" attribute is 
set then the Generator will not
     * produce any output, but the template will run. If the disable="yes" attribute 
is set then the Velocity template
     * will not run at all.
     *
     * @param template               The body of the block tag
     * @param attributes             The attributes of the template tag
     * @exception TemplateException
     * @doc.tag                      type="block"
     */
    public void generator(String template, Properties attributes) throws 
TemplateException
    {
        Log log = LogUtil.getLog(BsfEngineTagHandler.class, "generator");

        log.debug("generator() called.");
        generate(getBsfEngine(), template, attributes);
    }

    /**
     * Clear all BSF variables
     *
     * @doc.tag                  type="content"
     * @throws XDocletException
     */
    public void clearVariables() throws XDocletException
    {
        getBsfEngine().clearVariables();
    }

    private BsfSubTemplateEngine getBsfEngine()
    {
        if (bsfEngine == null) {
            bsfEngine = new BsfSubTemplateEngine();
        }
        return bsfEngine;
    }
}


--- NEW FILE: BsfSubTemplateEngine.java ---
/*
 * Copyright (c) 2001, 2002 The XDoclet team
 * All rights reserved.
 */
package xdoclet.modules.apache.bsf;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import java.util.Properties;
import org.apache.bsf.BSFEngine;
import org.apache.bsf.BSFException;
import org.apache.bsf.BSFManager;
import org.apache.bsf.util.ObjectRegistry;
import org.apache.commons.logging.Log;
import xdoclet.XDocletException;

import xdoclet.modules.apache.SubTemplateEngine;
import xdoclet.util.LogUtil;

/**
 * Template engine uses a BSF script to generate output
 *
 * @author    zluspai
 * @created   July 16, 2003
 */
public class BsfSubTemplateEngine implements SubTemplateEngine
{
    /*
     * The scripts share the same namespaces, and should use the
     * bsf.lookupBean() to access the objects. The generated script should be written 
to the 'out' PrintStream. An example
     * to use it: <pre>
     * <XDtTemplateEngines:BSFGenerator scriptengine="javascript" >
     * out =  bsf.lookupBean ("out");
     * out.println ("Hello World from BSF script!");
     * print out the methods of the current class
     * currentClass = bsf.lookupBean ("currentClass");
     * out.println ("Current class:"+currentClass.getName());
     * out.println ("Methods are:");
     * imethods = currentClass.getMethods().iterator();
     * while (imethods.hasNext()) {
     * method = imethods.next();
     * out.println ("   "+method.getName());
     * }
     * </XDtTemplateEngines:BSFGenerator> </pre>
     */
    private BSFManager bsfManager;


    public BsfSubTemplateEngine()
    {
        Log log = LogUtil.getLog(BsfEngineTagHandler.class, "ctor");

        try {
            log.debug("Creating BSFManager()");
            bsfManager = new BSFManager();
        }
        catch (Exception ex) {
            log.error("Exception when creating BSFManager", ex);
        }
    }

    /*
     * (non-Javadoc)
     * @see xdoclet.templateutil.SubTemplateEngine#getVariable(java.lang.String)
     */
    public Object getVariable(String name)
    {
        ObjectRegistry objreg = bsfManager.getObjectRegistry();

        return objreg.lookup(name);
    }

    /*
     * (non-Javadoc)
     * @see xdoclet.templateutil.SubTemplateEngine#setVariable(java.lang.String, 
java.lang.Object)
     */
    public void setVariable(String name, Object value)
    {
        // can't store null value in BSF registry
        if (value != null) {
            ObjectRegistry objreg = bsfManager.getObjectRegistry();

            objreg.register(name, value);
        }
    }

    /*
     * (non-Javadoc)
     * @see xdoclet.templateutil.SubTemplateEngine#clearVariables()
     */
    public void clearVariables()
    {
        bsfManager.setObjectRegistry(new ObjectRegistry());
    }

    /*
     * (non-Javadoc)
     * @see xdoclet.templateutil.SubTemplateEngine#generate(java.lang.String, 
java.util.Properties)
     */
    public String generate(String template, Properties attributes) throws 
XDocletException
    {
        Log log = LogUtil.getLog(BsfEngineTagHandler.class, "generate");

        log.debug("generate() called with attributes:" + attributes);

        String scriptingEngine = attributes.getProperty("scriptengine");

        if (scriptingEngine == null) {
            throw new XDocletException("Missing attribute 'scriptengine' specifying 
BSF script language");
        }

        // create 'out' object
        ByteArrayOutputStream bbuf = new ByteArrayOutputStream();
        PrintStream out = new PrintStream(bbuf);

        try {
            BSFEngine bsfEngine = bsfManager.loadScriptingEngine(scriptingEngine);

            bsfManager.getObjectRegistry().register("out", out);
            bsfEngine.exec("", 0, 0, template);

        }
        catch (BSFException e) {
            throw new XDocletException(e, "Exception when running scriptengine='" + 
scriptingEngine + "'");
        }
        out.flush();
        return bbuf.toString();
    }

}



-------------------------------------------------------
This SF.net email is sponsored by: VM Ware
With VMware you can run multiple operating systems on a single machine.
WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
same time. Free trial click here: http://www.vmware.com/wl/offer/345/0
_______________________________________________
xdoclet-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel

Reply via email to