3e906b660ad273707e9648c796c1022028c8bf36
[occt.git] / src / XmlMDataStd / XmlMDataStd_TreeNodeDriver.cxx
1 // Created on: 2001-08-24
2 // Created by: Alexnder GRIGORIEV
3 // Copyright (c) 2001-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <CDM_MessageDriver.hxx>
18 #include <NCollection_LocalArray.hxx>
19 #include <Standard_Type.hxx>
20 #include <TDataStd_TreeNode.hxx>
21 #include <TDF_Attribute.hxx>
22 #include <XmlMDataStd_TreeNodeDriver.hxx>
23 #include <XmlObjMgt.hxx>
24 #include <XmlObjMgt_Persistent.hxx>
25
26 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_TreeNodeDriver,XmlMDF_ADriver)
27 IMPLEMENT_DOMSTRING (TreeIdString,   "treeid")
28 IMPLEMENT_DOMSTRING (ChildrenString, "children")
29
30 //=======================================================================
31 //function : XmlMDataStd_TreeNodeDriver
32 //purpose  : Constructor
33 //=======================================================================
34
35 XmlMDataStd_TreeNodeDriver::XmlMDataStd_TreeNodeDriver
36                         (const Handle(CDM_MessageDriver)& theMsgDriver)
37       : XmlMDF_ADriver (theMsgDriver, NULL)
38 {}
39
40 //=======================================================================
41 //function : NewEmpty
42 //purpose  : 
43 //=======================================================================
44 Handle(TDF_Attribute) XmlMDataStd_TreeNodeDriver::NewEmpty() const
45 {
46   return (new TDataStd_TreeNode());
47 }
48
49 //=======================================================================
50 //function : Paste
51 //purpose  : 
52 //=======================================================================
53 Standard_Boolean XmlMDataStd_TreeNodeDriver::Paste
54                                (const XmlObjMgt_Persistent&  theSource,
55                                 const Handle(TDF_Attribute)& theTarget,
56                                 XmlObjMgt_RRelocationTable& theRelocTable) const
57 {
58   Handle(TDataStd_TreeNode) aT = Handle(TDataStd_TreeNode)::DownCast(theTarget);
59   const XmlObjMgt_Element& anElement = theSource;
60
61   // tree id
62   Standard_GUID aGUID;
63   XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::TreeIdString());
64   if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
65     aGUID = TDataStd_TreeNode::GetDefaultTreeID();
66   else
67     aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString()));
68   aT->SetTreeID(aGUID);
69
70   // children
71   Handle(TDataStd_TreeNode) aTChild;
72
73   XmlObjMgt_DOMString aChildrenStr = anElement.getAttribute(::ChildrenString());
74   if (aChildrenStr != NULL)                     // void list is allowed
75   {
76     Standard_CString aChildren = Standard_CString(aChildrenStr.GetString());
77     Standard_Integer aNb = 0;
78     if (!XmlObjMgt::GetInteger(aChildren, aNb)) return Standard_False;
79
80     while (aNb > 0)
81     {
82       // Find or create TreeNode attribute with the given ID
83       if (theRelocTable.IsBound(aNb))
84       {
85         aTChild = Handle(TDataStd_TreeNode)::DownCast(theRelocTable.Find(aNb));
86         if (aTChild.IsNull())
87           return Standard_False;
88       }
89       else
90       {
91         aTChild = new TDataStd_TreeNode;
92         theRelocTable.Bind(aNb, aTChild);
93       }
94
95       // Add the child to the current tree
96       aTChild->SetTreeID(aGUID);
97       aT->Append(aTChild);
98
99       // Get next child ID
100       if (!XmlObjMgt::GetInteger(aChildren, aNb)) aNb = 0;
101     }
102   }
103   return Standard_True;
104 }
105
106 //=======================================================================
107 //function : Paste
108 //purpose  : 
109 //=======================================================================
110 void XmlMDataStd_TreeNodeDriver::Paste
111                                (const Handle(TDF_Attribute)& theSource,
112                                 XmlObjMgt_Persistent&       theTarget,
113                                 XmlObjMgt_SRelocationTable& theRelocTable) const
114 {
115   Handle(TDataStd_TreeNode) aS = Handle(TDataStd_TreeNode)::DownCast(theSource);
116
117   // tree id
118   if (aS->ID() != TDataStd_TreeNode::GetDefaultTreeID())
119   {
120     Standard_Character aGuidStr [40];
121     Standard_PCharacter pGuidStr=aGuidStr;
122     aS->ID().ToCString (pGuidStr);
123     theTarget.Element().setAttribute(::TreeIdString(), aGuidStr);
124   }
125
126   // Find number of children.
127   int nbChildren = aS->NbChildren();
128   
129   // Allocate 11 digits for each ID (an integer) of the child + a space.
130   Standard_Integer iChar = 0;
131   NCollection_LocalArray<Standard_Character> str;
132   if (nbChildren)
133     str.Allocate(11 * nbChildren + 1);
134
135   // form the string of numbers for the list of children
136   Handle(TDataStd_TreeNode) aF = aS->First();
137   while (!aF.IsNull())
138   {
139     Standard_Integer aNb = theRelocTable.FindIndex(aF);
140     if (aNb == 0)
141     {
142       aNb = theRelocTable.Add(aF);
143     }
144
145     // Add number to the long string.
146     iChar += Sprintf(&(str[iChar]), "%d ", aNb);
147
148     // next child
149     aF = aF->Next();
150   }
151
152   if (nbChildren)
153   {
154     theTarget.Element().setAttribute(::ChildrenString(), (Standard_Character*)str);
155   }
156 }