package votorola.g.web.wic; // Copyright 2008-2010, 2012-2013, 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 java.util.regex.*;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import votorola.g.*;
import votorola.g.lang.*;
/** PageParameters utilities.
*/
public @ThreadSafe final class PageParametersX
{
private PageParametersX() {}
/** Constructs a PageParameters, copying its state from pP if it is not null.
*/
public static PageParameters copyOrNew( final PageParameters pP )
{
return pP == null? new PageParameters(): new PageParameters( pP );
}
/** Returns the value of the specified parameter as a string. This is a convenient
* short hand for p.get(name).toString()
.
*/
public static final String getString( final PageParameters pP, final String name )
{
return pP.get(name).toString();
}
/** Returns the null or non-empty value of the specified parameter as a string.
*
* @throws VotorolaRuntimeException if the value is the empty string "".
*
* @see votorola.g.web.HTTPServletRequestX#getParameterNonEmpty(String,javax.servlet.ServletRequest)
*/
public static final String getStringNonEmpty( final PageParameters pP, final String name )
{
final String value = getString( pP, name );
if( !"".equals( value )) return value;
throw new VotorolaRuntimeException( "empty query parameter '" + name + "'" );
}
/** Returns the non-null, non-empty value of the specified parameter as a string.
*
* @throws VotorolaRuntimeException if the value is either null, or the empty
* string "".
*
* @see votorola.g.web.HTTPServletRequestX#getParameterRequired(String,javax.servlet.ServletRequest)
*/
public static final String getStringRequired( final PageParameters pP, final String name )
{
final String value = getString( pP, name );
if( value != null && !"".equals( value )) return value;
throw new VotorolaRuntimeException( "missing query parameter '" + name + "'" );
}
// ------------------------------------------------------------------------------------
// public static String getStringNonEmpty( final PageParameters p, final String name )
/// see votorola.a.web.wic.VPageHTML.stringNonEmpty
/** Splits the specified value into a set of strings, separated by the given pattern.
* The set will not include the empty string.
*/
public static Set splitAsSet( final String value, final Pattern pattern )
{
final HashSet set = new HashSet( /*intial capacity*/4 );
for( String s: pattern.split( value )) set.add( s );
return set;
}
/** A pattern that splits on vertical bars '|'.
*/
public static final Pattern SPLIT_ON_BAR_PATTERN = Pattern.compile( "\\|" );
/** Returns the specified page parameters (p) with a new value set. This is just a
* convenience method for setting values on the fly. If p is null but the value is
* non-null, then p is automatically constructed and returned.
*
* @param pP the parameters, which may be null.
* @param value the value to set. If null, then this method is a no-op.
*
* @return the same p; or, if p was null and the value was non-null, the p that
* was constucted in order to set the value.
*/
public static PageParameters withSet( PageParameters pP, final String name, final String value )
{
if( value != null )
{
if( pP == null ) pP = new PageParameters();
pP.set( name, value );
}
return pP;
}
}