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 <CDM_MessageDriver.hxx>
18 #include <LDOM_MemManager.hxx>
19 #include <Standard_Type.hxx>
20 #include <TDataStd_ReferenceArray.hxx>
21 #include <TDF_Attribute.hxx>
22 #include <TDF_Label.hxx>
23 #include <TDF_Tool.hxx>
24 #include <XmlMDataStd_ReferenceArrayDriver.hxx>
25 #include <XmlObjMgt.hxx>
26 #include <XmlObjMgt_Document.hxx>
27 #include <XmlObjMgt_Persistent.hxx>
29 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_ReferenceArrayDriver,XmlMDF_ADriver)
30 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
31 IMPLEMENT_DOMSTRING (LastIndexString, "last")
32 IMPLEMENT_DOMSTRING (ExtString, "string")
33 IMPLEMENT_DOMSTRING (AttributeIDString, "refarrattguid")
34 //=======================================================================
35 //function : XmlMDataStd_ReferenceArrayDriver
36 //purpose : Constructor
37 //=======================================================================
38 XmlMDataStd_ReferenceArrayDriver::XmlMDataStd_ReferenceArrayDriver(const Handle(CDM_MessageDriver)& theMsgDriver)
39 : XmlMDF_ADriver (theMsgDriver, NULL)
44 //=======================================================================
47 //=======================================================================
48 Handle(TDF_Attribute) XmlMDataStd_ReferenceArrayDriver::NewEmpty() const
50 return new TDataStd_ReferenceArray();
53 //=======================================================================
55 //purpose : persistent -> transient (retrieve)
56 //=======================================================================
57 Standard_Boolean XmlMDataStd_ReferenceArrayDriver::Paste(const XmlObjMgt_Persistent& theSource,
58 const Handle(TDF_Attribute)& theTarget,
59 XmlObjMgt_RRelocationTable& ) const
61 Standard_Integer aFirstInd, aLastInd;
62 const XmlObjMgt_Element& anElement = theSource;
64 // Read the FirstIndex; if the attribute is absent initialize to 1
65 XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
66 if (aFirstIndex == NULL)
68 else if (!aFirstIndex.GetInteger(aFirstInd))
70 TCollection_ExtendedString aMessageString =
71 TCollection_ExtendedString("Cannot retrieve the first index"
72 " for ReferenceArray attribute as \"")
74 WriteMessage (aMessageString);
75 return Standard_False;
78 // Read the LastIndex; the attribute should present
79 if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd))
81 TCollection_ExtendedString aMessageString =
82 TCollection_ExtendedString("Cannot retrieve the last index"
83 " for ReferenceArray attribute as \"")
85 WriteMessage (aMessageString);
86 return Standard_False;
89 Handle(TDataStd_ReferenceArray) aReferenceArray = Handle(TDataStd_ReferenceArray)::DownCast(theTarget);
90 aReferenceArray->Init(aFirstInd, aLastInd);
92 if (!anElement.hasChildNodes())
94 TCollection_ExtendedString aMessageString =
95 TCollection_ExtendedString("Cannot retrieve a Array of reference");
96 WriteMessage (aMessageString);
97 return Standard_False;
100 LDOM_Node aCurNode = anElement.getFirstChild();
101 LDOM_Element* aCurElement = (LDOM_Element*)&aCurNode;
102 XmlObjMgt_DOMString aValueStr;
103 Standard_Integer i = aFirstInd;
104 while (*aCurElement != anElement.getLastChild())
106 aValueStr = XmlObjMgt::GetStringValue( *aCurElement );
107 if (aValueStr == NULL)
109 WriteMessage ("Cannot retrieve reference string from element");
110 return Standard_False;
112 TCollection_AsciiString anEntry;
113 if (XmlObjMgt::GetTagEntryString (aValueStr, anEntry) == Standard_False)
115 TCollection_ExtendedString aMessage =
116 TCollection_ExtendedString ("Cannot retrieve reference from \"")
118 WriteMessage (aMessage);
119 return Standard_False;
121 // Find label by entry
122 TDF_Label tLab; // Null label.
123 if (anEntry.Length() > 0)
125 TDF_Tool::Label(aReferenceArray->Label().Data(), anEntry, tLab, Standard_True);
127 aReferenceArray->SetValue(i++, tLab);
128 aCurNode = aCurElement->getNextSibling();
129 aCurElement = (LDOM_Element*)&aCurNode;
133 aValueStr = XmlObjMgt::GetStringValue( *aCurElement );
134 if (aValueStr == NULL)
136 WriteMessage ("Cannot retrieve reference string from element");
137 return Standard_False;
139 TCollection_AsciiString anEntry;
140 if (XmlObjMgt::GetTagEntryString (aValueStr, anEntry) == Standard_False)
142 TCollection_ExtendedString aMessage =
143 TCollection_ExtendedString ("Cannot retrieve reference from \"")
145 WriteMessage (aMessage);
146 return Standard_False;
148 // Find label by entry
149 TDF_Label tLab; // Null label.
150 if (anEntry.Length() > 0)
152 TDF_Tool::Label(aReferenceArray->Label().Data(), anEntry, tLab, Standard_True);
154 aReferenceArray->SetValue(i, tLab);
158 XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
159 if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
160 aGUID = TDataStd_ReferenceArray::GetID(); //default case
162 aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
164 aReferenceArray->SetID(aGUID);
166 return Standard_True;
169 //=======================================================================
171 //purpose : transient -> persistent (store)
172 //=======================================================================
173 void XmlMDataStd_ReferenceArrayDriver::Paste(const Handle(TDF_Attribute)& theSource,
174 XmlObjMgt_Persistent& theTarget,
175 XmlObjMgt_SRelocationTable& ) const
177 Handle(TDataStd_ReferenceArray) aReferenceArray = Handle(TDataStd_ReferenceArray)::DownCast(theSource);
178 TDF_Label L = aReferenceArray->Label();
181 WriteMessage ("Label of a ReferenceArray is Null.");
185 Standard_Integer aL = aReferenceArray->Lower();
186 Standard_Integer anU = aReferenceArray->Upper();
187 XmlObjMgt_Element& anElement = theTarget;
188 anElement.setAttribute(::FirstIndexString(), aL);
189 anElement.setAttribute(::LastIndexString(), anU);
191 XmlObjMgt_Document aDoc (anElement.getOwnerDocument());
193 for (Standard_Integer i = aL; i <= anU; i++)
195 if (L.IsDescendant(aReferenceArray->Value(i).Root()))
197 // Internal reference
198 TCollection_AsciiString anEntry;
199 TDF_Tool::Entry(aReferenceArray->Value(i), anEntry);
201 XmlObjMgt_DOMString aDOMString;
202 XmlObjMgt::SetTagEntryString (aDOMString, anEntry);
203 XmlObjMgt_Element aCurTarget = aDoc.createElement( ::ExtString() );
204 XmlObjMgt::SetStringValue (aCurTarget, aDOMString, Standard_True);
205 anElement.appendChild( aCurTarget );
208 if(aReferenceArray->ID() != TDataStd_ReferenceArray::GetID()) {
210 Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
211 Standard_PCharacter pGuidStr = aGuidStr;
212 aReferenceArray->ID().ToCString (pGuidStr);
213 theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);