1 package org.apache.velocity.runtime.parser.node; 2 3 18 19 import org.apache.velocity.context.InternalContextAdapter; 20 import org.apache.velocity.runtime.parser.Parser; 21 22 import java.io.StringWriter ; 23 import java.io.BufferedReader ; 24 import java.io.StringReader ; 25 26 import org.apache.velocity.runtime.RuntimeConstants; 27 28 35 public class ASTStringLiteral extends SimpleNode 36 { 37 38 private boolean interpolate = true; 39 private SimpleNode nodeTree = null; 40 private String image = ""; 41 private String interpolateimage = ""; 42 43 public ASTStringLiteral(int id) 44 { 45 super(id); 46 } 47 48 public ASTStringLiteral(Parser p, int id) 49 { 50 super(p, id); 51 } 52 53 57 public Object init(InternalContextAdapter context, Object data) 58 throws Exception 59 { 60 63 64 super.init(context, data); 65 66 76 77 interpolate = rsvc.getBoolean(RuntimeConstants.INTERPOLATE_STRINGLITERALS , true) 78 && getFirstToken().image.startsWith("\"") 79 && ((getFirstToken().image.indexOf('$') != -1) 80 || (getFirstToken().image.indexOf('#') != -1)); 81 82 85 86 image = getFirstToken().image.substring(1, 87 getFirstToken().image.length() - 1); 88 89 92 93 interpolateimage = image + " "; 94 95 if (interpolate) 96 { 97 100 BufferedReader br = new BufferedReader (new StringReader (interpolateimage)); 101 102 108 109 nodeTree = rsvc.parse(br, (context != null) ? 110 context.getCurrentTemplateName() : "StringLiteral", false); 111 112 115 116 nodeTree.init(context, rsvc); 117 } 118 119 return data; 120 } 121 122 123 public Object jjtAccept(ParserVisitor visitor, Object data) 124 { 125 return visitor.visit(this, data); 126 } 127 128 134 public Object value(InternalContextAdapter context) 135 { 136 if (interpolate) 137 { 138 try 139 { 140 143 144 StringWriter writer = new StringWriter (); 145 nodeTree.render(context, writer); 146 147 150 151 String ret = writer.toString(); 152 153 156 157 return ret.substring(0, ret.length() - 1); 158 } 159 catch(Exception e) 160 { 161 165 rsvc.error("Error in interpolating string literal : " + e); 166 } 167 } 168 169 174 175 return image; 176 } 177 } 178 | Popular Tags |