8aca3a8125271292cba5811229657e592aec2779
[occt.git] / src / XmlMDataStd / XmlMDataStd_BooleanArrayDriver.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_BooleanArrayDriver.ixx>
22 #include <TDataStd_BooleanArray.hxx>
23 #include <TColStd_HArray1OfByte.hxx>
24 #include <XmlObjMgt.hxx>
25
26 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
27 IMPLEMENT_DOMSTRING (LastIndexString,  "last")
28
29 //=======================================================================
30 //function : XmlMDataStd_BooleanArrayDriver
31 //purpose  : Constructor
32 //=======================================================================
33 XmlMDataStd_BooleanArrayDriver::XmlMDataStd_BooleanArrayDriver(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_BooleanArrayDriver::NewEmpty() const
44 {
45   return new TDataStd_BooleanArray();
46 }
47
48 //=======================================================================
49 //function : Paste
50 //purpose  : persistent -> transient (retrieve)
51 //=======================================================================
52 Standard_Boolean XmlMDataStd_BooleanArrayDriver::Paste(const XmlObjMgt_Persistent&  theSource,
53                                                        const Handle(TDF_Attribute)& theTarget,
54                                                        XmlObjMgt_RRelocationTable&  ) const
55 {
56   Standard_Integer aFirstInd, aLastInd, aValue;
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 BooleanArray 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 BooleanArray attribute as \"")
79         + aFirstIndex + "\"";
80     WriteMessage (aMessageString);
81     return Standard_False;
82   }
83
84   if (aFirstInd > aLastInd)
85   {
86     TCollection_ExtendedString aMessageString =
87       TCollection_ExtendedString("The last index is greater than the first index"
88                                  " for BooleanArray attribute \"");
89     WriteMessage (aMessageString);
90     return Standard_False;
91   }
92
93   Handle(TDataStd_BooleanArray) aBooleanArray = Handle(TDataStd_BooleanArray)::DownCast(theTarget);
94   aBooleanArray->Init(aFirstInd, aLastInd);
95   Standard_Integer length = aLastInd - aFirstInd + 1;
96   Handle(TColStd_HArray1OfByte) array = new TColStd_HArray1OfByte(0, length >> 3);
97
98   Standard_Integer i = 0, upper = array->Upper();
99   Standard_CString aValueStr = Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
100   for (; i <= upper; i++)
101   {
102     if (!XmlObjMgt::GetInteger(aValueStr, aValue)) 
103     {
104       TCollection_ExtendedString aMessageString =
105         TCollection_ExtendedString("Cannot retrieve integer member"
106                                    " for BooleanArray attribute as \"")
107           + aValueStr + "\"";
108       WriteMessage (aMessageString);
109       return Standard_False;
110     }
111     array->SetValue(i, (Standard_Byte) aValue);
112   }
113   aBooleanArray->SetInternalArray(array);
114   
115   return Standard_True;
116 }
117
118 //=======================================================================
119 //function : Paste
120 //purpose  : transient -> persistent (store)
121 //=======================================================================
122 void XmlMDataStd_BooleanArrayDriver::Paste(const Handle(TDF_Attribute)& theSource,
123                                            XmlObjMgt_Persistent&        theTarget,
124                                            XmlObjMgt_SRelocationTable&  ) const
125 {
126   Handle(TDataStd_BooleanArray) aBooleanArray = Handle(TDataStd_BooleanArray)::DownCast(theSource);
127
128   Standard_Integer aL = aBooleanArray->Lower();
129   Standard_Integer anU = aBooleanArray->Upper();
130   TCollection_AsciiString aValueStr;
131
132   theTarget.Element().setAttribute(::FirstIndexString(), aL);
133   theTarget.Element().setAttribute(::LastIndexString(), anU);
134
135   const Handle(TColStd_HArray1OfByte)& array = aBooleanArray->InternalArray();
136   Standard_Integer lower = array->Lower(), i = lower, upper = array->Upper();
137   for (; i <= upper; i++)
138   {
139     aValueStr += TCollection_AsciiString((Standard_Integer) array->Value(i));
140     aValueStr += ' ';
141   }
142   XmlObjMgt::SetStringValue (theTarget, aValueStr.ToCString(), Standard_True);
143 }