1 package org.apache.velocity.runtime.parser.node; 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 org.apache.velocity.context.InternalContextAdapter; 23 import org.apache.velocity.exception.MethodInvocationException; 24 import org.apache.velocity.exception.ParseErrorException; 25 import org.apache.velocity.exception.ResourceNotFoundException; 26 import org.apache.velocity.exception.TemplateInitException; 27 import org.apache.velocity.runtime.RuntimeConstants.SpaceGobbling; 28 import org.apache.velocity.runtime.parser.Parser; 29 30 import java.io.IOException; 31 import java.io.Writer; 32 33 34 /** 35 * 36 */ 37 public class ASTBlock extends SimpleNode 38 { 39 private String prefix = ""; 40 private String postfix = ""; 41 42 // used during parsing 43 public boolean endsWithNewline = false; 44 45 /* 46 * '#' and '$' prefix characters eaten by javacc MORE mode, prefixing the '#' ending the block 47 */ 48 private String morePostfix = ""; 49 50 /** 51 * @param id 52 */ ASTBlock(int id)53 public ASTBlock(int id) 54 { 55 super(id); 56 } 57 58 /** 59 * @param p 60 * @param id 61 */ ASTBlock(Parser p, int id)62 public ASTBlock(Parser p, int id) 63 { 64 super(p, id); 65 } 66 67 /** 68 * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.node.ParserVisitor, java.lang.Object) 69 */ 70 @Override jjtAccept(ParserVisitor visitor, Object data)71 public Object jjtAccept(ParserVisitor visitor, Object data) 72 { 73 return visitor.visit(this, data); 74 } 75 76 /** 77 * @throws TemplateInitException 78 * @see org.apache.velocity.runtime.parser.node.Node#init(org.apache.velocity.context.InternalContextAdapter, java.lang.Object) 79 */ 80 @Override init(InternalContextAdapter context, Object data)81 public Object init(InternalContextAdapter context, Object data) throws TemplateInitException 82 { 83 Object obj = super.init(context, data); 84 cleanupParserAndTokens(); // drop reference to Parser and all JavaCC Tokens 85 return obj; 86 } 87 88 /** 89 * set indentation prefix 90 * @param prefix 91 */ setPrefix(String prefix)92 public void setPrefix(String prefix) 93 { 94 this.prefix = prefix; 95 } 96 97 /** 98 * get indentation prefix 99 * @return indentation prefix 100 */ getPrefix()101 public String getPrefix() 102 { 103 return prefix; 104 } 105 setMorePostfix(String morePosffix)106 public void setMorePostfix(String morePosffix) 107 { 108 this.morePostfix = morePosffix; 109 } 110 111 /** 112 * set indentation postfix 113 * @param postfix 114 */ setPostfix(String postfix)115 public void setPostfix(String postfix) 116 { 117 this.postfix = postfix; 118 } 119 120 /** 121 * get indentation postfix 122 * @return indentation prefix 123 */ getPostfix()124 public String getPostfix() 125 { 126 return postfix; 127 } 128 129 /** 130 * @see org.apache.velocity.runtime.parser.node.SimpleNode#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer) 131 */ 132 @Override render(InternalContextAdapter context, Writer writer)133 public boolean render(InternalContextAdapter context, Writer writer) 134 throws IOException, MethodInvocationException, 135 ResourceNotFoundException, ParseErrorException 136 { 137 SpaceGobbling spaceGobbling = rsvc.getSpaceGobbling(); 138 139 if (spaceGobbling == SpaceGobbling.NONE) 140 { 141 writer.write(prefix); 142 } 143 144 int i, k = jjtGetNumChildren(); 145 146 for (i = 0; i < k; i++) 147 jjtGetChild(i).render(context, writer); 148 149 if (morePostfix.length() > 0 || spaceGobbling.compareTo(SpaceGobbling.LINES) < 0) 150 { 151 writer.write(postfix); 152 } 153 154 writer.write(morePostfix); 155 156 return true; 157 } 158 } 159