0023024: Update headers of OCCT files
[occt.git] / src / XmlMDataStd / XmlMDataStd_TreeNodeDriver.cxx
1 // Created on: 2001-08-24
2 // Created by: Alexnder GRIGORIEV
3 // Copyright (c) 2001-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21 #include <XmlMDataStd_TreeNodeDriver.ixx>
22 #include <TDataStd_TreeNode.hxx>
23 #include <XmlObjMgt.hxx>
24
25 IMPLEMENT_DOMSTRING (TreeIdString,   "treeid")
26 IMPLEMENT_DOMSTRING (ChildrenString, "children")
27
28 //=======================================================================
29 //function : XmlMDataStd_TreeNodeDriver
30 //purpose  : Constructor
31 //=======================================================================
32
33 XmlMDataStd_TreeNodeDriver::XmlMDataStd_TreeNodeDriver
34                         (const Handle(CDM_MessageDriver)& theMsgDriver)
35       : XmlMDF_ADriver (theMsgDriver, NULL)
36 {}
37
38 //=======================================================================
39 //function : NewEmpty
40 //purpose  : 
41 //=======================================================================
42 Handle(TDF_Attribute) XmlMDataStd_TreeNodeDriver::NewEmpty() const
43 {
44   return (new TDataStd_TreeNode());
45 }
46
47 //=======================================================================
48 //function : Paste
49 //purpose  : 
50 //=======================================================================
51 Standard_Boolean XmlMDataStd_TreeNodeDriver::Paste
52                                (const XmlObjMgt_Persistent&  theSource,
53                                 const Handle(TDF_Attribute)& theTarget,
54                                 XmlObjMgt_RRelocationTable& theRelocTable) const
55 {
56   Handle(TDataStd_TreeNode) aT = Handle(TDataStd_TreeNode)::DownCast(theTarget);
57   const XmlObjMgt_Element& anElement = theSource;
58
59   // tree id
60   XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::TreeIdString());
61   Standard_GUID aGUID (Standard_CString(aGUIDStr.GetString()));
62   aT->SetTreeID(aGUID);
63
64   // children
65   Handle(TDataStd_TreeNode) aTChild;
66
67   XmlObjMgt_DOMString aChildrenStr = anElement.getAttribute(::ChildrenString());
68   if (aChildrenStr != NULL)                     // void list is allowed
69   {
70     Standard_CString aChildren = Standard_CString(aChildrenStr.GetString());
71     Standard_Integer aNb = 0;
72     if (!XmlObjMgt::GetInteger(aChildren, aNb)) return Standard_False;
73
74     while (aNb > 0)
75     {
76       // Find or create TreeNode attribute with the given ID
77       if (theRelocTable.IsBound(aNb))
78       {
79         aTChild = Handle(TDataStd_TreeNode)::DownCast(theRelocTable.Find(aNb));
80         if (aTChild.IsNull())
81           return Standard_False;
82       }
83       else
84       {
85         aTChild = new TDataStd_TreeNode;
86         theRelocTable.Bind(aNb, aTChild);
87       }
88
89       // Add the child to the current tree
90       aTChild->SetTreeID(aGUID);
91       aT->Append(aTChild);
92
93       // Get next child ID
94       if (!XmlObjMgt::GetInteger(aChildren, aNb)) aNb = 0;
95     }
96   }
97   return Standard_True;
98 }
99
100 //=======================================================================
101 //function : Paste
102 //purpose  : 
103 //=======================================================================
104 void XmlMDataStd_TreeNodeDriver::Paste
105                                (const Handle(TDF_Attribute)& theSource,
106                                 XmlObjMgt_Persistent&       theTarget,
107                                 XmlObjMgt_SRelocationTable& theRelocTable) const
108 {
109   Handle(TDataStd_TreeNode) aS = Handle(TDataStd_TreeNode)::DownCast(theSource);
110
111   Standard_Integer aNb;
112
113   TCollection_AsciiString aChildrenStr;
114
115   // tree id
116   Standard_Character aGuidStr [40];
117   Standard_PCharacter pGuidStr=aGuidStr;
118   aS->ID().ToCString (pGuidStr);
119   theTarget.Element().setAttribute(::TreeIdString(), aGuidStr);
120
121   // first child
122   Handle(TDataStd_TreeNode) aF = aS->First();
123
124   // form the string of numbers for the list of children
125   while (!aF.IsNull())
126   {
127     aNb = theRelocTable.FindIndex(aF);
128     if (aNb == 0)
129     {
130       aNb = theRelocTable.Add(aF);
131     }
132     TCollection_AsciiString aNbStr (aNb);
133     aChildrenStr += aNbStr + " ";
134
135     // next child
136     aF = aF->Next();
137   }
138
139   if (aChildrenStr.Length() > 0)
140     theTarget.Element().setAttribute(::ChildrenString(),
141                                       aChildrenStr.ToCString());
142 }