001package 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. 002 003import java.util.*; 004import java.util.regex.*; 005import org.apache.wicket.request.mapper.parameter.PageParameters; 006import votorola.g.*; 007import votorola.g.lang.*; 008 009 010/** PageParameters utilities. 011 */ 012public @ThreadSafe final class PageParametersX 013{ 014 015 016 private PageParametersX() {} 017 018 019 020 /** Constructs a PageParameters, copying its state from pP if it is not null. 021 */ 022 public static PageParameters copyOrNew( final PageParameters pP ) 023 { 024 return pP == null? new PageParameters(): new PageParameters( pP ); 025 } 026 027 028 029 /** Returns the value of the specified parameter as a string. This is a convenient 030 * short hand for <code>p.get(name).toString()</code>. 031 */ 032 public static final String getString( final PageParameters pP, final String name ) 033 { 034 return pP.get(name).toString(); 035 } 036 037 038 039 /** Returns the null or non-empty value of the specified parameter as a string. 040 * 041 * @throws VotorolaRuntimeException if the value is the empty string "". 042 * 043 * @see votorola.g.web.HTTPServletRequestX#getParameterNonEmpty(String,javax.servlet.ServletRequest) 044 */ 045 public static final String getStringNonEmpty( final PageParameters pP, final String name ) 046 { 047 final String value = getString( pP, name ); 048 if( !"".equals( value )) return value; 049 050 throw new VotorolaRuntimeException( "empty query parameter '" + name + "'" ); 051 } 052 053 054 055 /** Returns the non-null, non-empty value of the specified parameter as a string. 056 * 057 * @throws VotorolaRuntimeException if the value is either null, or the empty 058 * string "". 059 * 060 * @see votorola.g.web.HTTPServletRequestX#getParameterRequired(String,javax.servlet.ServletRequest) 061 */ 062 public static final String getStringRequired( final PageParameters pP, final String name ) 063 { 064 final String value = getString( pP, name ); 065 if( value != null && !"".equals( value )) return value; 066 067 throw new VotorolaRuntimeException( "missing query parameter '" + name + "'" ); 068 } 069 070 071 072 // ------------------------------------------------------------------------------------ 073 074 075 // public static String getStringNonEmpty( final PageParameters p, final String name ) 076 /// see votorola.a.web.wic.VPageHTML.stringNonEmpty 077 078 079 080 /** Splits the specified value into a set of strings, separated by the given pattern. 081 * The set will not include the empty string. 082 */ 083 public static Set<String> splitAsSet( final String value, final Pattern pattern ) 084 { 085 final HashSet<String> set = new HashSet<String>( /*intial capacity*/4 ); 086 for( String s: pattern.split( value )) set.add( s ); 087 088 return set; 089 } 090 091 092 093 /** A pattern that splits on vertical bars '|'. 094 */ 095 public static final Pattern SPLIT_ON_BAR_PATTERN = Pattern.compile( "\\|" ); 096 097 098 099 /** Returns the specified page parameters (p) with a new value set. This is just a 100 * convenience method for setting values on the fly. If p is null but the value is 101 * non-null, then p is automatically constructed and returned. 102 * 103 * @param pP the parameters, which may be null. 104 * @param value the value to set. If null, then this method is a no-op. 105 * 106 * @return the same p; or, if p was null and the value was non-null, the p that 107 * was constucted in order to set the value. 108 */ 109 public static PageParameters withSet( PageParameters pP, final String name, final String value ) 110 { 111 if( value != null ) 112 { 113 if( pP == null ) pP = new PageParameters(); 114 115 pP.set( name, value ); 116 } 117 118 return pP; 119 } 120 121 122 123}