KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.modules;
3
4 import org.python.core.*;
5 import org.apache.oro.text.regex.*;
6
7 public class MatchObject extends PyObject
8 {
9     public String JavaDoc string;
10     public int pos, endpos;
11     public RegexObject re;
12     private MatchResult match;
13
14     public MatchObject(RegexObject re, String JavaDoc string,
15                        int pos, int endpos, MatchResult match)
16     {
17         this.string = string;
18         this.pos = pos;
19         this.endpos = endpos;
20         this.re = re;
21         this.match = match;
22     }
23
24     public int start(int g) {
25         return match.beginOffset(g);
26     }
27
28     public int start() {
29         return start(0);
30     }
31
32     public int start(PyString s) {
33         return start(getindex(s));
34     }
35
36     public int end(int g) {
37         return match.endOffset(g);
38     }
39
40     public int end() {
41         return end(0);
42     }
43
44     public int end(PyString s) {
45         return end(getindex(s));
46     }
47
48     public PyTuple span(int g) {
49         return new PyTuple(
50             new PyObject[] {
51                 new PyInteger(start(g)),
52                 new PyInteger(end(g))
53             });
54     }
55
56     public PyTuple span() {
57         return span(0);
58     }
59
60     public PyTuple span(PyString s) {
61         return span(getindex(s));
62     }
63
64     public PyTuple groups(PyObject defalt) {
65         int n = match.groups()-1;
66         PyObject[] ret = new PyObject[n];
67         for(int i=0; i<n; i++) {
68             String JavaDoc tmp = match.group(i+1);
69             if (tmp == null) {
70                 ret[i] = defalt;
71             } else {
72                 ret[i] = new PyString(tmp);
73             }
74         }
75         return new PyTuple(ret);
76     }
77
78     public PyTuple groups() {
79         return groups(Py.None);
80     }
81
82     private int getindex(PyString s) {
83         PyInteger v = (PyInteger)re.groupindex.__finditem__(s);
84         if (v == null)
85             throw Py.IndexError("group '"+s+"' is undefined");
86         return v.getValue();
87     }
88
89     private String JavaDoc group(int i) {
90         if (i >= match.groups()) {
91             throw Py.IndexError("group "+i+" is undefined");
92         }
93         return match.group(i);
94     }
95
96     private String JavaDoc group(PyString s) {
97         return group(getindex(s));
98     }
99
100     private PyObject group(PyObject o) {
101         String JavaDoc s;
102
103         if (o instanceof PyInteger) {
104             s = group(((PyInteger)o).getValue());
105         } else if (o instanceof PyString) {
106             s = group((PyString)o);
107         } else {
108             throw org.python.modules.re.ReError(
109                 "group index must be a string or integer");
110         }
111         if (s == null)
112             return Py.None;
113         else
114             return new PyString(s);
115     }
116
117     public PyObject group(PyObject[] args) {
118         int n = args.length;
119
120         if (n == 0)
121             return new PyString(group(0));
122         if (n == 1)
123             return group(args[0]);
124
125         PyObject[] res = new PyObject[n];
126         for(int i=0; i < n; i++) {
127             res[i] = group(args[i]);
128         }
129         return new PyTuple(res);
130     }
131
132     public PyObject groupdict() {
133         return groupdict(Py.None);
134     }
135
136     public PyObject groupdict(PyObject defalt) {
137         PyDictionary dict = new PyDictionary();
138         PyList items = re.groupindex.items();
139
140         for (int i=0; i < items.__len__(); i++) {
141             PyTuple t = (PyTuple)items.__getitem__(i);
142             PyString name = (PyString)t.__getitem__(0);
143             PyInteger index = (PyInteger)t.__getitem__(1);
144
145             String JavaDoc s = group(index.getValue());
146             if (s == null)
147                 dict.__setitem__(name, defalt);
148             else
149                 dict.__setitem__(name, new PyString(s));
150         }
151         return dict;
152     }
153 }
154
Popular Tags