Source Code Cross Referenced for REMatch.java in  » Database-DBMS » mckoi » 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 » Database DBMS » mckoi » 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:                    copy.start = (int[]) start.clone();
059:                    copy.end = (int[]) end.clone();
060:
061:                    return copy;
062:                } catch (CloneNotSupportedException e) {
063:                    throw new Error(); // doesn't happen
064:                }
065:            }
066:
067:            void assignFrom(REMatch other) {
068:                start = other.start;
069:                end = other.end;
070:                index = other.index;
071:                // need to deep clone?
072:                next = other.next;
073:            }
074:
075:            REMatch(int subs, int anchor, int eflags) {
076:                start = new int[subs + 1];
077:                end = new int[subs + 1];
078:                this .anchor = anchor;
079:                this .eflags = eflags;
080:                clear(anchor);
081:            }
082:
083:            void finish(CharIndexed text) {
084:                start[0] = 0;
085:                StringBuffer sb = new StringBuffer();
086:                int i;
087:                for (i = 0; i < end[0]; i++)
088:                    sb.append(text.charAt(i));
089:                matchedText = sb.toString();
090:                for (i = 0; i < start.length; i++) {
091:                    // If any subexpressions didn't terminate, they don't count
092:                    // TODO check if this code ever gets hit
093:                    if ((start[i] == -1) ^ (end[i] == -1)) {
094:                        start[i] = -1;
095:                        end[i] = -1;
096:                    }
097:                }
098:                next = null; // cut off alternates
099:            }
100:
101:            /** Clears the current match and moves the offset to the new index. */
102:            void clear(int index) {
103:                offset = index;
104:                this .index = 0;
105:                for (int i = 0; i < start.length; i++) {
106:                    start[i] = end[i] = -1;
107:                }
108:                next = null; // cut off alternates
109:            }
110:
111:            /**
112:             * Returns the string matching the pattern.  This makes it convenient
113:             * to write code like the following:
114:             * <P>
115:             * <code> 
116:             * REMatch myMatch = myExpression.getMatch(myString);<br>
117:             * if (myMatch != null) System.out.println("Regexp found: "+myMatch);
118:             * </code>
119:             */
120:            public String toString() {
121:                return matchedText;
122:            }
123:
124:            /**
125:             * Returns the index within the input text where the match in its entirety
126:             * began.
127:             */
128:            public int getStartIndex() {
129:                return offset + start[0];
130:            }
131:
132:            /**
133:             * Returns the index within the input string where the match in
134:             * its entirety ends.  The return value is the next position after
135:             * the end of the string; therefore, a match created by the
136:             * following call:
137:             *
138:             * <P>
139:             * <code>REMatch myMatch = myExpression.getMatch(myString);</code>
140:             * <P>
141:             * can be viewed (given that myMatch is not null) by creating
142:             * <P>
143:             * <code>String theMatch = myString.substring(myMatch.getStartIndex(),
144:             * myMatch.getEndIndex());</code>
145:             * <P>
146:             * But you can save yourself that work, since the <code>toString()</code>
147:             * method (above) does exactly that for you.  
148:             */
149:            public int getEndIndex() {
150:                return offset + end[0];
151:            }
152:
153:            /**
154:             * Returns the string matching the given subexpression.  The subexpressions
155:             * are indexed starting with one, not zero.  That is, the subexpression
156:             * identified by the first set of parentheses in a regular expression
157:             * could be retrieved from an REMatch by calling match.toString(1).
158:             *
159:             * @param sub Index of the subexpression.
160:             */
161:            public String toString(int sub) {
162:                if ((sub >= start.length) || (start[sub] == -1))
163:                    return "";
164:                return (matchedText.substring(start[sub], end[sub]));
165:            }
166:
167:            /** 
168:             * Returns the index within the input string used to generate this match
169:             * where subexpression number <i>sub</i> begins, or <code>-1</code> if
170:             * the subexpression does not exist.  The initial position is zero.
171:             *
172:             * @param sub Subexpression index
173:             * @deprecated Use getStartIndex(int) instead.
174:             */
175:            public int getSubStartIndex(int sub) {
176:                if (sub >= start.length)
177:                    return -1;
178:                int x = start[sub];
179:                return (x == -1) ? x : offset + x;
180:            }
181:
182:            /** 
183:             * Returns the index within the input string used to generate this match
184:             * where subexpression number <i>sub</i> begins, or <code>-1</code> if
185:             * the subexpression does not exist.  The initial position is zero.
186:             *
187:             * @param sub Subexpression index
188:             * @since gnu.regexp 1.1.0
189:             */
190:            public int getStartIndex(int sub) {
191:                if (sub >= start.length)
192:                    return -1;
193:                int x = start[sub];
194:                return (x == -1) ? x : offset + x;
195:            }
196:
197:            /** 
198:             * Returns the index within the input string used to generate this match
199:             * where subexpression number <i>sub</i> ends, or <code>-1</code> if
200:             * the subexpression does not exist.  The initial position is zero.
201:             *
202:             * @param sub Subexpression index
203:             * @deprecated Use getEndIndex(int) instead
204:             */
205:            public int getSubEndIndex(int sub) {
206:                if (sub >= start.length)
207:                    return -1;
208:                int x = end[sub];
209:                return (x == -1) ? x : offset + x;
210:            }
211:
212:            /** 
213:             * Returns the index within the input string used to generate this match
214:             * where subexpression number <i>sub</i> ends, or <code>-1</code> if
215:             * the subexpression does not exist.  The initial position is zero.
216:             *
217:             * @param sub Subexpression index
218:             */
219:            public int getEndIndex(int sub) {
220:                if (sub >= start.length)
221:                    return -1;
222:                int x = end[sub];
223:                return (x == -1) ? x : offset + x;
224:            }
225:
226:            /**
227:             * Substitute the results of this match to create a new string.
228:             * This is patterned after PERL, so the tokens to watch out for are
229:             * <code>$0</code> through <code>$9</code>.  <code>$0</code> matches
230:             * the full substring matched; <code>$<i>n</i></code> matches
231:             * subexpression number <i>n</i>.
232:             *
233:             * @param input A string consisting of literals and <code>$<i>n</i></code> tokens.
234:             */
235:            public String substituteInto(String input) {
236:                // a la Perl, $0 is whole thing, $1 - $9 are subexpressions
237:                StringBuffer output = new StringBuffer();
238:                int pos;
239:                for (pos = 0; pos < input.length() - 1; pos++) {
240:                    if ((input.charAt(pos) == '$')
241:                            && (Character.isDigit(input.charAt(pos + 1)))) {
242:                        int val = Character.digit(input.charAt(++pos), 10);
243:                        if (val < start.length) {
244:                            output.append(toString(val));
245:                        }
246:                    } else
247:                        output.append(input.charAt(pos));
248:                }
249:                if (pos < input.length())
250:                    output.append(input.charAt(pos));
251:                return output.toString();
252:            }
253:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.