package votorola.g.util.regex; // Copyright 2009, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.util.regex.*; /** Pattern utilities. */ public final class PatternX { private PatternX() {} // ------------------------------------------------------------------------------------ /** The same as pattern.matcher() but reuses a pre-existing matcher created from * another pattern. * * @param pattern the new pattern for the matcher. * @param input the new input sequence for the matcher. * @param matcherOrNull the provided matcher to reuse, if any. * @return the matcher reassigned to the new pattern and reset to the new input * sequence; or, if no matcher was provided, a newly created one. * * @see Pattern#matcher(CharSequence) */ public static Matcher matcher( final Pattern pattern, final CharSequence input, Matcher matcherOrNull ) { // if( matcherOrNull == null ) matcherOrNull = pattern.matcher( input ); // else // { // matcherOrNull.usePattern( pattern ); // matcherOrNull.reset( input ); // } //////// // BUG in Sun Java 1.6.0_07. Reported to http://bugreport.sun.com/bugreport/. A // matcher once threw NullPointerException at Matcher.java:1105, which means either // its parentPattern or parentPattern.root was null. I suspect the latter. I // suspect the pattern was deserialized by Wicket, in which case it had yet to lazily // recompile. Apparently, there is no way to force the recompilation, except by // requesting a matcher. So we cannot resuse the matcher, after all: matcherOrNull = pattern.matcher( input ); return matcherOrNull; } }