KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > runtime > parser > node > ASTStringLiteral


1 package org.apache.velocity.runtime.parser.node;
2
3 /*
4  * Copyright 2000-2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.velocity.context.InternalContextAdapter;
20 import org.apache.velocity.runtime.parser.Parser;
21
22 import java.io.StringWriter JavaDoc;
23 import java.io.BufferedReader JavaDoc;
24 import java.io.StringReader JavaDoc;
25
26 import org.apache.velocity.runtime.RuntimeConstants;
27
28 /**
29  * ASTStringLiteral support. Will interpolate!
30  *
31  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
32  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
33  * @version $Id: ASTStringLiteral.java,v 1.17.4.1 2004/03/03 23:22:59 geirm Exp $
34  */

35 public class ASTStringLiteral extends SimpleNode
36 {
37     /* cache the value of the interpolation switch */
38     private boolean interpolate = true;
39     private SimpleNode nodeTree = null;
40     private String JavaDoc image = "";
41     private String JavaDoc 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     /**
54      * init : we don't have to do much. Init the tree (there
55      * shouldn't be one) and then see if interpolation is turned on.
56      */

57     public Object JavaDoc init(InternalContextAdapter context, Object JavaDoc data)
58         throws Exception JavaDoc
59     {
60         /*
61          * simple habit... we prollie don't have an AST beneath us
62          */

63
64         super.init(context, data);
65
66         /*
67          * the stringlit is set at template parse time, so we can
68          * do this here for now. if things change and we can somehow
69          * create stringlits at runtime, this must
70          * move to the runtime execution path
71          *
72          * so, only if interpolation is turned on AND it starts
73          * with a " AND it has a directive or reference, then we
74          * can interpolate. Otherwise, don't bother.
75          */

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         /*
83          * get the contents of the string, minus the '/" at each end
84          */

85         
86         image = getFirstToken().image.substring(1,
87                                                 getFirstToken().image.length() - 1);
88
89         /*
90          * tack a space on the end (dreaded <MORE> kludge)
91          */

92
93         interpolateimage = image + " ";
94
95         if (interpolate)
96         {
97             /*
98              * now parse and init the nodeTree
99              */

100             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new StringReader JavaDoc(interpolateimage));
101
102             /*
103              * it's possible to not have an initialization context - or we don't
104              * want to trust the caller - so have a fallback value if so
105              *
106              * Also, do *not* dump the VM namespace for this template
107              */

108
109             nodeTree = rsvc.parse(br, (context != null) ?
110                     context.getCurrentTemplateName() : "StringLiteral", false);
111
112             /*
113              * init with context. It won't modify anything
114              */

115
116             nodeTree.init(context, rsvc);
117         }
118
119         return data;
120     }
121
122     /** Accept the visitor. **/
123     public Object JavaDoc jjtAccept(ParserVisitor visitor, Object JavaDoc data)
124     {
125         return visitor.visit(this, data);
126     }
127
128     /**
129      * renders the value of the string literal
130      * If the properties allow, and the string literal contains a $ or a #
131      * the literal is rendered against the context
132      * Otherwise, the stringlit is returned.
133      */

134     public Object JavaDoc value(InternalContextAdapter context)
135     {
136         if (interpolate)
137         {
138             try
139             {
140                 /*
141                  * now render against the real context
142                  */

143
144                 StringWriter JavaDoc writer = new StringWriter JavaDoc();
145                 nodeTree.render(context, writer);
146                 
147                 /*
148                  * and return the result as a String
149                  */

150
151                 String JavaDoc ret = writer.toString();
152
153                 /*
154                  * remove the space from the end (dreaded <MORE> kludge)
155                  */

156
157                 return ret.substring(0, ret.length() - 1);
158             }
159             catch(Exception JavaDoc e)
160             {
161                 /*
162                  * eh. If anything wrong, just punt
163                  * and output the literal
164                  */

165                 rsvc.error("Error in interpolating string literal : " + e);
166             }
167         }
168         
169         /*
170          * ok, either not allowed to interpolate, there wasn't
171          * a ref or directive, or we failed, so
172          * just output the literal
173          */

174
175         return image;
176     }
177 }
178
Popular Tags