0027970: Improvement of standard attributes usability - containers.
[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")
5a1271c8 32IMPLEMENT_DOMSTRING (AttributeIDString, "intarrattguid")
33
7fd59977 34//=======================================================================
35//function : XmlMDataStd_IntegerArrayDriver
36//purpose : Constructor
37//=======================================================================
38
39XmlMDataStd_IntegerArrayDriver::XmlMDataStd_IntegerArrayDriver
40 (const Handle(CDM_MessageDriver)& theMsgDriver)
41 : XmlMDF_ADriver (theMsgDriver, NULL)
42{}
43
44//=======================================================================
45//function : NewEmpty
46//purpose :
47//=======================================================================
48Handle(TDF_Attribute) XmlMDataStd_IntegerArrayDriver::NewEmpty() const
49{
50 return (new TDataStd_IntegerArray());
51}
52
53//=======================================================================
54//function : Paste
55//purpose : persistent -> transient (retrieve)
56//=======================================================================
57Standard_Boolean XmlMDataStd_IntegerArrayDriver::Paste
58 (const XmlObjMgt_Persistent& theSource,
59 const Handle(TDF_Attribute)& theTarget,
60 XmlObjMgt_RRelocationTable& ) const
61{
62 Standard_Integer aFirstInd, aLastInd, aValue, ind;
63 const XmlObjMgt_Element& anElement = theSource;
64
65 // Read the FirstIndex; if the attribute is absent initialize to 1
66 XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
67 if (aFirstIndex == NULL)
68 aFirstInd = 1;
69 else if (!aFirstIndex.GetInteger(aFirstInd)) {
70 TCollection_ExtendedString aMessageString =
71 TCollection_ExtendedString("Cannot retrieve the first index"
72 " for IntegerArray attribute as \"")
73 + aFirstIndex + "\"";
74 WriteMessage (aMessageString);
75 return Standard_False;
76 }
77
78 // Read the LastIndex; the attribute should be present
79 if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) {
80 TCollection_ExtendedString aMessageString =
81 TCollection_ExtendedString("Cannot retrieve the last index"
82 " for IntegerArray attribute as \"")
83 + aFirstIndex + "\"";
84 WriteMessage (aMessageString);
85 return Standard_False;
86 }
87
88 Handle(TDataStd_IntegerArray) anIntArray =
89 Handle(TDataStd_IntegerArray)::DownCast(theTarget);
90 anIntArray->Init(aFirstInd, aLastInd);
91
92 if(aFirstInd == aLastInd) {
93 Standard_Integer anInteger;
94 if(!XmlObjMgt::GetStringValue(anElement).GetInteger( anInteger)) {
95 TCollection_ExtendedString aMessageString =
96 TCollection_ExtendedString("Cannot retrieve integer member"
97 " for IntegerArray attribute as \"");
98 WriteMessage (aMessageString);
99 return Standard_False;
100 }
101 anIntArray->SetValue(aFirstInd, anInteger);
102
103 }
104 else {
105 // Warning: check implementation of XmlObjMgt_DOMString !! For LDOM this is OK
106 Standard_CString aValueStr =
107 Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
108
109 for (ind = aFirstInd; ind <= aLastInd; ind++)
110 {
111 if (!XmlObjMgt::GetInteger(aValueStr, aValue)) {
112 TCollection_ExtendedString aMessageString =
113 TCollection_ExtendedString("Cannot retrieve integer member"
114 " for IntegerArray attribute as \"")
115 + aValueStr + "\"";
116 WriteMessage (aMessageString);
117 return Standard_False;
118 }
119 anIntArray->SetValue(ind, aValue);
120 }
121 }
7fd59977 122 Standard_Boolean aDelta(Standard_False);
123
124 if(XmlMDataStd::DocumentVersion() > 2) {
125 Standard_Integer aDeltaValue;
126 if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue))
127 {
5a1271c8 128 TCollection_ExtendedString aMessageString =
129 TCollection_ExtendedString("Cannot retrieve the isDelta value"
130 " for IntegerArray attribute as \"")
131 + aDeltaValue + "\"";
132 WriteMessage (aMessageString);
133 return Standard_False;
7fd59977 134 }
135 else
dde68833 136 aDelta = aDeltaValue != 0;
7fd59977 137 }
0797d9d3 138#ifdef OCCT_DEBUG
7fd59977 139 else if(XmlMDataStd::DocumentVersion() == -1)
140 cout << "Current DocVersion field is not initialized. " <<endl;
141#endif
142 anIntArray->SetDelta(aDelta);
5a1271c8 143
144 // attribute id
145 Standard_GUID aGUID;
146 XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
147 if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
148 aGUID = TDataStd_IntegerArray::GetID(); //default case
149 else
150 aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
151
152 anIntArray->SetID(aGUID);
153
7fd59977 154 return Standard_True;
155}
156
157//=======================================================================
158//function : Paste
159//purpose : transient -> persistent (store)
160//=======================================================================
161void XmlMDataStd_IntegerArrayDriver::Paste
162 (const Handle(TDF_Attribute)& theSource,
163 XmlObjMgt_Persistent& theTarget,
164 XmlObjMgt_SRelocationTable& ) const
165{
166 Handle(TDataStd_IntegerArray) anIntArray =
167 Handle(TDataStd_IntegerArray)::DownCast(theSource);
f7b4312f 168 const Handle(TColStd_HArray1OfInteger)& hIntArray = anIntArray->Array();
169 const TColStd_Array1OfInteger& intArray = hIntArray->Array1();
170 Standard_Integer aL = intArray.Lower(), anU = intArray.Upper();
7fd59977 171
f7b4312f 172 if (aL != 1)
173 theTarget.Element().setAttribute(::FirstIndexString(), aL);
7fd59977 174 theTarget.Element().setAttribute(::LastIndexString(), anU);
dde68833 175 theTarget.Element().setAttribute(::IsDeltaOn(), anIntArray->GetDelta() ? 1 : 0);
7fd59977 176
f7b4312f 177 // Allocation of 12 chars for each integer including the space.
178 // An example: -2 147 483 648
179 Standard_Integer iChar = 0;
180 NCollection_LocalArray<Standard_Character> str;
181 if (intArray.Length())
182 str.Allocate(12 * intArray.Length() + 1);
183
7fd59977 184 Standard_Integer i = aL;
302f96fb 185 for (;;)
f7b4312f 186 {
302f96fb 187 iChar += Sprintf(&(str[iChar]), "%d ", intArray.Value(i));
f7b4312f 188 if (i >= anU)
189 break;
7fd59977 190 ++i;
191 }
f7b4312f 192
193 if (intArray.Length())
194 {
195 // No occurrence of '&', '<' and other irregular XML characters
196 str[iChar - 1] = '\0';
197 XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True);
198 }
5a1271c8 199 if(anIntArray->ID() != TDataStd_IntegerArray::GetID()) {
200 //convert GUID
201 Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
202 Standard_PCharacter pGuidStr = aGuidStr;
203 anIntArray->ID().ToCString (pGuidStr);
204 theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);
205 }
7fd59977 206}