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: import com.caucho.util.IntMap;
032:
033: import java.util.Iterator;
034: import java.util.regex.Pattern;
035:
036: public class ESRegexp extends ESObject {
037: static ESId GLOBAL = ESId.intern("global");
038: static ESId IGNORE_CASE = ESId.intern("ignoreCase");
039: static ESId LAST_INDEX = ESId.intern("lastIndex");
040: static ESId SOURCE = ESId.intern("source");
041:
042: ESString pattern;
043: ESString flags;
044: Pattern _regexp;
045:
046: boolean hasSetProps;
047: int lastIndex;
048: ESString lastString;
049: int lastStart;
050:
051: ESRegexp(ESString pattern, ESString flags) throws ESException {
052: super ("RegExp", getPrototype());
053:
054: this .pattern = pattern;
055: this .flags = flags;
056: lastString = ESString.NULL;
057:
058: /* java.util.regex
059: try {
060: _regexp = Pattern.parse(pattern.toString(), flags.toString());
061: } catch (Exception e) {
062: throw new ESException("regexp: " + e.getMessage());
063: }
064: */
065: }
066:
067: public ESRegexp(String pattern, String flags) throws ESException {
068: super ("RegExp", getPrototype());
069:
070: this .pattern = new ESString(pattern);
071: this .flags = new ESString(flags);
072: lastString = ESString.NULL;
073:
074: try {
075: _regexp = Pattern.compile(pattern); // , flags
076: } catch (Exception e) {
077: throw new ESException("regexp: " + e.getMessage());
078: }
079: }
080:
081: protected ESRegexp() {
082: }
083:
084: private static ESBase getPrototype() {
085: Global resin = Global.getGlobalProto();
086: if (resin == null)
087: return null;
088: else
089: return resin.getRegexpProto();
090: }
091:
092: private void setProps() {
093: if (hasSetProps)
094: return;
095:
096: int flags = READ_ONLY | DONT_DELETE;
097: hasSetProps = true;
098:
099: // java.util.regex
100: // put(GLOBAL, ESBoolean.create(_regexp.isGlobal())); // , flags);
101: // put(IGNORE_CASE, ESBoolean.create(_regexp.ignoreCase())); // , flags);
102: put(LAST_INDEX, ESNumber.create(lastIndex), DONT_DELETE);
103: put(SOURCE, pattern, flags);
104: }
105:
106: int getLastIndex() throws Throwable {
107: if (!hasSetProps)
108: return lastIndex;
109: else
110: return getProperty(LAST_INDEX).toInt32();
111: }
112:
113: void setLastIndex(int index) {
114: lastIndex = index;
115: hasSetProps = false;
116: }
117:
118: public ESBase getProperty(ESString key) throws Throwable {
119: if (!hasSetProps)
120: setProps();
121:
122: return super .getProperty(key);
123: }
124:
125: public void setProperty(ESString key, ESBase value)
126: throws Throwable {
127: if (!hasSetProps)
128: setProps();
129:
130: super .setProperty(key, value);
131: }
132:
133: public ESBase delete(ESString key) throws Throwable {
134: if (!hasSetProps)
135: setProps();
136:
137: return super .delete(key);
138: }
139:
140: public Iterator keys() throws ESException {
141: if (!hasSetProps)
142: setProps();
143:
144: return super .keys();
145: }
146:
147: public ESString toSource(IntMap map, boolean isLoopPass)
148: throws Throwable {
149: if (isLoopPass)
150: return null;
151: else
152: return toStr();
153: }
154:
155: void compile(ESString pattern, ESString flags) throws ESException {
156: if (!this .pattern.equals(pattern) || !this .flags.equals(flags)) {
157: this .pattern = pattern;
158: this .flags = flags;
159:
160: try {
161: // XXX: java.util.regex
162: // _regexp = Pattern.parse(pattern.toString(), flags.toString());
163: } catch (Exception e) {
164: throw new ESException("regexp: " + e);
165: }
166: }
167:
168: lastIndex = 0;
169: hasSetProps = false;
170: }
171:
172: boolean exec(ESString string, boolean useGlobal) throws Throwable {
173: return false;
174:
175: /* change to java.util.regex
176: lastString = string;
177: lastStart = getLastIndex();
178:
179: if (! useGlobal) {
180: lastStart = 0;
181: return _regexp.match(string.toString());
182: }
183: else if (regexp.match(string.toString(), lastStart)) {
184: hasSetProps = false;
185: lastIndex = 0;
186:
187: return false;
188: } else {
189: hasSetProps = false;
190: lastIndex = regexp.getEnd(0);
191:
192: if (regexp.getBegin(0) == lastIndex)
193: lastIndex++;
194:
195: return true;
196: }
197: */
198: }
199:
200: public Object toJavaObject() {
201: // java.util.regex
202: // return regexp;
203: return null;
204: }
205:
206: boolean exec(ESString string) throws Throwable {
207: return false;
208: // java.util.regex
209: // return exec(string, regexp.isGlobal());
210: }
211:
212: public ESBase call(Call call, int length) throws Throwable {
213: call.setThis(this );
214:
215: return NativeRegexp.exec(call, length);
216: }
217:
218: protected ESObject dup() {
219: return new ESRegexp();
220: }
221:
222: protected void copy(Object newObj) {
223: ESRegexp newRegexp = (ESRegexp) newObj;
224:
225: super .copy(newObj);
226:
227: newRegexp.pattern = pattern;
228: newRegexp.flags = flags;
229: newRegexp.lastString = lastString;
230:
231: // XXX: bogus
232: try {
233: // java.util.regex
234: // newRegexp.regexp = new Regexp(pattern.toString(), flags.toString());
235: } catch (Exception e) {
236: }
237: }
238: }
|