1 package net.sourceforge.pmd.lang.vm.ast; 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 /** 23 * This class is responsible for handling the references in VTL ($foo). 24 * 25 * Please look at the Parser.jjt file which is what controls the generation of this class. 26 * 27 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> 28 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> 29 * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph Reck</a> 30 * @author <a href="mailto:kjohnson@transparent.com>Kent Johnson</a> 31 * @version $Id: ASTReference.java 806597 2009-08-21 15:21:44Z nbubna $ 32 */ 33 public class ASTReference extends AbstractVmNode { 34 private String rootString; 35 36 private String literal = null; 37 38 /** 39 * Indicates if we are running in strict reference mode. 40 */ 41 public boolean strictRef = false; 42 43 /** 44 * Indicates if toString() should be called during condition evaluation just to ensure it does not return null. 45 * Check is unnecessary if all toString() implementations are known to have non-null return values. Disabling the 46 * check will give a performance improval since toString() may be a complex operation on large objects. 47 */ 48 public boolean toStringNullCheck = true; 49 50 /** 51 * @param id 52 */ 53 public ASTReference(final int id) { 54 super(id); 55 } 56 57 /** 58 * @param p 59 * @param id 60 */ 61 public ASTReference(final VmParser p, final int id) { 62 super(p, id); 63 } 64 65 /** 66 * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.node.VmParserVisitor, 67 * java.lang.Object) 68 */ 69 @Override 70 public Object jjtAccept(final VmParserVisitor visitor, final Object data) { 71 return visitor.visit(this, data); 72 } 73 74 /** 75 * Returns the 'root string', the reference key 76 * 77 * @return the root string. 78 */ 79 public String getRootString() { 80 return rootString; 81 } 82 83 /** 84 * Routine to allow the literal representation to be externally overridden. Used now in the VM system to override a 85 * reference in a VM tree with the literal of the calling arg to make it work nicely when calling arg is null. It 86 * seems a bit much, but does keep things consistant. 87 * 88 * Note, you can only set the literal once... 89 * 90 * @param literal String to render to when null 91 */ 92 public void setLiteral(final String literal) { 93 /* 94 * do only once 95 */ 96 97 if (this.literal == null) 98 this.literal = literal; 99 } 100 101 /** 102 * Override of the SimpleNode method literal() Returns the literal representation of the node. Should be something 103 * like $<token>. 104 * 105 * @return A literal string. 106 */ 107 @Override 108 public String literal() { 109 if (literal != null) 110 return literal; 111 112 // this value could be cached in this.literal but it increases memory usage 113 return super.literal(); 114 } 115 }