1 package org.apache.velocity.runtime.visitor;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.Map;
23
24 import org.apache.velocity.runtime.parser.node.ASTReference;
25
26 /**
27 * This class is a visitor used by the VM proxy to change the
28 * literal representation of a reference in a VM. The reason is
29 * to preserve the 'render literal if null' behavior w/o making
30 * the VMProxy stuff more complicated than it is already.
31 *
32 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
33 * @version $Id: VMReferenceMungeVisitor.java 463298 2006-10-12 16:10:32Z henning $
34 */
35 public class VMReferenceMungeVisitor extends BaseVisitor
36 {
37 /**
38 * Map containing VM arg to instance-use reference
39 * Passed in with CTOR
40 */
41 private Map argmap = null;
42
43 /**
44 * CTOR - takes a map of args to reference
45 * @param map
46 */
47 public VMReferenceMungeVisitor( Map map )
48 {
49 argmap = map;
50 }
51
52 /**
53 * Visitor method - if the literal is right, will
54 * set the literal in the ASTReference node
55 *
56 * @param node ASTReference to work on
57 * @param data Object to pass down from caller
58 * @return A visitor object result.
59 */
60 public Object visit( ASTReference node, Object data)
61 {
62 /*
63 * see if there is an override value for this
64 * reference
65 */
66 String override = (String) argmap.get( node.literal().substring(1) );
67
68 /*
69 * if so, set in the node
70 */
71 if( override != null)
72 {
73 node.setLiteral( override );
74 }
75
76 /*
77 * feed the children...
78 */
79 data = node.childrenAccept(this, data);
80
81 return data;
82 }
83 }
84