001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es;
030:
031: /**
032: * JavaScript object
033: */
034: class NativeRegexp extends Native {
035: static ESId INDEX = ESId.intern("index");
036: static ESId INPUT = ESId.intern("input");
037:
038: static final int NEW = 1;
039: static final int COMPILE = NEW + 1;
040: static final int EXEC = COMPILE + 1;
041: static final int TEST = EXEC + 1;
042: static final int TO_STRING = TEST + 1;
043:
044: /**
045: * Create a new object based on a prototype
046: */
047: private NativeRegexp(String name, int n, int len) {
048: super (name, len);
049:
050: this .n = n;
051: }
052:
053: /**
054: * Creates the native Regexp object
055: */
056: static ESRegexpWrapper create(Global resin) {
057: NativeRegexp nativeRegexp = new NativeRegexp("Regexp", NEW, 1);
058: ESRegexp proto;
059:
060: try {
061: proto = new ESRegexp("", "");
062: } catch (Exception e) {
063: throw new RuntimeException();
064: }
065: proto.prototype = resin.objProto;
066: resin.regexpProto = proto;
067: ESRegexpWrapper regexp = new ESRegexpWrapper(resin,
068: nativeRegexp, proto);
069:
070: put(proto, "exec", EXEC, 1);
071: put(proto, "compile", COMPILE, 2);
072: put(proto, "test", TEST, 1);
073: put(proto, "toString", TO_STRING, 0);
074:
075: proto.setClean();
076: regexp.setClean();
077:
078: return regexp;
079: }
080:
081: static private void put(ESObject proto, String name, int n, int len) {
082: ESId id = ESId.intern(name);
083: NativeRegexp fun = new NativeRegexp(name, n, len);
084:
085: proto.put(id, fun, DONT_ENUM);
086: }
087:
088: public ESBase call(Call eval, int length) throws Throwable {
089: switch (n) {
090: case NEW:
091: return create(eval, length);
092:
093: case TO_STRING:
094: try {
095: ESRegexp regexp = (ESRegexp) eval.getThis();
096: String s = regexp.pattern.toString();
097: String f = regexp.flags.toString();
098:
099: return ESString.create("/" + s + "/" + f);
100: } catch (ClassCastException e) {
101: throw new ESException("toString expected regexp object");
102: }
103:
104: case EXEC:
105: return exec(eval, length);
106:
107: case COMPILE:
108: return compile(eval, length);
109:
110: case TEST:
111: return test(eval, length);
112:
113: default:
114: throw new ESException("Unknown object function");
115: }
116: }
117:
118: private ESBase create(Call eval, int length) throws Throwable {
119: ESString pattern;
120: ESString flags = null;
121:
122: if (length == 0)
123: pattern = ESString.NULL;
124: else
125: pattern = eval.getArg(0).toStr();
126:
127: if (length > 1)
128: flags = eval.getArg(1).toStr();
129: else
130: flags = ESString.NULL;
131:
132: ESObject obj;
133: obj = new ESRegexp(pattern, flags);
134:
135: return obj;
136: }
137:
138: private ESBase compile(Call eval, int length) throws Throwable {
139: ESString pattern;
140: ESString flags = null;
141: ESBase arg = eval.getArg(-1);
142:
143: if (arg instanceof ESThunk)
144: arg = ((ESThunk) arg).toObject();
145:
146: if (!(arg instanceof ESRegexp))
147: throw new ESException("compile must be bound to regexp");
148: ESRegexp regexp = (ESRegexp) arg;
149:
150: if (length == 0)
151: return esUndefined;
152: else
153: pattern = eval.getArg(0).toStr();
154:
155: if (length > 1)
156: flags = eval.getArg(1).toStr();
157: else
158: flags = ESString.NULL;
159:
160: regexp.compile(pattern, flags);
161:
162: return regexp;
163: }
164:
165: static ESBase exec(Call eval, int length) throws Throwable {
166: ESBase reg = eval.getArg(-1);
167: ESRegexp regexp;
168:
169: if (reg instanceof ESThunk)
170: reg = ((ESThunk) reg).toObject();
171:
172: if (reg instanceof ESRegexp)
173: regexp = (ESRegexp) reg;
174: else
175: regexp = new ESRegexp(reg.toStr(), ESString.NULL);
176: if (regexp.prototype == null)
177: throw new RuntimeException();
178:
179: ESString string;
180:
181: Global global = Global.getGlobalProto();
182: if (length == 0)
183: string = global.getRegexp().getProperty(
184: global.getRegexp().INPUT).toStr();
185: else
186: string = eval.getArg(0).toStr();
187:
188: global.getRegexp().setRegexp(regexp);
189: if (!regexp.exec(string))
190: return esNull;
191:
192: return esNull;
193: /* java.util.regex
194:
195: ESArray array = global.createArray();
196: for (int i = 0; i < regexp.regexp.length(); i++) {
197: int begin = regexp.regexp.getBegin(i);
198: int end = regexp.regexp.getEnd(i);
199: if (begin < end && begin >= 0)
200: array.setProperty(i, string.substring(begin, end));
201: else
202: array.setProperty(i, ESString.create("")); // XXX: possible?
203: }
204:
205: // java.util.regex
206: // array.setProperty(INDEX, ESNumber.create(regexp.regexp.getBegin(0)));
207: array.setProperty(INPUT, string);
208:
209: return array;
210: */
211: }
212:
213: private ESBase test(Call eval, int length) throws Throwable {
214: ESBase reg = eval.getArg(-1);
215: ESRegexp regexp;
216:
217: if (reg instanceof ESThunk)
218: reg = ((ESThunk) reg).toObject();
219:
220: if (reg instanceof ESRegexp)
221: regexp = (ESRegexp) reg;
222: else
223: regexp = new ESRegexp(reg.toStr(), ESString.NULL);
224: if (regexp.prototype == null)
225: throw new RuntimeException();
226:
227: ESString string;
228: ESRegexpWrapper globalRegexp = Global.getGlobalProto()
229: .getRegexp();
230: if (length == 0)
231: string = globalRegexp.getProperty(globalRegexp.INPUT)
232: .toStr();
233: else
234: string = eval.getArg(0).toStr();
235:
236: globalRegexp.setRegexp(regexp);
237: return ESBoolean.create(regexp.exec(string));
238: }
239: }
|