batik详解(5) 转载

自定义 Rhino 解释器
一个有用的Rhino解释器自定义的例子来自ECMAScript标准不提供任何预先确定的I/O设备来结合控制台的事实。无论如何,对于ECMAScript兼容语言来提供一个命名为打印输出信息到控制台的功能是非常通用的。在这我们将描述一个Batik Rhino 解释器自定义的例子来添加如下功能。
你应该首先将默认的Batik ECMAScript 解释器子集化以便象下边所示的那样添加功能import org.apache.batik.script.rhino.RhinoInterpreter;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.PropertyException;
public class ExtendedRhinoInterpreter extends RhinoIntepreter {
    public ExtendedRhinoInterpreter() {
        // Array of functions to put in the global object.
        final String[] names = { "print" }
        try {
            // Add the functions to the global object.
            getGlobalObject().defineFunctionProperties
                (names, ExtendedRhinoIntepreter.class,
                 ScriptableObject.DONTENUM);
        } catch (PropertyException e) {
            throw new Error(e);
        }
    }
  
    public static void print(Context cx, Scriptable thisObj,
                             Object[] args, Function funObj) {
        for (int i = 0; i < args.length; i++) {
            if (i > 0) {
                System.out.print(" ");
            }
  
            // Convert the ECMAScript value into a string form.
            String s = Context.toString(args[i]);
            System.out.print(s);
        }
        System.out.println();
    }
}
现在,你需要告诉Batik使用这个解释器代替默认的那个。为了实现这个目的,你必须首先定义一个工厂来创建一个你的解释器实例。
import org.apache.batik.script.Interpreter;
import org.apache.batik.script.InterpreterFactory;
public class ExtendedRhinoInterpreterFactory implements InterpreterFactory {
    public Interpreter createInterpreter() {
        return new ExtendedRhinoInterpreter();
    }
}
然后,你必须构建一个将使用这个工厂的IntepreterPool,并且在你的应用程序的 BridgeContext 上设定池
org.apache.batik.bridge.BridgeContext ctx = ...;
org.apache.batik.script.InterpreterPool pool =
    new org.apache.batik.script.InterpreterPool();
pool.putInterpreterFactory("text/ecmascript",
                           new ExtendedRhinoInterpreterFactory());
ctx.setIntepreterPool(pool);
举个例子,如果你使用Batik SVG 浏览器程序,你应该可以使用在createBridgeContext()方法中的JSVGCanvas类中先前的代码片段。 更深的是用Rhino工作参考Rhino主页
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值