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 <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>
26 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_TreeNodeDriver,XmlMDF_ADriver)
27 IMPLEMENT_DOMSTRING (TreeIdString, "treeid")
28 IMPLEMENT_DOMSTRING (ChildrenString, "children")
30 //=======================================================================
31 //function : XmlMDataStd_TreeNodeDriver
32 //purpose : Constructor
33 //=======================================================================
35 XmlMDataStd_TreeNodeDriver::XmlMDataStd_TreeNodeDriver
36 (const Handle(CDM_MessageDriver)& theMsgDriver)
37 : XmlMDF_ADriver (theMsgDriver, NULL)
40 //=======================================================================
43 //=======================================================================
44 Handle(TDF_Attribute) XmlMDataStd_TreeNodeDriver::NewEmpty() const
46 return (new TDataStd_TreeNode());
49 //=======================================================================
52 //=======================================================================
53 Standard_Boolean XmlMDataStd_TreeNodeDriver::Paste
54 (const XmlObjMgt_Persistent& theSource,
55 const Handle(TDF_Attribute)& theTarget,
56 XmlObjMgt_RRelocationTable& theRelocTable) const
58 Handle(TDataStd_TreeNode) aT = Handle(TDataStd_TreeNode)::DownCast(theTarget);
59 const XmlObjMgt_Element& anElement = theSource;
63 XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::TreeIdString());
64 if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
65 aGUID = TDataStd_TreeNode::GetDefaultTreeID();
67 aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString()));
71 Handle(TDataStd_TreeNode) aTChild;
73 XmlObjMgt_DOMString aChildrenStr = anElement.getAttribute(::ChildrenString());
74 if (aChildrenStr != NULL) // void list is allowed
76 Standard_CString aChildren = Standard_CString(aChildrenStr.GetString());
77 Standard_Integer aNb = 0;
78 if (!XmlObjMgt::GetInteger(aChildren, aNb)) return Standard_False;
82 // Find or create TreeNode attribute with the given ID
83 if (theRelocTable.IsBound(aNb))
85 aTChild = Handle(TDataStd_TreeNode)::DownCast(theRelocTable.Find(aNb));
87 return Standard_False;
91 aTChild = new TDataStd_TreeNode;
92 theRelocTable.Bind(aNb, aTChild);
95 // Add the child to the current tree
96 aTChild->SetTreeID(aGUID);
100 if (!XmlObjMgt::GetInteger(aChildren, aNb)) aNb = 0;
103 return Standard_True;
106 //=======================================================================
109 //=======================================================================
110 void XmlMDataStd_TreeNodeDriver::Paste
111 (const Handle(TDF_Attribute)& theSource,
112 XmlObjMgt_Persistent& theTarget,
113 XmlObjMgt_SRelocationTable& theRelocTable) const
115 Handle(TDataStd_TreeNode) aS = Handle(TDataStd_TreeNode)::DownCast(theSource);
118 if (aS->ID() != TDataStd_TreeNode::GetDefaultTreeID())
120 Standard_Character aGuidStr [40];
121 Standard_PCharacter pGuidStr=aGuidStr;
122 aS->ID().ToCString (pGuidStr);
123 theTarget.Element().setAttribute(::TreeIdString(), aGuidStr);
126 // Find number of children.
127 int nbChildren = aS->NbChildren();
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;
133 str.Allocate(11 * nbChildren + 1);
135 // form the string of numbers for the list of children
136 Handle(TDataStd_TreeNode) aF = aS->First();
139 Standard_Integer aNb = theRelocTable.FindIndex(aF);
142 aNb = theRelocTable.Add(aF);
145 // Add number to the long string.
146 iChar += Sprintf(&(str[iChar]), "%d ", aNb);
154 theTarget.Element().setAttribute(::ChildrenString(), (Standard_Character*)str);