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 java.util.HashMap;
032: import java.util.Iterator;
033:
034: class ESRegexpWrapper extends NativeWrapper {
035: static ESId INPUT = ESId.intern("input");
036: static ESId MULTILINE = ESId.intern("multiline");
037: static ESId LAST_MATCH = ESId.intern("lastMatch");
038: static ESId LAST_PAREN = ESId.intern("lastParen");
039: static ESId LEFT_CONTEXT = ESId.intern("leftContext");
040: static ESId RIGHT_CONTEXT = ESId.intern("rightContext");
041: static ESId G1 = ESId.intern("$1");
042: static ESId G2 = ESId.intern("$2");
043: static ESId G3 = ESId.intern("$3");
044: static ESId G4 = ESId.intern("$4");
045: static ESId G5 = ESId.intern("$5");
046: static ESId G6 = ESId.intern("$6");
047: static ESId G7 = ESId.intern("$7");
048: static ESId G8 = ESId.intern("$8");
049: static ESId G9 = ESId.intern("$9");
050:
051: static HashMap aliases;
052:
053: static {
054: aliases = new HashMap();
055:
056: aliases.put(ESId.intern("$_"), INPUT);
057: aliases.put(ESId.intern("$`"), LEFT_CONTEXT);
058: aliases.put(ESId.intern("$'"), RIGHT_CONTEXT);
059: aliases.put(ESId.intern("$&"), LAST_MATCH);
060: aliases.put(ESId.intern("$0"), LAST_MATCH);
061: aliases.put(ESId.intern("$+"), LAST_PAREN);
062: }
063:
064: ESRegexp regexp;
065:
066: boolean hasSetProps;
067:
068: ESRegexpWrapper(Global resin, Native fun, ESRegexp proto) {
069: super (resin, fun, proto, ESThunk.REGEXP_THUNK);
070:
071: regexp = proto;
072: hasSetProps = false;
073:
074: put(INPUT, ESString.NULL, DONT_DELETE);
075: put(MULTILINE, ESBoolean.FALSE, DONT_DELETE);
076: }
077:
078: protected ESRegexpWrapper() {
079: }
080:
081: private void setProps() {
082: /* convert to java.util.regex
083: if (hasSetProps)
084: return;
085:
086: int flags = READ_ONLY|DONT_DELETE;
087: hasSetProps = true;
088:
089: ESString string = regexp.lastString;
090: Regexp reg = regexp.regexp;
091:
092: put(LAST_MATCH, string.substring(reg.getBegin(0), reg.getEnd(0)), flags);
093: put(G1, string.substring(reg.getBegin(1), reg.getEnd(1)), flags);
094: put(G2, string.substring(reg.getBegin(2), reg.getEnd(2)), flags);
095: put(G3, string.substring(reg.getBegin(3), reg.getEnd(3)), flags);
096: put(G4, string.substring(reg.getBegin(4), reg.getEnd(4)), flags);
097: put(G5, string.substring(reg.getBegin(5), reg.getEnd(5)), flags);
098: put(G6, string.substring(reg.getBegin(6), reg.getEnd(6)), flags);
099: put(G7, string.substring(reg.getBegin(7), reg.getEnd(7)), flags);
100: put(G8, string.substring(reg.getBegin(8), reg.getEnd(8)), flags);
101: put(G9, string.substring(reg.getBegin(9), reg.getEnd(9)), flags);
102:
103: if (reg.length() > 0)
104: put(LAST_PAREN, string.substring(reg.getBegin(reg.length() - 1),
105: reg.getEnd(reg.length() - 1)), flags);
106: else
107: put(LAST_PAREN, string.NULL, flags);
108:
109: if (regexp.lastStart >= string.length())
110: put(LEFT_CONTEXT, ESString.NULL, flags);
111: else
112: put(LEFT_CONTEXT, string.substring(regexp.lastStart, reg.getBegin(0)),
113: flags);
114:
115: put(RIGHT_CONTEXT, string.substring(reg.getEnd(0)), flags);
116: */
117: }
118:
119: void setRegexp(ESRegexp regexp) {
120: this .regexp = regexp;
121: hasSetProps = false;
122: }
123:
124: public ESBase getProperty(ESString key) throws Throwable {
125: if (!hasSetProps)
126: setProps();
127:
128: ESId alias = (ESId) aliases.get(key);
129: return super .getProperty(alias != null ? alias : key);
130: }
131:
132: public void setProperty(ESString key, ESBase value)
133: throws Throwable {
134: if (!hasSetProps && regexp != null)
135: setProps();
136:
137: ESId alias = (ESId) aliases.get(key);
138: super .setProperty(alias != null ? alias : key, value);
139: }
140:
141: public ESBase delete(ESString key) throws Throwable {
142: if (!hasSetProps)
143: setProps();
144:
145: ESId alias = (ESId) aliases.get(key);
146: return super .delete(alias != null ? alias : key);
147: }
148:
149: public Iterator keys() throws ESException {
150: if (!hasSetProps)
151: setProps();
152:
153: return super .keys();
154: }
155:
156: protected ESObject dup() {
157: return new ESRegexpWrapper();
158: }
159:
160: protected void copy(Object obj) {
161: ESRegexpWrapper wrap = (ESRegexpWrapper) obj;
162:
163: super .copy(obj);
164:
165: hasSetProps = false;
166: }
167: }
|