View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/expression/ConstantExpression.java,v 1.4 2002/05/17 15:18:15 jstrachan Exp $ 3 * $Revision: 1.4 $ 4 * $Date: 2002/05/17 15:18:15 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * $Id: ConstantExpression.java,v 1.4 2002/05/17 15:18:15 jstrachan Exp $ 61 */ 62 package org.apache.commons.jelly.expression; 63 64 import java.util.ArrayList; 65 import java.util.Collections; 66 import java.util.Iterator; 67 import java.util.List; 68 69 import org.apache.commons.collections.SingletonIterator; 70 71 import org.apache.commons.jelly.JellyContext; 72 import org.apache.commons.jelly.JellyException; 73 74 /*** 75 * <p><code>CompositeExpression</code> is a Composite expression made up of several 76 * Expression objects which are concatenated into a single String.</p> 77 * 78 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 79 * @version $Revision: 1.4 $ 80 */ 81 public class CompositeExpression extends ExpressionSupport { 82 83 /*** The expressions */ 84 private List expressions; 85 86 public CompositeExpression() { 87 this.expressions = new ArrayList(); 88 } 89 90 public CompositeExpression(List expressions) { 91 this.expressions = expressions; 92 } 93 94 public String toString() { 95 return super.toString() + "[expressions=" + expressions +"]"; 96 } 97 98 /*** 99 * Parses the given String to be either a ConstantExpresssion, an Expression denoted as 100 * "${foo}" or some String with embedded expresssions such as "abc${something}def${else}xyz" 101 * which results in a CompositeExpression being returned. 102 * 103 * @param text is the String to parse into expressions 104 * @param factory is the Factory of Expression objects used to create expresssions for the contents 105 * of the String "foo" inside expressions such as "${foo}" 106 * 107 * @return the Expresssion for the given String. 108 * @throws JellyException if the text is invalid (such as missing '}' character). 109 * @throws Exception if there was some problem creating the underlying Expression object 110 * from the ExpressionFactory 111 */ 112 public static Expression parse(String text, ExpressionFactory factory) throws Exception { 113 114 int len = text.length(); 115 116 int startIndex = text.indexOf( "${" ); 117 118 if ( startIndex < 0) { 119 return new ConstantExpression(text); 120 } 121 122 int endIndex = text.indexOf( "}", startIndex+2 ); 123 124 if ( endIndex < 0 ) { 125 throw new JellyException( "Missing '}' character at the end of expression: " + text ); 126 } 127 if ( startIndex == 0 && endIndex == len - 1 ) { 128 return factory.createExpression(text.substring(2, endIndex)); 129 } 130 131 CompositeExpression answer = new CompositeExpression(); 132 133 int cur = 0; 134 char c = 0; 135 136 StringBuffer chars = new StringBuffer(); 137 StringBuffer expr = new StringBuffer(); 138 139 MAIN: 140 while ( cur < len ) 141 { 142 c = text.charAt( cur ); 143 144 switch ( c ) 145 { 146 case('$'): 147 { 148 if ( cur+1<len ) 149 { 150 ++cur; 151 c = text.charAt( cur ); 152 153 switch ( c ) 154 { 155 case('$'): 156 { 157 chars.append( c ); 158 break; 159 } 160 case('{'): 161 { 162 if ( chars.length() > 0 ) 163 { 164 answer.addTextExpression( chars.toString() ); 165 chars.delete(0, chars.length() ); 166 } 167 168 if (cur+1<len) 169 { 170 ++cur; 171 172 while (cur<len) 173 { 174 c = text.charAt(cur); 175 { 176 switch ( c ) 177 { 178 case('"'): 179 { 180 expr.append( c ); 181 ++cur; 182 183 DOUBLE_QUOTE: 184 while(cur<len) 185 { 186 c = text.charAt(cur); 187 188 boolean escape = false; 189 190 switch ( c ) 191 { 192 case('//'): 193 { 194 escape = true; 195 ++cur; 196 expr.append(c); 197 break; 198 } 199 case('"'): 200 { 201 ++cur; 202 expr.append(c); 203 break DOUBLE_QUOTE; 204 } 205 default: 206 { 207 escape=false; 208 ++cur; 209 expr.append(c); 210 } 211 } 212 } 213 break; 214 } 215 case('\''): 216 { 217 expr.append( c ); 218 ++cur; 219 220 SINGLE_QUOTE: 221 while(cur<len) 222 { 223 c = text.charAt(cur); 224 225 boolean escape = false; 226 227 switch ( c ) 228 { 229 case('//'): 230 { 231 escape = true; 232 ++cur; 233 expr.append(c); 234 break; 235 } 236 case('\''): 237 { 238 ++cur; 239 expr.append(c); 240 break SINGLE_QUOTE; 241 } 242 default: 243 { 244 escape=false; 245 ++cur; 246 expr.append(c); 247 } 248 } 249 } 250 break; 251 } 252 case('}'): 253 { 254 answer.addExpression(factory.createExpression(expr.toString())); 255 expr.delete(0, expr.length()); 256 ++cur; 257 continue MAIN; 258 } 259 default: 260 { 261 expr.append( c ); 262 ++cur; 263 } 264 } 265 } 266 } 267 } 268 break; 269 } 270 default: 271 { 272 chars.append(c); 273 } 274 } 275 } 276 else 277 { 278 chars.append(c); 279 } 280 break; 281 } 282 default: 283 { 284 chars.append( c ); 285 } 286 } 287 ++cur; 288 } 289 290 if ( chars.length() > 0 ) 291 { 292 answer.addTextExpression( chars.toString() ); 293 } 294 295 return answer; 296 } 297 298 // Properties 299 //------------------------------------------------------------------------- 300 301 /*** 302 * @return the Expression objects that make up this 303 * composite expression 304 */ 305 public List getExpressions() { 306 return expressions; 307 } 308 309 /*** 310 * Sets the Expression objects that make up this 311 * composite expression 312 */ 313 public void setExpressions(List expressions) { 314 this.expressions = expressions; 315 } 316 317 /*** 318 * Adds a new expression to the end of the expression list 319 */ 320 public void addExpression(Expression expression) { 321 expressions.add(expression); 322 } 323 324 /*** 325 * A helper method to add a new constant text expression 326 */ 327 public void addTextExpression(String text) { 328 addExpression(new ConstantExpression(text)); 329 } 330 331 // Expression interface 332 //------------------------------------------------------------------------- 333 334 335 // inherit javadoc from interface 336 public Object evaluate(JellyContext context) { 337 return evaluateAsString(context); 338 } 339 340 // inherit javadoc from interface 341 public String evaluateAsString(JellyContext context) { 342 StringBuffer buffer = new StringBuffer(); 343 for (Iterator iter = expressions.iterator(); iter.hasNext(); ) { 344 Expression expression = (Expression) iter.next(); 345 String value = expression.evaluateAsString(context); 346 if ( value != null ) { 347 buffer.append( value ); 348 } 349 } 350 return buffer.toString(); 351 352 } 353 354 // inherit javadoc from interface 355 public Iterator evaluateAsIterator(JellyContext context) { 356 String value = evaluateAsString(context); 357 if ( value == null ) { 358 return Collections.EMPTY_LIST.iterator(); 359 } 360 else { 361 return new SingletonIterator( value ); 362 } 363 } 364 }

This page was automatically generated by Maven