Warnings on vc14 were eliminated
[occt.git] / src / XmlMFunction / XmlMFunction_ScopeDriver.cxx
CommitLineData
b311480e 1// Created on: 2008-03-05
2// Created by: Vlad ROMASHKO
973c2be1 3// Copyright (c) 2008-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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
7fd59977 16
42cf5bc1 17#include <CDM_MessageDriver.hxx>
18#include <LDOM_MemManager.hxx>
19#include <Standard_Type.hxx>
7fd59977 20#include <TColStd_ListIteratorOfListOfInteger.hxx>
42cf5bc1 21#include <TColStd_ListOfInteger.hxx>
22#include <TDF_Attribute.hxx>
7fd59977 23#include <TDF_Label.hxx>
24#include <TDF_LabelList.hxx>
25#include <TDF_ListIteratorOfLabelList.hxx>
42cf5bc1 26#include <TDF_Tool.hxx>
7fd59977 27#include <TFunction_DoubleMapIteratorOfDoubleMapOfIntegerLabel.hxx>
42cf5bc1 28#include <TFunction_Scope.hxx>
29#include <XmlMFunction_ScopeDriver.hxx>
30#include <XmlObjMgt.hxx>
31#include <XmlObjMgt_Document.hxx>
32#include <XmlObjMgt_Persistent.hxx>
7fd59977 33
92efcf78 34IMPLEMENT_STANDARD_RTTIEXT(XmlMFunction_ScopeDriver,XmlMDF_ADriver)
7fd59977 35IMPLEMENT_DOMSTRING (LastIDIndex, "lastid")
36IMPLEMENT_DOMSTRING (LastLabelIndex, "lastlabel")
37
38IMPLEMENT_DOMSTRING (ExtString, "string")
39
40//=======================================================================
41//function : XmlMFunction_ScopeDriver
42//purpose : Constructor
43//=======================================================================
44XmlMFunction_ScopeDriver::XmlMFunction_ScopeDriver(const Handle(CDM_MessageDriver)& theMsgDriver)
45 : XmlMDF_ADriver (theMsgDriver, NULL)
46{
47
48}
49
50//=======================================================================
51//function : NewEmpty
52//purpose :
53//=======================================================================
54Handle(TDF_Attribute) XmlMFunction_ScopeDriver::NewEmpty() const
55{
56 return (new TFunction_Scope());
57}
58
59//=======================================================================
60//function : Paste
61//purpose : persistent -> transient (retrieve)
62//=======================================================================
63Standard_Boolean XmlMFunction_ScopeDriver::Paste(const XmlObjMgt_Persistent& theSource,
64 const Handle(TDF_Attribute)& theTarget,
65 XmlObjMgt_RRelocationTable& ) const
66{
67 Handle(TFunction_Scope) S = Handle(TFunction_Scope)::DownCast(theTarget);
68 TColStd_ListOfInteger IDs;
69 TDF_LabelList Labels;
70
71 Standard_Integer aFirstInd, aLastInd, aValue, ind, nbIDs = 0, nbLabels = 0;
72 const XmlObjMgt_Element& anElement = theSource;
73
74 // IDs
75 // ===
76
77 // Read the FirstIndex; if the attribute is absent initialize to 1
78 aFirstInd = 1;
79
80 // Read the LastIndex; the attribute should present
81 if (!anElement.getAttribute(::LastIDIndex()).GetInteger(aLastInd))
82 {
83 TCollection_ExtendedString aMessageString =
84 TCollection_ExtendedString("Cannot retrieve the last index"
85 " for Scope attribute");
86 WriteMessage (aMessageString);
87 return Standard_False;
88 }
89 nbIDs = aLastInd - aFirstInd + 1;
90
91 if (aFirstInd == aLastInd)
92 {
93 Standard_Integer anInteger;
94 if (!XmlObjMgt::GetStringValue(anElement).GetInteger(anInteger))
95 {
96 TCollection_ExtendedString aMessageString =
97 TCollection_ExtendedString("Cannot retrieve integer member"
98 " for Scope attribute as \"");
99 WriteMessage (aMessageString);
100 return Standard_False;
101 }
102 IDs.Append(anInteger);
103 }
104 else
105 {
106 Standard_CString aValueStr =
107 Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
108
109 for (ind = aFirstInd; ind <= aLastInd; ind++)
110 {
111 if (!XmlObjMgt::GetInteger(aValueStr, aValue))
112 {
113 TCollection_ExtendedString aMessageString =
114 TCollection_ExtendedString("Cannot retrieve integer member"
115 " for Scope attribute as \"")
116 + aValueStr + "\"";
117 WriteMessage (aMessageString);
118 return Standard_False;
119 }
120 IDs.Append(aValue);
121 }
122 }
123
124
125 // Labels
126 // ======
127
128 aFirstInd = 1;
129
130 // Read the LastIndex; the attribute should present
131 if (!anElement.getAttribute(::LastLabelIndex()).GetInteger(aLastInd))
132 {
133 TCollection_ExtendedString aMessageString =
134 TCollection_ExtendedString("Cannot retrieve the last index"
135 " for Scope attribute");
136 WriteMessage (aMessageString);
137 return Standard_False;
138 }
139 nbLabels = aLastInd - aFirstInd + 1;
140
141 if (!anElement.hasChildNodes())
142 {
143 TCollection_ExtendedString aMessageString =
144 TCollection_ExtendedString("Cannot retrieve an array of labels");
145 WriteMessage (aMessageString);
146 return Standard_False;
147 }
148
149 LDOM_Node aCurNode = anElement.getFirstChild()/*.getNextSibling().getNextSibling()*/;
150 LDOM_Element* aCurElement = (LDOM_Element*)&aCurNode;
151 XmlObjMgt_DOMString aValueStr;
7fd59977 152 while (*aCurElement != anElement.getLastChild())
153 {
154 aValueStr = XmlObjMgt::GetStringValue( *aCurElement );
155 if (aValueStr == NULL)
156 {
157 aCurNode = aCurElement->getNextSibling();
158 aCurElement = (LDOM_Element*)&aCurNode;
159 continue;
160 }
161 TCollection_AsciiString anEntry;
162 if (XmlObjMgt::GetTagEntryString (aValueStr, anEntry) == Standard_False)
163 {
164 TCollection_ExtendedString aMessage =
165 TCollection_ExtendedString ("Cannot retrieve reference from \"")
166 + aValueStr + '\"';
167 WriteMessage (aMessage);
168 return Standard_False;
169 }
170 // Find label by entry
171 TDF_Label tLab; // Null label.
172 if (anEntry.Length() > 0)
173 {
174 TDF_Tool::Label(S->Label().Data(), anEntry, tLab, Standard_True);
175 }
176 Labels.Append(tLab);
177 aCurNode = aCurElement->getNextSibling();
178 aCurElement = (LDOM_Element*)&aCurNode;
179 }
180
181 // Last reference
182 aValueStr = XmlObjMgt::GetStringValue( *aCurElement );
183 if (aValueStr == NULL)
184 {
185 WriteMessage ("Cannot retrieve reference string from element");
186 return Standard_False;
187 }
188 TCollection_AsciiString anEntry;
189 if (XmlObjMgt::GetTagEntryString (aValueStr, anEntry) == Standard_False)
190 {
191 TCollection_ExtendedString aMessage =
192 TCollection_ExtendedString ("Cannot retrieve reference from \"")
193 + aValueStr + '\"';
194 WriteMessage (aMessage);
195 return Standard_False;
196 }
197 // Find label by entry
198 TDF_Label tLab; // Null label.
199 if (anEntry.Length() > 0)
200 {
201 TDF_Tool::Label(S->Label().Data(), anEntry, tLab, Standard_True);
202 }
203 Labels.Append(tLab);
204
205 // Check equality of lengths of the list of IDs & Labels.
206 if (nbIDs != nbLabels)
207 {
208 TCollection_ExtendedString aMessage =
209 TCollection_ExtendedString ("Numbers of IDs & Labels are different");
210 WriteMessage (aMessage);
211 return Standard_False;
212 }
213
214 // Set IDs & Labels into the Scope attribute
215 int freeID = 0;
216 TColStd_ListIteratorOfListOfInteger itri(IDs);
217 TDF_ListIteratorOfLabelList itrl(Labels);
218 for (; itri.More(); itri.Next(), itrl.Next())
219 {
220 int ID = itri.Value();
221 if (ID > freeID)
222 freeID = ID;
223 S->ChangeFunctions().Bind(ID, itrl.Value());
224 }
225 freeID++;
226 S->SetFreeID(freeID);
227
228 return Standard_True;
229}
230
231//=======================================================================
232//function : Paste
233//purpose : transient -> persistent (store)
234//=======================================================================
235void XmlMFunction_ScopeDriver::Paste (const Handle(TDF_Attribute)& theSource,
236 XmlObjMgt_Persistent& theTarget,
237 XmlObjMgt_SRelocationTable& ) const
238{
239 Handle(TFunction_Scope) S = Handle(TFunction_Scope)::DownCast(theSource);
240
241 // IDs
242 // ===
243
244 theTarget.Element().setAttribute(::LastIDIndex(), S->GetFunctions().Extent());
245
246 TCollection_AsciiString aValueStr;
247 TFunction_DoubleMapIteratorOfDoubleMapOfIntegerLabel itrd(S->GetFunctions());
248 for (; itrd.More(); itrd.Next())
249 {
250 const Standard_Integer ID = itrd.Key1();
251 aValueStr += TCollection_AsciiString(ID);
252 aValueStr += ' ';
253 }
254 aValueStr += "\n";
255
256 XmlObjMgt::SetStringValue (theTarget, aValueStr.ToCString(), Standard_True);
257
258
259 // Labels
260 // ======
261
262 XmlObjMgt_Element& anElement = theTarget;
263 anElement.setAttribute(::LastLabelIndex(), S->GetFunctions().Extent());
264
e8862cf4 265 XmlObjMgt_Document aDoc (anElement.getOwnerDocument());
7fd59977 266
267 for (itrd.Initialize(S->GetFunctions()); itrd.More(); itrd.Next())
268 {
269 TDF_Label L = itrd.Key2();
270
271 TCollection_AsciiString anEntry;
272 TDF_Tool::Entry(L, anEntry);
273
274 XmlObjMgt_DOMString aDOMString;
275 XmlObjMgt::SetTagEntryString (aDOMString, anEntry);
276 XmlObjMgt_Element aCurTarget = aDoc.createElement( ::ExtString() );
277 XmlObjMgt::SetStringValue (aCurTarget, aDOMString, Standard_True);
278 anElement.appendChild( aCurTarget );
279 }
280}