package votorola.g.util.regex; // Copyright 2009, 2012, 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.Locale; import java.util.regex.*; import org.apache.wicket.AttributeModifier; import org.apache.wicket.request.cycle.RequestCycle; import org.apache.wicket.markup.html.form.*; import org.apache.wicket.util.convert.*; import votorola.g.lang.*; import votorola.g.locale.BundleFormatter; import votorola.g.web.wic.*; /** A Wicket I/O converter of regular expressions. */ public @ThreadRestricted("wicket") class WicPatternConverter implements IConverter { private static final long serialVersionUID = 1L; /** Constructs a WicPatternConverter. * * @see #maxInputLength() */ public WicPatternConverter( int _maxInputLength ) { maxInputLength = _maxInputLength; } // ------------------------------------------------------------------------------------ /** The maximum length of input that will be accepted. * * @see #convertToObject(String,Locale) */ public int maxInputLength() { return maxInputLength; } private final int maxInputLength; /** Sets the converted input to the model's current value and returns true; or, if * conversion is really needed, leaves it unset and returns false. Call this from * your FormComponent.convertInput() in order to guard against multiple redundant * conversions. (Why is Wicket requesting them?) * * @return true if converted input is set, false otherwise * * @see FormComponent#convertInput() */ public final boolean setConvertedInputUnaltered( final FormComponent component ) { // final Pattern value = component.getConvertedInput(); //// not useful, because convertedInput is nulled prior to calling convertInput() final Pattern value = component.getModelObject(); if( !ObjectX.nullEquals( normalized( component.getInput() ), convertToString( value, null ))) return false; component.setConvertedInput( value ); return true; } /** A convenience method to initialize a text field by adding a 'maxLength' attribute * to its view, and by setting a model type of {@linkplain Pattern Pattern}. * * @return the same field * * @see #maxInputLength() */ public > C setMaxLength_Type( final C field ) { field.add( AttributeModifier.replace( "maxlength", Integer.toString( maxInputLength ))); assert field.getType() == null: "not clobbering a pre-assigned type"; field.setType( Pattern.class ); return field; } // - I - C o n v e r t e r ------------------------------------------------------------ /** @throws IllegalArgumentException if input exceeds * {@linkplain #maxInputLength() maxInputLength}() */ public final Pattern convertToObject( final String input, final Locale _ignored ) throws IllegalArgumentException { ModelLengthLimiter.lengthConstrained( input, maxInputLength ); if( normalized(input).length() == 0 ) return null; // IConverter says input never null try { return patternFor( input ); } catch( final PatternSyntaxException x ) { throw new ConversionExceptionLM( ((BundleFormatter.GProvider)RequestCycle.get()).bunG().l( "g.malformedRegex", input, x.getMessage() )); } } public final String convertToString( final Pattern value, final Locale _ignored ) { return value == null? null: value.toString(); } // - W i c - P a t t e r n - C o n v e r t e r ---------------------------------------- /** Compiles the pattern for a string value. The base implementation of this method * (in RegexConverter) simply returns Pattern.compile(value). */ protected Pattern patternFor( final String value ) throws PatternSyntaxException { return Pattern.compile( value ); } private static String normalized( String input ) { // if( input != null ) /// guard redundant, IConverter says input never null { input = input.trim(); } return input; } }