1 // File: XmlMFunction_ScopeDriver.cxx
2 // Created: 05.03.08 17:17:31
3 // Author: Vlad ROMASHKO
4 // Copyright: Open Cascade 2008
6 #include <XmlMFunction_ScopeDriver.ixx>
7 #include <XmlObjMgt.hxx>
8 #include <XmlObjMgt_Document.hxx>
9 #include <LDOM_MemManager.hxx>
11 #include <TColStd_ListOfInteger.hxx>
12 #include <TColStd_ListIteratorOfListOfInteger.hxx>
14 #include <TDF_Tool.hxx>
15 #include <TDF_Label.hxx>
16 #include <TDF_LabelList.hxx>
17 #include <TDF_ListIteratorOfLabelList.hxx>
19 #include <TFunction_Scope.hxx>
20 #include <TFunction_DoubleMapIteratorOfDoubleMapOfIntegerLabel.hxx>
22 IMPLEMENT_DOMSTRING (LastIDIndex, "lastid")
23 IMPLEMENT_DOMSTRING (LastLabelIndex, "lastlabel")
25 IMPLEMENT_DOMSTRING (ExtString, "string")
27 //=======================================================================
28 //function : XmlMFunction_ScopeDriver
29 //purpose : Constructor
30 //=======================================================================
31 XmlMFunction_ScopeDriver::XmlMFunction_ScopeDriver(const Handle(CDM_MessageDriver)& theMsgDriver)
32 : XmlMDF_ADriver (theMsgDriver, NULL)
37 //=======================================================================
40 //=======================================================================
41 Handle(TDF_Attribute) XmlMFunction_ScopeDriver::NewEmpty() const
43 return (new TFunction_Scope());
46 //=======================================================================
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
54 Handle(TFunction_Scope) S = Handle(TFunction_Scope)::DownCast(theTarget);
55 TColStd_ListOfInteger IDs;
58 Standard_Integer aFirstInd, aLastInd, aValue, ind, nbIDs = 0, nbLabels = 0;
59 const XmlObjMgt_Element& anElement = theSource;
64 // Read the FirstIndex; if the attribute is absent initialize to 1
67 // Read the LastIndex; the attribute should present
68 if (!anElement.getAttribute(::LastIDIndex()).GetInteger(aLastInd))
70 TCollection_ExtendedString aMessageString =
71 TCollection_ExtendedString("Cannot retrieve the last index"
72 " for Scope attribute");
73 WriteMessage (aMessageString);
74 return Standard_False;
76 nbIDs = aLastInd - aFirstInd + 1;
78 if (aFirstInd == aLastInd)
80 Standard_Integer anInteger;
81 if (!XmlObjMgt::GetStringValue(anElement).GetInteger(anInteger))
83 TCollection_ExtendedString aMessageString =
84 TCollection_ExtendedString("Cannot retrieve integer member"
85 " for Scope attribute as \"");
86 WriteMessage (aMessageString);
87 return Standard_False;
89 IDs.Append(anInteger);
93 Standard_CString aValueStr =
94 Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
96 for (ind = aFirstInd; ind <= aLastInd; ind++)
98 if (!XmlObjMgt::GetInteger(aValueStr, aValue))
100 TCollection_ExtendedString aMessageString =
101 TCollection_ExtendedString("Cannot retrieve integer member"
102 " for Scope attribute as \"")
104 WriteMessage (aMessageString);
105 return Standard_False;
117 // Read the LastIndex; the attribute should present
118 if (!anElement.getAttribute(::LastLabelIndex()).GetInteger(aLastInd))
120 TCollection_ExtendedString aMessageString =
121 TCollection_ExtendedString("Cannot retrieve the last index"
122 " for Scope attribute");
123 WriteMessage (aMessageString);
124 return Standard_False;
126 nbLabels = aLastInd - aFirstInd + 1;
128 if (!anElement.hasChildNodes())
130 TCollection_ExtendedString aMessageString =
131 TCollection_ExtendedString("Cannot retrieve an array of labels");
132 WriteMessage (aMessageString);
133 return Standard_False;
136 LDOM_Node aCurNode = anElement.getFirstChild()/*.getNextSibling().getNextSibling()*/;
137 LDOM_Element* aCurElement = (LDOM_Element*)&aCurNode;
138 XmlObjMgt_DOMString aValueStr;
139 while (*aCurElement != anElement.getLastChild())
141 aValueStr = XmlObjMgt::GetStringValue( *aCurElement );
142 if (aValueStr == NULL)
144 aCurNode = aCurElement->getNextSibling();
145 aCurElement = (LDOM_Element*)&aCurNode;
148 TCollection_AsciiString anEntry;
149 if (XmlObjMgt::GetTagEntryString (aValueStr, anEntry) == Standard_False)
151 TCollection_ExtendedString aMessage =
152 TCollection_ExtendedString ("Cannot retrieve reference from \"")
154 WriteMessage (aMessage);
155 return Standard_False;
157 // Find label by entry
158 TDF_Label tLab; // Null label.
159 if (anEntry.Length() > 0)
161 TDF_Tool::Label(S->Label().Data(), anEntry, tLab, Standard_True);
164 aCurNode = aCurElement->getNextSibling();
165 aCurElement = (LDOM_Element*)&aCurNode;
169 aValueStr = XmlObjMgt::GetStringValue( *aCurElement );
170 if (aValueStr == NULL)
172 WriteMessage ("Cannot retrieve reference string from element");
173 return Standard_False;
175 TCollection_AsciiString anEntry;
176 if (XmlObjMgt::GetTagEntryString (aValueStr, anEntry) == Standard_False)
178 TCollection_ExtendedString aMessage =
179 TCollection_ExtendedString ("Cannot retrieve reference from \"")
181 WriteMessage (aMessage);
182 return Standard_False;
184 // Find label by entry
185 TDF_Label tLab; // Null label.
186 if (anEntry.Length() > 0)
188 TDF_Tool::Label(S->Label().Data(), anEntry, tLab, Standard_True);
192 // Check equality of lengths of the list of IDs & Labels.
193 if (nbIDs != nbLabels)
195 TCollection_ExtendedString aMessage =
196 TCollection_ExtendedString ("Numbers of IDs & Labels are different");
197 WriteMessage (aMessage);
198 return Standard_False;
201 // Set IDs & Labels into the Scope attribute
203 TColStd_ListIteratorOfListOfInteger itri(IDs);
204 TDF_ListIteratorOfLabelList itrl(Labels);
205 for (; itri.More(); itri.Next(), itrl.Next())
207 int ID = itri.Value();
210 S->ChangeFunctions().Bind(ID, itrl.Value());
213 S->SetFreeID(freeID);
215 return Standard_True;
218 //=======================================================================
220 //purpose : transient -> persistent (store)
221 //=======================================================================
222 void XmlMFunction_ScopeDriver::Paste (const Handle(TDF_Attribute)& theSource,
223 XmlObjMgt_Persistent& theTarget,
224 XmlObjMgt_SRelocationTable& ) const
226 Handle(TFunction_Scope) S = Handle(TFunction_Scope)::DownCast(theSource);
231 theTarget.Element().setAttribute(::LastIDIndex(), S->GetFunctions().Extent());
233 TCollection_AsciiString aValueStr;
234 TFunction_DoubleMapIteratorOfDoubleMapOfIntegerLabel itrd(S->GetFunctions());
235 for (; itrd.More(); itrd.Next())
237 const Standard_Integer ID = itrd.Key1();
238 aValueStr += TCollection_AsciiString(ID);
243 XmlObjMgt::SetStringValue (theTarget, aValueStr.ToCString(), Standard_True);
249 XmlObjMgt_Element& anElement = theTarget;
250 anElement.setAttribute(::LastLabelIndex(), S->GetFunctions().Extent());
252 XmlObjMgt_Document aDoc = anElement.getOwnerDocument().Doc();
254 for (itrd.Initialize(S->GetFunctions()); itrd.More(); itrd.Next())
256 TDF_Label L = itrd.Key2();
258 TCollection_AsciiString anEntry;
259 TDF_Tool::Entry(L, anEntry);
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 );