Source Code Cross Referenced for SimpleRegexMatcher.java in  » Library » Apache-commons-digester-1.8-src » org » apache » commons » digester » 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 » Library » Apache commons digester 1.8 src » org.apache.commons.digester 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* $Id: SimpleRegexMatcher.java 471661 2006-11-06 08:09:25Z skitching $
002:         *
003:         * Licensed to the Apache Software Foundation (ASF) under one or more
004:         * contributor license agreements.  See the NOTICE file distributed with
005:         * this work for additional information regarding copyright ownership.
006:         * The ASF licenses this file to You under the Apache License, Version 2.0
007:         * (the "License"); you may not use this file except in compliance with
008:         * the License.  You may obtain a copy of the License at
009:         * 
010:         *      http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing, software
013:         * distributed under the License is distributed on an "AS IS" BASIS,
014:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015:         * See the License for the specific language governing permissions and
016:         * limitations under the License.
017:         */
018:
019:        package org.apache.commons.digester;
020:
021:        import org.apache.commons.logging.Log;
022:        import org.apache.commons.logging.LogFactory;
023:
024:        /**
025:         * <p>Simple regex pattern matching algorithm.</p>
026:         * 
027:         * <p>This uses just two wildcards:
028:         * <ul>
029:         *  <li><code>*</code> matches any sequence of none, one or more characters
030:         *  <li><code>?</code> matches any one character 
031:         * </ul>
032:         * Escaping these wildcards is not supported .</p>
033:         *
034:         * @since 1.5
035:         */
036:
037:        public class SimpleRegexMatcher extends RegexMatcher {
038:
039:            // --------------------------------------------------------- Fields
040:
041:            /** Default log (class wide) */
042:            private static final Log baseLog = LogFactory
043:                    .getLog(SimpleRegexMatcher.class);
044:
045:            /** Custom log (can be set per object) */
046:            private Log log = baseLog;
047:
048:            // --------------------------------------------------------- Properties
049:
050:            /** 
051:             * Gets the <code>Log</code> implementation.
052:             */
053:            public Log getLog() {
054:                return log;
055:            }
056:
057:            /**
058:             * Sets the current <code>Log</code> implementation used by this class.
059:             */
060:            public void setLog(Log log) {
061:                this .log = log;
062:            }
063:
064:            // --------------------------------------------------------- Public Methods
065:
066:            /** 
067:             * Matches using simple regex algorithm.
068:             * 
069:             *
070:             * @param basePattern the standard digester path representing the element
071:             * @param regexPattern the regex pattern the path will be tested against
072:             * @return true if the given pattern matches the given path
073:             */
074:            public boolean match(String basePattern, String regexPattern) {
075:                // check for nulls
076:                if (basePattern == null || regexPattern == null) {
077:                    return false;
078:                }
079:                return match(basePattern, regexPattern, 0, 0);
080:            }
081:
082:            // --------------------------------------------------------- Implementations Methods
083:
084:            /**
085:             * Implementation of regex matching algorithm.
086:             * This calls itself recursively.
087:             */
088:            private boolean match(String basePattern, String regexPattern,
089:                    int baseAt, int regexAt) {
090:                if (log.isTraceEnabled()) {
091:                    log.trace("Base: " + basePattern);
092:                    log.trace("Regex: " + regexPattern);
093:                    log.trace("Base@" + baseAt);
094:                    log.trace("Regex@" + regexAt);
095:                }
096:
097:                // check bounds
098:                if (regexAt >= regexPattern.length()) {
099:                    // maybe we've got a match
100:                    if (baseAt >= basePattern.length()) {
101:                        // ok!
102:                        return true;
103:                    }
104:                    // run out early
105:                    return false;
106:
107:                } else {
108:                    if (baseAt >= basePattern.length()) {
109:                        // run out early
110:                        return false;
111:                    }
112:                }
113:
114:                // ok both within bounds
115:                char regexCurrent = regexPattern.charAt(regexAt);
116:                switch (regexCurrent) {
117:                case '*':
118:                    // this is the tricky case
119:                    // check for terminal 
120:                    if (++regexAt >= regexPattern.length()) {
121:                        // this matches anything let - so return true
122:                        return true;
123:                    }
124:                    // go through every subsequent apperance of the next character
125:                    // and so if the rest of the regex matches
126:                    char nextRegex = regexPattern.charAt(regexAt);
127:                    if (log.isTraceEnabled()) {
128:                        log
129:                                .trace("Searching for next '" + nextRegex
130:                                        + "' char");
131:                    }
132:                    int nextMatch = basePattern.indexOf(nextRegex, baseAt);
133:                    while (nextMatch != -1) {
134:                        if (log.isTraceEnabled()) {
135:                            log.trace("Trying '*' match@" + nextMatch);
136:                        }
137:                        if (match(basePattern, regexPattern, nextMatch, regexAt)) {
138:                            return true;
139:                        }
140:                        nextMatch = basePattern.indexOf(nextRegex,
141:                                nextMatch + 1);
142:                    }
143:                    log.trace("No matches found.");
144:                    return false;
145:
146:                case '?':
147:                    // this matches anything
148:                    return match(basePattern, regexPattern, ++baseAt, ++regexAt);
149:
150:                default:
151:                    if (log.isTraceEnabled()) {
152:                        log.trace("Camparing " + regexCurrent + " to "
153:                                + basePattern.charAt(baseAt));
154:                    }
155:                    if (regexCurrent == basePattern.charAt(baseAt)) {
156:                        // still got more to go
157:                        return match(basePattern, regexPattern, ++baseAt,
158:                                ++regexAt);
159:                    }
160:                    return false;
161:                }
162:            }
163:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.