1 // Created on: 2008-03-05
2 // Created by: Vlad ROMASHKO
3 // Copyright (c) 2008-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.
16 #include <XmlMFunction_ScopeDriver.ixx>
17 #include <XmlObjMgt.hxx>
18 #include <XmlObjMgt_Document.hxx>
19 #include <LDOM_MemManager.hxx>
21 #include <TColStd_ListOfInteger.hxx>
22 #include <TColStd_ListIteratorOfListOfInteger.hxx>
24 #include <TDF_Tool.hxx>
25 #include <TDF_Label.hxx>
26 #include <TDF_LabelList.hxx>
27 #include <TDF_ListIteratorOfLabelList.hxx>
29 #include <TFunction_Scope.hxx>
30 #include <TFunction_DoubleMapIteratorOfDoubleMapOfIntegerLabel.hxx>
32 IMPLEMENT_DOMSTRING (LastIDIndex, "lastid")
33 IMPLEMENT_DOMSTRING (LastLabelIndex, "lastlabel")
35 IMPLEMENT_DOMSTRING (ExtString, "string")
37 //=======================================================================
38 //function : XmlMFunction_ScopeDriver
39 //purpose : Constructor
40 //=======================================================================
41 XmlMFunction_ScopeDriver::XmlMFunction_ScopeDriver(const Handle(CDM_MessageDriver)& theMsgDriver)
42 : XmlMDF_ADriver (theMsgDriver, NULL)
47 //=======================================================================
50 //=======================================================================
51 Handle(TDF_Attribute) XmlMFunction_ScopeDriver::NewEmpty() const
53 return (new TFunction_Scope());
56 //=======================================================================
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
64 Handle(TFunction_Scope) S = Handle(TFunction_Scope)::DownCast(theTarget);
65 TColStd_ListOfInteger IDs;
68 Standard_Integer aFirstInd, aLastInd, aValue, ind, nbIDs = 0, nbLabels = 0;
69 const XmlObjMgt_Element& anElement = theSource;
74 // Read the FirstIndex; if the attribute is absent initialize to 1
77 // Read the LastIndex; the attribute should present
78 if (!anElement.getAttribute(::LastIDIndex()).GetInteger(aLastInd))
80 TCollection_ExtendedString aMessageString =
81 TCollection_ExtendedString("Cannot retrieve the last index"
82 " for Scope attribute");
83 WriteMessage (aMessageString);
84 return Standard_False;
86 nbIDs = aLastInd - aFirstInd + 1;
88 if (aFirstInd == aLastInd)
90 Standard_Integer anInteger;
91 if (!XmlObjMgt::GetStringValue(anElement).GetInteger(anInteger))
93 TCollection_ExtendedString aMessageString =
94 TCollection_ExtendedString("Cannot retrieve integer member"
95 " for Scope attribute as \"");
96 WriteMessage (aMessageString);
97 return Standard_False;
99 IDs.Append(anInteger);
103 Standard_CString aValueStr =
104 Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
106 for (ind = aFirstInd; ind <= aLastInd; ind++)
108 if (!XmlObjMgt::GetInteger(aValueStr, aValue))
110 TCollection_ExtendedString aMessageString =
111 TCollection_ExtendedString("Cannot retrieve integer member"
112 " for Scope attribute as \"")
114 WriteMessage (aMessageString);
115 return Standard_False;
127 // Read the LastIndex; the attribute should present
128 if (!anElement.getAttribute(::LastLabelIndex()).GetInteger(aLastInd))
130 TCollection_ExtendedString aMessageString =
131 TCollection_ExtendedString("Cannot retrieve the last index"
132 " for Scope attribute");
133 WriteMessage (aMessageString);
134 return Standard_False;
136 nbLabels = aLastInd - aFirstInd + 1;
138 if (!anElement.hasChildNodes())
140 TCollection_ExtendedString aMessageString =
141 TCollection_ExtendedString("Cannot retrieve an array of labels");
142 WriteMessage (aMessageString);
143 return Standard_False;
146 LDOM_Node aCurNode = anElement.getFirstChild()/*.getNextSibling().getNextSibling()*/;
147 LDOM_Element* aCurElement = (LDOM_Element*)&aCurNode;
148 XmlObjMgt_DOMString aValueStr;
149 while (*aCurElement != anElement.getLastChild())
151 aValueStr = XmlObjMgt::GetStringValue( *aCurElement );
152 if (aValueStr == NULL)
154 aCurNode = aCurElement->getNextSibling();
155 aCurElement = (LDOM_Element*)&aCurNode;
158 TCollection_AsciiString anEntry;
159 if (XmlObjMgt::GetTagEntryString (aValueStr, anEntry) == Standard_False)
161 TCollection_ExtendedString aMessage =
162 TCollection_ExtendedString ("Cannot retrieve reference from \"")
164 WriteMessage (aMessage);
165 return Standard_False;
167 // Find label by entry
168 TDF_Label tLab; // Null label.
169 if (anEntry.Length() > 0)
171 TDF_Tool::Label(S->Label().Data(), anEntry, tLab, Standard_True);
174 aCurNode = aCurElement->getNextSibling();
175 aCurElement = (LDOM_Element*)&aCurNode;
179 aValueStr = XmlObjMgt::GetStringValue( *aCurElement );
180 if (aValueStr == NULL)
182 WriteMessage ("Cannot retrieve reference string from element");
183 return Standard_False;
185 TCollection_AsciiString anEntry;
186 if (XmlObjMgt::GetTagEntryString (aValueStr, anEntry) == Standard_False)
188 TCollection_ExtendedString aMessage =
189 TCollection_ExtendedString ("Cannot retrieve reference from \"")
191 WriteMessage (aMessage);
192 return Standard_False;
194 // Find label by entry
195 TDF_Label tLab; // Null label.
196 if (anEntry.Length() > 0)
198 TDF_Tool::Label(S->Label().Data(), anEntry, tLab, Standard_True);
202 // Check equality of lengths of the list of IDs & Labels.
203 if (nbIDs != nbLabels)
205 TCollection_ExtendedString aMessage =
206 TCollection_ExtendedString ("Numbers of IDs & Labels are different");
207 WriteMessage (aMessage);
208 return Standard_False;
211 // Set IDs & Labels into the Scope attribute
213 TColStd_ListIteratorOfListOfInteger itri(IDs);
214 TDF_ListIteratorOfLabelList itrl(Labels);
215 for (; itri.More(); itri.Next(), itrl.Next())
217 int ID = itri.Value();
220 S->ChangeFunctions().Bind(ID, itrl.Value());
223 S->SetFreeID(freeID);
225 return Standard_True;
228 //=======================================================================
230 //purpose : transient -> persistent (store)
231 //=======================================================================
232 void XmlMFunction_ScopeDriver::Paste (const Handle(TDF_Attribute)& theSource,
233 XmlObjMgt_Persistent& theTarget,
234 XmlObjMgt_SRelocationTable& ) const
236 Handle(TFunction_Scope) S = Handle(TFunction_Scope)::DownCast(theSource);
241 theTarget.Element().setAttribute(::LastIDIndex(), S->GetFunctions().Extent());
243 TCollection_AsciiString aValueStr;
244 TFunction_DoubleMapIteratorOfDoubleMapOfIntegerLabel itrd(S->GetFunctions());
245 for (; itrd.More(); itrd.Next())
247 const Standard_Integer ID = itrd.Key1();
248 aValueStr += TCollection_AsciiString(ID);
253 XmlObjMgt::SetStringValue (theTarget, aValueStr.ToCString(), Standard_True);
259 XmlObjMgt_Element& anElement = theTarget;
260 anElement.setAttribute(::LastLabelIndex(), S->GetFunctions().Extent());
262 XmlObjMgt_Document aDoc = anElement.getOwnerDocument().Doc();
264 for (itrd.Initialize(S->GetFunctions()); itrd.More(); itrd.Next())
266 TDF_Label L = itrd.Key2();
268 TCollection_AsciiString anEntry;
269 TDF_Tool::Entry(L, anEntry);
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 );