You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tf...@apache.org on 2013/04/20 23:23:42 UTC
svn commit: r1470235 [3/7] - in /db/torque/torque4/trunk: torque-generator/
torque-generator/src/main/java/org/apache/torque/generator/configuration/
torque-generator/src/main/java/org/apache/torque/generator/configuration/outlet/
torque-generator/src/...
Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/AttributeTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/AttributeTransformer.java?rev=1470235&r1=1470234&r2=1470235&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/AttributeTransformer.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/AttributeTransformer.java Sat Apr 20 21:23:39 2013
@@ -182,8 +182,8 @@ public class AttributeTransformer implem
* Adds attributes to the sourceElement according to the
* transformDefinition.
*
- * @param toTransform the sourceElement to transform. Must implement
- * EditableSourceElement.
+ * @param toTransform the object to transform. Must be a
+ * SourceElement.
*
* @param controllerState the current state of the controller, not null.
*
@@ -192,11 +192,12 @@ public class AttributeTransformer implem
* @throws SourceTransformerException if an error occurs during
* transforming.
*/
- public SourceElement transform(
- SourceElement toTransform,
+ public Object transform(
+ Object toTransform,
ControllerState controllerState)
throws SourceTransformerException
{
+ SourceElement toTransformElement = (SourceElement) toTransform;
for (TransformRule transformRule : transformRules)
{
StringBuilder sourceResult = new StringBuilder();
@@ -209,7 +210,7 @@ public class AttributeTransformer implem
}
else if (Definition.Type.ATTRIBUTE == type)
{
- Object attributeValue = toTransform.getAttribute(
+ Object attributeValue = toTransformElement.getAttribute(
definition.getContent());
if (attributeValue != null)
{
@@ -235,9 +236,9 @@ public class AttributeTransformer implem
if (Definition.Type.ATTRIBUTE_NO_OVERRIDE == target.getType())
{
String attributeName = target.getContent();
- if (toTransform.getAttribute(attributeName) == null)
+ if (toTransformElement.getAttribute(attributeName) == null)
{
- toTransform.setAttribute(
+ toTransformElement.setAttribute(
attributeName,
sourceResult.toString());
if (log.isTraceEnabled())
@@ -245,7 +246,7 @@ public class AttributeTransformer implem
log.trace("Setting Attribute "
+ attributeName
+ " on element "
- + toTransform.getName()
+ + toTransformElement.getName()
+ " to "
+ sourceResult);
}
@@ -257,7 +258,7 @@ public class AttributeTransformer implem
log.trace("Attribute "
+ attributeName
+ " already set on element "
- + toTransform.getName()
+ + toTransformElement.getName()
+ " skipping");
}
}
@@ -265,14 +266,14 @@ public class AttributeTransformer implem
else if (Definition.Type.ATTRIBUTE_OVERRIDE == target.getType())
{
String attributeName = target.getContent();
- if (toTransform.getAttribute(attributeName) == null)
+ if (toTransformElement.getAttribute(attributeName) == null)
{
if (log.isTraceEnabled())
{
log.trace("Setting Attribute "
+ attributeName
+ " on element "
- + toTransform.getName()
+ + toTransformElement.getName()
+ " to "
+ sourceResult);
}
@@ -284,12 +285,12 @@ public class AttributeTransformer implem
log.trace("Overriding Attribute "
+ attributeName
+ " on element "
- + toTransform.getName()
+ + toTransformElement.getName()
+ " with value "
+ sourceResult);
}
}
- toTransform.setAttribute(
+ toTransformElement.setAttribute(
attributeName,
sourceResult.toString());
}
Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/BeanPropertyMethodNameTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/BeanPropertyMethodNameTransformer.java?rev=1470235&r1=1470234&r2=1470235&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/BeanPropertyMethodNameTransformer.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/BeanPropertyMethodNameTransformer.java Sat Apr 20 21:23:39 2013
@@ -111,18 +111,27 @@ public class BeanPropertyMethodNameTrans
/**
* Fills the target attribute according to the settings.
*
- * @param sourceElement the source element to modify, not null.
+ * @param rootObject the root of the source graph, not null.
* @param controllerState the controller state.
*
* @return the modified source element, not null.
*
+ * @throws SourceTransformerException if rootObject is not a SourceElement.
* @throws IllegalStateException if targetAttributeName was not set.
*/
@Override
public SourceElement transform(
- SourceElement sourceElement,
- ControllerState controllerState)
+ Object rootObject,
+ ControllerState controllerState)
+ throws SourceTransformerException
{
+ if (!(rootObject instanceof SourceElement))
+ {
+ throw new SourceTransformerException(
+ "rootObject is not a SourceElement but has the class "
+ + rootObject.getClass().getName());
+ }
+ SourceElement sourceElement = (SourceElement) rootObject;
String targetAttributeName = getTargetAttributeName();
if (targetAttributeName == null)
{
Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/CopyAttributeTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/CopyAttributeTransformer.java?rev=1470235&r1=1470234&r2=1470235&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/CopyAttributeTransformer.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/CopyAttributeTransformer.java Sat Apr 20 21:23:39 2013
@@ -40,18 +40,27 @@ public class CopyAttributeTransformer ex
/**
* Fills the target attribute according to the settings.
*
- * @param sourceElement the source element to modify, not null.
+ * @param rootObject the root of the source graph, not null.
* @param controllerState the controller state.
*
* @return the modified source element, not null.
*
+ * @throws SourceTransformerException if rootObject is not a SourceElement.
* @throws IllegalStateException if sourceAttributeName or
* targetAttributeName was not set.
*/
public SourceElement transform(
- SourceElement sourceElement,
- ControllerState controllerState)
+ Object rootObject,
+ ControllerState controllerState)
+ throws SourceTransformerException
{
+ if (!(rootObject instanceof SourceElement))
+ {
+ throw new SourceTransformerException(
+ "rootObject is not a SourceElement but has the class "
+ + rootObject.getClass().getName());
+ }
+ SourceElement sourceElement = (SourceElement) rootObject;
String sourceAttributeName = getSourceAttributeName();
if (sourceAttributeName == null)
{
Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/GetterSetterNameTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/GetterSetterNameTransformer.java?rev=1470235&r1=1470234&r2=1470235&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/GetterSetterNameTransformer.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/GetterSetterNameTransformer.java Sat Apr 20 21:23:39 2013
@@ -145,18 +145,27 @@ public class GetterSetterNameTransformer
/**
* Fills the target attributes according to the settings.
*
- * @param sourceElement the source element to modify, not null.
+ * @param rootObject the root of the source graph, not null.
* @param controllerState the controller state.
*
* @return the modified source element, not null.
*
* @throws IllegalStateException if sourceAttributeName or
* targetAttributeName was not set.
+ * @throws SourceTransformerException if rootObject is not a SourceElement.
*/
public SourceElement transform(
- SourceElement sourceElement,
- ControllerState controllerState)
+ Object rootObject,
+ ControllerState controllerState)
+ throws SourceTransformerException
{
+ if (!(rootObject instanceof SourceElement))
+ {
+ throw new SourceTransformerException(
+ "rootObject is not a SourceElement but has the class "
+ + rootObject.getClass().getName());
+ }
+ SourceElement sourceElement = (SourceElement) rootObject;
Object attributeValue = sourceElement.getAttribute(attributeName);
if (attributeValue == null)
{
Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/LoadAdditionalSourceTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/LoadAdditionalSourceTransformer.java?rev=1470235&r1=1470234&r2=1470235&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/LoadAdditionalSourceTransformer.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/LoadAdditionalSourceTransformer.java Sat Apr 20 21:23:39 2013
@@ -71,17 +71,25 @@ public class LoadAdditionalSourceTransfo
/**
* Loads the additional source into the current source graph.
*
- * @param root the root of the source graph, not null.
+ * @param rootObject the root of the source graph, not null.
* @param controllerState the controller state, not null.
*
* @throws SourceTransformerException if the additional source
* cannot be loaded or the element to add to does not exist.
*/
public SourceElement transform(
- SourceElement root,
+ Object rootObject,
ControllerState controllerState)
throws SourceTransformerException
{
+ if (!(rootObject instanceof SourceElement))
+ {
+ throw new SourceTransformerException(
+ "rootObject is not a SourceElement but has the class "
+ + rootObject.getClass().getName());
+ }
+ SourceElement root = (SourceElement) rootObject;
+
// the element where the additional source should be anchored.
SourceElement sourceElement;
List<SourceElement> sourceElementList
Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/LoadAllSourceFilesTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/LoadAllSourceFilesTransformer.java?rev=1470235&r1=1470234&r2=1470235&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/LoadAllSourceFilesTransformer.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/LoadAllSourceFilesTransformer.java Sat Apr 20 21:23:39 2013
@@ -77,17 +77,24 @@ public class LoadAllSourceFilesTransform
/**
* Loads the additional source into the current source graph.
*
- * @param root the root of the source graph, not null.
+ * @param rootObject the root of the source graph, not null.
* @param controllerState the controller state, not null.
*
* @throws SourceTransformerException if the additional source
* cannot be loaded or the element to add to does not exist.
*/
- public SourceElement transform(
- SourceElement root,
+ public Object transform(
+ Object rootObject,
ControllerState controllerState)
throws SourceTransformerException
{
+ if (!(rootObject instanceof SourceElement))
+ {
+ throw new SourceTransformerException(
+ "rootObject is not a SourceElement but has the class "
+ + rootObject.getClass().getName());
+ }
+ SourceElement root = (SourceElement) rootObject;
Output output = controllerState.getOutput();
log.debug("adding all sources of output " + output.getName()
+ " to current source tree at element " + addTo);
@@ -143,7 +150,7 @@ public class LoadAllSourceFilesTransform
}
}
- rootElement = helperController.transformSource(
+ rootElement = (SourceElement) helperController.transformSource(
rootElement,
transformerDefinitions,
controllerState);
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceElementToModelTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceElementToModelTransformer.java?rev=1470235&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceElementToModelTransformer.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceElementToModelTransformer.java Sat Apr 20 21:23:39 2013
@@ -0,0 +1,283 @@
+package org.apache.torque.generator.source.transform;
+
+/*
+ * 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.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.torque.generator.configuration.ClassHelper;
+import org.apache.torque.generator.configuration.UnitConfiguration;
+import org.apache.torque.generator.control.ControllerState;
+import org.apache.torque.generator.processor.string.Camelbacker;
+import org.apache.torque.generator.processor.string.WrapReservedJavaWords;
+import org.apache.torque.generator.source.SourceElement;
+import org.apache.torque.generator.source.transform.model.NoSuchPropertyException;
+import org.apache.torque.generator.source.transform.model.PropertyAccess;
+
+/**
+ * A Source transformer transforming a SourceElement graph to a typed model.
+ *
+ * @version $Id: $
+ */
+public class SourceElementToModelTransformer implements SourceTransformer
+{
+ /**
+ * The field name in the model which is filled if the attribute name
+ * <code>null</code> appears in the source graph.
+ */
+ public static final String NULL_ATTRIBUTE_FIELD_NAME = "value";
+
+ /** The processor which does the camelback processing. */
+ private final Camelbacker camelbacker = new Camelbacker();
+
+ /** The processor which wraps reserved java words. */
+ private final WrapReservedJavaWords reservedWordsWrapper
+ = new WrapReservedJavaWords();
+
+ /** The class the model root must have. */
+ private String modelRootClass;
+
+ /**
+ * Standard constructor.
+ */
+ public SourceElementToModelTransformer()
+ {
+ camelbacker.setDefaultLowerCase(false);
+ camelbacker.setFirstCharUppercase(false);
+ }
+
+ /**
+ * Constructor defining the model root class.
+ *
+ * @param modelRootClass the model root class, not null.
+ */
+ public SourceElementToModelTransformer(final Class<?> modelRootClass)
+ {
+ this();
+ this.modelRootClass = modelRootClass.getName();
+ }
+
+ /**
+ * Sets the class which the model root must have.
+ *
+ * @param modelRootClass the class the model root must have,
+ * or null for any class
+ */
+ public void setModelRootClass(final String modelRootClass)
+ {
+ this.modelRootClass = modelRootClass;
+ }
+
+ /**
+ * Transforms the source graph into the model. This is done by recursively
+ * walking through the source graph and setting the fields corresponding
+ * to the names of the attributes resp. child elements.
+ *
+ * @param modelRoot the root object to fill, not null.
+ * @param rootSourceElement the root source element to traverse, not null.
+ * @param unitConfiguration the unit configuration, not null.
+ *
+ * @throws SourceTransformerException if filling the source graph fails,
+ * e.g. if an attribute name or element name has no corresponding
+ * field in a model element.
+ */
+ public Object transform(final Object modelRoot,
+ final ControllerState controllerState) throws SourceTransformerException
+ {
+ if (modelRootClass == null)
+ {
+ throw new SourceTransformerException(
+ "modelRootClass must not be null "
+ + "for the transformer of type "
+ + getClass().getName());
+ }
+ if (!(modelRoot instanceof SourceElement))
+ {
+ throw new SourceTransformerException(
+ "modelRoot must be of type "
+ + SourceElement.class.getName());
+ }
+ final Object targetModelRoot = getInstance(
+ modelRootClass,
+ controllerState.getUnitConfiguration());
+ fillModelElement(
+ targetModelRoot,
+ (SourceElement) modelRoot,
+ controllerState.getUnitConfiguration(),
+ new HashMap<SourceElement, Object>());
+ return targetModelRoot;
+ }
+
+ /**
+ * Constructs a part of the model from a part of the source graph.
+ * This is done by recursively walking through the source graph
+ * and setting the fields corresponding to the names of the attributes
+ * resp. child elements.
+ *
+ * @param model the model object to fill, not null.
+ * @param sourceElement the source element corresponding to the
+ * model object, not null.
+ * @param unitConfiguration the unit configuration, not null.
+ * @param alreadyMapped a map of already mapped source elements,
+ * mapped to the corresponding model elements.
+ *
+ * @throws SourceTransformerException if filling the target object graph
+ * fails, e.g. if an attribute name or element name
+ * has no corresponding field in a model element.
+ */
+ private void fillModelElement(
+ final Object model,
+ final SourceElement sourceElement,
+ final UnitConfiguration unitConfiguration,
+ final Map<SourceElement, Object> alreadyMapped)
+ throws SourceTransformerException
+ {
+ for (String attributeName : sourceElement.getAttributeNames())
+ {
+ final Object attributeValue = sourceElement.getAttribute(attributeName);
+ attributeName = camelbacker.process(attributeName);
+ attributeName = reservedWordsWrapper.process(attributeName);
+ if (attributeName == null)
+ {
+ attributeName = NULL_ATTRIBUTE_FIELD_NAME;
+ }
+ final PropertyAccess propertyAccess = new PropertyAccess(
+ model,
+ attributeName);
+ propertyAccess.setProperty(attributeValue);
+ }
+ for (final SourceElement child : sourceElement.getChildren())
+ {
+ String propertyName = child.getName();
+ propertyName = camelbacker.process(propertyName);
+ propertyName = reservedWordsWrapper.process(propertyName);
+ final PropertyAccess propertyAccess = new PropertyAccess(
+ model,
+ propertyName);
+ if (!propertyAccess.isPropertyAccessible())
+ {
+ throw new NoSuchPropertyException(
+ model,
+ propertyName,
+ propertyAccess.getPrefixList(),
+ propertyAccess.getSuffixList());
+ }
+ Object childModelElement = alreadyMapped.get(child);
+ if (childModelElement != null)
+ {
+ propertyAccess.setProperty(childModelElement);
+ continue;
+ }
+ if (Collection.class.isAssignableFrom(
+ propertyAccess.getPropertyType()))
+ {
+ childModelElement = getInstance(
+ propertyAccess.getFirstGenericTypeArgument().getName(),
+ unitConfiguration);
+ }
+ else if (propertyAccess.getPropertyType().isArray())
+ {
+ childModelElement = getInstance(
+ propertyAccess.getPropertyType().getComponentType().getName(),
+ unitConfiguration);
+ }
+ else
+ {
+ childModelElement = getInstance(
+ propertyAccess.getPropertyType().getName(),
+ unitConfiguration);
+ }
+ propertyAccess.setProperty(childModelElement);
+ alreadyMapped.put(child, childModelElement);
+ PropertyAccess parentPropertyAccess
+ = new PropertyAccess(childModelElement, "parent");
+ if (!parentPropertyAccess.isPropertyAccessible())
+ {
+ final String modelClassName = model.getClass().getSimpleName();
+ parentPropertyAccess = new PropertyAccess(
+ childModelElement,
+ "parent" + modelClassName);
+ }
+ if (parentPropertyAccess.isPropertyAccessible())
+ {
+ parentPropertyAccess.setProperty(model);
+ }
+ fillModelElement(
+ childModelElement,
+ child,
+ unitConfiguration,
+ alreadyMapped);
+ }
+ }
+
+ /**
+ * Creates an instance of a class.
+ *
+ * @param className the fully qualified name of the class to instantiate.
+ * @param unitDescriptor The description of the generation unit, not null.
+ *
+ * @throws SourceTransformerException if the class cannot be instantiated.
+ */
+ protected static Object getInstance(
+ final String className,
+ final UnitConfiguration unitConfiguration)
+ throws SourceTransformerException
+ {
+ if (className == null)
+ {
+ return null;
+ }
+ Object result;
+ try
+ {
+ ClassLoader classLoader = unitConfiguration.getClassLoader();
+ if (classLoader == null)
+ {
+ classLoader = ClassHelper.class.getClassLoader();
+ }
+ final Class<?> clazz = Class.forName(className, true, classLoader);
+ result = clazz.newInstance();
+ }
+ catch (final ClassNotFoundException e)
+ {
+ throw new SourceTransformerException("The class "
+ + className
+ + " could not be found.",
+ e);
+ }
+ catch (final IllegalAccessException e)
+ {
+ throw new SourceTransformerException("Instantiating "
+ + className
+ + " is not allowed",
+ e);
+ }
+ catch (final InstantiationException e)
+ {
+ throw new SourceTransformerException("The class "
+ + className
+ + " has no standard constructor.",
+ e);
+ }
+ return result;
+ }
+
+}
Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceTargetAttributeTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceTargetAttributeTransformer.java?rev=1470235&r1=1470234&r2=1470235&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceTargetAttributeTransformer.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceTargetAttributeTransformer.java Sat Apr 20 21:23:39 2013
@@ -21,7 +21,6 @@ package org.apache.torque.generator.sour
import org.apache.log4j.Logger;
import org.apache.torque.generator.control.ControllerState;
-import org.apache.torque.generator.source.SourceElement;
/**
* A transformer which has a source attribute and a target attribute.
@@ -154,7 +153,7 @@ public abstract class SourceTargetAttrib
/**
* Performs the transformation.
*
- * @param toTransformRoot the root element of the source tree
+ * @param toTransformRoot the root object of the source tree
* to transform, not null.
* @param controllerState the state of the controller, not null.
*
@@ -162,10 +161,10 @@ public abstract class SourceTargetAttrib
*
* @throws SourceTransformerException if the source cannot be transformed.
*
- * @see SourceTransformer#transform(SourceElement, ControllerState)
+ * @see SourceTransformer#transform(Object, ControllerState)
*/
- public abstract SourceElement transform(
- SourceElement toTransformRoot,
+ public abstract Object transform(
+ Object toTransformRoot,
ControllerState controllerState)
throws SourceTransformerException;
}
Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceTransformer.java?rev=1470235&r1=1470234&r2=1470235&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceTransformer.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceTransformer.java Sat Apr 20 21:23:39 2013
@@ -20,7 +20,6 @@ package org.apache.torque.generator.sour
*/
import org.apache.torque.generator.control.ControllerState;
-import org.apache.torque.generator.source.SourceElement;
/**
* Transforms one source tree into another source tree.
@@ -28,20 +27,19 @@ import org.apache.torque.generator.sourc
public interface SourceTransformer
{
/**
- * Transforms one source tree into another source tree.
+ * Transforms one source root into another source root.
* It is allowed to modify the toTransformRoot tree and
* return the same tree.
*
- * @param toTransformRoot the root element of the source tree
- * to transform, not null.
+ * @param modelRoot the root of the model to transform, not null.
* @param controllerState the state of the controller, not null.
*
- * @return the root element of the transformed source tree, not null.
+ * @return the transformed source root, not null.
*
* @throws SourceTransformerException if the source cannot be transformed.
*/
- SourceElement transform(
- SourceElement toTransformRoot,
+ Object transform(
+ Object modelRoot,
ControllerState controllerState)
throws SourceTransformerException;
}
Copied: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/NoSuchPropertyException.java (from r1465295, db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/model/NoSuchPropertyException.java)
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/NoSuchPropertyException.java?p2=db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/NoSuchPropertyException.java&p1=db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/model/NoSuchPropertyException.java&r1=1465295&r2=1470235&rev=1470235&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/model/NoSuchPropertyException.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/NoSuchPropertyException.java Sat Apr 20 21:23:39 2013
@@ -1,4 +1,4 @@
-package org.apache.torque.generator.source.model;
+package org.apache.torque.generator.source.transform.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -21,14 +21,14 @@ package org.apache.torque.generator.sour
import java.util.List;
-import org.apache.torque.generator.source.SourceException;
+import org.apache.torque.generator.source.transform.SourceTransformerException;
/**
* Indicates that an attempt was made to access a property which does not exist.
*
* @version $Id: $
*/
-public class NoSuchPropertyException extends SourceException
+public class NoSuchPropertyException extends SourceTransformerException
{
/** Serial Version UID. */
private static final long serialVersionUID = 1L;
Copied: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/PropertyAccess.java (from r1465295, db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/model/PropertyAccess.java)
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/PropertyAccess.java?p2=db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/PropertyAccess.java&p1=db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/model/PropertyAccess.java&r1=1465295&r2=1470235&rev=1470235&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/model/PropertyAccess.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/PropertyAccess.java Sat Apr 20 21:23:39 2013
@@ -1,4 +1,4 @@
-package org.apache.torque.generator.source.model;
+package org.apache.torque.generator.source.transform.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -38,7 +38,7 @@ import java.util.Queue;
import java.util.Set;
import org.apache.commons.beanutils.PropertyUtils;
-import org.apache.torque.generator.source.SourceException;
+import org.apache.torque.generator.source.transform.SourceTransformerException;
/**
* Accesses properties of java classes by reflection.
@@ -47,11 +47,20 @@ import org.apache.torque.generator.sourc
*/
public class PropertyAccess
{
+ /** The automatic converters used for properties. */
+ private static List<TypeConverter> converters;
+
+ static
+ {
+ converters = new ArrayList<TypeConverter>();
+ converters.add(new StringToBooleanConverter());
+ }
+
/** The object on which the property should be accessed, not null. */
- private Object target;
+ private final Object target;
/** The name of the property to access, not null. */
- private String propertyName;
+ private final String propertyName;
/**
* List of all possible prefixes for field names.
@@ -99,13 +108,14 @@ public class PropertyAccess
* @param target the target object, not null.
* @param propertyName the name of the property, not null.
*
- * @throws SourceException if reflection access to fields or methods fails.
+ * @throws SourceTransformerException if reflection access to fields
+ * or methods fails.
* @throws NullPointerException if target or propertyName are null.
*/
public PropertyAccess(
- Object target,
- String propertyName)
- throws SourceException
+ final Object target,
+ final String propertyName)
+ throws SourceTransformerException
{
this(target,
propertyName,
@@ -133,15 +143,16 @@ public class PropertyAccess
* "name" + "suffix" is also tried as field name and base method name
* if "suffix" is contained in this list.
*
- * @throws SourceException if reflection access to fields or methods fails.
+ * @throws SourceTransformerException if reflection access to fields
+ * or methods fails.
* @throws NullPointerException if target or propertyName are null.
*/
public PropertyAccess(
- Object target,
- String propertyName,
- List<String> prefixList,
- List<String> suffixList)
- throws SourceException
+ final Object target,
+ final String propertyName,
+ final List<String> prefixList,
+ final List<String> suffixList)
+ throws SourceTransformerException
{
if (target == null)
{
@@ -171,7 +182,7 @@ public class PropertyAccess
}
this.suffixList.add(0, "");
- for (String suffix : this.suffixList)
+ for (final String suffix : this.suffixList)
{
this.field = determinePublicField(
target,
@@ -183,7 +194,7 @@ public class PropertyAccess
}
if (this.field == null)
{
- for (String prefix : this.prefixList)
+ for (final String prefix : this.prefixList)
{
this.field = determinePublicField(
target,
@@ -197,7 +208,7 @@ public class PropertyAccess
if (this.field == null)
{
PropertyDescriptor propertyDescriptor = null;
- for (String suffix : this.suffixList)
+ for (final String suffix : this.suffixList)
{
propertyDescriptor = determinePropertyDescriptor(
target,
@@ -224,21 +235,21 @@ public class PropertyAccess
* @return the field if a public field with the given name exists,
* or null if no public field with the given name exists.
*
- * @throws SourceException if security settings prevent reading the field
- * of the object.
+ * @throws SourceTransformerException if security settings
+ * prevent reading the field of the object.
*/
- private static Field determinePublicField(Object target, String name)
- throws SourceException
+ private static Field determinePublicField(final Object target, final String name)
+ throws SourceTransformerException
{
try
{
- Field fieldCandidate = target.getClass().getField(name);
+ final Field fieldCandidate = target.getClass().getField(name);
if (Modifier.isPublic(fieldCandidate.getModifiers()))
{
return fieldCandidate;
}
}
- catch (SecurityException e)
+ catch (final SecurityException e)
{
throw createSetFieldException(
target,
@@ -247,7 +258,7 @@ public class PropertyAccess
" because access is denied to the field or package",
e);
}
- catch (NoSuchFieldException e)
+ catch (final NoSuchFieldException e)
{
// do nothing, field does not exist
}
@@ -264,31 +275,31 @@ public class PropertyAccess
* the property name exists, or null if no such getter
* and setter exists.
*
- * @throws SourceException if security settings prevent reading the field
- * of the object.
+ * @throws SourceTransformerException if security settings
+ * prevent reading the field of the object.
*/
private static PropertyDescriptor determinePropertyDescriptor(
- Object target,
- String name)
- throws SourceException
+ final Object target,
+ final String name)
+ throws SourceTransformerException
{
try
{
- PropertyDescriptor propertyDescriptor
+ final PropertyDescriptor propertyDescriptor
= PropertyUtils.getPropertyDescriptor(target, name);
return propertyDescriptor;
}
- catch (NoSuchMethodException e)
+ catch (final NoSuchMethodException e)
{
- throw new SourceException(e);
+ throw new SourceTransformerException(e);
}
- catch (IllegalAccessException e)
+ catch (final IllegalAccessException e)
{
- throw new SourceException(e);
+ throw new SourceTransformerException(e);
}
- catch (InvocationTargetException e)
+ catch (final InvocationTargetException e)
{
- throw new SourceException(e);
+ throw new SourceTransformerException(e);
}
}
@@ -302,13 +313,13 @@ public class PropertyAccess
*
* @param value the value to set the property to.
*
- * @throws SourceException if the property cannot be set.
+ * @throws SourceTransformerException if the property cannot be set.
* Common reasons for such an exception are that the property
* does not exist at all, or is not writeable, or value has the
* wrong class.
*/
- public void setProperty(Object value)
- throws SourceException
+ public void setProperty(final Object value)
+ throws SourceTransformerException
{
if (field != null)
{
@@ -377,13 +388,13 @@ public class PropertyAccess
*
* @param value the value to set the property to.
*
- * @throws SourceException if the property cannot be set.
+ * @throws SourceTransformerException if the property cannot be set.
* Common reasons for such an exception are that the property
* does not exist at all, or is not writeable, or value has the
* wrong class.
*/
- public void setPropertyStrict(Object value)
- throws SourceException
+ public void setPropertyStrict(final Object value)
+ throws SourceTransformerException
{
if (field != null)
{
@@ -416,17 +427,25 @@ public class PropertyAccess
*
* @param value the value to set the property to.
*
- * @throws SourceException if the property cannot be set.
+ * @throws SourceTransformerException if the property cannot be set.
* A common reason is that value has the wrong class.
*/
private void setPropertyStrictUsingField(Object value)
- throws SourceException
+ throws SourceTransformerException
{
+ for (final TypeConverter converter : converters)
+ {
+ if (converter.accept(value, field.getType()))
+ {
+ value = converter.convert(value, field.getType());
+ break;
+ }
+ }
try
{
field.set(target, value);
}
- catch (IllegalArgumentException e)
+ catch (final IllegalArgumentException e)
{
if (value == null)
{
@@ -442,7 +461,7 @@ public class PropertyAccess
+ value.getClass().getName() ,
e);
}
- catch (IllegalAccessException e)
+ catch (final IllegalAccessException e)
{
throw createSetFieldException(
null,
@@ -459,18 +478,28 @@ public class PropertyAccess
*
* @param value the value to set the property to.
*
- * @throws SourceException if the property cannot be set.
+ * @throws SourceTransformerException if the property cannot be set.
* A common reason is that value has the wrong class.
*/
private void setPropertyStrictUsingSetter(Object value)
- throws SourceException
+ throws SourceTransformerException
{
+ for (final TypeConverter converter : converters)
+ {
+ if (converter.accept(value, writeMethod.getParameterTypes()[0]))
+ {
+ value = converter.convert(
+ value,
+ writeMethod.getParameterTypes()[0]);
+ break;
+ }
+ }
try
{
writeMethod.invoke(target, value);
return;
}
- catch (IllegalArgumentException e)
+ catch (final IllegalArgumentException e)
{
if (value == null)
{
@@ -486,13 +515,13 @@ public class PropertyAccess
+ value.getClass().getName() ,
e);
}
- catch (IllegalAccessException e)
+ catch (final IllegalAccessException e)
{
- throw new SourceException(e);
+ throw new SourceTransformerException(e);
}
- catch (InvocationTargetException e)
+ catch (final InvocationTargetException e)
{
- throw new SourceException(e);
+ throw new SourceTransformerException(e);
}
}
@@ -501,11 +530,11 @@ public class PropertyAccess
*
* @return the value of the property.
*
- * @throws SourceException if the property is not readable.
+ * @throws SourceTransformerException if the property is not readable.
* A common reason for this is that no public field and no getter
* exists with the given name.
*/
- public Object getProperty() throws SourceException
+ public Object getProperty() throws SourceTransformerException
{
if (field != null)
{
@@ -526,23 +555,23 @@ public class PropertyAccess
*
* @return the value of the property.
*
- * @throws SourceException if the property is not readable.
+ * @throws SourceTransformerException if the property is not readable.
* A common reason for this is that no public field exists
* with the given name.
*/
- private Object getPropertyUsingField() throws SourceException
+ private Object getPropertyUsingField() throws SourceTransformerException
{
try
{
return field.get(target);
}
- catch (IllegalArgumentException e)
+ catch (final IllegalArgumentException e)
{
- throw new SourceException(e);
+ throw new SourceTransformerException(e);
}
- catch (IllegalAccessException e)
+ catch (final IllegalAccessException e)
{
- throw new SourceException(e);
+ throw new SourceTransformerException(e);
}
}
@@ -552,27 +581,27 @@ public class PropertyAccess
*
* @return the value of the property.
*
- * @throws SourceException if the property is not readable.
+ * @throws SourceTransformerException if the property is not readable.
* A common reason for this is that no public getter exists
* with the corresponding name.
*/
- private Object getPropertyUsingGetter() throws SourceException
+ private Object getPropertyUsingGetter() throws SourceTransformerException
{
try
{
return readMethod.invoke(target);
}
- catch (IllegalArgumentException e)
+ catch (final IllegalArgumentException e)
{
- throw new SourceException(e);
+ throw new SourceTransformerException(e);
}
- catch (IllegalAccessException e)
+ catch (final IllegalAccessException e)
{
- throw new SourceException(e);
+ throw new SourceTransformerException(e);
}
- catch (InvocationTargetException e)
+ catch (final InvocationTargetException e)
{
- throw new SourceException(e);
+ throw new SourceTransformerException(e);
}
}
@@ -637,10 +666,10 @@ public class PropertyAccess
*/
public Class<?> getFirstGenericTypeArgument()
{
- Type type = getPropertyGenericType();
+ final Type type = getPropertyGenericType();
if (type instanceof ParameterizedType)
{
- Object firstType
+ final Object firstType
= ((ParameterizedType) type).getActualTypeArguments()[0];
if (firstType instanceof Class)
{
@@ -680,12 +709,13 @@ public class PropertyAccess
*
* @param value the value to add to the array.
*
- * @throws SourceException if adding the value fails.
+ * @throws SourceTransformerException if adding the value fails.
* A common reason is that value has the wrong class.
*/
- private void setMemberinArrayField(Object value) throws SourceException
+ private void setMemberinArrayField(final Object value)
+ throws SourceTransformerException
{
- Object[] oldContent = (Object[]) getProperty();
+ final Object[] oldContent = (Object[]) getProperty();
int newIndex;
Object newContent;
if (oldContent == null)
@@ -714,11 +744,11 @@ public class PropertyAccess
*
* @param value the value to add to the array.
*
- * @throws SourceException if adding the value fails.
+ * @throws SourceTransformerException if adding the value fails.
* A common reason is that value has the wrong class.
*/
- private void setMemberinCollectionProperty(Object value)
- throws SourceException
+ private void setMemberinCollectionProperty(final Object value)
+ throws SourceTransformerException
{
@SuppressWarnings("unchecked")
Collection<Object> content = (Collection<Object>) getProperty();
@@ -742,12 +772,13 @@ public class PropertyAccess
*
* @return the collection instance to use, not null.
*
- * @throws SourceException if instantiation fails, e.g. if the collection
- * is an unknown abstract collection.
+ * @throws SourceTransformerException if instantiation fails,
+ * e.g. if the collection is an unknown abstract collection.
*/
- private Collection<Object> getCollectionInstance() throws SourceException
+ private Collection<Object> getCollectionInstance()
+ throws SourceTransformerException
{
- Class<?> type = getPropertyType();
+ final Class<?> type = getPropertyType();
if (Collection.class == type || List.class == type)
{
return new ArrayList<Object>();
@@ -765,34 +796,35 @@ public class PropertyAccess
try
{
@SuppressWarnings("unchecked")
+ final
Collection<Object> result
= (Collection<Object>) type.newInstance();
return result;
}
- catch (InstantiationException e)
+ catch (final InstantiationException e)
{
- throw new SourceException(e);
+ throw new SourceTransformerException(e);
}
- catch (IllegalAccessException e)
+ catch (final IllegalAccessException e)
{
- throw new SourceException(e);
+ throw new SourceTransformerException(e);
}
}
}
/**
- * Constructs a source exception when setting a field fails.
+ * Constructs a SourceTransformerException when setting a field fails.
*
* @param value the value to which the field was set.
* @param reason the reason why setting the field failed.
* @param cause the root cause of the exception.
*
- * @return An appropriate SourceException to throw.
+ * @return An appropriate SourceTransformerException to throw.
*/
- private SourceException createSetFieldException(
- Object value,
- String reason,
- Throwable cause)
+ private SourceTransformerException createSetFieldException(
+ final Object value,
+ final String reason,
+ final Throwable cause)
{
return createSetFieldException(
target,
@@ -803,7 +835,7 @@ public class PropertyAccess
}
/**
- * Constructs a source exception when setting a field fails.
+ * Constructs a SourceTransformerException when setting a field fails.
*
* @param target the target object on which the property was set.
* @param propertyName the name of the property which was set.
@@ -811,16 +843,16 @@ public class PropertyAccess
* @param reason the reason why setting the field failed.
* @param cause the root cause of the exception.
*
- * @return An appropriate SourceException to throw.
+ * @return An appropriate SourceTransformerException to throw.
*/
- private static SourceException createSetFieldException(
- Object target,
- String propertyName,
- Object value,
- String reason,
- Throwable cause)
+ private static SourceTransformerException createSetFieldException(
+ final Object target,
+ final String propertyName,
+ final Object value,
+ final String reason,
+ final Throwable cause)
{
- StringBuilder message = new StringBuilder("The field ")
+ final StringBuilder message = new StringBuilder("The field ")
.append(propertyName)
.append(" of class ")
.append(target.getClass().getName());
@@ -829,6 +861,6 @@ public class PropertyAccess
message.append(" cannot be set to ").append(value);
}
message.append(reason);
- return new SourceException(message.toString(), cause);
+ return new SourceTransformerException(message.toString(), cause);
}
}
Copied: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/PropertyNotReadableException.java (from r1464334, db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/model/PropertyNotReadableException.java)
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/PropertyNotReadableException.java?p2=db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/PropertyNotReadableException.java&p1=db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/model/PropertyNotReadableException.java&r1=1464334&r2=1470235&rev=1470235&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/model/PropertyNotReadableException.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/PropertyNotReadableException.java Sat Apr 20 21:23:39 2013
@@ -1,4 +1,4 @@
-package org.apache.torque.generator.source.model;
+package org.apache.torque.generator.source.transform.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -19,14 +19,14 @@ package org.apache.torque.generator.sour
* under the License.
*/
-import org.apache.torque.generator.source.SourceException;
+import org.apache.torque.generator.source.transform.SourceTransformerException;
/**
* Indicates that an attempt was made to read a property which is not readable.
*
* @version $Id: $
*/
-public class PropertyNotReadableException extends SourceException
+public class PropertyNotReadableException extends SourceTransformerException
{
/** Serial Version UID. */
private static final long serialVersionUID = 1L;
Copied: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/PropertyNotWriteableException.java (from r1464334, db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/model/PropertyNotWriteableException.java)
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/PropertyNotWriteableException.java?p2=db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/PropertyNotWriteableException.java&p1=db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/model/PropertyNotWriteableException.java&r1=1464334&r2=1470235&rev=1470235&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/model/PropertyNotWriteableException.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/PropertyNotWriteableException.java Sat Apr 20 21:23:39 2013
@@ -1,4 +1,4 @@
-package org.apache.torque.generator.source.model;
+package org.apache.torque.generator.source.transform.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -19,7 +19,7 @@ package org.apache.torque.generator.sour
* under the License.
*/
-import org.apache.torque.generator.source.SourceException;
+import org.apache.torque.generator.source.transform.SourceTransformerException;
/**
* Indicates that an attempt was made to write a property which is not
@@ -27,7 +27,7 @@ import org.apache.torque.generator.sourc
*
* @version $Id: $
*/
-public class PropertyNotWriteableException extends SourceException
+public class PropertyNotWriteableException extends SourceTransformerException
{
/** Serial Version UID. */
private static final long serialVersionUID = 1L;
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/StringToBooleanConverter.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/StringToBooleanConverter.java?rev=1470235&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/StringToBooleanConverter.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/StringToBooleanConverter.java Sat Apr 20 21:23:39 2013
@@ -0,0 +1,41 @@
+package org.apache.torque.generator.source.transform.model;
+
+/*
+ * 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.
+ */
+
+/**
+ * Converts a String to a Boolean.
+ *
+ * @version $Id: $
+ *
+ */
+public class StringToBooleanConverter implements TypeConverter
+{
+
+ public boolean accept(final Object value, final Class<?> targetClass)
+ {
+ return (value instanceof String && Boolean.class == targetClass);
+ }
+
+ public Object convert(final Object value, final Class<?> targetClass)
+ {
+ return Boolean.parseBoolean((String) value);
+ }
+
+}
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/TypeConverter.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/TypeConverter.java?rev=1470235&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/TypeConverter.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/model/TypeConverter.java Sat Apr 20 21:23:39 2013
@@ -0,0 +1,50 @@
+package org.apache.torque.generator.source.transform.model;
+
+/*
+ * 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.
+ */
+
+/**
+ * Converts a type to another type.
+ *
+ * @version $Id: $
+ */
+public interface TypeConverter
+{
+ /**
+ * Returns true if and only if the converter can convert the passed value
+ * to the passed class.
+ *
+ * @param value the value to convert, may be null.
+ * @param targetClass the target class, not null.
+ *
+ * @return whether the converter can convert the value to the passed
+ * target class.
+ */
+ boolean accept(Object value, Class<?> targetClass);
+ /**
+ * Converts the passed value to the passed class.
+ * Is only called if accept has returned true.
+ *
+ * @param value the value to convert, may be null.
+ * @param targetClass the target class, not null.
+ *
+ * @return the converted value.
+ */
+ Object convert(Object value, Class<?> targetClass);
+}
Modified: 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=1470235&r1=1470234&r2=1470235&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyOutlet.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyOutlet.java Sat Apr 20 21:23:39 2013
@@ -19,10 +19,6 @@ package org.apache.torque.generator.temp
* 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;
@@ -46,9 +42,9 @@ import org.apache.torque.generator.varia
/**
- * A Outlet which uses a groovy script for generation.
+ * A Outlet which uses groovy for generation.
*/
-public class GroovyOutlet extends TemplateOutletImpl
+public abstract class GroovyOutlet extends TemplateOutletImpl
{
/**
* The name under which the Torque generator interface will be put
@@ -66,18 +62,18 @@ public class GroovyOutlet extends Templa
private static Log log = LogFactory.getLog(GroovyOutlet.class);
/**
- * Whether the options should be put into the context.
+ * Whether the options should be put into the binding.
*/
private boolean optionsInBinding = true;
/**
- * Whether the variables should be put into the context.
+ * Whether the variables should be put into the binding.
*/
private boolean variablesInBinding = true;
/**
* Whether the attributes of the current source element should be put
- * into the context.
+ * into the binding.
*/
private boolean sourceAttributesInBinding = true;
@@ -96,10 +92,10 @@ public class GroovyOutlet extends Templa
* template.
*/
public GroovyOutlet(
- QualifiedName name,
- ConfigurationProvider configurationProvider,
- String path,
- String encoding)
+ final QualifiedName name,
+ final ConfigurationProvider configurationProvider,
+ final String path,
+ final String encoding)
throws ConfigurationException
{
super(name,
@@ -119,7 +115,7 @@ public class GroovyOutlet extends Templa
* @see org.apache.torque.generator.outlet.Outlet#execute(ControllerState)
*/
@Override
- public OutletResult execute(ControllerState controllerState)
+ public OutletResult execute(final ControllerState controllerState)
throws GeneratorException
{
@@ -128,34 +124,18 @@ public class GroovyOutlet extends Templa
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);
+ final 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();
+ final String result = executeGroovy(binding, controllerState);
return new OutletResult(result);
}
- catch (Exception e)
+ catch (final Exception e)
{
- throw new GeneratorException(e);
+ throw new GeneratorException(
+ "Error executing template " + getName(),
+ e);
}
finally
{
@@ -166,21 +146,34 @@ public class GroovyOutlet extends Templa
}
}
+ /**
+ * Executes the Groovy script or template and retuns the reult.
+ * @param binding the binding, not null.
+ * @param controllerState the controller state, not null.
+ *
+ * @return the generation result.
+ *
+ * @throws GeneratorException if generation fails.
+ */
+ protected abstract String executeGroovy(
+ Map<String, Object> binding,
+ ControllerState controllerState)
+ throws GeneratorException;
- public Map<String, Object> createBinding(ControllerState controllerState)
+ public Map<String, Object> createBinding(final ControllerState controllerState)
{
- Map<String, Object> binding = new HashMap<String, Object>();
+ final 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
+ final Options visibleOptions
= controllerState.getVisibleOptions();
- for (Option option : visibleOptions.values())
+ for (final Option option : visibleOptions.values())
{
- QualifiedName qualifiedName = option.getQualifiedName();
+ final QualifiedName qualifiedName = option.getQualifiedName();
binding.put(qualifiedName.getName(), option.getValue());
}
log.debug("Put options in context " + visibleOptions.keySet());
@@ -190,13 +183,14 @@ public class GroovyOutlet extends Templa
log.debug("options in binding are disabled");
}
- SourceElement sourceElement = controllerState.getSourceElement();
- if (sourceAttributesInBinding)
+ final Object model = controllerState.getModel();
+ if (sourceAttributesInBinding && model instanceof SourceElement)
{
- Set<String> attributes = sourceElement.getAttributeNames();
+ final SourceElement sourceElement = (SourceElement) model;
+ final Set<String> attributes = sourceElement.getAttributeNames();
for (String key : attributes)
{
- Object value = sourceElement.getAttribute(key);
+ final Object value = sourceElement.getAttribute(key);
if (key == null)
{
// The null key cannot be accessed in the binding.
@@ -224,16 +218,16 @@ public class GroovyOutlet extends Templa
// 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
+ final Namespace namespace = getName().getNamespace();
+ final VariableStore variableStore
= controllerState.getVariableStore();
- QualifiedNameMap<Variable> visibleVariables
+ final QualifiedNameMap<Variable> visibleVariables
= variableStore
.getContent()
.getInHierarchy(namespace);
- for (Variable variable : visibleVariables.values())
+ for (final Variable variable : visibleVariables.values())
{
- QualifiedName qualifiedName = variable.getName();
+ final QualifiedName qualifiedName = variable.getName();
binding.put(
qualifiedName.getName(),
variable.getValue());
@@ -256,7 +250,7 @@ public class GroovyOutlet extends Templa
*
* @param optionsInBinding whether to put the options into the context.
*/
- public void setOptionsInBinding(boolean optionsInBinding)
+ public void setOptionsInBinding(final boolean optionsInBinding)
{
this.optionsInBinding = optionsInBinding;
}
@@ -280,7 +274,7 @@ public class GroovyOutlet extends Templa
*
* @param variablesInBinding whether to put the variables into the context.
*/
- public void setVariablesInContext(boolean variablesInBinding)
+ public void setVariablesInContext(final boolean variablesInBinding)
{
this.variablesInBinding = variablesInBinding;
}
@@ -304,7 +298,7 @@ public class GroovyOutlet extends Templa
* into the binding.
*/
public void setSourceAttributesInBinding(
- boolean sourceAttributesInBinding)
+ final boolean sourceAttributesInBinding)
{
this.sourceAttributesInBinding = sourceAttributesInBinding;
}
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyScriptOutlet.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyScriptOutlet.java?rev=1470235&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyScriptOutlet.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyScriptOutlet.java Sat Apr 20 21:23:39 2013
@@ -0,0 +1,88 @@
+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.Binding;
+import groovy.lang.GroovyShell;
+
+import java.util.Map;
+
+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.qname.QualifiedName;
+
+
+/**
+ * A Outlet which uses a groovy script for generation.
+ */
+public class GroovyScriptOutlet extends GroovyOutlet
+{
+
+ /**
+ * Constructs a new GroovyScriptOutlet.
+ *
+ * @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 GroovyScriptOutlet(
+ final QualifiedName name,
+ final ConfigurationProvider configurationProvider,
+ final String path,
+ final String encoding)
+ throws ConfigurationException
+ {
+ super(name, configurationProvider, path, encoding);
+ }
+
+ @Override
+ protected String executeGroovy(
+ final Map<String, Object> binding,
+ final ControllerState controllerState)
+ throws GeneratorException
+ {
+ try
+ {
+ final Binding scriptBinding = new Binding(binding);
+ final GroovyShell shell = new GroovyShell(scriptBinding);
+ final Object result = shell.evaluate(getContent(controllerState));
+ if (result == null)
+ {
+ return "";
+ }
+ return result.toString();
+ }
+ catch (final Exception e)
+ {
+ throw new GeneratorException(
+ "Error executing groovy script " + getName(),
+ e);
+ }
+ }
+}
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyTemplateOutlet.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyTemplateOutlet.java?rev=1470235&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyTemplateOutlet.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyTemplateOutlet.java Sat Apr 20 21:23:39 2013
@@ -0,0 +1,87 @@
+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.Map;
+
+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.qname.QualifiedName;
+
+
+/**
+ * A Outlet which uses a groovy template for generation.
+ */
+public class GroovyTemplateOutlet extends GroovyOutlet
+{
+
+ /**
+ * Constructs a new GroovyTemplateOutlet.
+ *
+ * @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 GroovyTemplateOutlet(
+ final QualifiedName name,
+ final ConfigurationProvider configurationProvider,
+ final String path,
+ final String encoding)
+ throws ConfigurationException
+ {
+ super(name, configurationProvider, path, encoding);
+ }
+
+ @Override
+ protected String executeGroovy(
+ final Map<String, Object> binding,
+ final ControllerState controllerState)
+ throws GeneratorException
+ {
+ try
+ {
+ final GStringTemplateEngine templateEngine = new GStringTemplateEngine();
+ final Template template = templateEngine.createTemplate(
+ getContent(controllerState));
+ final Writable writable = template.make(binding);
+ final String result = writable.toString();
+ return result;
+ }
+ catch (final Exception e)
+ {
+ throw new GeneratorException(
+ "Error executing groovy template " + getName(),
+ e);
+ }
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org