Author: tfischer Date: Sat Mar 23 20:32:39 2013 New Revision: 1460227 URL: http://svn.apache.org/r1460227 Log: TORQUE-272 Support groovy templates out of the box
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/GroovyOutletSaxHandler.java db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/GroovyOutletSaxHandlerFactory.java db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyOutlet.java db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/TorqueGenGroovy.java db/torque/torque4/trunk/torque-generator/src/test/groovyTemplates/ - copied from r1456690, db/torque/torque4/trunk/torque-generator/src/test/otherTemplateLanguages/ db/torque/torque4/trunk/torque-generator/src/test/groovyTemplates/src/main/torque-gen/templates/template.gtl - copied, changed from r1456690, db/torque/torque4/trunk/torque-generator/src/test/otherTemplateLanguages/src/main/torque-gen/templates/template.groovy db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/template/groovy/GroovyTemplateTest.java - copied, changed from r1456690, db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/configuration/outlet/OtherTemplateLanguageTest.java Removed: db/torque/torque4/trunk/torque-generator/src/test/groovyTemplates/groovyOutlet.xsd db/torque/torque4/trunk/torque-generator/src/test/groovyTemplates/src/main/torque-gen/templates/template.groovy db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/configuration/outlet/GroovyOutletSaxHandler.java db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/configuration/outlet/OtherTemplateLanguageTest.java db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/template/groovy/GroovyOutlet.java db/torque/torque4/trunk/torque-generator/src/test/otherTemplateLanguages/ Modified: db/torque/torque4/trunk/torque-generator/pom.xml db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/OutletConfigurationTags.java db/torque/torque4/trunk/torque-generator/src/main/resources/org/apache/torque/generator/configuration/outlet.xsd db/torque/torque4/trunk/torque-generator/src/test/groovyTemplates/src/main/torque-gen/outlets/outlets.xml Modified: db/torque/torque4/trunk/torque-generator/pom.xml URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/pom.xml?rev=1460227&r1=1460226&r2=1460227&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-generator/pom.xml (original) +++ db/torque/torque4/trunk/torque-generator/pom.xml Sat Mar 23 20:32:39 2013 @@ -39,11 +39,6 @@ <dependencies> <dependency> - <groupId>org.apache.velocity</groupId> - <artifactId>velocity</artifactId> - <version>1.7</version> - </dependency> - <dependency> <groupId>org.eclipse.jgit</groupId> <artifactId>org.eclipse.jgit</artifactId> <version>2.1.0.201209190230-r</version> @@ -85,6 +80,17 @@ <version>1.4</version> </dependency> <dependency> + <groupId>org.apache.velocity</groupId> + <artifactId>velocity</artifactId> + <version>1.7</version> + </dependency> + <dependency> + <groupId>org.codehaus.groovy</groupId> + <artifactId>groovy-all</artifactId> + <version>2.1.0</version> + <scope>test</scope> + </dependency> + <dependency> <!-- We do not depend on any xerces features. However, the standard xml parser packaged in java is sometimes buggy (e.g.1.6.0_20 on windows) @@ -119,12 +125,6 @@ <version>10.5.3.0_1</version> <scope>test</scope> </dependency> - <dependency> - <groupId>org.codehaus.groovy</groupId> - <artifactId>groovy-all</artifactId> - <version>1.6.0</version> - <scope>test</scope> - </dependency> </dependencies> <build> Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/GroovyOutletSaxHandler.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/GroovyOutletSaxHandler.java?rev=1460227&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/GroovyOutletSaxHandler.java (added) +++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/GroovyOutletSaxHandler.java Sat Mar 23 20:32:39 2013 @@ -0,0 +1,162 @@ +package org.apache.torque.generator.configuration.outlet; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import static org.apache.torque.generator.configuration.outlet.OutletConfigurationTags.OUTLET_ENCODING_ATTRIBUTE; +import static org.apache.torque.generator.configuration.outlet.OutletConfigurationTags.OUTLET_NAME_ATTRIBUTE; +import static org.apache.torque.generator.configuration.outlet.OutletConfigurationTags.OUTLET_OPTIONS_IN_BINDING_ATTRIBUTE; +import static org.apache.torque.generator.configuration.outlet.OutletConfigurationTags.OUTLET_PATH_ATTRIBUTE; +import static org.apache.torque.generator.configuration.outlet.OutletConfigurationTags.OUTLET_SOURCE_ATTRIBUTES_IN_BINDING_ATTRIBUTE; +import static org.apache.torque.generator.configuration.outlet.OutletConfigurationTags.OUTLET_VARIABLES_IN_BINDING_ATTRIBUTE; + +import org.apache.torque.generator.configuration.ConfigurationException; +import org.apache.torque.generator.configuration.ConfigurationHandlers; +import org.apache.torque.generator.configuration.ConfigurationProvider; +import org.apache.torque.generator.configuration.SaxHelper; +import org.apache.torque.generator.configuration.UnitDescriptor; +import org.apache.torque.generator.qname.QualifiedName; +import org.apache.torque.generator.template.groovy.GroovyOutlet; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + +/** + * Handles a declaration of a groovy outlet within a outlet + * configuration file. + */ +class GroovyOutletSaxHandler extends OutletSaxHandler +{ + /** + * Constructor. + * + * @param outletName the name for the outlet which configuration + * will be read in by the generated SaxHandlerFactory, + * or null if the name of the outlet should be determined from + * the parsed XML. + * @param configurationProvider The access object for the configuration + * files, not null. + * @param unitDescriptor The description of the generation unit, not null. + * @param configurationHandlers the available configuration handlers, + * not null. + * + * @throws SAXException if an error occurs during creation of the outlet. + */ + public GroovyOutletSaxHandler( + QualifiedName outletName, + ConfigurationProvider configurationProvider, + UnitDescriptor unitDescriptor, + ConfigurationHandlers configurationHandlers) + throws SAXException + { + super(outletName, + configurationProvider, + unitDescriptor, + configurationHandlers); + } + + /** + * Instantiates and configures a groovy outlet. + * + * @param outletName the name for the outlet which configuration + * will be read in by the generated SaxHandlerFactory, + * or null if the name of the outlet should be determined from + * the parsed xml. + * @param uri - The Namespace URI, or the empty string if the + * element has no Namespace URI or if Namespace processing is not + * being performed. + * @param localName - The local name (without prefix), or + * the empty string if Namespace processing is not being performed. + * @param rawName - The qualified name (with prefix), or the empty string if + * qualified names are not available. + * @param attributes - The attributes attached to the element. + * If there are no attributes, it shall be an empty Attributes + * object. + * + * @return the created outlet, not null. + * + * @throws SAXException if an error occurs during creation. + */ + protected GroovyOutlet createOutlet( + QualifiedName outletName, + String uri, + String localName, + String rawName, + Attributes attributes) + throws SAXException + { + if (outletName == null) + { + String nameAttribute + = attributes.getValue(OUTLET_NAME_ATTRIBUTE); + if (nameAttribute == null) + { + throw new SAXException("The attribute " + + OUTLET_NAME_ATTRIBUTE + + " must be set on the element " + + rawName + + " for Groovy Outlets"); + } + outletName = new QualifiedName(nameAttribute); + } + + String encoding = attributes.getValue(OUTLET_ENCODING_ATTRIBUTE); + String path = attributes.getValue(OUTLET_PATH_ATTRIBUTE); + + try + { + GroovyOutlet result + = new GroovyOutlet( + outletName, + getConfigurationProvider(), + path, + encoding); + Boolean optionsInContext = SaxHelper.getBooleanAttribute( + OUTLET_OPTIONS_IN_BINDING_ATTRIBUTE, + attributes, + "the groovyOutlet" + outletName); + if (optionsInContext != null) + { + result.setOptionsInBinding(optionsInContext); + } + Boolean sourceElementAttributesInContext + = SaxHelper.getBooleanAttribute( + OUTLET_SOURCE_ATTRIBUTES_IN_BINDING_ATTRIBUTE, + attributes, + "the groovyOutlet" + outletName); + if (sourceElementAttributesInContext != null) + { + result.setSourceAttributesInBinding( + sourceElementAttributesInContext); + } + Boolean variablesInContext = SaxHelper.getBooleanAttribute( + OUTLET_VARIABLES_IN_BINDING_ATTRIBUTE, + attributes, + "the groovyOutlet" + outletName); + if (variablesInContext != null) + { + result.setVariablesInContext(variablesInContext); + } + return result; + } + catch (ConfigurationException e) + { + throw new SAXException(e); + } + } +} Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/GroovyOutletSaxHandlerFactory.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/GroovyOutletSaxHandlerFactory.java?rev=1460227&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/GroovyOutletSaxHandlerFactory.java (added) +++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/GroovyOutletSaxHandlerFactory.java Sat Mar 23 20:32:39 2013 @@ -0,0 +1,81 @@ +package org.apache.torque.generator.configuration.outlet; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.torque.generator.configuration.ConfigurationHandlers; +import org.apache.torque.generator.configuration.ConfigurationProvider; +import org.apache.torque.generator.configuration.UnitDescriptor; +import org.apache.torque.generator.qname.QualifiedName; +import org.xml.sax.SAXException; + +/** + * Creates Groovy outlet SAX handlers. + */ +public class GroovyOutletSaxHandlerFactory + implements TypedOutletSaxHandlerFactory +{ + /** + * The type of the outlets which can be processed by the + * OutletSaxHandlers created by this factory. + */ + private static final String OUTLET_TYPE = "groovyOutlet"; + + /** + * Returns the outlet type which can be handled by the + * OutletSaxHandlers created by this factory. + * + * @return "groovyOutlet". + */ + public String getType() + { + return OUTLET_TYPE; + } + + /** + * Returns a GroovyOutletSaxHandler for reading the configuration of + * Groovy outlets. This implementation uses the provided name + * as outlet name. + * + * @param outletName the name for the outlet which configuration + * will be read in by the generated SaxHandlerFactory, + * or null if the name of the outlet should be determined from + * the parsed xml. + * @param configurationProvider The access object for the configuration + * files, not null. + * @param unitDescriptor The description of the generation unit, not null. + * @param configurationHandlers the available configuration handlers, + * not null. + * + * @return a new GroovyOutletSaxHandler. + */ + public final OutletSaxHandler getOutletSaxHandler( + QualifiedName outletName, + ConfigurationProvider configurationProvider, + UnitDescriptor unitDescriptor, + ConfigurationHandlers configurationHandlers) + throws SAXException + { + return new GroovyOutletSaxHandler( + outletName, + configurationProvider, + unitDescriptor, + configurationHandlers); + } +} Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/OutletConfigurationTags.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/OutletConfigurationTags.java?rev=1460227&r1=1460226&r2=1460227&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/OutletConfigurationTags.java (original) +++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/OutletConfigurationTags.java Sat Mar 23 20:32:39 2013 @@ -74,6 +74,28 @@ public final class OutletConfigurationTa public static final String OUTLET_VARIABLES_IN_CONTEXT_ATTRIBUTE = "variablesInContext"; + /** + * Attribute name for the attribute "optionsInBinding" + * of the "outlet" tag. + */ + public static final String OUTLET_OPTIONS_IN_BINDING_ATTRIBUTE + = "optionsInBinding"; + + /** + * Attribute name for the attribute "sourceAttributesInBinding" + * of the "outlet" tag. + */ + public static final String + OUTLET_SOURCE_ATTRIBUTES_IN_BINDING_ATTRIBUTE + = "sourceAttributesInBinding"; + + /** + * Attribute name for the attribute "variablesInBinding" + * of the "outlet" tag. + */ + public static final String OUTLET_VARIABLES_IN_BINDING_ATTRIBUTE + = "variablesInBinding"; + /** Tag name for the "input" tag. */ public static final String INPUT_TAG = "input"; Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyOutlet.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyOutlet.java?rev=1460227&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyOutlet.java (added) +++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyOutlet.java Sat Mar 23 20:32:39 2013 @@ -0,0 +1,324 @@ +package org.apache.torque.generator.template.groovy; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import groovy.lang.Writable; +import groovy.text.GStringTemplateEngine; +import groovy.text.Template; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.torque.generator.GeneratorException; +import org.apache.torque.generator.configuration.ConfigurationException; +import org.apache.torque.generator.configuration.ConfigurationProvider; +import org.apache.torque.generator.control.ControllerState; +import org.apache.torque.generator.option.Option; +import org.apache.torque.generator.option.Options; +import org.apache.torque.generator.outlet.OutletResult; +import org.apache.torque.generator.qname.Namespace; +import org.apache.torque.generator.qname.QualifiedName; +import org.apache.torque.generator.qname.QualifiedNameMap; +import org.apache.torque.generator.source.SourceElement; +import org.apache.torque.generator.template.TemplateOutletImpl; +import org.apache.torque.generator.variable.Variable; +import org.apache.torque.generator.variable.VariableStore; + + +/** + * A Outlet which uses a groovy script for generation. + */ +public class GroovyOutlet extends TemplateOutletImpl +{ + /** + * The name under which the Torque generator interface will be put + * into the binding. + */ + public static final String TORQUE_GEN_BINDING_NAME = "torqueGen"; + + /** + * The key under which the null attribute of a source element is put + * into the binding. + */ + public static final String NULL_KEY_BINDING_NAME = "value"; + + /** The log. */ + private static Log log = LogFactory.getLog(GroovyOutlet.class); + + /** + * Whether the options should be put into the context. + */ + private boolean optionsInBinding = true; + + /** + * Whether the variables should be put into the context. + */ + private boolean variablesInBinding = true; + + /** + * Whether the attributes of the current source element should be put + * into the context. + */ + private boolean sourceAttributesInBinding = true; + + /** + * Constructs a new GroovyOutlet. + * + * @param name the name of this outlet, not null. + * @param configurationProvider the provider for reading the templates, + * not null. + * @param path the path to the templates, not null. + * @param encoding the encoding of the file, or null if the system's + * default encoding should be used. + * + * @throws NullPointerException if name, path or directories are null. + * @throws ConfigurationException if an error occurs while reading the + * template. + */ + public GroovyOutlet( + QualifiedName name, + ConfigurationProvider configurationProvider, + String path, + String encoding) + throws ConfigurationException + { + super(name, + configurationProvider, + path, + encoding, + null); + } + + /** + * Executes the generation process; the result is returned. + * + * @param controllerState the current controller state. + * + * @return the result of the generation, not null. + * + * @see org.apache.torque.generator.outlet.Outlet#execute(ControllerState) + */ + @Override + public OutletResult execute(ControllerState controllerState) + throws GeneratorException + + { + if (log.isDebugEnabled()) + { + log.debug("Start executing GroovyOutlet " + getName()); + } + + SourceElement sourceElement = controllerState.getSourceElement(); + + String inputElementName = getInputElementName(); + if (inputElementName != null + && !inputElementName.equals(sourceElement.getName())) + { + throw new GeneratorException("Input element name, " + + sourceElement.getName() + + ", is not the expected name, " + + getInputElementName() + + ", for outlet " + + getName()); + } + + try + { + Map<String, Object> binding = createBinding(controllerState); + + GStringTemplateEngine templateEngine = new GStringTemplateEngine(); + Template template = templateEngine.createTemplate( + getContent(controllerState)); + Writable writable = template.make(binding); + String result = writable.toString(); + return new OutletResult(result); + } + catch (Exception e) + { + throw new GeneratorException(e); + } + finally + { + if (log.isDebugEnabled()) + { + log.debug("End executing GroovyOutlet " + getName()); + } + } + } + + + public Map<String, Object> createBinding(ControllerState controllerState) + { + Map<String, Object> binding = new HashMap<String, Object>(); + binding.put( + TORQUE_GEN_BINDING_NAME, + new TorqueGenGroovy(this, controllerState)); + if (optionsInBinding) + { + // Only consider options visible from the current namespace. + Options visibleOptions + = controllerState.getVisibleOptions(); + for (Option option : visibleOptions.values()) + { + QualifiedName qualifiedName = option.getQualifiedName(); + binding.put(qualifiedName.getName(), option.getValue()); + } + log.debug("Put options in context " + visibleOptions.keySet()); + } + else + { + log.debug("options in binding are disabled"); + } + + SourceElement sourceElement = controllerState.getSourceElement(); + if (sourceAttributesInBinding) + { + Set<String> attributes = sourceElement.getAttributeNames(); + for (String key : attributes) + { + Object value = sourceElement.getAttribute(key); + if (key == null) + { + // The null key cannot be accessed in the binding. + // So if the attribute NULL_KEY_CONTEXT_NAME does not + // exist, use this as attribute name. + if (sourceElement.getAttributeNames().contains( + NULL_KEY_BINDING_NAME)) + { + continue; + } + key = NULL_KEY_BINDING_NAME; + } + binding.put(key, value); + } + log.debug("Put attributes in binding " + attributes); + } + else + { + log.debug("source attributes in binding are disabled"); + } + + if (variablesInBinding) + { + // Only consider variables visible from the namespace + // of this outlet. If a name exists in different + // namespaces visible from this namespace, + // only consider the most significant name. + Namespace namespace = getName().getNamespace(); + VariableStore variableStore + = controllerState.getVariableStore(); + QualifiedNameMap<Variable> visibleVariables + = variableStore + .getContent() + .getInHierarchy(namespace); + for (Variable variable : visibleVariables.values()) + { + QualifiedName qualifiedName = variable.getName(); + binding.put( + qualifiedName.getName(), + variable.getValue()); + } + log.debug("Put variables in binding " + + visibleVariables.keySet()); + } + else + { + log.debug("variables in binding are disabled"); + } + return binding; + } + + /** + * Tells the outlet to put all options which name space is visible to + * the namespace of this outlet into the binding. + * Only the variable names are used as keys in the context, the namespaces + * are stripped. + * + * @param optionsInBinding whether to put the options into the context. + */ + public void setOptionsInBinding(boolean optionsInBinding) + { + this.optionsInBinding = optionsInBinding; + } + + /** + * Returns whether all options which namespaces are visible to + * the name space of this outlet are put into the binding. + * + * @return whether the outlet puts the options into the context. + */ + public boolean isOptionsInBinding() + { + return optionsInBinding; + } + + /** + * Tells the outlet to put all variables which are visible to this + * outlet into the binding. + * Only the variable names are used as keys in the binding, the namespaces + * are stripped. + * + * @param variablesInBinding whether to put the variables into the context. + */ + public void setVariablesInContext(boolean variablesInBinding) + { + this.variablesInBinding = variablesInBinding; + } + + /** + * Returns whether all variables which are visible to this + * outlet are put into the binding. + * + * @return whether the outlet puts the variables into the binding. + */ + public boolean isVariablesInBinding() + { + return variablesInBinding; + } + + /** + * Tells the outlet to put the attributes of the current source element + * into the binding or not. + * + * @param sourceAttributesInBinding whether to put the source attributes + * into the binding. + */ + public void setSourceAttributesInBinding( + boolean sourceAttributesInBinding) + { + this.sourceAttributesInBinding = sourceAttributesInBinding; + } + + /** + * Returns whether the attributes of the current source element + * are put into the binding. + * + * @return whether the outlet puts the attributes of the current source + * element into the binding. + */ + public boolean isSourceAttributesInBinding() + { + return sourceAttributesInBinding; + } + +} Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/TorqueGenGroovy.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/TorqueGenGroovy.java?rev=1460227&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/TorqueGenGroovy.java (added) +++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/TorqueGenGroovy.java Sat Mar 23 20:32:39 2013 @@ -0,0 +1,330 @@ +package org.apache.torque.generator.template.groovy; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.util.Date; +import java.util.List; + +import org.apache.commons.lang.StringUtils; +import org.apache.torque.generator.GeneratorException; +import org.apache.torque.generator.control.ControllerState; +import org.apache.torque.generator.source.SourceElement; +import org.apache.torque.generator.variable.Variable; + +/** + * This class acts as an interface to the Torque generator from the + * templates. It lets the user access Torque generator properties from the + * templates, and allows to execute certain action from within the templates. + */ +public class TorqueGenGroovy +{ + /** + * The state of the controller in which this generator interface is used. + */ + private ControllerState controllerState; + + /** + * The outlet in which context this class is used. + */ + private GroovyOutlet outlet; + + /** + * A counter which can be used in velocity templates. + */ + private static int counter = 1; + + /** + * Constructs a generator interface within the given controllerState. + * + * @param outlet the outlet in which this generator interface will be used, + * not null. + * @param controllerState the controller context. + * + * @throws NullPointerException if outlet or controllerState are null. + */ + public TorqueGenGroovy( + GroovyOutlet outlet, + ControllerState controllerState) + { + if (controllerState == null) + { + throw new NullPointerException("controllerState may not be null"); + } + if (outlet == null) + { + throw new NullPointerException("outlet may not be null"); + } + this.controllerState = controllerState; + this.outlet = outlet; + } + + /** + * Processes the mergepoint with the given name. + * + * @param mergepointName the name of the mergepoint. + * @return the output generated by the mergepoint. + * @throws GeneratorException if the mergepoint could not be processed + * completely. + */ + public String mergepoint(String mergepointName) + throws GeneratorException + { + return outlet.mergepoint(mergepointName, controllerState); + } + + /** + * Returns the current controller state. + * + * @return The current controller state, never null. + */ + public ControllerState getControllerState() + { + return controllerState; + } + + /** + * Returns the current source element. This method is shorthand for + * <code>getControllerState().getSourceElement()</code> + * + * @return the current source element, never null. + */ + public SourceElement getSourceElement() + { + return controllerState.getSourceElement(); + } + + /** + * Returns all children of the current source element. + * This method is shorthand for + * <code>getSourceElement().getChildren()</code> + * + * @return the children of the current source element, never null. + */ + public List<SourceElement> getChildren() + { + return getSourceElement().getChildren(); + } + + /** + * Returns the children of the current source element with a certain name. + * This method is shorthand for + * <code>getSourceElement().getChildren(name)</code> + * + * @param name the name of the children elements to select. + * + * @return the children of the current source element with the name name, + * never null. + */ + public List<SourceElement> getChildren(String name) + { + return getSourceElement().getChildren(name); + } + + /** + * Returns the first child of the current source element + * with the given name. + * This method is shorthand for + * <code>getSourceElement().getChild(name)</code> + * + * @param name the name of the child element to select. + * + * @return the first child with the given name, or null if no such child + * exists. + */ + public SourceElement getChild(String name) + { + return getSourceElement().getChild(name); + } + + /** + * Returns the parent of the current source element. + * <code>getSourceElement().getParent()</code> + * + * @return the parent of the current source element, or null if the current + * source element has no parent. + */ + public SourceElement getParent() + { + return getSourceElement().getParent(); + } + + /** + * Returns the option with the given key. The key can either be a name + * prefixed with a namespace, or a name without namespace, in which case + * the namespace of the currently active outlet is used. + * + * In the case that the option is not set in this namespace, the parent + * namespaces are searched recursively. If the option is not set in any + * of the parent namespaces, null is returned. + * + * @param key the key for the option to retrieve. + * @return the option for the given key. + */ + public Object option(String key) + { + Object result = controllerState.getOption(key); + + return result; + } + + /** + * Returns the option with the given key as boolean value. + * The key can either be a name prefixed with a namespace, + * or a name without namespace, in which case the namespace of the + * currently active outlet is used. + * + * In the case that the option is not set in this namespace, the parent + * namespaces are searched recursively. If the option is not set in any + * of the parent namespaces, false is returned. + * + * @param key the key for the option to retrieve. + * @return the option for the given key, converted to a boolean + */ + public boolean booleanOption(String key) + { + boolean result = controllerState.getBooleanOption(key); + + return result; + } + + /** + * Returns the option with the given key as int value. + * The key can either be a name prefixed with a namespace, + * or a name without namespace, in which case the namespace of the + * currently active outlet is used. + * + * In the case that the option is not set in this namespace, the parent + * namespaces are searched recursively. If the option is not set in any + * of the parent namespaces or empty, 0 is returned. + * + * @param key the key for the option to retrieve. + * @return the option for the given key, converted to a boolean + */ + public int intOption(String key) + { + Object optionValue = controllerState.getOption(key); + if (optionValue == null) + { + return 0; + } + String optionString = optionValue.toString(); + if (StringUtils.isBlank(optionString)) + { + return 0; + } + + return Integer.parseInt(optionString); + } + + /** + * Returns the variable with the given key. The key can either be a name + * prefixed with a namespace, or a name without namespace, in which case + * the namespace of the currently active outlet is used. + * + * In the case that the variable is not set in this namespace, the parent + * namespaces are searched recursively. If the variable is not set in any + * of the parent namespaces, null is returned. + * + * @param key the key for the variable to retrieve. + * @return the variable for the given key, or null if the variable is not + * set or explicitly set to null. + */ + public Object getVariable(String key) + { + return outlet.getVariable(key, controllerState); + } + + /** + * Sets a variable. The key can be given with or without namespace; + * in the latter case, the variable is set in the namespace of the + * currently active outlet. + * The Scope of the variable is this outlet and its children. + * + * @param key the name of the variable, not null + * @param value the value of the variable, may be null. + * + * @throws NullPointerException if key or scope is null. + * @throws IllegalArgumentException if the key is no valid QualifiedName. + */ + public void setVariable(String key, Object value) + { + outlet.setVariable(key, value, controllerState); + } + + /** + * Sets a variable. The key can be given with or without namespace; + * in the latter case, the variable is set in the namespace of the + * currently active outlet. + * + * @param key the name of the variable, not null. + * @param value the value of the variable, may be null. + * @param scope the scope of the variable, not null. + * + * @throws NullPointerException if key or scope is null. + * @throws IllegalArgumentException if the key is no valid QualifiedName. + */ + public void setVariable(String key, Object value, String scope) + { + Variable.Scope scopeValue = Variable.Scope.valueOf(scope); + outlet.setVariable(key, value, scopeValue, controllerState); + } + + /** + * Returns the currently processed source file. + * + * @return the source file which is currently processed. + */ + public File getSourceFile() + { + return controllerState.getSourceFile(); + } + + /** + * Returns the current date. + * + * @return the current date, not null. + */ + public Date now() + { + return new Date(); + } + + /** + * Returns a counter value which is increased each time this function is + * accessed. Start value is 1. + * If <code>resetCounter</code> is not called, the returned value is unique + * over the generation process. + * + * @return the counter value. + */ + public static synchronized int getCounter() + { + return counter++; + } + + /** + * Resets the counter accessible though <code>getCounter()</code> back to 1. + */ + public static synchronized void resetCounter() + { + counter = 1; + } +} Modified: db/torque/torque4/trunk/torque-generator/src/main/resources/org/apache/torque/generator/configuration/outlet.xsd URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/resources/org/apache/torque/generator/configuration/outlet.xsd?rev=1460227&r1=1460226&r2=1460227&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-generator/src/main/resources/org/apache/torque/generator/configuration/outlet.xsd (original) +++ db/torque/torque4/trunk/torque-generator/src/main/resources/org/apache/torque/generator/configuration/outlet.xsd Sat Mar 23 20:32:39 2013 @@ -75,6 +75,18 @@ </complexContent> </complexType> + <complexType name="groovyOutlet"> + <complexContent> + <extension base="configuration:baseOutlet"> + <attribute name="path" type="string" use="required"/> + <attribute name="optionsInBinding" type="string" use="optional"/> + <attribute name="sourceAttributesInBinding" type="string" use="optional"/> + <attribute name="variablesInBinding" type="string" use="optional"/> + <attribute name="encoding" type="string" use="optional"/> + </extension> + </complexContent> + </complexType> + <complexType name="copyOutlet"> <complexContent> <extension base="configuration:baseOutlet"> Modified: db/torque/torque4/trunk/torque-generator/src/test/groovyTemplates/src/main/torque-gen/outlets/outlets.xml URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/test/groovyTemplates/src/main/torque-gen/outlets/outlets.xml?rev=1460227&r1=1456690&r2=1460227&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-generator/src/test/groovyTemplates/src/main/torque-gen/outlets/outlets.xml (original) +++ db/torque/torque4/trunk/torque-generator/src/test/groovyTemplates/src/main/torque-gen/outlets/outlets.xml Sat Mar 23 20:32:39 2013 @@ -19,11 +19,11 @@ --> <outlets xmlns="http://db.apache.org/torque/4.0/generator/configuration" - xsi:schemaLocation="http://db.apache.org/torque/4.0/generator/configuration src/test/otherTemplateLanguages/groovyOutlet.xsd" + xsi:schemaLocation="http://db.apache.org/torque/4.0/generator/configuration http://db.apache.org/torque/4.0/generator/configuration.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <outlet name="testGroovyOutlet" xsi:type="groovyOutlet" - path="template.groovy"> + path="template.gtl"> </outlet> </outlets> \ No newline at end of file Copied: db/torque/torque4/trunk/torque-generator/src/test/groovyTemplates/src/main/torque-gen/templates/template.gtl (from r1456690, db/torque/torque4/trunk/torque-generator/src/test/otherTemplateLanguages/src/main/torque-gen/templates/template.groovy) URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/test/groovyTemplates/src/main/torque-gen/templates/template.gtl?p2=db/torque/torque4/trunk/torque-generator/src/test/groovyTemplates/src/main/torque-gen/templates/template.gtl&p1=db/torque/torque4/trunk/torque-generator/src/test/otherTemplateLanguages/src/main/torque-gen/templates/template.groovy&r1=1456690&r2=1460227&rev=1460227&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-generator/src/test/otherTemplateLanguages/src/main/torque-gen/templates/template.groovy (original) +++ db/torque/torque4/trunk/torque-generator/src/test/groovyTemplates/src/main/torque-gen/templates/template.gtl Sat Mar 23 20:32:39 2013 @@ -1,4 +1,4 @@ -/* +<% /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -15,5 +15,5 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. - */ -return 'groovy test output' \ No newline at end of file + */ +%>groovy template output: ${torqueGen.getSourceElement().getName()} \ No newline at end of file Copied: db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/template/groovy/GroovyTemplateTest.java (from r1456690, db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/configuration/outlet/OtherTemplateLanguageTest.java) URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/template/groovy/GroovyTemplateTest.java?p2=db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/template/groovy/GroovyTemplateTest.java&p1=db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/configuration/outlet/OtherTemplateLanguageTest.java&r1=1456690&r2=1460227&rev=1460227&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/configuration/outlet/OtherTemplateLanguageTest.java (original) +++ db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/template/groovy/GroovyTemplateTest.java Sat Mar 23 20:32:39 2013 @@ -1,4 +1,4 @@ -package org.apache.torque.generator.configuration.outlet; +package org.apache.torque.generator.template.groovy; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -40,7 +40,7 @@ import org.junit.Test; * * @version $Id$ */ -public class OtherTemplateLanguageTest +public class GroovyTemplateTest { private static final File TARGET_DIR = new File("target/test/otherTemplateLanguages"); @@ -60,13 +60,13 @@ public class OtherTemplateLanguageTest * @throws Exception if an error occurs. */ @Test - public void testOtherTemplateLanguages() throws Exception + public void testGroovyTemplate() throws Exception { Controller controller = new Controller(); List<UnitDescriptor> unitDescriptors = new ArrayList<UnitDescriptor>(); CustomProjectPaths projectPaths = new CustomProjectPaths( new Maven2DirectoryProjectPaths( - new File("src/test/otherTemplateLanguages"))); + new File("src/test/groovyTemplates"))); projectPaths.setOutputDirectory(null, TARGET_DIR); unitDescriptors.add(new UnitDescriptor( UnitDescriptor.Packaging.DIRECTORY, @@ -76,7 +76,7 @@ public class OtherTemplateLanguageTest // TODO: check outcome against reference file assertTrue(OUTPUT_FILE.exists()); assertEquals( - "groovy test output", + "groovy template output: root", FileUtils.readFileToString(OUTPUT_FILE)); } } --------------------------------------------------------------------- To unsubscribe, e-mail: torque-dev-unsubscr...@db.apache.org For additional commands, e-mail: torque-dev-h...@db.apache.org