KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > NativeRegexp


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es;
30
31 /**
32  * JavaScript object
33  */

34 class NativeRegexp extends Native {
35   static ESId INDEX = ESId.intern("index");
36   static ESId INPUT = ESId.intern("input");
37
38   static final int NEW = 1;
39   static final int COMPILE = NEW + 1;
40   static final int EXEC = COMPILE + 1;
41   static final int TEST = EXEC + 1;
42   static final int TO_STRING = TEST + 1;
43
44   /**
45    * Create a new object based on a prototype
46    */

47   private NativeRegexp(String JavaDoc name, int n, int len)
48   {
49     super(name, len);
50
51     this.n = n;
52   }
53
54   /**
55    * Creates the native Regexp object
56    */

57   static ESRegexpWrapper create(Global resin)
58   {
59     NativeRegexp nativeRegexp = new NativeRegexp("Regexp", NEW, 1);
60     ESRegexp proto;
61
62     try {
63       proto = new ESRegexp("", "");
64     } catch (Exception JavaDoc e) {
65       throw new RuntimeException JavaDoc();
66     }
67     proto.prototype = resin.objProto;
68     resin.regexpProto = proto;
69     ESRegexpWrapper regexp = new ESRegexpWrapper(resin, nativeRegexp, proto);
70
71     put(proto, "exec", EXEC, 1);
72     put(proto, "compile", COMPILE, 2);
73     put(proto, "test", TEST, 1);
74     put(proto, "toString", TO_STRING, 0);
75
76     proto.setClean();
77     regexp.setClean();
78
79     return regexp;
80   }
81
82   static private void put(ESObject proto, String JavaDoc name, int n, int len)
83   {
84     ESId id = ESId.intern(name);
85     NativeRegexp fun = new NativeRegexp(name, n, len);
86
87     proto.put(id, fun, DONT_ENUM);
88   }
89
90   public ESBase call(Call eval, int length) throws Throwable JavaDoc
91   {
92     switch (n) {
93     case NEW:
94       return create(eval, length);
95
96     case TO_STRING:
97       try {
98         ESRegexp regexp = (ESRegexp) eval.getThis();
99         String JavaDoc s = regexp.pattern.toString();
100         String JavaDoc f = regexp.flags.toString();
101
102         return ESString.create("/" + s + "/" + f);
103       } catch (ClassCastException JavaDoc e) {
104         throw new ESException("toString expected regexp object");
105       }
106
107     case EXEC:
108       return exec(eval, length);
109
110     case COMPILE:
111       return compile(eval, length);
112
113     case TEST:
114       return test(eval, length);
115
116     default:
117       throw new ESException("Unknown object function");
118     }
119   }
120
121   private ESBase create(Call eval, int length) throws Throwable JavaDoc
122   {
123     ESString pattern;
124     ESString flags = null;
125
126     if (length == 0)
127       pattern = ESString.NULL;
128     else
129       pattern = eval.getArg(0).toStr();
130
131     if (length > 1)
132       flags = eval.getArg(1).toStr();
133     else
134       flags = ESString.NULL;
135
136     ESObject obj;
137     obj = new ESRegexp(pattern, flags);
138
139     return obj;
140   }
141
142   private ESBase compile(Call eval, int length) throws Throwable JavaDoc
143   {
144     ESString pattern;
145     ESString flags = null;
146     ESBase arg = eval.getArg(-1);
147
148     if (arg instanceof ESThunk)
149       arg = ((ESThunk) arg).toObject();
150
151     if (! (arg instanceof ESRegexp))
152       throw new ESException("compile must be bound to regexp");
153     ESRegexp regexp = (ESRegexp) arg;
154
155     if (length == 0)
156       return esUndefined;
157     else
158       pattern = eval.getArg(0).toStr();
159
160     if (length > 1)
161       flags = eval.getArg(1).toStr();
162     else
163       flags = ESString.NULL;
164
165     regexp.compile(pattern, flags);
166
167     return regexp;
168   }
169
170   static ESBase exec(Call eval, int length) throws Throwable JavaDoc
171   {
172     ESBase reg = eval.getArg(-1);
173     ESRegexp regexp;
174
175     if (reg instanceof ESThunk)
176       reg = ((ESThunk) reg).toObject();
177
178     if (reg instanceof ESRegexp)
179       regexp = (ESRegexp) reg;
180     else
181       regexp = new ESRegexp(reg.toStr(), ESString.NULL);
182     if (regexp.prototype == null)
183       throw new RuntimeException JavaDoc();
184
185     ESString string;
186
187     Global global = Global.getGlobalProto();
188     if (length == 0)
189       string = global.getRegexp().getProperty(global.getRegexp().INPUT).toStr();
190     else
191       string = eval.getArg(0).toStr();
192
193     global.getRegexp().setRegexp(regexp);
194     if (! regexp.exec(string))
195       return esNull;
196
197     return esNull;
198     /* java.util.regex
199       
200     ESArray array = global.createArray();
201     for (int i = 0; i < regexp.regexp.length(); i++) {
202       int begin = regexp.regexp.getBegin(i);
203       int end = regexp.regexp.getEnd(i);
204       if (begin < end && begin >= 0)
205     array.setProperty(i, string.substring(begin, end));
206       else
207     array.setProperty(i, ESString.create("")); // XXX: possible?
208     }
209
210     // java.util.regex
211     // array.setProperty(INDEX, ESNumber.create(regexp.regexp.getBegin(0)));
212     array.setProperty(INPUT, string);
213
214     return array;
215     */

216   }
217
218   private ESBase test(Call eval, int length) throws Throwable JavaDoc
219   {
220     ESBase reg = eval.getArg(-1);
221     ESRegexp regexp;
222
223     if (reg instanceof ESThunk)
224       reg = ((ESThunk) reg).toObject();
225
226     if (reg instanceof ESRegexp)
227       regexp = (ESRegexp) reg;
228     else
229       regexp = new ESRegexp(reg.toStr(), ESString.NULL);
230     if (regexp.prototype == null)
231       throw new RuntimeException JavaDoc();
232
233     ESString string;
234     ESRegexpWrapper globalRegexp = Global.getGlobalProto().getRegexp();
235     if (length == 0)
236       string = globalRegexp.getProperty(globalRegexp.INPUT).toStr();
237     else
238       string = eval.getArg(0).toStr();
239
240     globalRegexp.setRegexp(regexp);
241     return ESBoolean.create(regexp.exec(string));
242   }
243 }
244
Popular Tags