c4c22dacc00d1a74f4b26b31134887599f9cd29b
[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   if(aLastInd == 0) aFirstInd = 0;
89   if (aFirstInd == aLastInd && aLastInd > 0) 
90   {
91     Standard_Integer anInteger;
92     if (!XmlObjMgt::GetStringValue(anElement).GetInteger(anInteger)) 
93     {
94       TCollection_ExtendedString aMessageString =
95         TCollection_ExtendedString("Cannot retrieve integer member"
96                                    " for BooleanList attribute as \"");
97       myMessageDriver->Send (aMessageString, Message_Fail);
98       return Standard_False;
99     }
100     aBooleanList->Append(anInteger ? Standard_True : Standard_False);
101   }
102   else if(aLastInd >= 1)
103   {
104     Standard_CString aValueStr = Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
105     for (ind = aFirstInd; ind <= aLastInd; ind++)
106     {
107       if (!XmlObjMgt::GetInteger(aValueStr, aValue)) 
108       {
109         TCollection_ExtendedString aMessageString =
110           TCollection_ExtendedString("Cannot retrieve integer member"
111                                      " for BooleanList attribute as \"")
112             + aValueStr + "\"";
113         myMessageDriver->Send (aMessageString, Message_Fail);
114         return Standard_False;
115       }
116       aBooleanList->Append(aValue ? Standard_True : Standard_False);
117     }
118   }
119
120   // attribute id
121   Standard_GUID aGUID;
122   XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
123   if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
124     aGUID = TDataStd_BooleanList::GetID(); //default case
125   else
126     aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
127
128   aBooleanList->SetID(aGUID);
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 }