b311480e |
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 | |
7fd59977 |
20 | |
21 | #include <XmlMDataStd_TreeNodeDriver.ixx> |
f7b4312f |
22 | #include <NCollection_LocalArray.hxx> |
7fd59977 |
23 | #include <TDataStd_TreeNode.hxx> |
24 | #include <XmlObjMgt.hxx> |
25 | |
26 | IMPLEMENT_DOMSTRING (TreeIdString, "treeid") |
27 | IMPLEMENT_DOMSTRING (ChildrenString, "children") |
28 | |
29 | //======================================================================= |
30 | //function : XmlMDataStd_TreeNodeDriver |
31 | //purpose : Constructor |
32 | //======================================================================= |
33 | |
34 | XmlMDataStd_TreeNodeDriver::XmlMDataStd_TreeNodeDriver |
35 | (const Handle(CDM_MessageDriver)& theMsgDriver) |
36 | : XmlMDF_ADriver (theMsgDriver, NULL) |
37 | {} |
38 | |
39 | //======================================================================= |
40 | //function : NewEmpty |
41 | //purpose : |
42 | //======================================================================= |
43 | Handle(TDF_Attribute) XmlMDataStd_TreeNodeDriver::NewEmpty() const |
44 | { |
45 | return (new TDataStd_TreeNode()); |
46 | } |
47 | |
48 | //======================================================================= |
49 | //function : Paste |
50 | //purpose : |
51 | //======================================================================= |
52 | Standard_Boolean XmlMDataStd_TreeNodeDriver::Paste |
53 | (const XmlObjMgt_Persistent& theSource, |
54 | const Handle(TDF_Attribute)& theTarget, |
55 | XmlObjMgt_RRelocationTable& theRelocTable) const |
56 | { |
57 | Handle(TDataStd_TreeNode) aT = Handle(TDataStd_TreeNode)::DownCast(theTarget); |
58 | const XmlObjMgt_Element& anElement = theSource; |
59 | |
60 | // tree id |
61 | XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::TreeIdString()); |
62 | Standard_GUID aGUID (Standard_CString(aGUIDStr.GetString())); |
63 | aT->SetTreeID(aGUID); |
64 | |
65 | // children |
66 | Handle(TDataStd_TreeNode) aTChild; |
67 | |
68 | XmlObjMgt_DOMString aChildrenStr = anElement.getAttribute(::ChildrenString()); |
69 | if (aChildrenStr != NULL) // void list is allowed |
70 | { |
71 | Standard_CString aChildren = Standard_CString(aChildrenStr.GetString()); |
72 | Standard_Integer aNb = 0; |
73 | if (!XmlObjMgt::GetInteger(aChildren, aNb)) return Standard_False; |
74 | |
75 | while (aNb > 0) |
76 | { |
77 | // Find or create TreeNode attribute with the given ID |
78 | if (theRelocTable.IsBound(aNb)) |
79 | { |
80 | aTChild = Handle(TDataStd_TreeNode)::DownCast(theRelocTable.Find(aNb)); |
81 | if (aTChild.IsNull()) |
82 | return Standard_False; |
83 | } |
84 | else |
85 | { |
86 | aTChild = new TDataStd_TreeNode; |
87 | theRelocTable.Bind(aNb, aTChild); |
88 | } |
89 | |
90 | // Add the child to the current tree |
91 | aTChild->SetTreeID(aGUID); |
92 | aT->Append(aTChild); |
93 | |
94 | // Get next child ID |
95 | if (!XmlObjMgt::GetInteger(aChildren, aNb)) aNb = 0; |
96 | } |
97 | } |
98 | return Standard_True; |
99 | } |
100 | |
101 | //======================================================================= |
102 | //function : Paste |
103 | //purpose : |
104 | //======================================================================= |
105 | void XmlMDataStd_TreeNodeDriver::Paste |
106 | (const Handle(TDF_Attribute)& theSource, |
107 | XmlObjMgt_Persistent& theTarget, |
108 | XmlObjMgt_SRelocationTable& theRelocTable) const |
109 | { |
110 | Handle(TDataStd_TreeNode) aS = Handle(TDataStd_TreeNode)::DownCast(theSource); |
111 | |
7fd59977 |
112 | // tree id |
f7b4312f |
113 | Standard_Integer aNb; |
7fd59977 |
114 | Standard_Character aGuidStr [40]; |
115 | Standard_PCharacter pGuidStr=aGuidStr; |
116 | aS->ID().ToCString (pGuidStr); |
117 | theTarget.Element().setAttribute(::TreeIdString(), aGuidStr); |
118 | |
f7b4312f |
119 | // Find number of children. |
120 | int nbChildren = aS->NbChildren(); |
121 | |
122 | // Allocate 11 digits for each ID (an integer) of the child + a space. |
123 | Standard_Integer iChar = 0; |
124 | NCollection_LocalArray<Standard_Character> str; |
125 | if (nbChildren) |
126 | str.Allocate(11 * nbChildren + 1); |
7fd59977 |
127 | |
128 | // form the string of numbers for the list of children |
f7b4312f |
129 | Handle(TDataStd_TreeNode) aF = aS->First(); |
7fd59977 |
130 | while (!aF.IsNull()) |
131 | { |
132 | aNb = theRelocTable.FindIndex(aF); |
133 | if (aNb == 0) |
134 | { |
135 | aNb = theRelocTable.Add(aF); |
136 | } |
f7b4312f |
137 | |
138 | // Add number to the long string. |
139 | iChar += Sprintf(&(str[iChar]), "%d ", aNb); |
7fd59977 |
140 | |
141 | // next child |
142 | aF = aF->Next(); |
143 | } |
144 | |
f7b4312f |
145 | if (nbChildren) |
146 | { |
147 | theTarget.Element().setAttribute(::ChildrenString(), (Standard_Character*)str); |
148 | } |
7fd59977 |
149 | } |