001: /*=============================================================================
002: * Copyright Texas Instruments 2003. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * $ProjectHeader: OSCRIPT 0.155 Fri, 20 Dec 2002 18:34:22 -0800 rclark $
019: */
020:
021: package oscript.data;
022:
023: import oscript.exceptions.*;
024: import oscript.util.MemberTable;
025: import oscript.*;
026:
027: import java.util.regex.*;
028:
029: /**
030: * An implementation of <code>RegExp</code> that under the hood uses
031: * the <code>java.util.regex</code> package that comes with j2se v1.4
032: *
033: * @author Rob Clark (rob@ti.com)
034: * <!--$Format: " * @version $Revision$"$-->
035: * @version 1.15
036: */
037: public class SunRegExp extends RegExp {
038: public final static Value TYPE = BuiltinType
039: .makeBuiltinType("oscript.data.SunRegExp");
040: public final static String PARENT_TYPE_NAME = RegExp.PARENT_TYPE_NAME;
041: public final static String TYPE_NAME = RegExp.TYPE_NAME;
042: public final static String[] MEMBER_NAMES = RegExp.MEMBER_NAMES;
043:
044: /**
045: * The compiled reg-exp pattern
046: */
047: private Pattern pattern;
048:
049: /*=======================================================================*/
050: /**
051: * Class Constructor.
052: *
053: * @param exp the expression string
054: * @param flags the flags string
055: */
056: private SunRegExp(Value exp, Value oflags) {
057: super (exp, oflags);
058:
059: String str = oflags.castToString();
060: int flags = 0;
061:
062: for (int i = 0; i < str.length(); i++) {
063: switch (str.charAt(i)) {
064: case 'i':
065: flags |= Pattern.CASE_INSENSITIVE;
066: break;
067: case 'I':
068: flags |= Pattern.UNICODE_CASE;
069: break;
070: case 'm':
071: flags |= Pattern.MULTILINE;
072: break;
073: case 'd':
074: flags |= Pattern.DOTALL;
075: break;
076: case 'e':
077: flags |= Pattern.CANON_EQ;
078: break;
079: case 'w':
080: flags |= Pattern.COMMENTS;
081: break;
082: default:
083: throw PackagedScriptObjectException
084: .makeExceptionWrapper(new OIllegalArgumentException(
085: getType() + ": invalid flag: "
086: + str.charAt(i)));
087: }
088: }
089:
090: pattern = Pattern.compile(exp.castToString(), flags);
091: }
092:
093: /*=======================================================================*/
094: /**
095: * Class Constructor. This is the constructor that is called via a
096: * <code>BuiltinType</code> instance.
097: *
098: * @param args arguments to this constructor
099: * @throws PackagedScriptObjectException(Exception) if wrong number of args
100: */
101: public SunRegExp(MemberTable args) {
102: this (getExp(args), getFlags(args));
103: }
104:
105: private static Value getExp(MemberTable args) {
106: if (args.length() == 0)
107: throw PackagedScriptObjectException
108: .makeExceptionWrapper(new OIllegalArgumentException(
109: "wrong number of args!"));
110: return args.referenceAt(0);
111: }
112:
113: private static Value getFlags(MemberTable args) {
114: if (args.length() > 2)
115: throw PackagedScriptObjectException
116: .makeExceptionWrapper(new OIllegalArgumentException(
117: "wrong number of args!"));
118: if (args.length() == 1)
119: return new OString("");
120: return args.referenceAt(1);
121: }
122:
123: /*=======================================================================*/
124: /**
125: * allow SunRegExpResult to access the pattern
126: */
127: Pattern getPattern() {
128: return pattern;
129: }
130:
131: /*=======================================================================*/
132: /**
133: * Called from bootstrap code, to give us change to register this RegExp
134: * implementation
135: */
136: public static void init() {
137: RegExp.register(new RegExpFactory() {
138:
139: /**
140: * Get the type object, used to construct a RegExp object.
141: */
142: public Value getType() {
143: return TYPE;
144: }
145:
146: /**
147: * Construct a RegExp object.
148: *
149: * @param exp the expression string
150: * @param flags the string of zero or more flags
151: * @return the RegExp object
152: */
153: public RegExp createRegExp(Value exp, Value flags) {
154: return new SunRegExp(exp, flags);
155: }
156:
157: });
158: }
159:
160: /*=======================================================================*/
161: /**
162: * Execute the search for a match against a string.
163: * <p>
164: * Note that this API is modeled after the JavaScript RegExp API, for
165: * the benefit of users already familiar with JavaScript.
166: *
167: * @param str the string to match
168: * @return the result object
169: */
170: public RegExpResult exec(Value str) {
171: return new SunRegExpResult(this , str);
172: }
173: }
174:
175: /*
176: * Local Variables:
177: * tab-width: 2
178: * indent-tabs-mode: nil
179: * mode: java
180: * c-indentation-style: java
181: * c-basic-offset: 2
182: * eval: (c-set-offset 'substatement-open '0)
183: * eval: (c-set-offset 'case-label '+)
184: * eval: (c-set-offset 'inclass '+)
185: * eval: (c-set-offset 'inline-open '0)
186: * End:
187: */
|