1 // Created on: 2007-05-29
2 // Created by: Vlad Romashko
3 // Copyright (c) 2007-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 <LDOM_MemManager.hxx>
19 #include <Standard_Type.hxx>
20 #include <TDataStd_ReferenceList.hxx>
21 #include <TDF_Attribute.hxx>
22 #include <TDF_Label.hxx>
23 #include <TDF_ListIteratorOfLabelList.hxx>
24 #include <TDF_Tool.hxx>
25 #include <XmlMDataStd_ReferenceListDriver.hxx>
26 #include <XmlObjMgt.hxx>
27 #include <XmlObjMgt_Document.hxx>
28 #include <XmlObjMgt_Persistent.hxx>
30 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_ReferenceListDriver,XmlMDF_ADriver)
31 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
32 IMPLEMENT_DOMSTRING (LastIndexString, "last")
33 IMPLEMENT_DOMSTRING (ExtString, "string")
34 IMPLEMENT_DOMSTRING (AttributeIDString, "reflistattguid")
35 //=======================================================================
36 //function : XmlMDataStd_ReferenceListDriver
37 //purpose : Constructor
38 //=======================================================================
39 XmlMDataStd_ReferenceListDriver::XmlMDataStd_ReferenceListDriver(const Handle(Message_Messenger)& theMsgDriver)
40 : XmlMDF_ADriver (theMsgDriver, NULL)
45 //=======================================================================
48 //=======================================================================
49 Handle(TDF_Attribute) XmlMDataStd_ReferenceListDriver::NewEmpty() const
51 return new TDataStd_ReferenceList();
54 //=======================================================================
56 //purpose : persistent -> transient (retrieve)
57 //=======================================================================
58 Standard_Boolean XmlMDataStd_ReferenceListDriver::Paste(const XmlObjMgt_Persistent& theSource,
59 const Handle(TDF_Attribute)& theTarget,
60 XmlObjMgt_RRelocationTable& ) const
62 Standard_Integer aFirstInd, aLastInd;
63 const XmlObjMgt_Element& anElement = theSource;
65 // Read the FirstIndex; if the attribute is absent initialize to 1
66 XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
67 if (aFirstIndex == NULL)
69 else if (!aFirstIndex.GetInteger(aFirstInd))
71 TCollection_ExtendedString aMessageString =
72 TCollection_ExtendedString("Cannot retrieve the first index"
73 " for ReferenceList attribute as \"")
75 myMessageDriver->Send (aMessageString, Message_Fail);
76 return Standard_False;
79 // Read the LastIndex; the attribute should present
80 if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd))
82 TCollection_ExtendedString aMessageString =
83 TCollection_ExtendedString("Cannot retrieve the last index"
84 " for ReferenceList attribute as \"")
86 myMessageDriver->Send (aMessageString, Message_Fail);
87 return Standard_False;
90 const Handle(TDataStd_ReferenceList) aReferenceList = Handle(TDataStd_ReferenceList)::DownCast(theTarget);
93 XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
94 if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
95 aGUID = TDataStd_ReferenceList::GetID(); //default case
97 aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
98 aReferenceList->SetID(aGUID);
101 if (!anElement.hasChildNodes())
103 TCollection_ExtendedString aMessageString =
104 TCollection_ExtendedString("Cannot retrieve a list of reference");
105 myMessageDriver->Send (aMessageString, Message_Fail);
106 return Standard_False;
109 LDOM_Node aCurNode = anElement.getFirstChild();
110 LDOM_Element* aCurElement = (LDOM_Element*)&aCurNode;
111 XmlObjMgt_DOMString aValueStr;
112 while (*aCurElement != anElement.getLastChild())
114 aValueStr = XmlObjMgt::GetStringValue( *aCurElement );
115 if (aValueStr == NULL)
117 myMessageDriver->Send ("Cannot retrieve reference string from element", Message_Fail);
118 return Standard_False;
120 TCollection_AsciiString anEntry;
121 if (XmlObjMgt::GetTagEntryString (aValueStr, anEntry) == Standard_False)
123 TCollection_ExtendedString aMessage =
124 TCollection_ExtendedString ("Cannot retrieve reference from \"")
126 myMessageDriver->Send (aMessage, Message_Fail);
127 return Standard_False;
129 // Find label by entry
130 TDF_Label tLab; // Null label.
131 if (anEntry.Length() > 0)
132 TDF_Tool::Label(aReferenceList->Label().Data(), anEntry, tLab, Standard_True);
134 aReferenceList->Append(tLab);
135 aCurNode = aCurElement->getNextSibling();
136 aCurElement = (LDOM_Element*)&aCurNode;
140 aValueStr = XmlObjMgt::GetStringValue( *aCurElement );
141 if (aValueStr == NULL)
143 myMessageDriver->Send ("Cannot retrieve reference string from element", Message_Fail);
144 return Standard_False;
146 TCollection_AsciiString anEntry;
147 if (XmlObjMgt::GetTagEntryString (aValueStr, anEntry) == Standard_False)
149 TCollection_ExtendedString aMessage =
150 TCollection_ExtendedString ("Cannot retrieve reference from \"")
152 myMessageDriver->Send (aMessage, Message_Fail);
153 return Standard_False;
155 // Find label by entry
156 TDF_Label tLab; // Null label.
157 if (anEntry.Length() > 0)
159 TDF_Tool::Label(aReferenceList->Label().Data(), anEntry, tLab, Standard_True);
161 aReferenceList->Append(tLab);
164 return Standard_True;
167 //=======================================================================
169 //purpose : transient -> persistent (store)
170 //=======================================================================
171 void XmlMDataStd_ReferenceListDriver::Paste(const Handle(TDF_Attribute)& theSource,
172 XmlObjMgt_Persistent& theTarget,
173 XmlObjMgt_SRelocationTable& ) const
175 const Handle(TDataStd_ReferenceList) aReferenceList = Handle(TDataStd_ReferenceList)::DownCast(theSource);
176 TDF_Label L = aReferenceList->Label();
179 myMessageDriver->Send ("Label of a ReferenceList is Null.", Message_Fail);
183 Standard_Integer anU = aReferenceList->Extent();
184 XmlObjMgt_Element& anElement = theTarget;
185 anElement.setAttribute(::LastIndexString(), anU);
187 XmlObjMgt_Document aDoc (anElement.getOwnerDocument());
189 TDF_ListIteratorOfLabelList itr(aReferenceList->List());
190 for (; itr.More(); itr.Next())
192 if (L.IsDescendant(itr.Value().Root()))
194 // Internal reference
195 TCollection_AsciiString anEntry;
196 TDF_Tool::Entry(itr.Value(), anEntry);
198 XmlObjMgt_DOMString aDOMString;
199 XmlObjMgt::SetTagEntryString (aDOMString, anEntry);
200 XmlObjMgt_Element aCurTarget = aDoc.createElement( ::ExtString() );
201 XmlObjMgt::SetStringValue (aCurTarget, aDOMString, Standard_True);
202 anElement.appendChild( aCurTarget );
206 if(aReferenceList->ID() != TDataStd_ReferenceList::GetID()) {
208 Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
209 Standard_PCharacter pGuidStr = aGuidStr;
210 aReferenceList->ID().ToCString (pGuidStr);
211 theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);