01: /*
02: * @(#)init.java 1.2 04/12/06
03: *
04: * Copyright (c) 2001-2004 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;
10:
11: import pnuts.lang.Pnuts;
12: import pnuts.lang.Context;
13: import pnuts.lang.PnutsException;
14: import pnuts.ext.ModuleBase;
15: import java.io.InputStream;
16: import java.io.IOException;
17: import java.util.StringTokenizer;
18: import java.util.Enumeration;
19: import java.util.Vector;
20: import java.util.Properties;
21:
22: /**
23: * Initialization of the pnuts.regex module
24: */
25: public class init extends ModuleBase {
26:
27: static String regexAPI;
28:
29: static void useRegexModule(Context context) {
30: if (regexAPI != null) {
31: context.usePackage(regexAPI);
32: return;
33: }
34: String prop = null;
35: try {
36: prop = System.getProperty("pnuts.regex.module");
37: if (context.usePackage(prop, true)) {
38: regexAPI = prop;
39: return;
40: }
41: } catch (Exception e0) {
42: // ignore
43: }
44: if (prop == null) {
45: try {
46: Properties properties = new Properties();
47: InputStream in = init.class.getResource(
48: "regex.properties").openStream();
49: try {
50: properties.load(in);
51: } finally {
52: in.close();
53: }
54: prop = (String) properties.get("pnuts.regex.modules");
55: } catch (IOException e) {
56: /* ignore */
57: }
58: }
59: if (prop != null) {
60: Vector v = new Vector();
61: StringTokenizer st = new StringTokenizer(prop, ";,");
62: while (st.hasMoreTokens()) {
63: v.addElement(st.nextToken());
64: }
65: for (Enumeration e = v.elements(); e.hasMoreElements();) {
66: String modname = (String) e.nextElement();
67: try {
68: if (context.usePackage(modname, true)) {
69: regexAPI = modname;
70: break;
71: }
72: } catch (Exception ex) {
73: // ignore
74: }
75: }
76: } else {
77: throw new PnutsException("no.regex.modules", context);
78: }
79: }
80:
81: public Object execute(Context context) {
82: useRegexModule(context);
83: return null;
84: }
85: }
|