01: /*
02: * @(#)init.java 1.2 04/12/06
03: *
04: * Copyright (c) 2001-2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.regex.jsr51;
10:
11: import pnuts.lang.Runtime;
12: import pnuts.lang.Package;
13: import pnuts.lang.Context;
14: import pnuts.lang.AutoloadHook;
15: import pnuts.lang.PnutsException;
16: import pnuts.ext.ModuleBase;
17: import java.io.Serializable;
18:
19: public class init extends ModuleBase {
20:
21: static String[] functions = { "match", "split", "substitute",
22: "regex", "getMatch", "getMatches", "getMatchStart",
23: "getMatchEnd", "getMatchCount", "getNumberOfGroups",
24: "whichRegexAPI", "matchAll", "formatMatch" };
25:
26: public Object execute(Context context) {
27: try {
28: Class.forName("java.util.regex.Pattern");
29: } catch (ClassNotFoundException e) {
30: throw new PnutsException(e, context);
31: }
32: Package pkg = Package.getPackage("pnuts.regex.jsr51", context);
33: AutoloadHook ah = new HelperHook(pkg);
34: context.clearPackages();
35: for (int i = 0; i < functions.length; i++) {
36: context.autoload(functions[i], ah);
37: }
38: return null;
39: }
40:
41: static class HelperHook implements AutoloadHook, Serializable {
42: Package pkg;
43:
44: HelperHook(Package pkg) {
45: this .pkg = pkg;
46: }
47:
48: public void load(String name, Context context) {
49: PatternMatchHelper helper = new PatternMatchHelper();
50: helper.registerAll(pkg, context);
51: pkg.export(name);
52: }
53: }
54: }
|