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