1 package org.apache.torque.generator.template.velocity; 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.io.File; 23 import java.util.Date; 24 import java.util.List; 25 26 import org.apache.commons.lang.StringUtils; 27 import org.apache.torque.generator.GeneratorException; 28 import org.apache.torque.generator.control.ControllerState; 29 import org.apache.torque.generator.source.SourceElement; 30 import org.apache.torque.generator.variable.Variable; 31 32 /** 33 * This class acts as an interface to the Torque generator from the 34 * templates. It lets the user access Torque generator properties from the 35 * templates, and allows to execute certain action from within the templates. 36 */ 37 public class TorqueGenVelocity 38 { 39 /** 40 * The state of the controller in which this generator interface is used. 41 */ 42 private ControllerState controllerState; 43 44 /** 45 * The outlet in which context this class is used. 46 */ 47 private VelocityOutlet outlet; 48 49 /** 50 * A counter which can be used in velocity templates. 51 */ 52 private static int counter = 1; 53 54 /** 55 * Constructs a generator interface within the given controllerState. 56 * 57 * @param outlet the outlet in which this generator interface will be used, 58 * not null. 59 * @param controllerState the controller context. 60 * 61 * @throws NullPointerException if outlet or controllerState are null. 62 */ 63 public TorqueGenVelocity( 64 VelocityOutlet outlet, 65 ControllerState controllerState) 66 { 67 if (controllerState == null) 68 { 69 throw new NullPointerException("controllerState may not be null"); 70 } 71 if (outlet == null) 72 { 73 throw new NullPointerException("outlet may not be null"); 74 } 75 this.controllerState = controllerState; 76 this.outlet = outlet; 77 } 78 79 /** 80 * Processes the mergepoint with the given name. 81 * 82 * @param mergepointName the name of the mergepoint. 83 * @return the output generated by the mergepoint. 84 * @throws GeneratorException if the mergepoint could not be processed 85 * completely. 86 */ 87 public String mergepoint(String mergepointName) 88 throws GeneratorException 89 { 90 return outlet.mergepoint(mergepointName, controllerState); 91 } 92 93 /** 94 * Returns the current controller state. 95 * 96 * @return The current controller state, never null. 97 */ 98 public ControllerState getControllerState() 99 { 100 return controllerState; 101 } 102 103 /** 104 * Returns the current source element. This method is shorthand for 105 * <code>getControllerState().getSourceElement()</code> 106 * 107 * @return the current source element, never null. 108 */ 109 public SourceElement getSourceElement() 110 { 111 return controllerState.getSourceElement(); 112 } 113 114 /** 115 * Returns all children of the current source element. 116 * This method is shorthand for 117 * <code>getSourceElement().getChildren()</code> 118 * 119 * @return the children of the current source element, never null. 120 */ 121 public List<SourceElement> getChildren() 122 { 123 return getSourceElement().getChildren(); 124 } 125 126 /** 127 * Returns the children of the current source element with a certain name. 128 * This method is shorthand for 129 * <code>getSourceElement().getChildren(name)</code> 130 * 131 * @param name the name of the children elements to select. 132 * 133 * @return the children of the current source element with the name name, 134 * never null. 135 */ 136 public List<SourceElement> getChildren(String name) 137 { 138 return getSourceElement().getChildren(name); 139 } 140 141 /** 142 * Returns the first child of the current source element 143 * with the given name. 144 * This method is shorthand for 145 * <code>getSourceElement().getChild(name)</code> 146 * 147 * @param name the name of the child element to select. 148 * 149 * @return the first child with the given name, or null if no such child 150 * exists. 151 */ 152 public SourceElement getChild(String name) 153 { 154 return getSourceElement().getChild(name); 155 } 156 157 /** 158 * Returns the parent of the current source element. 159 * <code>getSourceElement().getParent()</code> 160 * 161 * @return the parent of the current source element, or null if the current 162 * source element has no parent. 163 */ 164 public SourceElement getParent() 165 { 166 return getSourceElement().getParent(); 167 } 168 169 /** 170 * Returns the option with the given key. The key can either be a name 171 * prefixed with a namespace, or a name without namespace, in which case 172 * the namespace of the currently active outlet is used. 173 * 174 * In the case that the option is not set in this namespace, the parent 175 * namespaces are searched recursively. If the option is not set in any 176 * of the parent namespaces, null is returned. 177 * 178 * @param key the key for the option to retrieve. 179 * @return the option for the given key. 180 */ 181 public Object option(String key) 182 { 183 Object result = controllerState.getOption(key); 184 185 return result; 186 } 187 188 /** 189 * Returns the option with the given key as boolean value. 190 * The key can either be a name prefixed with a namespace, 191 * or a name without namespace, in which case the namespace of the 192 * currently active outlet is used. 193 * 194 * In the case that the option is not set in this namespace, the parent 195 * namespaces are searched recursively. If the option is not set in any 196 * of the parent namespaces, false is returned. 197 * 198 * @param key the key for the option to retrieve. 199 * @return the option for the given key, converted to a boolean 200 */ 201 public boolean booleanOption(String key) 202 { 203 boolean result = controllerState.getBooleanOption(key); 204 205 return result; 206 } 207 208 /** 209 * Returns the option with the given key as int value. 210 * The key can either be a name prefixed with a namespace, 211 * or a name without namespace, in which case the namespace of the 212 * currently active outlet is used. 213 * 214 * In the case that the option is not set in this namespace, the parent 215 * namespaces are searched recursively. If the option is not set in any 216 * of the parent namespaces or empty, 0 is returned. 217 * 218 * @param key the key for the option to retrieve. 219 * @return the option for the given key, converted to a boolean 220 */ 221 public int intOption(String key) 222 { 223 Object optionValue = controllerState.getOption(key); 224 if (optionValue == null) 225 { 226 return 0; 227 } 228 String optionString = optionValue.toString(); 229 if (StringUtils.isBlank(optionString)) 230 { 231 return 0; 232 } 233 234 return Integer.parseInt(optionString); 235 } 236 237 /** 238 * Returns the variable with the given key. The key can either be a name 239 * prefixed with a namespace, or a name without namespace, in which case 240 * the namespace of the currently active outlet is used. 241 * 242 * In the case that the variable is not set in this namespace, the parent 243 * namespaces are searched recursively. If the variable is not set in any 244 * of the parent namespaces, null is returned. 245 * 246 * @param key the key for the variable to retrieve. 247 * @return the variable for the given key, or null if the variable is not 248 * set or explicitly set to null. 249 */ 250 public Object getVariable(String key) 251 { 252 return outlet.getVariable(key, controllerState); 253 } 254 255 /** 256 * Sets a variable. The key can be given with or without namespace; 257 * in the latter case, the variable is set in the namespace of the 258 * currently active outlet. 259 * The Scope of the variable is this outlet and its children. 260 * 261 * @param key the name of the variable, not null 262 * @param value the value of the variable, may be null. 263 * 264 * @throws NullPointerException if key or scope is null. 265 * @throws IllegalArgumentException if the key is no valid QualifiedName. 266 */ 267 public void setVariable(String key, Object value) 268 { 269 outlet.setVariable(key, value, controllerState); 270 } 271 272 /** 273 * Sets a variable. The key can be given with or without namespace; 274 * in the latter case, the variable is set in the namespace of the 275 * currently active outlet. 276 * 277 * @param key the name of the variable, not null. 278 * @param value the value of the variable, may be null. 279 * @param scope the scope of the variable, not null. 280 * 281 * @throws NullPointerException if key or scope is null. 282 * @throws IllegalArgumentException if the key is no valid QualifiedName. 283 */ 284 public void setVariable(String key, Object value, String scope) 285 { 286 Variable.Scope scopeValue = Variable.Scope.valueOf(scope); 287 outlet.setVariable(key, value, scopeValue, controllerState); 288 } 289 290 /** 291 * Returns the currently processed source file. 292 * 293 * @return the source file which is currently processed. 294 */ 295 public File getSourceFile() 296 { 297 return controllerState.getSourceFile(); 298 } 299 300 /** 301 * Returns the current date. 302 * 303 * @return the current date, not null. 304 */ 305 public Date now() 306 { 307 return new Date(); 308 } 309 310 /** 311 * Returns a counter value which is increased each time this function is 312 * accessed. Start value is 1. 313 * If <code>resetCounter</code> is not called, the returned value is unique 314 * over the generation process. 315 * 316 * @return the counter value. 317 */ 318 public static synchronized int getCounter() 319 { 320 return counter++; 321 } 322 323 /** 324 * Resets the counter accessible though <code>getCounter()</code> back to 1. 325 */ 326 public static synchronized void resetCounter() 327 { 328 counter = 1; 329 } 330 }