0022959: Accessing uninitialized variable (pointer): aCurElement
[occt.git] / src / XmlMDataStd / XmlMDataStd_ReferenceListDriver.cxx
1 // File:        XmlMDataStd_ReferenceListDriver.cxx
2 // Created:     May 29 11:40:00 2007
3 // Author:      Vlad Romashko
4 //              <vladislav.romashko@opencascade.com>
5 // Copyright:   Open CASCADE
6
7 #include <XmlMDataStd_ReferenceListDriver.ixx>
8 #include <XmlObjMgt.hxx>
9 #include <XmlObjMgt_Document.hxx>
10 #include <LDOM_MemManager.hxx>
11
12 #include <TDF_Tool.hxx>
13 #include <TDF_Label.hxx>
14 #include <TDataStd_ReferenceList.hxx>
15 #include <TDF_ListIteratorOfLabelList.hxx>
16
17 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
18 IMPLEMENT_DOMSTRING (LastIndexString,  "last")
19 IMPLEMENT_DOMSTRING (ExtString,        "string")
20
21 //=======================================================================
22 //function : XmlMDataStd_ReferenceListDriver
23 //purpose  : Constructor
24 //=======================================================================
25 XmlMDataStd_ReferenceListDriver::XmlMDataStd_ReferenceListDriver(const Handle(CDM_MessageDriver)& theMsgDriver)
26      : XmlMDF_ADriver (theMsgDriver, NULL)
27 {
28
29 }
30
31 //=======================================================================
32 //function : NewEmpty
33 //purpose  : 
34 //=======================================================================
35 Handle(TDF_Attribute) XmlMDataStd_ReferenceListDriver::NewEmpty() const
36 {
37   return new TDataStd_ReferenceList();
38 }
39
40 //=======================================================================
41 //function : Paste
42 //purpose  : persistent -> transient (retrieve)
43 //=======================================================================
44 Standard_Boolean XmlMDataStd_ReferenceListDriver::Paste(const XmlObjMgt_Persistent&  theSource,
45                                                         const Handle(TDF_Attribute)& theTarget,
46                                                         XmlObjMgt_RRelocationTable&  ) const
47 {
48   Standard_Integer aFirstInd, aLastInd;
49   const XmlObjMgt_Element& anElement = theSource;
50
51   // Read the FirstIndex; if the attribute is absent initialize to 1
52   XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
53   if (aFirstIndex == NULL)
54     aFirstInd = 1;
55   else if (!aFirstIndex.GetInteger(aFirstInd)) 
56   {
57     TCollection_ExtendedString aMessageString =
58       TCollection_ExtendedString("Cannot retrieve the first index"
59                                  " for ReferenceList attribute as \"")
60         + aFirstIndex + "\"";
61     WriteMessage (aMessageString);
62     return Standard_False;
63   }
64
65   // Read the LastIndex; the attribute should present
66   if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) 
67   {
68     TCollection_ExtendedString aMessageString =
69       TCollection_ExtendedString("Cannot retrieve the last index"
70                                  " for ReferenceList attribute as \"")
71         + aFirstIndex + "\"";
72     WriteMessage (aMessageString);
73     return Standard_False;
74   }
75
76   Handle(TDataStd_ReferenceList) aReferenceList = Handle(TDataStd_ReferenceList)::DownCast(theTarget);
77   
78   if (!anElement.hasChildNodes())
79   {
80     TCollection_ExtendedString aMessageString = 
81       TCollection_ExtendedString("Cannot retrieve a list of reference");
82     WriteMessage (aMessageString);
83     return Standard_False;
84   }
85
86   LDOM_Node aCurNode = anElement.getFirstChild();
87   LDOM_Element* aCurElement = (LDOM_Element*)&aCurNode;
88   XmlObjMgt_DOMString aValueStr;
89   while (*aCurElement != anElement.getLastChild())
90   {
91     aValueStr = XmlObjMgt::GetStringValue( *aCurElement );
92     if (aValueStr == NULL)
93     {
94       WriteMessage ("Cannot retrieve reference string from element");
95       return Standard_False;
96     }
97     TCollection_AsciiString anEntry;
98     if (XmlObjMgt::GetTagEntryString (aValueStr, anEntry) == Standard_False)
99     {
100       TCollection_ExtendedString aMessage =
101         TCollection_ExtendedString ("Cannot retrieve reference from \"")
102           + aValueStr + '\"';
103       WriteMessage (aMessage);
104       return Standard_False;
105     }
106     // Find label by entry
107     TDF_Label tLab; // Null label.
108     if (anEntry.Length() > 0)
109     {
110       TDF_Tool::Label(aReferenceList->Label().Data(), anEntry, tLab, Standard_True);
111     }
112     aReferenceList->Append(tLab);
113     aCurNode = aCurElement->getNextSibling();
114     aCurElement = (LDOM_Element*)&aCurNode;
115   }
116
117   // Last reference
118   aValueStr = XmlObjMgt::GetStringValue( *aCurElement );
119   if (aValueStr == NULL)
120   {
121     WriteMessage ("Cannot retrieve reference string from element");
122     return Standard_False;
123   }
124   TCollection_AsciiString anEntry;
125   if (XmlObjMgt::GetTagEntryString (aValueStr, anEntry) == Standard_False)
126   {
127     TCollection_ExtendedString aMessage =
128       TCollection_ExtendedString ("Cannot retrieve reference from \"")
129         + aValueStr + '\"';
130     WriteMessage (aMessage);
131     return Standard_False;
132   }
133   // Find label by entry
134   TDF_Label tLab; // Null label.
135   if (anEntry.Length() > 0)
136   {
137     TDF_Tool::Label(aReferenceList->Label().Data(), anEntry, tLab, Standard_True);
138   }
139   aReferenceList->Append(tLab);
140
141   return Standard_True;
142 }
143
144 //=======================================================================
145 //function : Paste
146 //purpose  : transient -> persistent (store)
147 //=======================================================================
148 void XmlMDataStd_ReferenceListDriver::Paste(const Handle(TDF_Attribute)& theSource,
149                                             XmlObjMgt_Persistent&        theTarget,
150                                             XmlObjMgt_SRelocationTable&  ) const
151 {
152   Handle(TDataStd_ReferenceList) aReferenceList = Handle(TDataStd_ReferenceList)::DownCast(theSource);
153   TDF_Label L = aReferenceList->Label();
154   if (L.IsNull())
155   {
156     WriteMessage ("Label of a ReferenceList is Null.");
157     return;
158   }
159
160   Standard_Integer anU = aReferenceList->Extent();
161   XmlObjMgt_Element& anElement = theTarget;
162   anElement.setAttribute(::LastIndexString(), anU);
163   
164   XmlObjMgt_Document aDoc = anElement.getOwnerDocument().Doc();
165   
166   TDF_ListIteratorOfLabelList itr(aReferenceList->List());
167   for (; itr.More(); itr.Next())
168   {
169     if (L.IsDescendant(itr.Value().Root()))
170     {
171       // Internal reference
172       TCollection_AsciiString anEntry;
173       TDF_Tool::Entry(itr.Value(), anEntry);
174
175       XmlObjMgt_DOMString aDOMString;
176       XmlObjMgt::SetTagEntryString (aDOMString, anEntry);
177       XmlObjMgt_Element aCurTarget = aDoc.createElement( ::ExtString() );
178       XmlObjMgt::SetStringValue (aCurTarget, aDOMString, Standard_True);
179       anElement.appendChild( aCurTarget );
180     }
181   }
182 }