0030101: Application Framework - A mistake reading a RealArray attribute with too...
[occt.git] / src / XmlMDataStd / XmlMDataStd_RealArrayDriver.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
83ae3591 18#include <Message_Messenger.hxx>
f7b4312f 19#include <NCollection_LocalArray.hxx>
8e1e79f0 20#include <Standard_ErrorHandler.hxx>
21#include <Standard_OutOfMemory.hxx>
42cf5bc1 22#include <Standard_Type.hxx>
23#include <TColStd_HArray1OfReal.hxx>
24#include <TDataStd_RealArray.hxx>
25#include <TDF_Attribute.hxx>
26#include <XmlMDataStd.hxx>
27#include <XmlMDataStd_RealArrayDriver.hxx>
28#include <XmlObjMgt.hxx>
29#include <XmlObjMgt_Persistent.hxx>
7fd59977 30
42cf5bc1 31#include <stdio.h>
92efcf78 32IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_RealArrayDriver,XmlMDF_ADriver)
7fd59977 33IMPLEMENT_DOMSTRING (FirstIndexString, "first")
34IMPLEMENT_DOMSTRING (LastIndexString, "last")
35IMPLEMENT_DOMSTRING (IsDeltaOn, "delta")
5a1271c8 36IMPLEMENT_DOMSTRING (AttributeIDString, "realarrattguid")
7fd59977 37//=======================================================================
38//function : XmlMDataStd_RealArrayDriver
39//purpose : Constructor
40//=======================================================================
41
42XmlMDataStd_RealArrayDriver::XmlMDataStd_RealArrayDriver
83ae3591 43 (const Handle(Message_Messenger)& theMsgDriver)
7fd59977 44 : XmlMDF_ADriver (theMsgDriver, NULL)
45{}
46
47//=======================================================================
48//function : NewEmpty
49//purpose :
50//=======================================================================
51Handle(TDF_Attribute) XmlMDataStd_RealArrayDriver::NewEmpty() const
52{
53 return (new TDataStd_RealArray());
54}
55
56//=======================================================================
57//function : Paste
58//purpose : persistent -> transient (retrieve)
59//=======================================================================
60Standard_Boolean XmlMDataStd_RealArrayDriver::Paste
61 (const XmlObjMgt_Persistent& theSource,
62 const Handle(TDF_Attribute)& theTarget,
b34d86cb 63 XmlObjMgt_RRelocationTable& theRelocTable) const
7fd59977 64{
e13b9464 65
66 Handle(TDataStd_RealArray) aRealArray = Handle(TDataStd_RealArray)::DownCast(theTarget);
7fd59977 67 const XmlObjMgt_Element& anElement = theSource;
68
e13b9464 69 // attribute id
70 Standard_GUID aGUID;
71 XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
72 if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
73 aGUID = TDataStd_RealArray::GetID(); //default case
74 else
75 aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
76 aRealArray->SetID(aGUID);
77
78 Standard_Integer aFirstInd, aLastInd, ind;
79
7fd59977 80 // Read the FirstIndex; if the attribute is absent initialize to 1
81 XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
82 if (aFirstIndex == NULL)
83 aFirstInd = 1;
84 else if (!aFirstIndex.GetInteger(aFirstInd)) {
85 TCollection_ExtendedString aMessageString =
86 TCollection_ExtendedString("Cannot retrieve the first index"
87 " for RealArray attribute as \"")
88 + aFirstIndex + "\"";
83ae3591 89 myMessageDriver->Send (aMessageString, Message_Fail);
7fd59977 90 return Standard_False;
91 }
92
93 // Read LastIndex; the attribute should be present
94 if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) {
95 TCollection_ExtendedString aMessageString =
96 TCollection_ExtendedString("Cannot retrieve the last index"
97 " for RealArray attribute as \"")
98 + aFirstIndex + "\"";
83ae3591 99 myMessageDriver->Send (aMessageString, Message_Fail);
7fd59977 100 return Standard_False;
101 }
102
7fd59977 103 aRealArray->Init(aFirstInd, aLastInd);
104
105 // Check the type of LDOMString
106 const XmlObjMgt_DOMString& aString = XmlObjMgt::GetStringValue(anElement);
107 if (aString.Type() == LDOMBasicString::LDOM_Integer) {
108 if (aFirstInd == aLastInd) {
109 Standard_Integer anIntValue;
110 if (aString.GetInteger(anIntValue))
111 aRealArray -> SetValue (aFirstInd, Standard_Real(anIntValue));
112 } else {
113 TCollection_ExtendedString aMessageString =
114 TCollection_ExtendedString("Cannot retrieve array of real members"
115 " for RealArray attribute from Integer \"")
116 + aString + "\"";
83ae3591 117 myMessageDriver->Send (aMessageString, Message_Fail);
7fd59977 118 return Standard_False;
119 }
120 } else {
e13b9464 121 Standard_Real aValue;
7fd59977 122 Standard_CString aValueStr = Standard_CString(aString.GetString());
123 for (ind = aFirstInd; ind <= aLastInd; ind++)
124 {
125 if (!XmlObjMgt::GetReal(aValueStr, aValue)) {
126 TCollection_ExtendedString aMessageString =
127 TCollection_ExtendedString("Cannot retrieve real member"
128 " for RealArray attribute as \"")
129 + aValueStr + "\"";
e13b9464 130 myMessageDriver->Send (aMessageString, Message_Warning);
00cdefc7 131 // skip the first space, if exists
132 while (*aValueStr != 0 && IsSpace (*aValueStr))
133 ++aValueStr;
e13b9464 134 // skip to the next space separator
00cdefc7 135 while (*aValueStr != 0 && ! IsSpace (*aValueStr))
136 ++aValueStr;
7fd59977 137 }
138 aRealArray->SetValue(ind, aValue);
139 }
140 }
7fd59977 141 Standard_Boolean aDelta(Standard_False);
142
b34d86cb 143 if(theRelocTable.GetHeaderData()->StorageVersion().IntegerValue() > 2) {
7fd59977 144 Standard_Integer aDeltaValue;
145 if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue))
146 {
5a1271c8 147 TCollection_ExtendedString aMessageString =
148 TCollection_ExtendedString("Cannot retrieve the isDelta value"
149 " for RealArray attribute as \"")
150 + aDeltaValue + "\"";
83ae3591 151 myMessageDriver->Send (aMessageString, Message_Fail);
5a1271c8 152 return Standard_False;
7fd59977 153 }
154 else
dde68833 155 aDelta = aDeltaValue != 0;
7fd59977 156 }
7fd59977 157 aRealArray->SetDelta(aDelta);
158
159 return Standard_True;
160}
161
162//=======================================================================
163//function : Paste
164//purpose : transient -> persistent (store)
165//=======================================================================
166void XmlMDataStd_RealArrayDriver::Paste (const Handle(TDF_Attribute)& theSource,
167 XmlObjMgt_Persistent& theTarget,
168 XmlObjMgt_SRelocationTable& ) const
169{
170 Handle(TDataStd_RealArray) aRealArray =
171 Handle(TDataStd_RealArray)::DownCast(theSource);
f7b4312f 172 const Handle(TColStd_HArray1OfReal)& hRealArray = aRealArray->Array();
173 const TColStd_Array1OfReal& realArray = hRealArray->Array1();
174 Standard_Integer aL = realArray.Lower(), anU = realArray.Upper();
7fd59977 175
176 if (aL != 1) theTarget.Element().setAttribute(::FirstIndexString(), aL);
177 theTarget.Element().setAttribute(::LastIndexString(), anU);
dde68833 178 theTarget.Element().setAttribute(::IsDeltaOn(), aRealArray->GetDelta() ? 1 : 0);
7fd59977 179
f7b4312f 180 // Allocation of 25 chars for each double value including the space:
181 // An example: -3.1512678732195273e+020
182 Standard_Integer iChar = 0;
183 NCollection_LocalArray<Standard_Character> str;
184 if (realArray.Length())
8e1e79f0 185 {
186 try
187 {
188 OCC_CATCH_SIGNALS
189 str.Allocate(25 * realArray.Length() + 1);
190 }
a738b534 191 catch (Standard_OutOfMemory const&)
8e1e79f0 192 {
193 // If allocation of big space for the string of double array values failed,
194 // try to calculate the necessary space more accurate and allocate it.
195 // It may take some time... therefore it was not done initially and
196 // an attempt to use a simple 25 chars for a double value was used.
197 Standard_Character buf[25];
198 Standard_Integer i(aL), nbChars(0);
199 while (i <= anU)
200 {
201 nbChars += Sprintf(buf, "%.17g ", realArray.Value(i++)) + 1/*a space*/;
202 }
203 if (nbChars)
204 str.Allocate(nbChars);
205 }
206 }
207
7fd59977 208 Standard_Integer i = aL;
302f96fb 209 for (;;)
f7b4312f 210 {
302f96fb 211 iChar += Sprintf(&(str[iChar]), "%.17g ", realArray.Value(i));
f7b4312f 212 if (i >= anU)
213 break;
7fd59977 214 ++i;
215 }
f7b4312f 216
7fd59977 217 // No occurrence of '&', '<' and other irregular XML characters
f7b4312f 218 if (realArray.Length())
219 {
220 str[iChar - 1] = '\0';
221 XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True);
222 }
5a1271c8 223 if(aRealArray->ID() != TDataStd_RealArray::GetID()) {
224 //convert GUID
225 Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
226 Standard_PCharacter pGuidStr = aGuidStr;
227 aRealArray->ID().ToCString (pGuidStr);
228 theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);
229 }
7fd59977 230}