Source Code Cross Referenced for RETokenRepeated.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/RETokenRepeated.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.util.Vector;
023:
024:        final class RETokenRepeated extends REToken {
025:            private REToken token;
026:            private int min, max;
027:            private boolean stingy;
028:
029:            RETokenRepeated(int subIndex, REToken token, int min, int max) {
030:                super (subIndex);
031:                this .token = token;
032:                this .min = min;
033:                this .max = max;
034:            }
035:
036:            /** Sets the minimal matching mode to true. */
037:            void makeStingy() {
038:                stingy = true;
039:            }
040:
041:            /** Queries if this token has minimal matching enabled. */
042:            boolean isStingy() {
043:                return stingy;
044:            }
045:
046:            /**
047:             * The minimum length of a repeated token is the minimum length
048:             * of the token multiplied by the minimum number of times it must
049:             * match.
050:             */
051:            int getMinimumLength() {
052:                return (min * token.getMinimumLength());
053:            }
054:
055:            // We do need to save every possible point, but the number of clone()
056:            // invocations here is really a killer for performance on non-stingy
057:            // repeat operators.  I'm open to suggestions...
058:
059:            // Hypothetical question: can you have a RE that matches 1 times,
060:            // 3 times, 5 times, but not 2 times or 4 times?  Does having
061:            // the subexpression back-reference operator allow that?
062:
063:            boolean match(CharIndexed input, REMatch mymatch) {
064:                // number of times we've matched so far
065:                int numRepeats = 0;
066:
067:                // Possible positions for the next repeat to match at
068:                REMatch newMatch = mymatch;
069:                REMatch last = null;
070:                REMatch current;
071:
072:                // Add the '0-repeats' index
073:                // positions.elementAt(z) == position [] in input after <<z>> matches
074:                Vector positions = new Vector();
075:                positions.addElement(newMatch);
076:
077:                // Declare variables used in loop
078:                REMatch doables;
079:                REMatch doablesLast;
080:                REMatch recurrent;
081:
082:                do {
083:                    // Check for stingy match for each possibility.
084:                    if (stingy && (numRepeats >= min)) {
085:                        REMatch result = matchRest(input, newMatch);
086:                        if (result != null) {
087:                            mymatch.assignFrom(result);
088:                            return true;
089:                        }
090:                    }
091:
092:                    doables = null;
093:                    doablesLast = null;
094:
095:                    // try next repeat at all possible positions
096:                    for (current = newMatch; current != null; current = current.next) {
097:                        recurrent = (REMatch) current.clone();
098:                        if (token.match(input, recurrent)) {
099:                            // add all items in current to doables array
100:                            if (doables == null) {
101:                                doables = recurrent;
102:                                doablesLast = recurrent;
103:                            } else {
104:                                // Order these from longest to shortest
105:                                // Start by assuming longest (more repeats)
106:                                doablesLast.next = recurrent;
107:                            }
108:                            // Find new doablesLast
109:                            while (doablesLast.next != null) {
110:                                doablesLast = doablesLast.next;
111:                            }
112:                        }
113:                    }
114:                    // if none of the possibilities worked out, break out of do/while
115:                    if (doables == null)
116:                        break;
117:
118:                    // reassign where the next repeat can match
119:                    newMatch = doables;
120:
121:                    // increment how many repeats we've successfully found
122:                    ++numRepeats;
123:
124:                    positions.addElement(newMatch);
125:                } while (numRepeats < max);
126:
127:                // If there aren't enough repeats, then fail
128:                if (numRepeats < min)
129:                    return false;
130:
131:                // We're greedy, but ease off until a true match is found 
132:                int posIndex = positions.size();
133:
134:                // At this point we've either got too many or just the right amount.
135:                // See if this numRepeats works with the rest of the regexp.
136:                REMatch allResults = null;
137:                REMatch allResultsLast = null;
138:
139:                REMatch results = null;
140:                while (--posIndex >= min) {
141:                    newMatch = (REMatch) positions.elementAt(posIndex);
142:                    results = matchRest(input, newMatch);
143:                    if (results != null) {
144:                        if (allResults == null) {
145:                            allResults = results;
146:                            allResultsLast = results;
147:                        } else {
148:                            // Order these from longest to shortest
149:                            // Start by assuming longest (more repeats)
150:                            allResultsLast.next = results;
151:                        }
152:                        // Find new doablesLast
153:                        while (allResultsLast.next != null) {
154:                            allResultsLast = allResultsLast.next;
155:                        }
156:                    }
157:                    // else did not match rest of the tokens, try again on smaller sample
158:                }
159:                if (allResults != null) {
160:                    mymatch.assignFrom(allResults); // does this get all?
161:                    return true;
162:                }
163:                // If we fall out, no matches.
164:                return false;
165:            }
166:
167:            private REMatch matchRest(CharIndexed input, final REMatch newMatch) {
168:                REMatch current, single;
169:                REMatch doneIndex = null;
170:                REMatch doneIndexLast = null;
171:                // Test all possible matches for this number of repeats
172:                for (current = newMatch; current != null; current = current.next) {
173:                    // clone() separates a single match from the chain
174:                    single = (REMatch) current.clone();
175:                    if (next(input, single)) {
176:                        // chain results to doneIndex
177:                        if (doneIndex == null) {
178:                            doneIndex = single;
179:                            doneIndexLast = single;
180:                        } else {
181:                            doneIndexLast.next = single;
182:                        }
183:                        // Find new doneIndexLast
184:                        while (doneIndexLast.next != null) {
185:                            doneIndexLast = doneIndexLast.next;
186:                        }
187:                    }
188:                }
189:                return doneIndex;
190:            }
191:
192:            void dump(StringBuffer os) {
193:                os.append("(?:");
194:                token.dumpAll(os);
195:                os.append(')');
196:                if ((max == Integer.MAX_VALUE) && (min <= 1))
197:                    os.append((min == 0) ? '*' : '+');
198:                else if ((min == 0) && (max == 1))
199:                    os.append('?');
200:                else {
201:                    os.append('{').append(min);
202:                    if (max > min) {
203:                        os.append(',');
204:                        if (max != Integer.MAX_VALUE)
205:                            os.append(max);
206:                    }
207:                    os.append('}');
208:                }
209:                if (stingy)
210:                    os.append('?');
211:            }
212:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.