You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by km...@apache.org on 2007/05/01 01:56:07 UTC

svn commit: r533887 - in /cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/xml: XMLDecoder.java XMLMappingDescriptor.java

Author: kmenard
Date: Mon Apr 30 16:56:06 2007
New Revision: 533887

URL: http://svn.apache.org/viewvc?view=rev&rev=533887
Log:
Backporting fix for CAY-763 (XML Deserialization fails on relationships when using a mapping file).

Modified:
    cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/xml/XMLDecoder.java
    cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/xml/XMLMappingDescriptor.java

Modified: cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/xml/XMLDecoder.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/xml/XMLDecoder.java?view=diff&rev=533887&r1=533886&r2=533887
==============================================================================
--- cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/xml/XMLDecoder.java (original)
+++ cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/xml/XMLDecoder.java Mon Apr 30 16:56:06 2007
@@ -238,18 +238,14 @@
 
         try {
             // This crazy conditional checks if we're decoding a collection. There are two
-            // ways
-            // to enter into this body:
+            // ways to enter into this body:
             // 1) If there are two elements at the same level with the same name, then
-            // they should
-            // part of a collection.
+            // they should be part of a collection.
             // 2) If a single occurring element has the "forceList" attribute set to
-            // "YES", then it
-            // too should be treated as a collection.
+            // "YES", then it too should be treated as a collection.
             // 
             // The final part checks that we have not previously attempted to decode this
-            // collection,
-            // which is necessary to prevent infinite loops .
+            // collection, which is necessary to prevent infinite loops .
             if ((((null != child.getParentNode()) && (XMLUtil.getChildren(
                     child.getParentNode(),
                     child.getNodeName()).size() > 1)) || (child
@@ -360,11 +356,7 @@
         // MappingUtils will really do all the work.
         XMLMappingDescriptor mu = new XMLMappingDescriptor(mappingUrl);
 
-        Object ret = mu.decode(data.getDocumentElement());
-
-        if (dataContext != null) {
-            dataContext.registerNewObject((DataObject) ret);
-        }
+        Object ret = mu.decode(data.getDocumentElement(), dataContext);
 
         return ret;
     }
@@ -534,11 +526,7 @@
             Object o;
 
             if (mu != null) {
-                o = mu.decode(e);
-
-                if (dataContext != null && o instanceof DataObject) {
-                    dataContext.registerNewObject((DataObject) o);
-                }
+                o = mu.decode(e, dataContext);
             }
             else {
                 // decoder will do DataContext registration if needed.

Modified: cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/xml/XMLMappingDescriptor.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/xml/XMLMappingDescriptor.java?view=diff&rev=533887&r1=533886&r2=533887
==============================================================================
--- cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/xml/XMLMappingDescriptor.java (original)
+++ cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/xml/XMLMappingDescriptor.java Mon Apr 30 16:56:06 2007
@@ -63,6 +63,8 @@
 import javax.xml.parsers.DocumentBuilder;
 
 import org.objectstyle.cayenne.CayenneRuntimeException;
+import org.objectstyle.cayenne.DataObject;
+import org.objectstyle.cayenne.access.DataContext;
 import org.objectstyle.cayenne.property.PropertyUtils;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
@@ -80,6 +82,7 @@
 
     private SerializableEntity rootEntity;
     private Map entities;
+    private DataContext dataContext;
 
     /**
      * Creates new XMLMappingDescriptor using a URL that points to the mapping file.
@@ -135,10 +138,13 @@
      * @return The decoded object.
      * @throws CayenneRuntimeException
      */
-    Object decode(Element xml) throws CayenneRuntimeException {
+    Object decode(Element xml, DataContext dataContext) throws CayenneRuntimeException {
 
         // TODO: Add an error check to make sure the mapping file actually is for this
         // data file.
+        
+        // Store a local copy of the data context.
+        this.dataContext = dataContext;
 
         // Create the object to be returned.
         Object ret = createObject(rootEntity.getDescriptor(), xml);
@@ -226,7 +232,7 @@
     }
 
     /**
-     * Sets decoded object property. If a property os of Collection type, an object is
+     * Sets decoded object property. If a property is of Collection type, an object is
      * added to the collection.
      */
     private void setProperty(Object object, String propertyName, Object value) {
@@ -271,6 +277,11 @@
         catch (Exception ex) {
             throw new CayenneRuntimeException("Error creating instance of class "
                     + className, ex);
+        }
+        
+        // If a data context has been supplied by the user, then register the data object with the context.
+        if ((null != dataContext) && (object instanceof DataObject)) {
+            dataContext.registerNewObject((DataObject) object);
         }
 
         NamedNodeMap attributes = objectData.getAttributes();