| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.jelly.expression.CompositeExpression |
|
|
| 1 | /* |
|
| 2 | * Copyright 2002,2004 The Apache Software Foundation. |
|
| 3 | * |
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 | * you may not use this file except in compliance with the License. |
|
| 6 | * You may obtain a copy of the License at |
|
| 7 | * |
|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 | * |
|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 | * See the License for the specific language governing permissions and |
|
| 14 | * limitations under the License. |
|
| 15 | */ |
|
| 16 | package org.apache.commons.jelly.expression; |
|
| 17 | ||
| 18 | import java.util.ArrayList; |
|
| 19 | import java.util.Collections; |
|
| 20 | import java.util.Iterator; |
|
| 21 | import java.util.List; |
|
| 22 | ||
| 23 | import org.apache.commons.collections.iterators.SingletonIterator; |
|
| 24 | ||
| 25 | import org.apache.commons.jelly.JellyContext; |
|
| 26 | import org.apache.commons.jelly.JellyException; |
|
| 27 | ||
| 28 | /** |
|
| 29 | * <p><code>CompositeExpression</code> is a Composite expression made up of several |
|
| 30 | * Expression objects which are concatenated into a single String.</p> |
|
| 31 | * |
|
| 32 | * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> |
|
| 33 | * @version $Revision: 155420 $ |
|
| 34 | */ |
|
| 35 | public class CompositeExpression extends ExpressionSupport { |
|
| 36 | ||
| 37 | /** The expressions */ |
|
| 38 | private List expressions; |
|
| 39 | ||
| 40 | 533 | public CompositeExpression() { |
| 41 | 533 | this.expressions = new ArrayList(); |
| 42 | 533 | } |
| 43 | ||
| 44 | 0 | public CompositeExpression(List expressions) { |
| 45 | 0 | this.expressions = expressions; |
| 46 | 0 | } |
| 47 | ||
| 48 | public String toString() { |
|
| 49 | 0 | return super.toString() + "[expressions=" + expressions +"]"; |
| 50 | } |
|
| 51 | ||
| 52 | /** |
|
| 53 | * Parses the given String to be either a ConstantExpresssion, an Expression denoted as |
|
| 54 | * "${foo}" or some String with embedded expresssions such as "abc${something}def${else}xyz" |
|
| 55 | * which results in a CompositeExpression being returned. |
|
| 56 | * |
|
| 57 | * @param text is the String to parse into expressions |
|
| 58 | * @param factory is the Factory of Expression objects used to create expresssions for the contents |
|
| 59 | * of the String "foo" inside expressions such as "${foo}" |
|
| 60 | * |
|
| 61 | * @return the Expresssion for the given String. |
|
| 62 | * @throws JellyException if the text is invalid (such as missing '}' character). |
|
| 63 | * @throws JellyException if there was some problem creating the underlying Expression object |
|
| 64 | * from the ExpressionFactory |
|
| 65 | */ |
|
| 66 | public static Expression parse(String text, ExpressionFactory factory) throws JellyException { |
|
| 67 | ||
| 68 | 53950 | int len = text.length(); |
| 69 | ||
| 70 | 53950 | int startIndex = text.indexOf( "${" ); |
| 71 | ||
| 72 | 53950 | if ( startIndex < 0) { |
| 73 | 46410 | return new ConstantExpression(text); |
| 74 | } |
|
| 75 | ||
| 76 | 7540 | int endIndex = text.indexOf( "}", startIndex+2 ); |
| 77 | ||
| 78 | 7540 | if ( endIndex < 0 ) { |
| 79 | 0 | throw new JellyException( "Missing '}' character at the end of expression: " + text ); |
| 80 | } |
|
| 81 | 7540 | if ( startIndex == 0 && endIndex == len - 1 ) { |
| 82 | 7007 | return factory.createExpression(text.substring(2, endIndex)); |
| 83 | } |
|
| 84 | ||
| 85 | 533 | CompositeExpression answer = new CompositeExpression(); |
| 86 | ||
| 87 | 533 | int cur = 0; |
| 88 | 533 | char c = 0; |
| 89 | ||
| 90 | 533 | StringBuffer chars = new StringBuffer(); |
| 91 | 533 | StringBuffer expr = new StringBuffer(); |
| 92 | ||
| 93 | MAIN: |
|
| 94 | 11232 | while ( cur < len ) { |
| 95 | 10699 | c = text.charAt( cur ); |
| 96 | ||
| 97 | 10699 | switch ( c ) { |
| 98 | case('$'): |
|
| 99 | 988 | if ( cur+1<len ) { |
| 100 | 988 | ++cur; |
| 101 | 988 | c = text.charAt( cur ); |
| 102 | ||
| 103 | 988 | switch ( c ) { |
| 104 | case('$'): |
|
| 105 | 0 | chars.append( c ); |
| 106 | 0 | break; |
| 107 | case('{'): |
|
| 108 | 988 | if ( chars.length() > 0 ) { |
| 109 | 507 | answer.addTextExpression( chars.toString() ); |
| 110 | 507 | chars.delete(0, chars.length() ); |
| 111 | } |
|
| 112 | ||
| 113 | 988 | if (cur+1<len) { |
| 114 | 988 | ++cur; |
| 115 | ||
| 116 | 10881 | while (cur<len) { |
| 117 | 10881 | c = text.charAt(cur); |
| 118 | 10881 | switch ( c ) { |
| 119 | case('"'): |
|
| 120 | 13 | expr.append( c ); |
| 121 | 13 | ++cur; |
| 122 | ||
| 123 | DOUBLE_QUOTE: |
|
| 124 | 91 | while(cur<len) { |
| 125 | 91 | c = text.charAt(cur); |
| 126 | ||
| 127 | 91 | switch ( c ) { |
| 128 | case('\\'): |
|
| 129 | 0 | ++cur; |
| 130 | 0 | expr.append(c); |
| 131 | 0 | break; |
| 132 | case('"'): |
|
| 133 | 13 | ++cur; |
| 134 | 13 | expr.append(c); |
| 135 | 13 | break DOUBLE_QUOTE; |
| 136 | default: |
|
| 137 | 78 | ++cur; |
| 138 | 78 | expr.append(c); |
| 139 | } // switch |
|
| 140 | 78 | } // while |
| 141 | break; |
|
| 142 | case('\''): |
|
| 143 | 0 | expr.append( c ); |
| 144 | 0 | ++cur; |
| 145 | ||
| 146 | SINGLE_QUOTE: |
|
| 147 | 0 | while(cur<len) { |
| 148 | 0 | c = text.charAt(cur); |
| 149 | ||
| 150 | 0 | switch ( c ) { |
| 151 | case('\\'): |
|
| 152 | 0 | ++cur; |
| 153 | 0 | expr.append(c); |
| 154 | 0 | break; |
| 155 | case('\''): |
|
| 156 | 0 | ++cur; |
| 157 | 0 | expr.append(c); |
| 158 | 0 | break SINGLE_QUOTE; |
| 159 | default: |
|
| 160 | 0 | ++cur; |
| 161 | 0 | expr.append(c); |
| 162 | } // switch |
|
| 163 | 0 | } // while |
| 164 | break; |
|
| 165 | case('}'): |
|
| 166 | 988 | answer.addExpression(factory.createExpression(expr.toString())); |
| 167 | 988 | expr.delete(0, expr.length()); |
| 168 | 988 | ++cur; |
| 169 | 988 | continue MAIN; |
| 170 | default: |
|
| 171 | 9880 | expr.append( c ); |
| 172 | 9880 | ++cur; |
| 173 | } |
|
| 174 | 9880 | } |
| 175 | } |
|
| 176 | break; |
|
| 177 | default: |
|
| 178 | 0 | chars.append(c); |
| 179 | } |
|
| 180 | 0 | } |
| 181 | else |
|
| 182 | { |
|
| 183 | 0 | chars.append(c); |
| 184 | } |
|
| 185 | 0 | break; |
| 186 | default: |
|
| 187 | 9711 | chars.append( c ); |
| 188 | } |
|
| 189 | 9711 | ++cur; |
| 190 | 9711 | } |
| 191 | ||
| 192 | 533 | if ( chars.length() > 0 ) |
| 193 | { |
|
| 194 | 286 | answer.addTextExpression( chars.toString() ); |
| 195 | } |
|
| 196 | ||
| 197 | 533 | return answer; |
| 198 | } |
|
| 199 | ||
| 200 | // Properties |
|
| 201 | //------------------------------------------------------------------------- |
|
| 202 | ||
| 203 | /** |
|
| 204 | * @return the Expression objects that make up this |
|
| 205 | * composite expression |
|
| 206 | */ |
|
| 207 | public List getExpressions() { |
|
| 208 | 208 | return expressions; |
| 209 | } |
|
| 210 | ||
| 211 | /** |
|
| 212 | * Sets the Expression objects that make up this |
|
| 213 | * composite expression |
|
| 214 | */ |
|
| 215 | public void setExpressions(List expressions) { |
|
| 216 | 0 | this.expressions = expressions; |
| 217 | 0 | } |
| 218 | ||
| 219 | /** |
|
| 220 | * Adds a new expression to the end of the expression list |
|
| 221 | */ |
|
| 222 | public void addExpression(Expression expression) { |
|
| 223 | 1781 | expressions.add(expression); |
| 224 | 1781 | } |
| 225 | ||
| 226 | /** |
|
| 227 | * A helper method to add a new constant text expression |
|
| 228 | */ |
|
| 229 | public void addTextExpression(String text) { |
|
| 230 | 793 | addExpression(new ConstantExpression(text)); |
| 231 | 793 | } |
| 232 | ||
| 233 | // Expression interface |
|
| 234 | //------------------------------------------------------------------------- |
|
| 235 | ||
| 236 | public String getExpressionText() { |
|
| 237 | 91 | StringBuffer buffer = new StringBuffer(); |
| 238 | 91 | for (Iterator iter = expressions.iterator(); iter.hasNext(); ) { |
| 239 | 273 | Expression expression = (Expression) iter.next(); |
| 240 | 273 | buffer.append( expression.getExpressionText() ); |
| 241 | 273 | } |
| 242 | 91 | return buffer.toString(); |
| 243 | } |
|
| 244 | ||
| 245 | ||
| 246 | // inherit javadoc from interface |
|
| 247 | public Object evaluate(JellyContext context) { |
|
| 248 | 1456 | return evaluateAsString(context); |
| 249 | } |
|
| 250 | ||
| 251 | // inherit javadoc from interface |
|
| 252 | public String evaluateAsString(JellyContext context) { |
|
| 253 | 1456 | StringBuffer buffer = new StringBuffer(); |
| 254 | 1456 | for (Iterator iter = expressions.iterator(); iter.hasNext(); ) { |
| 255 | 3003 | Expression expression = (Expression) iter.next(); |
| 256 | 3003 | String value = expression.evaluateAsString(context); |
| 257 | 3003 | if ( value != null ) { |
| 258 | 2977 | buffer.append( value ); |
| 259 | } |
|
| 260 | 3003 | } |
| 261 | 1456 | return buffer.toString(); |
| 262 | ||
| 263 | } |
|
| 264 | ||
| 265 | // inherit javadoc from interface |
|
| 266 | public Iterator evaluateAsIterator(JellyContext context) { |
|
| 267 | 0 | String value = evaluateAsString(context); |
| 268 | 0 | if ( value == null ) { |
| 269 | 0 | return Collections.EMPTY_LIST.iterator(); |
| 270 | } |
|
| 271 | else { |
|
| 272 | 0 | return new SingletonIterator( value ); |
| 273 | } |
|
| 274 | } |
|
| 275 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |