package votorola.g.servlet; // Copyright 2008, 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 org.apache.wicket.model.*; import votorola.g.lang.*; /** A model wrapper that enforces a maximum length constraint on * the wrapped model's {@linkplain Object#toString() string value}. */ public final @ThreadRestricted("wicket") class ModelLengthLimiter extends AbstractWrapModel { /** Constructs a ModelLengthLimiter, or throws an IllegalArgumentException * if the current value of the wrapped model exceeds the constraint. */ public ModelLengthLimiter( IModel wrappedModel, int maxLength ) { this.wrappedModel = wrappedModel; this.maxLength = maxLength; lengthConstrained( wrappedModel.getObject() ); } /** Returns the length constraint. */ public final int getMaxLength() { return maxLength; } private final int maxLength; // /** Sets the length constraint. // * // * @see #getMaxLength() // */ // public final void setMaxLength( int newMaxLength ) { maxLength = newMaxLength; } // - I - M o d e l -------------------------------------------------------------------- /** Sets the model object; or throws an IllegalArgumentException * if the object's string value is longer than * the {@linkplain #getMaxLength maximum length}. */ public @Override void setObject( Object o ) { wrappedModel.setObject( lengthConstrained( o )); } public @Override Object getObject() { return wrappedModel.getObject(); } // - I - W r a p - M o d e l ---------------------------------------------------------- public IModel getWrappedModel() { return wrappedModel; } private final IModel wrappedModel; //// P r i v a t e /////////////////////////////////////////////////////////////////////// /** Throws an IllegalArgumentException if the object's string value is longer than * the {@linkplain #getMaxLength maximum length}; otherwise returns the same object. */ private Object lengthConstrained( Object o ) { if( o != null ) { String s = o.toString(); if( s.length() > maxLength ) throw new IllegalArgumentException( "exceeds " + maxLength + " characters: '" + s.substring(0,16) + "...'" ); } return o; } }