0027772: Foundation Classes - define Standard_Boolean using C++ type "bool" instead...
[occt.git] / src / XmlMDataStd / XmlMDataStd_IntegerArrayDriver.cxx
CommitLineData
b311480e 1// Created on: 2001-08-24
2// Created by: Alexnder GRIGORIEV
973c2be1 3// Copyright (c) 2001-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
16//AGV 150202: Changed prototype XmlObjMgt::SetStringValue()
7fd59977 17
42cf5bc1 18#include <CDM_MessageDriver.hxx>
f7b4312f 19#include <NCollection_LocalArray.hxx>
42cf5bc1 20#include <Standard_Type.hxx>
21#include <TDataStd_IntegerArray.hxx>
22#include <TDF_Attribute.hxx>
7fd59977 23#include <XmlMDataStd.hxx>
42cf5bc1 24#include <XmlMDataStd_IntegerArrayDriver.hxx>
25#include <XmlObjMgt.hxx>
26#include <XmlObjMgt_Persistent.hxx>
7fd59977 27
92efcf78 28IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_IntegerArrayDriver,XmlMDF_ADriver)
7fd59977 29IMPLEMENT_DOMSTRING (FirstIndexString, "first")
30IMPLEMENT_DOMSTRING (LastIndexString, "last")
31IMPLEMENT_DOMSTRING (IsDeltaOn, "delta")
32//=======================================================================
33//function : XmlMDataStd_IntegerArrayDriver
34//purpose : Constructor
35//=======================================================================
36
37XmlMDataStd_IntegerArrayDriver::XmlMDataStd_IntegerArrayDriver
38 (const Handle(CDM_MessageDriver)& theMsgDriver)
39 : XmlMDF_ADriver (theMsgDriver, NULL)
40{}
41
42//=======================================================================
43//function : NewEmpty
44//purpose :
45//=======================================================================
46Handle(TDF_Attribute) XmlMDataStd_IntegerArrayDriver::NewEmpty() const
47{
48 return (new TDataStd_IntegerArray());
49}
50
51//=======================================================================
52//function : Paste
53//purpose : persistent -> transient (retrieve)
54//=======================================================================
55Standard_Boolean XmlMDataStd_IntegerArrayDriver::Paste
56 (const XmlObjMgt_Persistent& theSource,
57 const Handle(TDF_Attribute)& theTarget,
58 XmlObjMgt_RRelocationTable& ) const
59{
60 Standard_Integer aFirstInd, aLastInd, aValue, ind;
61 const XmlObjMgt_Element& anElement = theSource;
62
63 // Read the FirstIndex; if the attribute is absent initialize to 1
64 XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
65 if (aFirstIndex == NULL)
66 aFirstInd = 1;
67 else if (!aFirstIndex.GetInteger(aFirstInd)) {
68 TCollection_ExtendedString aMessageString =
69 TCollection_ExtendedString("Cannot retrieve the first index"
70 " for IntegerArray attribute as \"")
71 + aFirstIndex + "\"";
72 WriteMessage (aMessageString);
73 return Standard_False;
74 }
75
76 // Read the LastIndex; the attribute should be present
77 if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) {
78 TCollection_ExtendedString aMessageString =
79 TCollection_ExtendedString("Cannot retrieve the last index"
80 " for IntegerArray attribute as \"")
81 + aFirstIndex + "\"";
82 WriteMessage (aMessageString);
83 return Standard_False;
84 }
85
86 Handle(TDataStd_IntegerArray) anIntArray =
87 Handle(TDataStd_IntegerArray)::DownCast(theTarget);
88 anIntArray->Init(aFirstInd, aLastInd);
89
90 if(aFirstInd == aLastInd) {
91 Standard_Integer anInteger;
92 if(!XmlObjMgt::GetStringValue(anElement).GetInteger( anInteger)) {
93 TCollection_ExtendedString aMessageString =
94 TCollection_ExtendedString("Cannot retrieve integer member"
95 " for IntegerArray attribute as \"");
96 WriteMessage (aMessageString);
97 return Standard_False;
98 }
99 anIntArray->SetValue(aFirstInd, anInteger);
100
101 }
102 else {
103 // Warning: check implementation of XmlObjMgt_DOMString !! For LDOM this is OK
104 Standard_CString aValueStr =
105 Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
106
107 for (ind = aFirstInd; ind <= aLastInd; ind++)
108 {
109 if (!XmlObjMgt::GetInteger(aValueStr, aValue)) {
110 TCollection_ExtendedString aMessageString =
111 TCollection_ExtendedString("Cannot retrieve integer member"
112 " for IntegerArray attribute as \"")
113 + aValueStr + "\"";
114 WriteMessage (aMessageString);
115 return Standard_False;
116 }
117 anIntArray->SetValue(ind, aValue);
118 }
119 }
7fd59977 120 Standard_Boolean aDelta(Standard_False);
121
122 if(XmlMDataStd::DocumentVersion() > 2) {
123 Standard_Integer aDeltaValue;
124 if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue))
125 {
126 TCollection_ExtendedString aMessageString =
127 TCollection_ExtendedString("Cannot retrieve the isDelta value"
128 " for IntegerArray attribute as \"")
129 + aDeltaValue + "\"";
130 WriteMessage (aMessageString);
131 return Standard_False;
132 }
133 else
dde68833 134 aDelta = aDeltaValue != 0;
7fd59977 135 }
0797d9d3 136#ifdef OCCT_DEBUG
7fd59977 137 else if(XmlMDataStd::DocumentVersion() == -1)
138 cout << "Current DocVersion field is not initialized. " <<endl;
139#endif
140 anIntArray->SetDelta(aDelta);
141
142 return Standard_True;
143}
144
145//=======================================================================
146//function : Paste
147//purpose : transient -> persistent (store)
148//=======================================================================
149void XmlMDataStd_IntegerArrayDriver::Paste
150 (const Handle(TDF_Attribute)& theSource,
151 XmlObjMgt_Persistent& theTarget,
152 XmlObjMgt_SRelocationTable& ) const
153{
154 Handle(TDataStd_IntegerArray) anIntArray =
155 Handle(TDataStd_IntegerArray)::DownCast(theSource);
f7b4312f 156 const Handle(TColStd_HArray1OfInteger)& hIntArray = anIntArray->Array();
157 const TColStd_Array1OfInteger& intArray = hIntArray->Array1();
158 Standard_Integer aL = intArray.Lower(), anU = intArray.Upper();
7fd59977 159
f7b4312f 160 if (aL != 1)
161 theTarget.Element().setAttribute(::FirstIndexString(), aL);
7fd59977 162 theTarget.Element().setAttribute(::LastIndexString(), anU);
dde68833 163 theTarget.Element().setAttribute(::IsDeltaOn(), anIntArray->GetDelta() ? 1 : 0);
7fd59977 164
f7b4312f 165 // Allocation of 12 chars for each integer including the space.
166 // An example: -2 147 483 648
167 Standard_Integer iChar = 0;
168 NCollection_LocalArray<Standard_Character> str;
169 if (intArray.Length())
170 str.Allocate(12 * intArray.Length() + 1);
171
7fd59977 172 Standard_Integer i = aL;
302f96fb 173 for (;;)
f7b4312f 174 {
302f96fb 175 iChar += Sprintf(&(str[iChar]), "%d ", intArray.Value(i));
f7b4312f 176 if (i >= anU)
177 break;
7fd59977 178 ++i;
179 }
f7b4312f 180
181 if (intArray.Length())
182 {
183 // No occurrence of '&', '<' and other irregular XML characters
184 str[iChar - 1] = '\0';
185 XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True);
186 }
7fd59977 187}