KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > modules > sre > MatchObject


1 /*
2  * Copyright 2000 Finn Bock
3  *
4  * This program contains material copyrighted by:
5  * Copyright (c) 1997-2000 by Secret Labs AB. All rights reserved.
6  *
7  * This version of the SRE library can be redistributed under CNRI's
8  * Python 1.6 license. For any other use, please contact Secret Labs
9  * AB (info@pythonware.com).
10  *
11  * Portions of this engine have been developed in cooperation with
12  * CNRI. Hewlett-Packard provided funding for 1.6 integration and
13  * other compatibility work.
14  */

15
16 package org.python.modules.sre;
17
18 import org.python.core.*;
19
20
21 public class MatchObject extends PyObject {
22     public String JavaDoc string; /* link to the target string */
23     public PyObject regs; /* cached list of matching spans */
24     PatternObject pattern; /* link to the regex (pattern) object */
25     int pos, endpos; /* current target slice */
26     int lastindex; /* last index marker seen by the engine (-1 if none) */
27     int groups; /* number of groups (start/end marks) */
28     int[] mark;
29
30
31     public PyObject group(PyObject[] args) {
32         switch (args.length) {
33         case 0:
34             return getslice(Py.Zero, Py.None);
35         case 1:
36             return getslice(args[0], Py.None);
37         default:
38             PyObject[] result = new PyObject[args.length];
39             for (int i = 0; i < args.length; i++)
40                 result[i] = getslice(args[i], Py.None);
41             return new PyTuple(result);
42         }
43     }
44
45     public PyObject groups(PyObject[] args, String JavaDoc[] kws) {
46         ArgParser ap = new ArgParser("groups", args, kws, "default");
47         PyObject def = ap.getPyObject(0, Py.None);
48
49         PyObject[] result = new PyObject[groups-1];
50         for (int i = 1; i < groups; i++) {
51             result[i-1] = getslice_by_index(i, def);
52         }
53         return new PyTuple(result);
54     }
55
56     public PyObject groupdict(PyObject[] args, String JavaDoc[] kws) {
57         ArgParser ap = new ArgParser("groupdict", args, kws, "default");
58         PyObject def = ap.getPyObject(0, Py.None);
59
60         PyObject result = new PyDictionary();
61
62         if (pattern.groupindex == null)
63             return result;
64
65         PyObject keys = pattern.groupindex.invoke("keys");
66
67         PyObject key;
68         for (int i = 0; (key = keys.__finditem__(i)) != null; i++) {
69             PyObject item = getslice(key, def);
70             result.__setitem__(key, item);
71         }
72         return result;
73     }
74
75     public PyObject start() {
76         return start(Py.Zero);
77     }
78
79     public PyObject start(PyObject index_) {
80         int index = getindex(index_);
81
82         if (index < 0 || index >= groups)
83             throw Py.IndexError("no such group");
84
85         return Py.newInteger(mark[index*2]);
86     }
87
88     public PyObject end() {
89         return end(Py.Zero);
90     }
91
92     public PyObject end(PyObject index_) {
93         int index = getindex(index_);
94
95         if (index < 0 || index >= groups)
96             throw Py.IndexError("no such group");
97
98         return Py.newInteger(mark[index*2+1]);
99     }
100
101     public PyTuple span() {
102         return span(Py.Zero);
103     }
104
105     public PyTuple span(PyObject index_) {
106         int index = getindex(index_);
107
108         if (index < 0 || index >= groups)
109             throw Py.IndexError("no such group");
110
111         int start = mark[index*2];
112         int end = mark[index*2+1];
113
114         return _pair(start, end);
115     }
116
117     public PyObject regs() {
118
119         PyObject[] regs = new PyObject[groups];
120
121         for (int index = 0; index < groups; index++) {
122             regs[index] = _pair(mark[index*2], mark[index*2+1]);
123         }
124
125         return new PyTuple(regs);
126     }
127
128
129     PyTuple _pair(int i1, int i2) {
130         return new PyTuple(new PyObject[] {
131             Py.newInteger(i1), Py.newInteger(i2)
132         });
133     }
134
135     private PyObject getslice(PyObject index, PyObject def) {
136         return getslice_by_index(getindex(index), def);
137     }
138
139     private int getindex(PyObject index) {
140         if (index instanceof PyInteger)
141             return ((PyInteger) index).getValue();
142
143         int i = -1;
144
145         if (pattern.groupindex != null) {
146             index = pattern.groupindex.__finditem__(index);
147             if (index != null)
148                 if (index instanceof PyInteger)
149                     return ((PyInteger) index).getValue();
150         }
151         return i;
152     }
153
154     private PyObject getslice_by_index(int index, PyObject def) {
155         if (index < 0 || index >= groups)
156             throw Py.IndexError("no such group");
157
158         index *= 2;
159         int start = mark[index];
160         int end = mark[index+1];
161
162         //System.out.println("group:" + index + " " + start + " " +
163
// end + " l:" + string.length());
164

165         if (string == null || start < 0)
166             return def;
167
168         return new PyString(string.substring(start, end));
169     }
170
171     public PyObject __findattr__(String JavaDoc key) {
172         //System.out.println("__findattr__:" + key);
173
if (key == "flags")
174             return Py.newInteger(pattern.flags);
175         if (key == "groupindex")
176             return pattern.groupindex;
177         if (key == "re")
178             return pattern;
179         if (key == "pos")
180             return Py.newInteger(pos);
181         if (key == "endpos")
182             return Py.newInteger(endpos);
183         if (key == "lastindex")
184             return lastindex == -1 ? Py.None : Py.newInteger(lastindex);
185         return super.__findattr__(key);
186     }
187 }
188
Popular Tags