KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.caucho.util.IntMap;
32
33 import java.util.Iterator JavaDoc;
34 import java.util.regex.Pattern JavaDoc;
35
36 public class ESRegexp extends ESObject {
37   static ESId GLOBAL = ESId.intern("global");
38   static ESId IGNORE_CASE = ESId.intern("ignoreCase");
39   static ESId LAST_INDEX = ESId.intern("lastIndex");
40   static ESId SOURCE = ESId.intern("source");
41
42   ESString pattern;
43   ESString flags;
44   Pattern JavaDoc _regexp;
45
46   boolean hasSetProps;
47   int lastIndex;
48   ESString lastString;
49   int lastStart;
50
51   ESRegexp(ESString pattern, ESString flags) throws ESException
52   {
53     super("RegExp", getPrototype());
54
55     this.pattern = pattern;
56     this.flags = flags;
57     lastString = ESString.NULL;
58
59     /* java.util.regex
60     try {
61       _regexp = Pattern.parse(pattern.toString(), flags.toString());
62     } catch (Exception e) {
63       throw new ESException("regexp: " + e.getMessage());
64     }
65     */

66   }
67
68   public ESRegexp(String JavaDoc pattern, String JavaDoc flags) throws ESException
69   {
70     super("RegExp", getPrototype());
71
72     this.pattern = new ESString(pattern);
73     this.flags = new ESString(flags);
74     lastString = ESString.NULL;
75
76     try {
77       _regexp = Pattern.compile(pattern); // , flags
78
} catch (Exception JavaDoc e) {
79       throw new ESException("regexp: " + e.getMessage());
80     }
81   }
82
83   protected ESRegexp() {}
84
85   private static ESBase getPrototype()
86   {
87     Global resin = Global.getGlobalProto();
88     if (resin == null)
89       return null;
90     else
91       return resin.getRegexpProto();
92   }
93
94   private void setProps()
95   {
96     if (hasSetProps)
97       return;
98
99     int flags = READ_ONLY|DONT_DELETE;
100     hasSetProps = true;
101
102     // java.util.regex
103
// put(GLOBAL, ESBoolean.create(_regexp.isGlobal())); // , flags);
104
// put(IGNORE_CASE, ESBoolean.create(_regexp.ignoreCase())); // , flags);
105
put(LAST_INDEX, ESNumber.create(lastIndex), DONT_DELETE);
106     put(SOURCE, pattern, flags);
107   }
108
109   int getLastIndex() throws Throwable JavaDoc
110   {
111     if (! hasSetProps)
112       return lastIndex;
113     else
114       return getProperty(LAST_INDEX).toInt32();
115   }
116
117   void setLastIndex(int index)
118   {
119     lastIndex = index;
120     hasSetProps = false;
121   }
122
123   public ESBase getProperty(ESString key) throws Throwable JavaDoc
124   {
125     if (! hasSetProps)
126       setProps();
127
128     return super.getProperty(key);
129   }
130
131   public void setProperty(ESString key, ESBase value) throws Throwable JavaDoc
132   {
133     if (! hasSetProps)
134       setProps();
135
136     super.setProperty(key, value);
137   }
138
139   public ESBase delete(ESString key) throws Throwable JavaDoc
140   {
141     if (! hasSetProps)
142       setProps();
143
144     return super.delete(key);
145   }
146
147   public Iterator JavaDoc keys() throws ESException
148   {
149     if (! hasSetProps)
150       setProps();
151
152     return super.keys();
153   }
154
155   public ESString toSource(IntMap map, boolean isLoopPass) throws Throwable JavaDoc
156   {
157     if (isLoopPass)
158       return null;
159     else
160       return toStr();
161   }
162
163   void compile(ESString pattern, ESString flags) throws ESException
164   {
165     if (! this.pattern.equals(pattern) || ! this.flags.equals(flags)) {
166       this.pattern = pattern;
167       this.flags = flags;
168
169       try {
170     // XXX: java.util.regex
171
// _regexp = Pattern.parse(pattern.toString(), flags.toString());
172
} catch (Exception JavaDoc e) {
173     throw new ESException("regexp: " + e);
174       }
175     }
176
177     lastIndex = 0;
178     hasSetProps = false;
179   }
180
181   boolean exec(ESString string, boolean useGlobal) throws Throwable JavaDoc
182   {
183     return false;
184
185     /* change to java.util.regex
186     lastString = string;
187     lastStart = getLastIndex();
188
189     if (! useGlobal) {
190       lastStart = 0;
191       return _regexp.match(string.toString());
192     }
193     else if (regexp.match(string.toString(), lastStart)) {
194       hasSetProps = false;
195       lastIndex = 0;
196
197       return false;
198     } else {
199       hasSetProps = false;
200       lastIndex = regexp.getEnd(0);
201
202       if (regexp.getBegin(0) == lastIndex)
203     lastIndex++;
204
205       return true;
206     }
207     */

208   }
209
210   public Object JavaDoc toJavaObject()
211   {
212     // java.util.regex
213
// return regexp;
214
return null;
215   }
216
217   boolean exec(ESString string) throws Throwable JavaDoc
218   {
219     return false;
220     // java.util.regex
221
// return exec(string, regexp.isGlobal());
222
}
223
224   public ESBase call(Call call, int length) throws Throwable JavaDoc
225   {
226     call.setThis(this);
227
228     return NativeRegexp.exec(call, length);
229   }
230
231    protected ESObject dup() { return new ESRegexp(); }
232  
233    protected void copy(Object JavaDoc newObj)
234    {
235      ESRegexp newRegexp = (ESRegexp) newObj;
236  
237      super.copy(newObj);
238  
239      newRegexp.pattern = pattern;
240      newRegexp.flags = flags;
241      newRegexp.lastString = lastString;
242  
243      // XXX: bogus
244
try {
245        // java.util.regex
246
// newRegexp.regexp = new Regexp(pattern.toString(), flags.toString());
247
} catch (Exception JavaDoc e) {
248      }
249    }
250 }
251
Popular Tags