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 2014/03/06 22:55:43 UTC
svn commit: r1575063 -
/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceElementToModelTransformer.java
Author: tfischer
Date: Thu Mar 6 21:55:42 2014
New Revision: 1575063
URL: http://svn.apache.org/r1575063
Log:
gracefully skip nonexistent attributes and elements while transforming to a typed model
Modified:
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceElementToModelTransformer.java
Modified: 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=1575063&r1=1575062&r2=1575063&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceElementToModelTransformer.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/source/transform/SourceElementToModelTransformer.java Thu Mar 6 21:55:42 2014
@@ -23,6 +23,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
+import org.apache.log4j.Logger;
import org.apache.torque.generator.configuration.ClassHelper;
import org.apache.torque.generator.configuration.UnitConfiguration;
import org.apache.torque.generator.control.ControllerState;
@@ -45,6 +46,9 @@ public class SourceElementToModelTransfo
*/
public static final String NULL_ATTRIBUTE_FIELD_NAME = "value";
+ /** The class logger. */
+ private static final Logger log = Logger.getLogger(SourceTransformer.class);
+
/** The processor which does the camelback processing. */
private final Camelbacker camelbacker = new Camelbacker();
@@ -55,6 +59,12 @@ public class SourceElementToModelTransfo
/** The class the model root must have. */
private String modelRootClass;
+ /** Whether to ignore unknown attributes in the source tree. */
+ private boolean ignoreUnknownAttributes = true;
+
+ /** Whether to ignore unknown elements in the source tree. */
+ private boolean ignoreUnknownElements = true;
+
/**
* Standard constructor.
*/
@@ -87,6 +97,30 @@ public class SourceElementToModelTransfo
}
/**
+ * Sets whether to ignore unknown attributes in the source tree.
+ *
+ * @param ignoreUnknownAttributes true if unknown attributes
+ * should be ignored, false otherwise.
+ */
+ public void setIgnoreUnknownAttributes(
+ final boolean ignoreUnknownAttributes)
+ {
+ this.ignoreUnknownAttributes = ignoreUnknownAttributes;
+ }
+
+ /**
+ * Sets whether to ignore unknown elements in the source tree.
+ *
+ * @param ignoreUnknownElements true if unknown elements
+ * should be ignored, false otherwise.
+ */
+ public void setIgnoreUnknownElements(
+ final boolean ignoreUnknownElements)
+ {
+ this.ignoreUnknownElements = ignoreUnknownElements;
+ }
+
+ /**
* 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.
@@ -99,8 +133,10 @@ public class SourceElementToModelTransfo
* 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
+ public Object transform(
+ final Object modelRoot,
+ final ControllerState controllerState)
+ throws SourceTransformerException
{
if (modelRootClass == null)
{
@@ -162,6 +198,24 @@ public class SourceElementToModelTransfo
final PropertyAccess propertyAccess = new PropertyAccess(
model,
attributeName);
+ if (!propertyAccess.isPropertyAccessible())
+ {
+ if (!ignoreUnknownAttributes)
+ {
+ throw new NoSuchPropertyException(
+ model,
+ attributeName,
+ propertyAccess.getPrefixList(),
+ propertyAccess.getSuffixList());
+ }
+ else
+ {
+ log.debug("Cannot set property " + attributeName
+ + " on class " + model.getClass()
+ + ", skipping this property.");
+ continue;
+ }
+ }
propertyAccess.setProperty(attributeValue);
}
for (final SourceElement child : sourceElement.getChildren())
@@ -174,11 +228,21 @@ public class SourceElementToModelTransfo
propertyName);
if (!propertyAccess.isPropertyAccessible())
{
- throw new NoSuchPropertyException(
- model,
- propertyName,
- propertyAccess.getPrefixList(),
- propertyAccess.getSuffixList());
+ if (!ignoreUnknownElements)
+ {
+ throw new NoSuchPropertyException(
+ model,
+ propertyName,
+ propertyAccess.getPrefixList(),
+ propertyAccess.getSuffixList());
+ }
+ else
+ {
+ log.debug("Cannot set property " + propertyName
+ + " on class " + model.getClass()
+ + ", skipping this property.");
+ continue;
+ }
}
Object childModelElement = alreadyMapped.get(child);
if (childModelElement != null)
---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org