package votorola.a.voter; // 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.*; import javax.mail.internet.*; import org.apache.wicket.AttributeModifier; import org.apache.wicket.markup.html.form.*; import org.apache.wicket.util.convert.*; import votorola.a.*; import votorola.a.web.wic.*; import votorola.g.lang.*; import votorola.g.mail.*; import votorola.g.web.wic.*; /** A Wicket I/O converter of ID pairs. */ public @ThreadRestricted("wicket") final class IDPairConverter implements IConverter { private static final long serialVersionUID = 0L; /** A convenience method to initialize a text field by adding a 'maxLength' * attribute to its view, and setting a model type of {@linkplain IDPair IDPair}. * * @return the same field * * @see VoterInputTable#MAX_INPUT_LENGTH */ public static > C setMaxLength_Type( final C field ) { field.add( AttributeModifier.replace( "maxlength", Integer.toString( VoterInputTable.MAX_INPUT_LENGTH ))); assert field.getType() == null: "not clobbering a pre-assigned type"; field.setType( IDPair.class ); return field; } // - I - C o n v e r t e r ------------------------------------------------------------ /** @throws IllegalArgumentException if input exceeds * {@linkplain VoterInputTable#MAX_INPUT_LENGTH MAX_INPUT_LENGTH} */ public final IDPair convertToObject( final String input, final Locale _ignored ) throws IllegalArgumentException { ModelLengthLimiter.lengthConstrained( input, VoterInputTable.MAX_INPUT_LENGTH ); // if( input == null ) return null; if( input.length() == 0 ) return null; // IConverter says input never null final boolean isEmail = input.indexOf('@') >= 0; final IDPair idPair; if( isEmail ) { try{ idPair = IDPair.fromEmail( InternetAddressX.canonicalAddress( input )); } catch( final AddressException x ) { throw new ConversionExceptionLM( VRequestCycle.get().bunG().l( "g.malformedEmail", input, x.getMessage() )); } } else // username { try{ idPair = IDPair.fromUsername( input ); } catch( final AddressException x ) { throw new ConversionExceptionLM( VRequestCycle.get().bunW().l( "a.voter.IDPairConverter.malformedUsername", input, x.getMessage() )); } } return idPair; } public final String convertToString( final IDPair value, final Locale _ignored ) { return value == null? null: value.toString(); } }