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