1 18 package org.apache.beehive.netui.core.urltemplates; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 28 32 public class URLTemplate 33 { 34 private static final char BEGIN_TOKEN_QUALIFIER = '{'; 35 private static final char END_TOKEN_QUALIFIER = '}'; 36 37 private String _template; 39 40 private ArrayList _parsedTemplate = null; 42 43 private boolean _isParsed = false; 44 45 private Collection _knownTokens = null; 47 48 private Collection _requiredTokens = null; 50 51 private HashMap _tokenValuesMap = new HashMap (); 52 53 69 public URLTemplate( String template, Collection knownTokens, Collection requiredTokens ) 70 { 71 setTemplate( template ); 72 _knownTokens = knownTokens; 73 _requiredTokens = requiredTokens; 74 } 75 76 86 public URLTemplate( URLTemplate template ) 87 { 88 setTemplate( template.getTemplate() ); 89 _knownTokens = template._knownTokens; 90 _requiredTokens = template._requiredTokens; 91 _parsedTemplate = template._parsedTemplate; 92 _isParsed = template._isParsed; 93 } 94 95 102 public void setTemplate( String template ) 103 { 104 if ( template == null || template.length() == 0 ) 105 { 106 throw new IllegalStateException ( "Template cannot be null or empty." ); 107 } 108 109 if ( template.equals( _template ) ) return; 110 111 _template = template; 112 _isParsed = false; 113 _parsedTemplate = null; 114 } 115 116 121 public String getTemplate() 122 { 123 return _template; 124 } 125 126 132 public void verify() throws IllegalStateException 133 { 134 if ( _knownTokens != null ) 136 { 137 for ( java.util.Iterator ii = _knownTokens.iterator(); ii.hasNext(); ) 138 { 139 String token = ( String ) ii.next(); 140 if ( token != null && token.length() > 2 ) 141 { 142 token = token.substring( 1, token.length() - 1 ); 144 int index = _template.indexOf( token ); 145 if ( index != -1 ) 146 { 147 if ( _template.charAt( index - 1 ) != BEGIN_TOKEN_QUALIFIER 148 || _template.charAt( index + token.length() ) != END_TOKEN_QUALIFIER ) 149 { 150 throw new IllegalStateException ( "Template token, " + token 151 + ", is not correctly enclosed with braces in template: " + _template ); 152 } 153 } 154 } 155 } 156 } 157 158 parseTemplate(); 160 161 if ( _requiredTokens != null ) 163 { 164 for ( java.util.Iterator ii = _requiredTokens.iterator(); ii.hasNext(); ) 165 { 166 String token = ( String ) ii.next(); 167 String qualifiedToken = token; 168 TemplateItem requiredItem = new TemplateItem( qualifiedToken, true ); 169 170 if ( !_parsedTemplate.contains( requiredItem ) ) 171 { 172 throw new IllegalStateException ( "Required token, " + token 173 + ", not found in template: " + _template ); 174 } 175 } 176 } 177 } 178 179 private void parseTemplate() 180 { 181 if ( _isParsed ) return; 182 183 _parsedTemplate = new ArrayList (); 184 TemplateTokenizer tokenizer = new TemplateTokenizer( getTemplate() ); 185 for ( ; tokenizer.hasNext(); ) 186 { 187 boolean isToken = tokenizer.isTokenNext(); 188 String tokenOrLiteral = ( String ) tokenizer.next(); 189 TemplateItem item = new TemplateItem( tokenOrLiteral, isToken ); 190 _parsedTemplate.add( item ); 191 } 192 193 _isParsed = true; 194 } 195 196 201 public void substitute( Map tokensAndValues ) 202 { 203 if ( tokensAndValues != null ) 204 { 205 _tokenValuesMap.putAll( tokensAndValues ); 206 } 207 } 208 209 213 public void substitute( String token, String value ) 214 { 215 _tokenValuesMap.put( token, value ); 216 } 217 218 222 public void substitute( String token, int value ) 223 { 224 String valueStr = Integer.toString( value ); 225 _tokenValuesMap.put( token, valueStr ); 226 } 227 228 232 public String toString() 233 { 234 if ( !_isParsed ) 236 { 237 parseTemplate(); 239 } 240 241 InternalStringBuilder result = new InternalStringBuilder( _template.length() + 16 ); 242 for ( java.util.Iterator ii = _parsedTemplate.iterator(); ii.hasNext(); ) 243 { 244 TemplateItem item = ( TemplateItem ) ii.next(); 245 if ( item.isToken() ) 246 { 247 if ( _tokenValuesMap.containsKey( item.getValue() ) ) 248 { 249 appendToResult( result, ( String ) _tokenValuesMap.get( item.getValue() ) ); 250 } 251 else 252 { 253 appendToResult( result, item.getValue() ); 255 } 256 } 257 else 258 { 259 appendToResult( result, item.getValue() ); 260 } 261 } 262 263 return result.toString(); 264 } 265 266 private void appendToResult( InternalStringBuilder result, String value ) 269 { 270 if ( value == null || value.length() == 0 ) return; 271 272 if ( result.length() > 0 && result.charAt( result.length() - 1 ) == '/' 273 && value.charAt( 0 ) == '/' ) 274 { 275 result.deleteCharAt( result.length() - 1 ); 276 } 277 result.append( value ); 278 } 279 280 protected class TemplateItem 281 { 282 private String value; 283 private boolean isToken = false; 284 285 public TemplateItem( String value, boolean isToken ) 286 { 287 assert value != null : "TemplateItem value cannot be null."; 288 this.value = value; 289 this.isToken = isToken; 290 } 291 292 public String getValue() 293 { 294 return value; 295 } 296 297 public boolean isToken() 298 { 299 return isToken; 300 } 301 302 public boolean equals( Object o ) 303 { 304 if ( this == o ) 305 { 306 return true; 307 } 308 if ( !( o instanceof TemplateItem ) ) 309 { 310 return false; 311 } 312 313 final TemplateItem templateItem = ( TemplateItem ) o; 314 315 if ( isToken != templateItem.isToken() ) 316 { 317 return false; 318 } 319 if ( !value.equals( templateItem.getValue() ) ) 320 { 321 return false; 322 } 323 324 return true; 325 } 326 327 public int hashCode() 328 { 329 int result; 330 result = value.hashCode(); 331 result = 29 * result + ( isToken ? 1 : 0 ); 332 return result; 333 } 334 } 335 } 336 | Popular Tags |