KickJava   Java API By Example, From Geeks To Geeks.

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


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 re implements ClassDictInit
8 {
9     public static String JavaDoc[] __all__ = new String JavaDoc[] {
10         "match","search","sub","subn","split","findall","escape","compile",
11         "I","L","M","S","X","IGNORECASE","LOCALE","MULTILINE","DOTALL",
12         "VERBOSE","error"
13     };
14
15     public static PyObject error = new PyString("re.error");
16
17     public static PyException ReError(String JavaDoc message) {
18         return new PyException(error, message);
19     }
20
21     public static void classDictInit(PyObject dict) {
22         dict.__setitem__("IGNORECASE",
23                          new PyInteger(Perl5Compiler.CASE_INSENSITIVE_MASK));
24         dict.__setitem__("I",
25                          new PyInteger(Perl5Compiler.CASE_INSENSITIVE_MASK));
26         dict.__setitem__("MULTILINE",
27                          new PyInteger(Perl5Compiler.MULTILINE_MASK));
28         dict.__setitem__("M", new PyInteger(Perl5Compiler.MULTILINE_MASK));
29         dict.__setitem__("DOTALL",
30                          new PyInteger(Perl5Compiler.SINGLELINE_MASK));
31         dict.__setitem__("S", new PyInteger(Perl5Compiler.SINGLELINE_MASK));
32         dict.__setitem__("VERBOSE",
33                          new PyInteger(Perl5Compiler.EXTENDED_MASK));
34         dict.__setitem__("X", new PyInteger(Perl5Compiler.EXTENDED_MASK));
35         // These are not implemented
36
dict.__setitem__("LOCALE", new PyInteger(0));
37         dict.__setitem__("L", new PyInteger(0));
38         dict.__setitem__("UNICODE", new PyInteger(0));
39         dict.__setitem__("U", new PyInteger(0));
40     }
41
42     // Skip caching for now...
43
private static RegexObject cachecompile(String JavaDoc pattern, int flags) {
44         return compile(pattern, flags);
45     }
46
47     private static RegexObject cachecompile(String JavaDoc pattern) {
48         return cachecompile(pattern, 0);
49     }
50
51     public static MatchObject match(String JavaDoc pattern, String JavaDoc string) {
52         return match(pattern, string, 0);
53     }
54
55     public static MatchObject match(String JavaDoc pattern, String JavaDoc string,
56                                     int flags)
57     {
58         return cachecompile(pattern, flags).match(string);
59     }
60
61     public static MatchObject search(String JavaDoc pattern, String JavaDoc string) {
62         return search(pattern, string, 0);
63     }
64
65     public static MatchObject search(String JavaDoc pattern, String JavaDoc string, int flags)
66     {
67         return cachecompile(pattern, flags).search(string);
68     }
69
70     private static RegexObject getPattern(PyObject pattern) {
71         if (pattern instanceof PyString) {
72             return cachecompile(pattern.toString());
73         } else if (pattern instanceof RegexObject) {
74             return (RegexObject)pattern;
75         }
76         throw Py.TypeError("pattern must be string or RegexObject");
77     }
78
79     public static PyString sub(PyObject pattern, PyObject repl, String JavaDoc string)
80     {
81         return sub(pattern, repl, string, 0);
82     }
83
84     public static PyString sub(PyObject pattern, PyObject repl,
85                                String JavaDoc string, int count)
86     {
87         return getPattern(pattern).sub(repl, string, count);
88     }
89
90     public static PyTuple subn(PyObject pattern, PyObject repl, String JavaDoc string)
91     {
92         return subn(pattern, repl, string, 0);
93     }
94
95     public static PyTuple subn(PyObject pattern, PyObject repl,
96                                String JavaDoc string, int count)
97     {
98         return getPattern(pattern).subn(repl, string, count);
99     }
100
101     public static PyList split(PyObject pattern, String JavaDoc string) {
102         return split(pattern, string, 0);
103     }
104
105     public static PyList split(PyObject pattern, String JavaDoc string,
106                                int maxsplit)
107     {
108         return getPattern(pattern).split(string, maxsplit);
109     }
110
111     public static PyList findall(PyObject pattern, String JavaDoc string) {
112         return getPattern(pattern).findall(string);
113     }
114
115     public static String JavaDoc escape(String JavaDoc s) {
116         return Perl5Compiler.quotemeta(s);
117     }
118
119     public static RegexObject compile(String JavaDoc pattern) {
120         return new RegexObject(pattern, 0);
121     }
122
123     public static RegexObject compile(String JavaDoc pattern, int flags) {
124         return new RegexObject(pattern, flags);
125     }
126 }
127
Popular Tags