Source Code Cross Referenced for MatchObject.java in  » Scripting » jython » org » python » modules » sre » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Scripting » jython » org.python.modules.sre 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2000 Finn Bock
003:         *
004:         * This program contains material copyrighted by:
005:         * Copyright (c) 1997-2000 by Secret Labs AB.  All rights reserved.
006:         *
007:         * This version of the SRE library can be redistributed under CNRI's
008:         * Python 1.6 license.  For any other use, please contact Secret Labs
009:         * AB (info@pythonware.com).
010:         *
011:         * Portions of this engine have been developed in cooperation with
012:         * CNRI.  Hewlett-Packard provided funding for 1.6 integration and
013:         * other compatibility work.
014:         */
015:
016:        package org.python.modules.sre;
017:
018:        import org.python.core.ArgParser;
019:        import org.python.core.Py;
020:        import org.python.core.PyDictionary;
021:        import org.python.core.PyInteger;
022:        import org.python.core.PyObject;
023:        import org.python.core.PyString;
024:        import org.python.core.PyTuple;
025:        import org.python.core.imp;
026:
027:        public class MatchObject extends PyObject {
028:            public PyString string; /* link to the target string */
029:            public PyObject regs; /* cached list of matching spans */
030:            PatternObject pattern; /* link to the regex (pattern) object */
031:            int pos, endpos; /* current target slice */
032:            int lastindex; /* last index marker seen by the engine (-1 if none) */
033:            int groups; /* number of groups (start/end marks) */
034:            int[] mark;
035:
036:            public PyObject expand(PyObject[] args) {
037:                if (args.length == 0) {
038:                    throw Py
039:                            .TypeError("expand() takes exactly 1 argument (0 given)");
040:                }
041:                PyObject mod = imp.importName("sre", true);
042:                PyObject func = mod.__getattr__("_expand");
043:                return func.__call__(new PyObject[] { pattern, this , args[0] });
044:            }
045:
046:            public PyObject group(PyObject[] args) {
047:                switch (args.length) {
048:                case 0:
049:                    return getslice(Py.Zero, Py.None);
050:                case 1:
051:                    return getslice(args[0], Py.None);
052:                default:
053:                    PyObject[] result = new PyObject[args.length];
054:                    for (int i = 0; i < args.length; i++)
055:                        result[i] = getslice(args[i], Py.None);
056:                    return new PyTuple(result);
057:                }
058:            }
059:
060:            public PyObject groups(PyObject[] args, String[] kws) {
061:                ArgParser ap = new ArgParser("groups", args, kws, "default");
062:                PyObject def = ap.getPyObject(0, Py.None);
063:
064:                PyObject[] result = new PyObject[groups - 1];
065:                for (int i = 1; i < groups; i++) {
066:                    result[i - 1] = getslice_by_index(i, def);
067:                }
068:                return new PyTuple(result);
069:            }
070:
071:            public PyObject groupdict(PyObject[] args, String[] kws) {
072:                ArgParser ap = new ArgParser("groupdict", args, kws, "default");
073:                PyObject def = ap.getPyObject(0, Py.None);
074:
075:                PyObject result = new PyDictionary();
076:
077:                if (pattern.groupindex == null)
078:                    return result;
079:
080:                PyObject keys = pattern.groupindex.invoke("keys");
081:
082:                PyObject key;
083:                for (int i = 0; (key = keys.__finditem__(i)) != null; i++) {
084:                    PyObject item = getslice(key, def);
085:                    result.__setitem__(key, item);
086:                }
087:                return result;
088:            }
089:
090:            public PyObject start() {
091:                return start(Py.Zero);
092:            }
093:
094:            public PyObject start(PyObject index_) {
095:                int index = getindex(index_);
096:
097:                if (index < 0 || index >= groups)
098:                    throw Py.IndexError("no such group");
099:
100:                return Py.newInteger(mark[index * 2]);
101:            }
102:
103:            public PyObject end() {
104:                return end(Py.Zero);
105:            }
106:
107:            public PyObject end(PyObject index_) {
108:                int index = getindex(index_);
109:
110:                if (index < 0 || index >= groups)
111:                    throw Py.IndexError("no such group");
112:
113:                return Py.newInteger(mark[index * 2 + 1]);
114:            }
115:
116:            public PyTuple span() {
117:                return span(Py.Zero);
118:            }
119:
120:            public PyTuple span(PyObject index_) {
121:                int index = getindex(index_);
122:
123:                if (index < 0 || index >= groups)
124:                    throw Py.IndexError("no such group");
125:
126:                int start = mark[index * 2];
127:                int end = mark[index * 2 + 1];
128:
129:                return _pair(start, end);
130:            }
131:
132:            public PyObject regs() {
133:
134:                PyObject[] regs = new PyObject[groups];
135:
136:                for (int index = 0; index < groups; index++) {
137:                    regs[index] = _pair(mark[index * 2], mark[index * 2 + 1]);
138:                }
139:
140:                return new PyTuple(regs);
141:            }
142:
143:            PyTuple _pair(int i1, int i2) {
144:                return new PyTuple(new PyObject[] { Py.newInteger(i1),
145:                        Py.newInteger(i2) });
146:            }
147:
148:            private PyObject getslice(PyObject index, PyObject def) {
149:                return getslice_by_index(getindex(index), def);
150:            }
151:
152:            private int getindex(PyObject index) {
153:                if (index instanceof  PyInteger)
154:                    return ((PyInteger) index).getValue();
155:
156:                int i = -1;
157:
158:                if (pattern.groupindex != null) {
159:                    index = pattern.groupindex.__finditem__(index);
160:                    if (index != null)
161:                        if (index instanceof  PyInteger)
162:                            return ((PyInteger) index).getValue();
163:                }
164:                return i;
165:            }
166:
167:            private PyObject getslice_by_index(int index, PyObject def) {
168:                if (index < 0 || index >= groups)
169:                    throw Py.IndexError("no such group");
170:
171:                index *= 2;
172:                int start = mark[index];
173:                int end = mark[index + 1];
174:
175:                //System.out.println("group:" + index + " " + start + " " +
176:                //                   end + " l:" + string.length());
177:
178:                if (string == null || start < 0)
179:                    return def;
180:                return string.__getslice__(Py.newInteger(start), Py
181:                        .newInteger(end));
182:
183:            }
184:
185:            public PyObject __findattr__(String key) {
186:                //System.out.println("__findattr__:" + key);
187:                if (key == "flags")
188:                    return Py.newInteger(pattern.flags);
189:                if (key == "groupindex")
190:                    return pattern.groupindex;
191:                if (key == "re")
192:                    return pattern;
193:                if (key == "pos")
194:                    return Py.newInteger(pos);
195:                if (key == "endpos")
196:                    return Py.newInteger(endpos);
197:                if (key == "lastindex")
198:                    return lastindex == -1 ? Py.None : Py.newInteger(lastindex);
199:                if (key == "lastgroup") {
200:                    if (pattern.indexgroup != null && lastindex >= 0)
201:                        return pattern.indexgroup.__getitem__(lastindex);
202:                    return Py.None;
203:                }
204:                if (key == "regs") {
205:                    return regs();
206:                }
207:                return super.__findattr__(key);
208:            }
209:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.