0029443: It is not possible to store an ExtStringArray Ocaf attribute to any previous...
[occt.git] / src / XmlMDataStd / XmlMDataStd_ExtStringArrayDriver.cxx
index 42ab8da..d4f93ae 100644 (file)
@@ -14,7 +14,7 @@
 // commercial license or contractual agreement.
 
 
-#include <CDM_MessageDriver.hxx>
+#include <Message_Messenger.hxx>
 #include <LDOM_MemManager.hxx>
 #include <Standard_Type.hxx>
 #include <TDataStd_ExtStringArray.hxx>
@@ -24,6 +24,7 @@
 #include <XmlObjMgt.hxx>
 #include <XmlObjMgt_Document.hxx>
 #include <XmlObjMgt_Persistent.hxx>
+#include <XmlLDrivers.hxx>
 
 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_ExtStringArrayDriver,XmlMDF_ADriver)
 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
@@ -31,6 +32,7 @@ IMPLEMENT_DOMSTRING (LastIndexString, "last")
 IMPLEMENT_DOMSTRING (ExtString,       "string")
 IMPLEMENT_DOMSTRING (IsDeltaOn,       "delta")
 IMPLEMENT_DOMSTRING (Separator,       "separator")
+IMPLEMENT_DOMSTRING (AttributeIDString, "extstrarrattguid")
 
 // Searches for a symbol within an array of strings.
 // Returns TRUE if the symbol is found.
@@ -54,7 +56,7 @@ static Standard_Boolean Contains(const Handle(TDataStd_ExtStringArray)& arr,
 //=======================================================================
 
 XmlMDataStd_ExtStringArrayDriver::XmlMDataStd_ExtStringArrayDriver
-                        ( const Handle(CDM_MessageDriver)& theMsgDriver )
+                        ( const Handle(Message_Messenger)& theMsgDriver )
 : XmlMDF_ADriver( theMsgDriver, NULL )
 {}
 
@@ -72,9 +74,9 @@ Handle(TDF_Attribute) XmlMDataStd_ExtStringArrayDriver::NewEmpty() const
 //purpose  : persistent -> transient (retrieve)
 //=======================================================================
 Standard_Boolean XmlMDataStd_ExtStringArrayDriver::Paste
-                                        ( const XmlObjMgt_Persistent&  theSource,
-                                          const Handle(TDF_Attribute)& theTarget,
-                                          XmlObjMgt_RRelocationTable& ) const
+                                        (const XmlObjMgt_Persistent&  theSource,
+                                         const Handle(TDF_Attribute)& theTarget,
+                                         XmlObjMgt_RRelocationTable& ) const
 {
   Standard_Integer aFirstInd, aLastInd, ind;
   TCollection_ExtendedString aValue;
@@ -89,7 +91,7 @@ Standard_Boolean XmlMDataStd_ExtStringArrayDriver::Paste
       TCollection_ExtendedString("Cannot retrieve the first index"
                                  " for ExtStringArray attribute as \"")
         + aFirstIndex + "\"";
-    WriteMessage (aMessageString);
+    myMessageDriver->Send (aMessageString, Message_Fail);
     return Standard_False;
   }
 
@@ -99,7 +101,7 @@ Standard_Boolean XmlMDataStd_ExtStringArrayDriver::Paste
       TCollection_ExtendedString("Cannot retrieve the last index"
                                  " for ExtStringArray attribute as \"")
         + aFirstIndex + "\"";
-    WriteMessage (aMessageString);
+    myMessageDriver->Send (aMessageString, Message_Fail);
     return Standard_False;
   }
 
@@ -187,15 +189,15 @@ Standard_Boolean XmlMDataStd_ExtStringArrayDriver::Paste
     Standard_Integer aDeltaValue;
     if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue)) 
       {
-       TCollection_ExtendedString aMessageString =
-         TCollection_ExtendedString("Cannot retrieve the isDelta value"
-                                 " for IntegerArray attribute as \"")
-        + aDeltaValue + "\"";
-       WriteMessage (aMessageString);
-       return Standard_False;
+        TCollection_ExtendedString aMessageString =
+          TCollection_ExtendedString("Cannot retrieve the isDelta value"
+                                     " for IntegerArray attribute as \"")
+                                     + aDeltaValue + "\"";
+        myMessageDriver->Send (aMessageString, Message_Fail);
+        return Standard_False;
       } 
     else
-      aDelta = (Standard_Boolean)aDeltaValue;
+      aDelta = aDeltaValue != 0;
   }
 #ifdef OCCT_DEBUG
   else if(XmlMDataStd::DocumentVersion() == -1)
@@ -203,6 +205,16 @@ Standard_Boolean XmlMDataStd_ExtStringArrayDriver::Paste
 #endif
   aExtStringArray->SetDelta(aDelta);
 
+  // attribute id
+  Standard_GUID aGUID;
+  XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
+  if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
+    aGUID = TDataStd_ExtStringArray::GetID(); //default case
+  else
+    aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
+
+  aExtStringArray->SetID(aGUID);
+
   return Standard_True;
 }
 
@@ -223,43 +235,49 @@ void XmlMDataStd_ExtStringArrayDriver::Paste (const Handle(TDF_Attribute)& theSo
 
   if (aL != 1) anElement.setAttribute(::FirstIndexString(), aL);
   anElement.setAttribute(::LastIndexString(), anU);
-  anElement.setAttribute(::IsDeltaOn(), aExtStringArray->GetDelta()); 
+  anElement.setAttribute(::IsDeltaOn(), aExtStringArray->GetDelta() ? 1 : 0);
 
   // Find a separator.
   Standard_Boolean found(Standard_True);
-  // Preferrable symbols for the separator: - _ . : ^ ~
-  // Don't use a space as a separator: XML low-level parser sometimes "eats" it.
+  // This improvement was defined in the version 8.
+  // So, if the user wants to save the document under the 7th or earlier versions,
+  // don't apply this improvement.
   Standard_Character c = '-';
-  static Standard_Character aPreferable[] = "-_.:^~";
-  for (i = 0; found && aPreferable[i]; i++)
-  {
-    c = aPreferable[i];
-    found = Contains(aExtStringArray, TCollection_ExtendedString(c));
-  }
-  // If all prefferable symbols exist in the array, 
-  // try to use any other simple symbols.
-  if (found)
+  if (XmlLDrivers::StorageVersion() > 7)
   {
-    c = '!';
-    while (found && c < '~')
+    // Preferrable symbols for the separator: - _ . : ^ ~
+    // Don't use a space as a separator: XML low-level parser sometimes "eats" it.
+    static Standard_Character aPreferable[] = "-_.:^~";
+    for (i = 0; found && aPreferable[i]; i++)
     {
-      found = Standard_False;
+      c = aPreferable[i];
+      found = Contains(aExtStringArray, TCollection_ExtendedString(c));
+    }
+    // If all prefferable symbols exist in the array, 
+    // try to use any other simple symbols.
+    if (found)
+    {
+      c = '!';
+      while (found && c < '~')
+      {
+        found = Standard_False;
 #ifdef _DEBUG
-      TCollection_AsciiString cseparator(c); // deb
+        TCollection_AsciiString cseparator(c); // deb
 #endif
-      TCollection_ExtendedString separator(c);
-      found = Contains(aExtStringArray, separator);
-      if (found)
-      {
-        c++;
-        // Skip forbidden symbols for XML.
-        while (c < '~' && (c == '&' || c == '<'))
+        TCollection_ExtendedString separator(c);
+        found = Contains(aExtStringArray, separator);
+        if (found)
         {
           c++;
+          // Skip forbidden symbols for XML.
+          while (c < '~' && (c == '&' || c == '<'))
+          {
+            c++;
+          }
         }
       }
     }
-  }
+  }// check doc version
   
   if (found)
   {
@@ -314,4 +332,11 @@ void XmlMDataStd_ExtStringArrayDriver::Paste (const Handle(TDF_Attribute)& theSo
       // Set UNICODE value.
       XmlObjMgt::SetExtendedString(theTarget, xstr);
   }
+  if(aExtStringArray->ID() != TDataStd_ExtStringArray::GetID()) {
+    //convert GUID
+    Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
+    Standard_PCharacter pGuidStr = aGuidStr;
+    aExtStringArray->ID().ToCString (pGuidStr);
+    theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);
+  }
 }