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