0fd9b0ef36052e267f25b87baaf913d17aab5aaa
[occt.git] / src / XmlMDataStd / XmlMDataStd_BooleanListDriver.cxx
1 // Created on: 2007-05-29
2 // Created by: Vlad Romashko
3 // Copyright (c) 2007-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21 #include <XmlMDataStd_BooleanListDriver.ixx>
22 #include <TDataStd_BooleanList.hxx>
23 #include <TDataStd_ListIteratorOfListOfByte.hxx>
24 #include <XmlObjMgt.hxx>
25
26 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
27 IMPLEMENT_DOMSTRING (LastIndexString,  "last")
28
29 //=======================================================================
30 //function : XmlMDataStd_BooleanListDriver
31 //purpose  : Constructor
32 //=======================================================================
33 XmlMDataStd_BooleanListDriver::XmlMDataStd_BooleanListDriver(const Handle(CDM_MessageDriver)& theMsgDriver)
34      : XmlMDF_ADriver (theMsgDriver, NULL)
35 {
36
37 }
38
39 //=======================================================================
40 //function : NewEmpty
41 //purpose  : 
42 //=======================================================================
43 Handle(TDF_Attribute) XmlMDataStd_BooleanListDriver::NewEmpty() const
44 {
45   return new TDataStd_BooleanList();
46 }
47
48 //=======================================================================
49 //function : Paste
50 //purpose  : persistent -> transient (retrieve)
51 //=======================================================================
52 Standard_Boolean XmlMDataStd_BooleanListDriver::Paste(const XmlObjMgt_Persistent&  theSource,
53                                                       const Handle(TDF_Attribute)& theTarget,
54                                                       XmlObjMgt_RRelocationTable&  ) const
55 {
56   Standard_Integer aFirstInd, aLastInd, aValue, ind;
57   const XmlObjMgt_Element& anElement = theSource;
58
59   // Read the FirstIndex; if the attribute is absent initialize to 1
60   XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
61   if (aFirstIndex == NULL)
62     aFirstInd = 1;
63   else if (!aFirstIndex.GetInteger(aFirstInd)) 
64   {
65     TCollection_ExtendedString aMessageString =
66       TCollection_ExtendedString("Cannot retrieve the first index"
67                                  " for BooleanList attribute as \"")
68         + aFirstIndex + "\"";
69     WriteMessage (aMessageString);
70     return Standard_False;
71   }
72
73   // Read the LastIndex; the attribute should be present
74   if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) 
75   {
76     TCollection_ExtendedString aMessageString =
77       TCollection_ExtendedString("Cannot retrieve the last index"
78                                  " for BooleanList attribute as \"")
79         + aFirstIndex + "\"";
80     WriteMessage (aMessageString);
81     return Standard_False;
82   }
83
84   Handle(TDataStd_BooleanList) aBooleanList = Handle(TDataStd_BooleanList)::DownCast(theTarget);
85   if (aFirstInd == aLastInd) 
86   {
87     Standard_Integer anInteger;
88     if (!XmlObjMgt::GetStringValue(anElement).GetInteger(anInteger)) 
89     {
90       TCollection_ExtendedString aMessageString =
91         TCollection_ExtendedString("Cannot retrieve integer member"
92                                    " for BooleanList attribute as \"");
93       WriteMessage (aMessageString);
94       return Standard_False;
95     }
96     aBooleanList->Append(anInteger ? Standard_True : Standard_False);
97   }
98   else 
99   {
100     Standard_CString aValueStr = Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
101     for (ind = aFirstInd; ind <= aLastInd; ind++)
102     {
103       if (!XmlObjMgt::GetInteger(aValueStr, aValue)) 
104       {
105         TCollection_ExtendedString aMessageString =
106           TCollection_ExtendedString("Cannot retrieve integer member"
107                                      " for BooleanList attribute as \"")
108             + aValueStr + "\"";
109         WriteMessage (aMessageString);
110         return Standard_False;
111       }
112       aBooleanList->Append(aValue ? Standard_True : Standard_False);
113     }
114   }
115   
116   return Standard_True;
117 }
118
119 //=======================================================================
120 //function : Paste
121 //purpose  : transient -> persistent (store)
122 //=======================================================================
123 void XmlMDataStd_BooleanListDriver::Paste(const Handle(TDF_Attribute)& theSource,
124                                           XmlObjMgt_Persistent&        theTarget,
125                                           XmlObjMgt_SRelocationTable&  ) const
126 {
127   Handle(TDataStd_BooleanList) aBooleanList = Handle(TDataStd_BooleanList)::DownCast(theSource);
128
129   Standard_Integer anU = aBooleanList->Extent();
130   TCollection_AsciiString aValueStr;
131
132   theTarget.Element().setAttribute(::LastIndexString(), anU);
133   if (anU >= 1)
134   {
135     TDataStd_ListIteratorOfListOfByte itr(aBooleanList->List());
136     for (; itr.More(); itr.Next())
137     {
138       aValueStr += TCollection_AsciiString(itr.Value());
139       aValueStr += ' ';
140     }
141   }
142   // No occurrence of '&', '<' and other irregular XML characters
143   XmlObjMgt::SetStringValue (theTarget, aValueStr.ToCString(), Standard_True);
144 }