Source Code Cross Referenced for REMatch.java in  » IDE » J » gnu » regexp » 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 » IDE » J » gnu.regexp 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  gnu/regexp/REMatch.java
003:         *  Copyright (C) 1998-2001 Wes Biggs
004:         *
005:         *  This library is free software; you can redistribute it and/or modify
006:         *  it under the terms of the GNU Lesser General Public License as published
007:         *  by the Free Software Foundation; either version 2.1 of the License, or
008:         *  (at your option) any later version.
009:         *
010:         *  This library is distributed in the hope that it will be useful,
011:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013:         *  GNU Lesser General Public License for more details.
014:         *
015:         *  You should have received a copy of the GNU Lesser General Public License
016:         *  along with this program; if not, write to the Free Software
017:         *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018:         */
019:
020:        package gnu.regexp;
021:
022:        import java.io.Serializable;
023:
024:        /**
025:         * An instance of this class represents a match
026:         * completed by a gnu.regexp matching function. It can be used
027:         * to obtain relevant information about the location of a match
028:         * or submatch.
029:         *
030:         * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
031:         */
032:        public final class REMatch implements  Serializable, Cloneable {
033:            private String matchedText;
034:
035:            // These variables are package scope for fast access within the engine
036:            int eflags; // execution flags this match was made using
037:
038:            // Offset in source text where match was tried.  This is zero-based;
039:            // the actual position in the source text is given by (offset + anchor).
040:            int offset;
041:
042:            // Anchor position refers to the index into the source input
043:            // at which the matching operation began.
044:            // This is also useful for the ANCHORINDEX option.
045:            int anchor;
046:
047:            // Package scope; used by RE.
048:            int index; // used while matching to mark current match position in input
049:            int[] start; // start positions (relative to offset) for each (sub)exp.
050:            int[] end; // end positions for the same
051:            REMatch next; // other possibility (to avoid having to use arrays)
052:
053:            public Object clone() {
054:                try {
055:                    REMatch copy = (REMatch) super .clone();
056:                    copy.next = null;
057:
058:                    // Jun 25 2003 3:39 PM PG
059:                    // Work around problem with jikes 1.18 and IBM Java 1.4.1.
060:                    //copy.start = (int[]) start.clone();
061:                    //copy.end = (int[]) end.clone();
062:                    copy.start = new int[start.length];
063:                    System.arraycopy(start, 0, copy.start, 0, start.length);
064:                    copy.end = new int[end.length];
065:                    System.arraycopy(end, 0, copy.end, 0, end.length);
066:
067:                    return copy;
068:                } catch (CloneNotSupportedException e) {
069:                    throw new Error(); // doesn't happen
070:                }
071:            }
072:
073:            void assignFrom(REMatch other) {
074:                start = other.start;
075:                end = other.end;
076:                index = other.index;
077:                // need to deep clone?
078:                next = other.next;
079:            }
080:
081:            REMatch(int subs, int anchor, int eflags) {
082:                start = new int[subs + 1];
083:                end = new int[subs + 1];
084:                this .anchor = anchor;
085:                this .eflags = eflags;
086:                clear(anchor);
087:            }
088:
089:            void finish(CharIndexed text) {
090:                start[0] = 0;
091:                StringBuffer sb = new StringBuffer();
092:                int i;
093:                for (i = 0; i < end[0]; i++)
094:                    sb.append(text.charAt(i));
095:                matchedText = sb.toString();
096:                for (i = 0; i < start.length; i++) {
097:                    // If any subexpressions didn't terminate, they don't count
098:                    // TODO check if this code ever gets hit
099:                    if ((start[i] == -1) ^ (end[i] == -1)) {
100:                        start[i] = -1;
101:                        end[i] = -1;
102:                    }
103:                }
104:                next = null; // cut off alternates
105:            }
106:
107:            /** Clears the current match and moves the offset to the new index. */
108:            void clear(int index) {
109:                offset = index;
110:                this .index = 0;
111:                for (int i = 0; i < start.length; i++) {
112:                    start[i] = end[i] = -1;
113:                }
114:                next = null; // cut off alternates
115:            }
116:
117:            /**
118:             * Returns the string matching the pattern.  This makes it convenient
119:             * to write code like the following:
120:             * <P>
121:             * <code>
122:             * REMatch myMatch = myExpression.getMatch(myString);<br>
123:             * if (myMatch != null) System.out.println("Regexp found: "+myMatch);
124:             * </code>
125:             */
126:            public String toString() {
127:                return matchedText;
128:            }
129:
130:            /**
131:             * Returns the index within the input text where the match in its entirety
132:             * began.
133:             */
134:            public int getStartIndex() {
135:                return offset + start[0];
136:            }
137:
138:            /**
139:             * Returns the index within the input string where the match in
140:             * its entirety ends.  The return value is the next position after
141:             * the end of the string; therefore, a match created by the
142:             * following call:
143:             *
144:             * <P>
145:             * <code>REMatch myMatch = myExpression.getMatch(myString);</code>
146:             * <P>
147:             * can be viewed (given that myMatch is not null) by creating
148:             * <P>
149:             * <code>String theMatch = myString.substring(myMatch.getStartIndex(),
150:             * myMatch.getEndIndex());</code>
151:             * <P>
152:             * But you can save yourself that work, since the <code>toString()</code>
153:             * method (above) does exactly that for you.
154:             */
155:            public int getEndIndex() {
156:                return offset + end[0];
157:            }
158:
159:            /**
160:             * Returns the string matching the given subexpression.  The subexpressions
161:             * are indexed starting with one, not zero.  That is, the subexpression
162:             * identified by the first set of parentheses in a regular expression
163:             * could be retrieved from an REMatch by calling match.toString(1).
164:             *
165:             * @param sub Index of the subexpression.
166:             */
167:            public String toString(int sub) {
168:                if ((sub >= start.length) || (start[sub] == -1))
169:                    return "";
170:                return (matchedText.substring(start[sub], end[sub]));
171:            }
172:
173:            /**
174:             * Returns the index within the input string used to generate this match
175:             * where subexpression number <i>sub</i> begins, or <code>-1</code> if
176:             * the subexpression does not exist.  The initial position is zero.
177:             *
178:             * @param sub Subexpression index
179:             * @deprecated Use getStartIndex(int) instead.
180:             */
181:            public int getSubStartIndex(int sub) {
182:                if (sub >= start.length)
183:                    return -1;
184:                int x = start[sub];
185:                return (x == -1) ? x : offset + x;
186:            }
187:
188:            /**
189:             * Returns the index within the input string used to generate this match
190:             * where subexpression number <i>sub</i> begins, or <code>-1</code> if
191:             * the subexpression does not exist.  The initial position is zero.
192:             *
193:             * @param sub Subexpression index
194:             * @since gnu.regexp 1.1.0
195:             */
196:            public int getStartIndex(int sub) {
197:                if (sub >= start.length)
198:                    return -1;
199:                int x = start[sub];
200:                return (x == -1) ? x : offset + x;
201:            }
202:
203:            /**
204:             * Returns the index within the input string used to generate this match
205:             * where subexpression number <i>sub</i> ends, or <code>-1</code> if
206:             * the subexpression does not exist.  The initial position is zero.
207:             *
208:             * @param sub Subexpression index
209:             * @deprecated Use getEndIndex(int) instead
210:             */
211:            public int getSubEndIndex(int sub) {
212:                if (sub >= start.length)
213:                    return -1;
214:                int x = end[sub];
215:                return (x == -1) ? x : offset + x;
216:            }
217:
218:            /**
219:             * Returns the index within the input string used to generate this match
220:             * where subexpression number <i>sub</i> ends, or <code>-1</code> if
221:             * the subexpression does not exist.  The initial position is zero.
222:             *
223:             * @param sub Subexpression index
224:             */
225:            public int getEndIndex(int sub) {
226:                if (sub >= start.length)
227:                    return -1;
228:                int x = end[sub];
229:                return (x == -1) ? x : offset + x;
230:            }
231:
232:            /**
233:             * Substitute the results of this match to create a new string.
234:             * This is patterned after PERL, so the tokens to watch out for are
235:             * <code>$0</code> through <code>$9</code>.  <code>$0</code> matches
236:             * the full substring matched; <code>$<i>n</i></code> matches
237:             * subexpression number <i>n</i>.
238:             *
239:             * @param input A string consisting of literals and <code>$<i>n</i></code> tokens.
240:             */
241:            public String substituteInto(String input) {
242:                // a la Perl, $0 is whole thing, $1 - $9 are subexpressions
243:                StringBuffer output = new StringBuffer();
244:                int pos;
245:                for (pos = 0; pos < input.length() - 1; pos++) {
246:                    if ((input.charAt(pos) == '$')
247:                            && (Character.isDigit(input.charAt(pos + 1)))) {
248:                        int val = Character.digit(input.charAt(++pos), 10);
249:                        if (val < start.length) {
250:                            output.append(toString(val));
251:                        }
252:                    } else
253:                        output.append(input.charAt(pos));
254:                }
255:                if (pos < input.length())
256:                    output.append(input.charAt(pos));
257:                return output.toString();
258:            }
259:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.