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.*;
025:
026: import java.util.regex.*;
027:
028: /**
029: * The implementation of {@link RegExpResult} using Sun's <code>java.util.regex</code>
030: * package that comes with j2se v1.4 and later.
031: *
032: * @author Rob Clark (rob@ti.com)
033: * <!--$Format: " * @version $Revision$"$-->
034: * @version 1.15
035: */
036: public class SunRegExpResult extends RegExpResult {
037: private Matcher matcher;
038:
039: /*=======================================================================*/
040: /**
041: * Class Constructor.
042: *
043: * @param input the string to match the regular expression against
044: * @param regexp the regular expression object that constructed this
045: */
046: SunRegExpResult(SunRegExp regexp, Value input) {
047: super (input);
048:
049: // XXX could be slick here, and avoid flattening string...
050: matcher = regexp.getPattern().matcher(input.castToString());
051:
052: if (!matcher.find())
053: matcher = null;
054: }
055:
056: /*=======================================================================*/
057: /**
058: * The index of the match in the string, or <code>-1</code> if no match.
059: */
060: public Value getIndex() {
061: int idx = (matcher != null) ? matcher.start() : -1;
062: return JavaBridge.convertToScriptObject(idx);
063: }
064:
065: /*=======================================================================*/
066: /**
067: * For types that implement <code>elementAt</code>, this returns the
068: * number of elements.
069: *
070: * @return an integer length
071: * @throws PackagedScriptObjectException(NoSuchMemberException)
072: * @see #elementAt
073: * @see #elementsAt
074: */
075: public int length() throws PackagedScriptObjectException {
076: if (matcher == null)
077: return 0;
078:
079: return matcher.groupCount() + 1;
080: }
081:
082: /*=======================================================================*/
083: /**
084: * Get the specified index of this object, if this object is an array. If
085: * needed, the array is grown to the appropriate size.
086: *
087: * @param idx the index to get
088: * @return a reference to the member
089: * @throws PackagedScriptObjectException(NoSuchMemberException)
090: * @see #length
091: */
092: public Value elementAt(Value oidx)
093: throws PackagedScriptObjectException {
094: if (matcher == null)
095: throw noSuchMember("elementAt");
096: int idx = (int) (oidx.castToExactNumber());
097: if ((idx < 0) || (idx >= length()))
098: throw PackagedScriptObjectException
099: .makeExceptionWrapper(new OIllegalArgumentException(
100: "invalid array index: " + idx));
101: return JavaBridge.convertToScriptObject(matcher.group(idx));
102: }
103:
104: /*=======================================================================*/
105: /**
106: * Convert this object to a native java <code>String</code> value.
107: *
108: * @return a String value
109: * @throws PackagedScriptObjectException(NoSuchMethodException)
110: */
111: public String castToString() throws PackagedScriptObjectException {
112: String str = "[";
113:
114: for (int i = 0; i < length(); i++) {
115: if (i != 0)
116: str += ", ";
117: str += matcher.group(i);
118: }
119:
120: return str + "]";
121: }
122: }
123:
124: /*
125: * Local Variables:
126: * tab-width: 2
127: * indent-tabs-mode: nil
128: * mode: java
129: * c-indentation-style: java
130: * c-basic-offset: 2
131: * eval: (c-set-offset 'substatement-open '0)
132: * eval: (c-set-offset 'case-label '+)
133: * eval: (c-set-offset 'inclass '+)
134: * eval: (c-set-offset 'inline-open '0)
135: * End:
136: */
|