KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
20 import java.io.Writer JavaDoc;
21
22 import org.apache.velocity.context.InternalContextAdapter;
23 import org.apache.velocity.runtime.RuntimeConstants;
24 import org.apache.velocity.runtime.parser.Parser;
25
26 import org.apache.velocity.exception.MethodInvocationException;
27
28 import org.apache.velocity.app.event.EventCartridge;
29
30 /**
31  * Node for the #set directive
32  *
33  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
34  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
35  * @version $Id: ASTSetDirective.java,v 1.22.4.1 2004/03/03 23:22:59 geirm Exp $
36  */

37 public class ASTSetDirective extends SimpleNode
38 {
39     private String JavaDoc leftReference = "";
40     private Node right;
41     private ASTReference left;
42     boolean blather = false;
43
44     public ASTSetDirective(int id)
45     {
46         super(id);
47     }
48
49     public ASTSetDirective(Parser p, int id)
50     {
51         super(p, id);
52     }
53
54     /** Accept the visitor. **/
55     public Object JavaDoc jjtAccept(ParserVisitor visitor, Object JavaDoc data)
56     {
57         return visitor.visit(this, data);
58     }
59
60     /**
61      * simple init. We can get the RHS and LHS as the the tree structure is static
62      */

63     public Object JavaDoc init(InternalContextAdapter context, Object JavaDoc data)
64             throws Exception JavaDoc
65     {
66         /*
67          * init the tree correctly
68          */

69
70         super.init( context, data );
71
72         right = getRightHandSide();
73         left = getLeftHandSide();
74
75         blather = rsvc.getBoolean(RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID, true);
76  
77         /*
78          * grab this now. No need to redo each time
79          */

80         leftReference = left.getFirstToken().image.substring(1);
81
82         return data;
83     }
84
85     /**
86      * puts the value of the RHS into the context under the key of the LHS
87      */

88     public boolean render( InternalContextAdapter context, Writer JavaDoc writer)
89         throws IOException JavaDoc, MethodInvocationException
90     {
91         /*
92          * get the RHS node, and it's value
93          */

94
95         Object JavaDoc value = right.value(context);
96
97         /*
98          * it's an error if we don't have a value of some sort
99          */

100
101         if ( value == null)
102         {
103             /*
104              * first, are we supposed to say anything anyway?
105              */

106             if(blather)
107             {
108                 EventCartridge ec = context.getEventCartridge();
109
110                 boolean doit = true;
111                
112                 /*
113                  * if we have an EventCartridge...
114                  */

115                 if (ec != null)
116                 {
117                     doit = ec.shouldLogOnNullSet( left.literal(), right.literal() );
118                 }
119
120                 if (doit)
121                 {
122                     rsvc.error("RHS of #set statement is null. Context will not be modified. "
123                                   + context.getCurrentTemplateName() + " [line " + getLine()
124                                   + ", column " + getColumn() + "]");
125                 }
126             }
127
128             return false;
129         }
130
131         /*
132          * if the LHS is simple, just punch the value into the context
133          * otherwise, use the setValue() method do to it.
134          * Maybe we should always use setValue()
135          */

136         
137         if (left.jjtGetNumChildren() == 0)
138         {
139             context.put( leftReference, value);
140         }
141         else
142         {
143             left.setValue(context, value);
144         }
145     
146         return true;
147     }
148
149     /**
150      * returns the ASTReference that is the LHS of the set statememt
151      */

152     private ASTReference getLeftHandSide()
153     {
154         return (ASTReference) jjtGetChild(0);
155
156      // return (ASTReference) jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
157
}
158
159     /**
160      * returns the RHS Node of the set statement
161      */

162     private Node getRightHandSide()
163     {
164         return jjtGetChild(1);
165 // return jjtGetChild(0).jjtGetChild(0).jjtGetChild(1).jjtGetChild(0);
166
}
167 }
168
Popular Tags