View Javadoc
Minimize
Table

1   /*
2    * Copyright 2003-2007 the original author or authors.
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.codehaus.groovy.ast.expr;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.codehaus.groovy.ast.ClassHelper;
22  import org.codehaus.groovy.ast.GroovyCodeVisitor;
23  
24  /**
25   * Represents a String expression which contains embedded values inside
26   * it such as "hello there ${user} how are you" which is expanded lazily
27   * 
28   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
29   * @version $Revision: 15807 $
30   */
31  public class GStringExpression extends Expression {
32  
33      private String verbatimText;
34      private List<ConstantExpression> strings = new ArrayList<ConstantExpression>();
35      private List<Expression> values = new ArrayList<Expression>();
36      
37      public GStringExpression(String verbatimText) {
38          this.verbatimText = verbatimText;
39          super.setType(ClassHelper.GSTRING_TYPE);
40      }
41  
42      public GStringExpression(String verbatimText, List<ConstantExpression> strings, List<Expression> values) {
43          this.verbatimText = verbatimText;
44          this.strings = strings;
45          this.values = values;
46          super.setType(ClassHelper.GSTRING_TYPE);
47      }
48  
49      public void visit(GroovyCodeVisitor visitor) {
50          visitor.visitGStringExpression(this);
51      }
52  
53      public Expression transformExpression(ExpressionTransformer transformer) {
54          Expression ret = new GStringExpression(
55                  verbatimText,
56                  transformExpressions(strings, transformer, ConstantExpression.class),
57                  transformExpressions(values, transformer));
58          ret.setSourcePosition(this);
59          return ret;        
60      }
61  
62      public String toString() {
63          return super.toString() + "[strings: " + strings + " values: " + values + "]";
64      }
65  
66      public String getText() {
67          return verbatimText;
68      }
69  
70      public List<ConstantExpression> getStrings() {
71          return strings;
72      }
73  
74      public List<Expression> getValues() {
75          return values;
76      }
77  
78      public void addString(ConstantExpression text) {
79          if (text == null) {
80              throw new NullPointerException("Cannot add a null text expression");
81          }
82          strings.add(text);
83      }
84  
85      public void addValue(Expression value) {
86          // If the first thing is an value, then we need a dummy empty string in front of it so that when we
87          // toString it they come out in the correct order.
88          if (strings.size() == 0)
89              strings.add(ConstantExpression.EMPTY_STRING);
90          values.add(value);
91      }
92  
93      public Expression getValue(int idx) {
94          return values.get(idx);
95      }
96  
97      public boolean isConstantString() {
98          return values.isEmpty();
99      }
100 
101     public Expression asConstantString() {
102         StringBuffer buffer = new StringBuffer();
103         for (ConstantExpression expression : strings) {
104             Object value = expression.getValue();
105             if (value != null) {
106                 buffer.append(value);
107             }
108         }
109         return new ConstantExpression(buffer.toString());
110     }
111 }