0029574: Protection of attributes retrieval against zero ID in Ocaf XML.
[occt.git] / src / XmlMDataStd / XmlMDataStd_BooleanListDriver.cxx
1 // Created on: 2007-05-29
2 // Created by: Vlad Romashko
3 // Copyright (c) 2007-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
17 #include <Message_Messenger.hxx>
18 #include <NCollection_LocalArray.hxx>
19 #include <Standard_Type.hxx>
20 #include <TDataStd_BooleanList.hxx>
21 #include <TDataStd_ListIteratorOfListOfByte.hxx>
22 #include <TDF_Attribute.hxx>
23 #include <XmlMDataStd_BooleanListDriver.hxx>
24 #include <XmlObjMgt.hxx>
25 #include <XmlObjMgt_Persistent.hxx>
26
27 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_BooleanListDriver,XmlMDF_ADriver)
28 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
29 IMPLEMENT_DOMSTRING (LastIndexString,  "last")
30 IMPLEMENT_DOMSTRING (AttributeIDString, "boollistattguid")
31
32 //=======================================================================
33 //function : XmlMDataStd_BooleanListDriver
34 //purpose  : Constructor
35 //=======================================================================
36 XmlMDataStd_BooleanListDriver::XmlMDataStd_BooleanListDriver(const Handle(Message_Messenger)& theMsgDriver)
37      : XmlMDF_ADriver (theMsgDriver, NULL)
38 {
39
40 }
41
42 //=======================================================================
43 //function : NewEmpty
44 //purpose  : 
45 //=======================================================================
46 Handle(TDF_Attribute) XmlMDataStd_BooleanListDriver::NewEmpty() const
47 {
48   return new TDataStd_BooleanList();
49 }
50
51 //=======================================================================
52 //function : Paste
53 //purpose  : persistent -> transient (retrieve)
54 //=======================================================================
55 Standard_Boolean XmlMDataStd_BooleanListDriver::Paste(const XmlObjMgt_Persistent&  theSource,
56                                                       const Handle(TDF_Attribute)& theTarget,
57                                                       XmlObjMgt_RRelocationTable&  ) const
58 {
59   Standard_Integer aFirstInd, aLastInd, aValue, ind;
60   const XmlObjMgt_Element& anElement = theSource;
61
62   // Read the FirstIndex; if the attribute is absent initialize to 1
63   XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
64   if (aFirstIndex == NULL)
65     aFirstInd = 1;
66   else if (!aFirstIndex.GetInteger(aFirstInd)) 
67   {
68     TCollection_ExtendedString aMessageString =
69       TCollection_ExtendedString("Cannot retrieve the first index"
70                                  " for BooleanList attribute as \"")
71         + aFirstIndex + "\"";
72     myMessageDriver->Send (aMessageString, Message_Fail);
73     return Standard_False;
74   }
75
76   // Read the LastIndex; the attribute should be present
77   if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) 
78   {
79     TCollection_ExtendedString aMessageString =
80       TCollection_ExtendedString("Cannot retrieve the last index"
81                                  " for BooleanList attribute as \"")
82         + aFirstIndex + "\"";
83     myMessageDriver->Send (aMessageString, Message_Fail);
84     return Standard_False;
85   }
86
87   const Handle(TDataStd_BooleanList) aBooleanList = Handle(TDataStd_BooleanList)::DownCast(theTarget);
88
89   // attribute id
90   Standard_GUID aGUID;
91   XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
92   if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
93     aGUID = TDataStd_BooleanList::GetID(); //default case
94   else
95     aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
96
97   aBooleanList->SetID(aGUID);
98
99   if(aLastInd == 0) aFirstInd = 0;
100   if (aFirstInd == aLastInd && aLastInd > 0) 
101   {
102     if (!XmlObjMgt::GetStringValue(anElement).GetInteger(aValue)) 
103     {
104       TCollection_ExtendedString aMessageString =
105         TCollection_ExtendedString("Cannot retrieve integer member"
106                                    " for BooleanList attribute as \"");
107       myMessageDriver->Send (aMessageString, Message_Warning);
108       aValue = 0;
109     }
110     aBooleanList->Append(aValue ? Standard_True : Standard_False);
111   }
112   else if(aLastInd >= 1)
113   {
114     Standard_CString aValueStr = Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
115     for (ind = aFirstInd; ind <= aLastInd; ind++)
116     {
117       if (!XmlObjMgt::GetInteger(aValueStr, aValue)) 
118       {
119         TCollection_ExtendedString aMessageString =
120           TCollection_ExtendedString("Cannot retrieve integer member"
121                                      " for BooleanList attribute as \"")
122             + aValueStr + "\"";
123         myMessageDriver->Send (aMessageString, Message_Warning);
124         aValue = 0;
125       }
126       aBooleanList->Append(aValue ? Standard_True : Standard_False);
127     }
128   }
129
130   return Standard_True;
131 }
132
133 //=======================================================================
134 //function : Paste
135 //purpose  : transient -> persistent (store)
136 //=======================================================================
137 void XmlMDataStd_BooleanListDriver::Paste(const Handle(TDF_Attribute)& theSource,
138                                           XmlObjMgt_Persistent&        theTarget,
139                                           XmlObjMgt_SRelocationTable&  ) const
140 {
141   const Handle(TDataStd_BooleanList) aBooleanList = Handle(TDataStd_BooleanList)::DownCast(theSource);
142
143   Standard_Integer anU = aBooleanList->Extent();
144   theTarget.Element().setAttribute(::LastIndexString(), anU);
145   // Allocation of 1 char for each boolean value + a space. 
146   NCollection_LocalArray<Standard_Character> str(2 * anU + 1);
147   if(anU == 0) str[0] = 0;
148   else if (anU >= 1)
149   {
150     Standard_Integer iChar(0);
151     TDataStd_ListIteratorOfListOfByte itr(aBooleanList->List());
152     for (; itr.More(); itr.Next())
153     {
154       const Standard_Byte& byte = itr.Value();
155       iChar += Sprintf(&(str[iChar]), "%d ", byte);
156     }
157   }
158   XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True);
159
160   if(aBooleanList->ID() != TDataStd_BooleanList::GetID()) {
161     //convert GUID
162     Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
163     Standard_PCharacter pGuidStr = aGuidStr;
164     aBooleanList->ID().ToCString (pGuidStr);
165     theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);
166   }
167 }