KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 class ESRegexpWrapper extends NativeWrapper {
35   static ESId INPUT = ESId.intern("input");
36   static ESId MULTILINE = ESId.intern("multiline");
37   static ESId LAST_MATCH = ESId.intern("lastMatch");
38   static ESId LAST_PAREN = ESId.intern("lastParen");
39   static ESId LEFT_CONTEXT = ESId.intern("leftContext");
40   static ESId RIGHT_CONTEXT = ESId.intern("rightContext");
41   static ESId G1 = ESId.intern("$1");
42   static ESId G2 = ESId.intern("$2");
43   static ESId G3 = ESId.intern("$3");
44   static ESId G4 = ESId.intern("$4");
45   static ESId G5 = ESId.intern("$5");
46   static ESId G6 = ESId.intern("$6");
47   static ESId G7 = ESId.intern("$7");
48   static ESId G8 = ESId.intern("$8");
49   static ESId G9 = ESId.intern("$9");
50
51   static HashMap JavaDoc aliases;
52
53   static {
54     aliases = new HashMap JavaDoc();
55
56     aliases.put(ESId.intern("$_"), INPUT);
57     aliases.put(ESId.intern("$`"), LEFT_CONTEXT);
58     aliases.put(ESId.intern("$'"), RIGHT_CONTEXT);
59     aliases.put(ESId.intern("$&"), LAST_MATCH);
60     aliases.put(ESId.intern("$0"), LAST_MATCH);
61     aliases.put(ESId.intern("$+"), LAST_PAREN);
62   }
63
64   ESRegexp regexp;
65
66   boolean hasSetProps;
67   
68   ESRegexpWrapper(Global resin, Native fun, ESRegexp proto)
69   {
70     super(resin, fun, proto, ESThunk.REGEXP_THUNK);
71
72     regexp = proto;
73     hasSetProps = false;
74
75     put(INPUT, ESString.NULL, DONT_DELETE);
76     put(MULTILINE, ESBoolean.FALSE, DONT_DELETE);
77   }
78
79   protected ESRegexpWrapper()
80   {
81   }
82
83   private void setProps()
84   {
85     /* convert to java.util.regex
86     if (hasSetProps)
87       return;
88
89     int flags = READ_ONLY|DONT_DELETE;
90     hasSetProps = true;
91
92     ESString string = regexp.lastString;
93     Regexp reg = regexp.regexp;
94
95     put(LAST_MATCH, string.substring(reg.getBegin(0), reg.getEnd(0)), flags);
96     put(G1, string.substring(reg.getBegin(1), reg.getEnd(1)), flags);
97     put(G2, string.substring(reg.getBegin(2), reg.getEnd(2)), flags);
98     put(G3, string.substring(reg.getBegin(3), reg.getEnd(3)), flags);
99     put(G4, string.substring(reg.getBegin(4), reg.getEnd(4)), flags);
100     put(G5, string.substring(reg.getBegin(5), reg.getEnd(5)), flags);
101     put(G6, string.substring(reg.getBegin(6), reg.getEnd(6)), flags);
102     put(G7, string.substring(reg.getBegin(7), reg.getEnd(7)), flags);
103     put(G8, string.substring(reg.getBegin(8), reg.getEnd(8)), flags);
104     put(G9, string.substring(reg.getBegin(9), reg.getEnd(9)), flags);
105
106     if (reg.length() > 0)
107       put(LAST_PAREN, string.substring(reg.getBegin(reg.length() - 1),
108                        reg.getEnd(reg.length() - 1)), flags);
109     else
110       put(LAST_PAREN, string.NULL, flags);
111
112     if (regexp.lastStart >= string.length())
113       put(LEFT_CONTEXT, ESString.NULL, flags);
114     else
115       put(LEFT_CONTEXT, string.substring(regexp.lastStart, reg.getBegin(0)),
116       flags);
117
118     put(RIGHT_CONTEXT, string.substring(reg.getEnd(0)), flags);
119     */

120   }
121
122   void setRegexp(ESRegexp regexp)
123   {
124     this.regexp = regexp;
125     hasSetProps = false;
126   }
127
128   public ESBase getProperty(ESString key) throws Throwable JavaDoc
129   {
130     if (! hasSetProps)
131       setProps();
132
133     ESId alias = (ESId) aliases.get(key);
134     return super.getProperty(alias != null ? alias : key);
135   }
136
137   public void setProperty(ESString key, ESBase value) throws Throwable JavaDoc
138   {
139     if (! hasSetProps && regexp != null)
140       setProps();
141
142     ESId alias = (ESId) aliases.get(key);
143     super.setProperty(alias != null ? alias : key, value);
144   }
145
146   public ESBase delete(ESString key) throws Throwable JavaDoc
147   {
148     if (! hasSetProps)
149       setProps();
150
151     ESId alias = (ESId) aliases.get(key);
152     return super.delete(alias != null ? alias : key);
153   }
154
155   public Iterator JavaDoc keys() throws ESException
156   {
157     if (! hasSetProps)
158       setProps();
159
160     return super.keys();
161   }
162
163   protected ESObject dup() { return new ESRegexpWrapper(); }
164
165   protected void copy(Object JavaDoc obj)
166   {
167     ESRegexpWrapper wrap = (ESRegexpWrapper) obj;
168  
169     super.copy(obj);
170  
171     hasSetProps = false;
172   }
173 }
174
Popular Tags