1 // Created on: 2001-08-24
2 // Created by: Alexnder GRIGORIEV
3 // Copyright (c) 2001-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <Message_Messenger.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 #include <XmlLDrivers.hxx>
27 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_TreeNodeDriver,XmlMDF_ADriver)
28 IMPLEMENT_DOMSTRING (TreeIdString, "treeid")
29 IMPLEMENT_DOMSTRING (ChildrenString, "children")
31 //=======================================================================
32 //function : XmlMDataStd_TreeNodeDriver
33 //purpose : Constructor
34 //=======================================================================
36 XmlMDataStd_TreeNodeDriver::XmlMDataStd_TreeNodeDriver
37 (const Handle(Message_Messenger)& theMsgDriver)
38 : XmlMDF_ADriver (theMsgDriver, NULL)
41 //=======================================================================
44 //=======================================================================
45 Handle(TDF_Attribute) XmlMDataStd_TreeNodeDriver::NewEmpty() const
47 return (new TDataStd_TreeNode());
50 //=======================================================================
53 //=======================================================================
54 Standard_Boolean XmlMDataStd_TreeNodeDriver::Paste
55 (const XmlObjMgt_Persistent& theSource,
56 const Handle(TDF_Attribute)& theTarget,
57 XmlObjMgt_RRelocationTable& theRelocTable) const
59 Handle(TDataStd_TreeNode) aT = Handle(TDataStd_TreeNode)::DownCast(theTarget);
60 const XmlObjMgt_Element& anElement = theSource;
64 XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::TreeIdString());
65 if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
66 aGUID = TDataStd_TreeNode::GetDefaultTreeID();
68 aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString()));
72 Handle(TDataStd_TreeNode) aTChild;
74 XmlObjMgt_DOMString aChildrenStr = anElement.getAttribute(::ChildrenString());
75 if (aChildrenStr != NULL) // void list is allowed
77 Standard_CString aChildren = Standard_CString(aChildrenStr.GetString());
78 Standard_Integer aNb = 0;
79 if (!XmlObjMgt::GetInteger(aChildren, aNb)) return Standard_False;
83 // Find or create TreeNode attribute with the given ID
84 if (theRelocTable.IsBound(aNb))
86 aTChild = Handle(TDataStd_TreeNode)::DownCast(theRelocTable.Find(aNb));
88 return Standard_False;
92 aTChild = new TDataStd_TreeNode;
93 theRelocTable.Bind(aNb, aTChild);
96 // Add the child to the current tree
97 aTChild->SetTreeID(aGUID);
101 if (!XmlObjMgt::GetInteger(aChildren, aNb)) aNb = 0;
104 return Standard_True;
107 //=======================================================================
110 //=======================================================================
111 void XmlMDataStd_TreeNodeDriver::Paste
112 (const Handle(TDF_Attribute)& theSource,
113 XmlObjMgt_Persistent& theTarget,
114 XmlObjMgt_SRelocationTable& theRelocTable) const
116 Handle(TDataStd_TreeNode) aS = Handle(TDataStd_TreeNode)::DownCast(theSource);
119 // A not default ID is skipped for storage version 8 and newer.
120 if (aS->ID() != TDataStd_TreeNode::GetDefaultTreeID() ||
121 theRelocTable.GetHeaderData()->StorageVersion().IntegerValue() < 8)
123 Standard_Character aGuidStr [40];
124 Standard_PCharacter pGuidStr=aGuidStr;
125 aS->ID().ToCString (pGuidStr);
126 theTarget.Element().setAttribute(::TreeIdString(), aGuidStr);
129 // Find number of children.
130 int nbChildren = aS->NbChildren();
132 // Allocate 11 digits for each ID (an integer) of the child + a space.
133 Standard_Integer iChar = 0;
134 NCollection_LocalArray<Standard_Character> str;
136 str.Allocate(11 * nbChildren + 1);
138 // form the string of numbers for the list of children
139 Handle(TDataStd_TreeNode) aF = aS->First();
142 Standard_Integer aNb = theRelocTable.FindIndex(aF);
145 aNb = theRelocTable.Add(aF);
148 // Add number to the long string.
149 iChar += Sprintf(&(str[iChar]), "%d ", aNb);
157 theTarget.Element().setAttribute(::ChildrenString(), (Standard_Character*)str);