You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by ro...@apache.org on 2006/12/05 12:31:07 UTC

svn commit: r482604 - in /incubator/tuscany/cpp/sdo/runtime/core: src/commonj/sdo/CopyHelper.cpp src/commonj/sdo/CopyHelper.h src/commonj/sdo/DataObjectImpl.cpp test/main.cpp test/sdotest.cpp test/sdotest.h

Author: robbinspg
Date: Tue Dec  5 03:30:59 2006
New Revision: 482604

URL: http://svn.apache.org/viewvc?view=rev&rev=482604
Log:
TUSCANY-950 Apply Simon Laws' patch to copy sequenced DO

Modified:
    incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/CopyHelper.cpp
    incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/CopyHelper.h
    incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/DataObjectImpl.cpp
    incubator/tuscany/cpp/sdo/runtime/core/test/main.cpp
    incubator/tuscany/cpp/sdo/runtime/core/test/sdotest.cpp
    incubator/tuscany/cpp/sdo/runtime/core/test/sdotest.h

Modified: incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/CopyHelper.cpp
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/CopyHelper.cpp?view=diff&rev=482604&r1=482603&r2=482604
==============================================================================
--- incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/CopyHelper.cpp (original)
+++ incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/CopyHelper.cpp Tue Dec  5 03:30:59 2006
@@ -177,6 +177,71 @@
         } // for
     } // method
 
+
+
+    void CopyHelper::transfersequenceitem(Sequence *to, Sequence *from, const Property& p, int index)
+    {
+        switch (p.getTypeEnum())
+        {
+        case Type::BooleanType:
+            to->addBoolean(    p, from->getBooleanValue(index));
+            break;
+        case Type::ByteType:
+            to->addByte( p, from->getByteValue(index));
+            break;
+        case Type::CharacterType:
+            to->addCharacter( p, from->getCharacterValue(index));
+            break;
+        case Type::IntegerType: 
+            to->addInteger( p, from->getIntegerValue(index));
+            break;
+        case Type::ShortType:
+            to->addShort( p,from->getShortValue(index));
+            break;
+        case Type::DoubleType:
+            to->addDouble( p, from->getDoubleValue(index));
+            break;
+        case Type::FloatType:
+            to->addFloat( p, from->getFloatValue(index));
+            break;
+        case Type::LongType:
+            to->addLong( p, from->getLongValue(index));
+            break;
+        case Type::DateType:
+            to->addDate( p, from->getDateValue(index));
+            break;
+        case Type::BigDecimalType: 
+        case Type::BigIntegerType: 
+        case Type::UriType:
+        case Type::StringType:
+            {
+                unsigned int siz =     from->getLength(index);
+                if (siz > 0)
+                {
+                    wchar_t * buf = new wchar_t[siz];
+                    from->getStringValue(index, buf, siz);
+                    to->addString(p, buf, siz);
+                    delete buf;
+                }
+            }
+            break;
+        case Type::BytesType:
+            {
+                unsigned int siz = from->getLength(index);
+                if (siz > 0)
+                {
+                    char * buf = new char[siz];
+                    from->getBytesValue(index, buf, siz);
+                    to->addBytes(p, buf, siz);
+                    delete buf;
+                }
+            }
+            break;
+        default:
+            break;
+        }  // switch
+    }
+
     /** CopyHelper provides static copying helper functions.
      *
      * copyShallow() copies the DataType members of the data object.
@@ -212,55 +277,148 @@
         DataObjectPtr newob = fac->create(t);
         if (!newob) return 0;
 
-        PropertyList pl = dataObject->getInstanceProperties();
-        for (unsigned int i=0;i < pl.size(); i++)
+        if ( dataObject->getType().isSequencedType() )
         {
-            if (dataObject->isSet(pl[i]))
+            Sequence* fromSequence = dataObject->getSequence();
+            int sequence_length = fromSequence->size();
+            
+            Sequence* toSequence = newob->getSequence();
+            
+            for (int i=0;i < sequence_length; i++)
             {
-                // data objects are only copied in the deep copy case
-                if (pl[i].getType().isDataObjectType()) 
+                if ( fromSequence->isText(i) )
                 {
-                    if (!fullCopy) 
-                    {
-                        continue;
-                    }
-                    else
-                    {
-                        if (pl[i].isMany())
+                    const char *text = fromSequence->getCStringValue(i);
+                    toSequence->addText(i, text);
+                } 
+                else 
+                {
+                    const Property& seqProperty = fromSequence->getProperty(i); 
+                    SDOXMLString seqPropertyName = seqProperty.getName();
+                    const Type& seqPropertyType = seqProperty.getType();
+
+                    if (seqPropertyType.isDataObjectType())
+                    {                                
+                        if (!fullCopy) 
                         {
-                            DataObjectList& dolold = dataObject->getList(pl[i]);
-                            DataObjectList& dolnew = newob->getList(pl[i]);
-                            for (unsigned int i=0;i< dolold.size(); i++)
+                            continue;
+                        }
+                        else
+                        {
+                            DataObjectPtr dob;
+
+                            // retrieve the data object to be copied
+                            if (seqProperty.isMany())
+                            {
+                                int index = fromSequence->getListIndex(i);
+                                dob = dataObject->getList(seqProperty)[index];
+                            }
+                            else
+                            {
+                                dob = dataObject->getDataObject(seqProperty);
+                            }
+                              
+                            // do the copying of referencing
+                            if (dob)
                             {
-                                dolnew.append(internalCopy(dolold[i],true));
+                                // Handle non-containment reference to DataObject
+                                if (seqProperty.isReference())
+                                {
+                                    // add just the reference into the sequence
+                                    // doesn't seem to bother in the non sequenced
+                                    // code so need to check with SDO people 
+                                    // In the mean time I'll thrown and exception
+                                    // just in case we get here
+                                    std::string msg("Attempt to close sequenced data object which has non containment reference ");
+                                    msg += (const char *)seqPropertyName;
+                                    SDO_THROW_EXCEPTION("internalCopy", 
+                                                        SDOUnsupportedOperationException,
+                                                        msg.c_str());
+                                }
+                                else
+                                {
+                                    // make a copy of the data object itself
+                                    // and add it to the sequence
+                                    toSequence->addDataObject(seqProperty,
+                                                              internalCopy(dob,
+                                                                           true));
+                                }
                             }
                         }
-                        else 
+                    } 
+                    else
+                    {
+                        // Sequence member is a primitive
+                        if (seqProperty.isMany())
                         {
-                            DataObjectPtr dob = dataObject->getDataObject(pl[i]);
-                            newob->setDataObject(pl[i],internalCopy(dob,true));
+                            int index = fromSequence->getListIndex(i);
+                            transfersequenceitem(toSequence,
+                                                 fromSequence,
+                                                 seqProperty,
+                                                 index);
                         }
-                    }
-                }
-                else 
+                        else
+                        {
+                            transfersequenceitem(toSequence,
+                                                 fromSequence,
+                                                 seqProperty,
+                                                 i);
+                        }                           
+                    } 
+                } // is it a text element
+            } // for all elements in sequence
+        }
+        else
+        {
+            PropertyList pl = dataObject->getInstanceProperties();
+            for (unsigned int i=0;i < pl.size(); i++)
+            {
+                if (dataObject->isSet(pl[i]))
                 {
-                    if (pl[i].isMany())
+                    // data objects are only copied in the deep copy case
+                    if (pl[i].getType().isDataObjectType()) 
                     {
-                        DataObjectList& dolold = dataObject->getList(pl[i]);
-                        DataObjectList& dolnew = newob->getList(pl[i]);
-                        transferlist(dolnew,dolold, pl[i].getTypeEnum());
+                        if (!fullCopy) 
+                        {
+                            continue;
+                        }
+                        else
+                        {
+                            if (pl[i].isMany())
+                            {
+                                DataObjectList& dolold = dataObject->getList(pl[i]);
+                                DataObjectList& dolnew = newob->getList(pl[i]);
+                                for (unsigned int i=0;i< dolold.size(); i++)
+                                {
+                                    dolnew.append(internalCopy(dolold[i],true));
+                                }
+                            }
+                            else 
+                            {
+                                DataObjectPtr dob = dataObject->getDataObject(pl[i]);
+                                newob->setDataObject(pl[i],internalCopy(dob,true));
+                            }
+                        }
                     }
                     else 
                     {
-                        transferitem(newob,dataObject, pl[i]);
-                    }
-                } // else
+                        if (pl[i].isMany())
+                        {
+                            DataObjectList& dolold = dataObject->getList(pl[i]);
+                            DataObjectList& dolnew = newob->getList(pl[i]);
+                            transferlist(dolnew,dolold, pl[i].getTypeEnum());
+                        }
+                        else 
+                        {
+                            transferitem(newob,dataObject, pl[i]);
+                        }
+                    } // else
+                } 
             } 
-        } 
+        }
+
         return newob;
     }
-    
-
 }
 };
 

Modified: incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/CopyHelper.h
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/CopyHelper.h?view=diff&rev=482604&r1=482603&r2=482604
==============================================================================
--- incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/CopyHelper.h (original)
+++ incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/CopyHelper.h Tue Dec  5 03:30:59 2006
@@ -62,6 +62,8 @@
     private:
     static void transferlist(DataObjectList& to, DataObjectList& from, Type::Types t);
     static void transferitem(DataObjectPtr   to, DataObjectPtr   from, const Property& p);
+    static void transfersequenceitem(Sequence *to, Sequence *from, const Property& p, int index);
+
     static DataObjectPtr internalCopy(DataObjectPtr dataObject, bool fullCopy);
 
 };

Modified: incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/DataObjectImpl.cpp
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/DataObjectImpl.cpp?view=diff&rev=482604&r1=482603&r2=482604
==============================================================================
--- incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/DataObjectImpl.cpp (original)
+++ incubator/tuscany/cpp/sdo/runtime/core/src/commonj/sdo/DataObjectImpl.cpp Tue Dec  5 03:30:59 2006
@@ -1511,7 +1511,7 @@
             }
         }
 
-        int propIndex = getPropertyIndex(p);
+        int propIndex = getPropertyIndexInternal(p);
         DataObjectImpl* d = getDataObjectImpl(propIndex);
         if (d == 0) {
             // There is no list yet, so we need to create an 
@@ -1612,14 +1612,27 @@
 			// tries to access the index of the property 
 			// and it doesn't exist because it hasn't been created yet. 
 			// This new method is used where properties are being set
-			// based on existing property objects. This is like to 
+			// based on existing property objects. This is likely to 
 			// occur when a data object is being copied. In this case
 			// we want properties that are open to be copied also 
 			// so we need to create the property and provide the index
 			if ( this->getType().isOpenType() )
 			{
-			    const Property *prop = defineProperty(p.getName(), p.getType());
-			    index = getPropertyIndex(p);
+                const Property *prop = NULL;
+                
+                // need to treat many valued properties specially
+                // because the property is a list rather than 
+                // a single value
+                if ( p.isMany() )
+                {
+                    prop = defineList(p.getName());                   
+                }
+                else
+                {
+			        prop = defineProperty(p.getName(), p.getType());
+                }
+                
+                index = getPropertyIndex(p);
 			}
 			else
 			{

Modified: incubator/tuscany/cpp/sdo/runtime/core/test/main.cpp
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sdo/runtime/core/test/main.cpp?view=diff&rev=482604&r1=482603&r2=482604
==============================================================================
--- incubator/tuscany/cpp/sdo/runtime/core/test/main.cpp (original)
+++ incubator/tuscany/cpp/sdo/runtime/core/test/main.cpp Tue Dec  5 03:30:59 2006
@@ -176,6 +176,7 @@
     TEST (  sdotest::testXPath() );
 
     TEST (  sdotest::cdatatest() );
+	TEST (  sdotest::cloneopentest() );
 	TEST (  sdotest::tuscany963() );
 
     cout << "Total tests:" << totaltests << " Tests passed:" << testspassed << endl;

Modified: incubator/tuscany/cpp/sdo/runtime/core/test/sdotest.cpp
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sdo/runtime/core/test/sdotest.cpp?view=diff&rev=482604&r1=482603&r2=482604
==============================================================================
--- incubator/tuscany/cpp/sdo/runtime/core/test/sdotest.cpp (original)
+++ incubator/tuscany/cpp/sdo/runtime/core/test/sdotest.cpp Tue Dec  5 03:30:59 2006
@@ -9119,8 +9119,89 @@
     }
     catch (SDORuntimeException e)
     {
-        cout << "Exception in interop test" <<  endl;
+        cout << "Exception in cdata test" <<  endl;
         cout << e.getMessageText();
         return 0;
     }
+}
+
+int sdotest::cloneopentest()
+{
+   try 
+   {
+      DataFactoryPtr mdg  = DataFactory::getDataFactory();
+
+      /**
+       * Get an XSD helper to load XSD information into the
+       * data factory
+       */
+      XSDHelperPtr myXSDHelper = HelperProvider::getXSDHelper(mdg);
+      myXSDHelper->defineFile("clone.xsd");
+
+      /**
+       * Check if there were any errors. The parse may still
+       * succeed, but errors indicate some elements were not
+       * understood 
+       */
+      unsigned int i = 0;
+      unsigned int j = 0;
+      if ((i = myXSDHelper->getErrorCount()) > 0)
+      {
+	 cout << "XSD Loading reported some errors:" << endl;
+	 for (j=0;j<i;j++)
+	 {
+	    const char *m = myXSDHelper->getErrorMessage(j);
+	    if (m != 0) cout << m;
+	    cout << endl;
+	    return 0;
+	 }
+      }
+
+      /** 
+       * Use the same data factory to load XML corresponding to
+       * data objects adhering to the previously loaded schema
+       */
+      XMLHelperPtr myXMLHelper = HelperProvider::getXMLHelper(mdg);
+      XMLDocumentPtr myXMLDocument = myXMLHelper->loadFile("clone-in.xml", "http://www.example.org/test");
+    
+      /**
+       * Check if there were any errors. The parse may still
+       * succeed, but errors indicate some elements did not match
+       * the schema, or were malformed.
+       * 
+       */
+      if ((i = myXMLHelper->getErrorCount()) > 0)
+      {
+	 cout << "XML Loading reported some errors:" << endl;
+	 for (j=0;j<i;j++)
+	 {
+	    const char *m = myXMLHelper->getErrorMessage(j);
+	    if (m != 0) cout << m;
+	    cout << endl;
+	    return 0;
+	 }
+      }
+
+      DataObjectPtr original = myXMLDocument->getRootDataObject();
+
+      // copy the data object we just read in
+      DataObjectPtr clone = CopyHelper::copy(original);
+
+      // create a new document based on this cloned data object
+      XMLDocumentPtr myNewXMLDocument = myXMLHelper->createDocument(clone,
+                                                                    "http://www.example.org/test",
+                                                                    "Clone");
+
+      // write the cloned document out to a file
+      myXMLHelper->save(myNewXMLDocument, "clone-testout.xml");
+
+      return comparefiles("clone-out.xml","clone-testout.xml");
+
+   }
+   catch (SDORuntimeException e)
+   {
+      cout << "Exception in clone test" <<  endl;
+      cout << e.getMessageText();
+      return 0;
+   }
 }

Modified: incubator/tuscany/cpp/sdo/runtime/core/test/sdotest.h
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sdo/runtime/core/test/sdotest.h?view=diff&rev=482604&r1=482603&r2=482604
==============================================================================
--- incubator/tuscany/cpp/sdo/runtime/core/test/sdotest.h (original)
+++ incubator/tuscany/cpp/sdo/runtime/core/test/sdotest.h Tue Dec  5 03:30:59 2006
@@ -199,5 +199,6 @@
         static int b45933();
         static int testXPath();
         static int cdatatest();
+        static int cloneopentest();
         static int tuscany963();
 };



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org