1 46 package org.codehaus.groovy.ast.expr; 47 48 import groovy.lang.GString; 49 50 import java.util.ArrayList ; 51 import java.util.Iterator ; 52 import java.util.List ; 53 54 import org.codehaus.groovy.ast.GroovyCodeVisitor; 55 import org.codehaus.groovy.classgen.AsmClassGenerator2; 56 57 64 public class GStringExpression extends Expression { 65 66 private String verbatimText; 67 private List strings = new ArrayList (); 68 private List values = new ArrayList (); 69 70 public GStringExpression(String verbatimText) { 71 this.verbatimText = verbatimText; 72 super.setTypeClass(GString.class); 73 } 74 75 public GStringExpression(String verbatimText, List strings, List values) { 76 this.verbatimText = verbatimText; 77 this.strings = strings; 78 this.values = values; 79 super.setTypeClass(GString.class); 80 } 81 82 public void visit(GroovyCodeVisitor visitor) { 83 visitor.visitGStringExpression(this); 84 } 85 86 public Expression transformExpression(ExpressionTransformer transformer) { 87 return new GStringExpression( 88 verbatimText, 89 transformExpressions(strings, transformer), 90 transformExpressions(values, transformer)); 91 } 92 93 protected void resolveType(AsmClassGenerator2 resolver) { 94 } 96 97 public String toString() { 98 return super.toString() + "[strings: " + strings + " values: " + values + "]"; 99 } 100 101 public List getStrings() { 102 return strings; 103 } 104 105 public List getValues() { 106 return values; 107 } 108 109 public void addString(ConstantExpression text) { 110 if (text == null) { 111 throw new NullPointerException ("Cannot add a null text expression"); 112 } 113 strings.add(text); 114 } 115 116 public void addValue(Expression value) { 117 if (strings.size() == 0) 120 strings.add(ConstantExpression.EMPTY_STRING); 121 values.add(value); 122 } 123 124 public Expression getValue(int idx) { 125 return (Expression) values.get(idx); 126 } 127 128 public boolean isConstantString() { 129 return values.isEmpty(); 130 } 131 132 public Expression asConstantString() { 133 StringBuffer buffer = new StringBuffer (); 134 for (Iterator iter = strings.iterator(); iter.hasNext();) { 135 ConstantExpression expression = (ConstantExpression) iter.next(); 136 Object value = expression.getValue(); 137 if (value != null) { 138 buffer.append(value); 139 } 140 } 141 return new ConstantExpression(buffer.toString()); 142 } 143 } 144 | Popular Tags |