001package 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.
002
003import java.util.regex.*;
004
005
006/** Pattern utilities.
007  */
008public final class PatternX
009{
010
011
012    private PatternX() {}
013
014
015
016   // ------------------------------------------------------------------------------------
017
018
019    /** The same as pattern.matcher() but reuses a pre-existing matcher created from
020      * another pattern.
021      *
022      *     @param pattern the new pattern for the matcher.
023      *     @param input the new input sequence for the matcher.
024      *     @param matcherOrNull the provided matcher to reuse, if any.
025      *     @return the matcher reassigned to the new pattern and reset to the new input
026      *       sequence; or, if no matcher was provided, a newly created one.
027      *
028      *     @see Pattern#matcher(CharSequence)
029      */
030    public static Matcher matcher( final Pattern pattern, final CharSequence input,
031      Matcher matcherOrNull )
032    {
033     // if( matcherOrNull == null ) matcherOrNull = pattern.matcher( input );
034     // else
035     // {
036     //     matcherOrNull.usePattern( pattern );
037     //     matcherOrNull.reset( input );
038     // }
039     ////////
040     // BUG in Sun Java 1.6.0_07.  Reported to http://bugreport.sun.com/bugreport/. A
041     // matcher once threw NullPointerException at Matcher.java:1105, which means either
042     // its parentPattern or parentPattern.root was null.  I suspect the latter.  I
043     // suspect the pattern was deserialized by Wicket, in which case it had yet to lazily
044     // recompile.  Apparently, there is no way to force the recompilation, except by
045     // requesting a matcher.  So we cannot resuse the matcher, after all:
046
047        matcherOrNull = pattern.matcher( input );
048        return matcherOrNull;
049    }
050
051
052}