0030101: Application Framework - A mistake reading a RealArray attribute with too...
[occt.git] / src / XmlMDataStd / XmlMDataStd_RealListDriver.cxx
CommitLineData
b311480e 1// Created on: 2007-05-29
2// Created by: Vlad Romashko
973c2be1 3// Copyright (c) 2007-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.
7fd59977 15
42cf5bc1 16
83ae3591 17#include <Message_Messenger.hxx>
f7b4312f 18#include <NCollection_LocalArray.hxx>
42cf5bc1 19#include <Standard_Type.hxx>
20#include <TColStd_ListIteratorOfListOfReal.hxx>
21#include <TDataStd_RealList.hxx>
22#include <TDF_Attribute.hxx>
23#include <XmlMDataStd_RealListDriver.hxx>
7fd59977 24#include <XmlObjMgt.hxx>
42cf5bc1 25#include <XmlObjMgt_Persistent.hxx>
7fd59977 26
92efcf78 27IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_RealListDriver,XmlMDF_ADriver)
7fd59977 28IMPLEMENT_DOMSTRING (FirstIndexString, "first")
29IMPLEMENT_DOMSTRING (LastIndexString, "last")
5a1271c8 30IMPLEMENT_DOMSTRING (AttributeIDString, "reallistattguid")
7fd59977 31//=======================================================================
32//function : XmlMDataStd_RealListDriver
33//purpose : Constructor
34//=======================================================================
83ae3591 35XmlMDataStd_RealListDriver::XmlMDataStd_RealListDriver(const Handle(Message_Messenger)& theMsgDriver)
7fd59977 36 : XmlMDF_ADriver (theMsgDriver, NULL)
37{
38
39}
40
41//=======================================================================
42//function : NewEmpty
43//purpose :
44//=======================================================================
45Handle(TDF_Attribute) XmlMDataStd_RealListDriver::NewEmpty() const
46{
47 return new TDataStd_RealList();
48}
49
50//=======================================================================
51//function : Paste
52//purpose : persistent -> transient (retrieve)
53//=======================================================================
54Standard_Boolean XmlMDataStd_RealListDriver::Paste(const XmlObjMgt_Persistent& theSource,
5a1271c8 55 const Handle(TDF_Attribute)& theTarget,
56 XmlObjMgt_RRelocationTable& ) const
7fd59977 57{
e13b9464 58 const Handle(TDataStd_RealList) aRealList = Handle(TDataStd_RealList)::DownCast(theTarget);
7fd59977 59 const XmlObjMgt_Element& anElement = theSource;
60
e13b9464 61 // attribute id
62 Standard_GUID aGUID;
63 XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
64 if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
65 aGUID = TDataStd_RealList::GetID(); //default case
66 else
67 aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
68 aRealList->SetID(aGUID);
69
7fd59977 70 // Read the FirstIndex; if the attribute is absent initialize to 1
e13b9464 71 Standard_Integer aFirstInd, aLastInd, ind;
7fd59977 72 XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
73 if (aFirstIndex == NULL)
74 aFirstInd = 1;
75 else if (!aFirstIndex.GetInteger(aFirstInd))
76 {
77 TCollection_ExtendedString aMessageString =
78 TCollection_ExtendedString("Cannot retrieve the first index"
79 " for RealList attribute as \"")
80 + aFirstIndex + "\"";
83ae3591 81 myMessageDriver->Send (aMessageString, Message_Fail);
7fd59977 82 return Standard_False;
83 }
84
85 // Read the LastIndex; the attribute should be present
86 if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd))
87 {
88 TCollection_ExtendedString aMessageString =
89 TCollection_ExtendedString("Cannot retrieve the last index"
90 " for RealList attribute as \"")
91 + aFirstIndex + "\"";
83ae3591 92 myMessageDriver->Send (aMessageString, Message_Fail);
7fd59977 93 return Standard_False;
94 }
95
7fd59977 96 // Check the type of LDOMString
97 const XmlObjMgt_DOMString& aString = XmlObjMgt::GetStringValue(anElement);
d585e74e 98 if(aLastInd == 0) aFirstInd = 0;
7fd59977 99 if (aString.Type() == LDOMBasicString::LDOM_Integer)
100 {
d585e74e 101 if (aFirstInd == aLastInd && aLastInd > 0)
7fd59977 102 {
103 Standard_Integer anIntValue;
104 if (aString.GetInteger(anIntValue))
105 aRealList->Append(Standard_Real(anIntValue));
106 }
107 else
108 {
109 TCollection_ExtendedString aMessageString =
110 TCollection_ExtendedString("Cannot retrieve array of real members"
111 " for RealList attribute from Integer \"")
112 + aString + "\"";
83ae3591 113 myMessageDriver->Send (aMessageString, Message_Fail);
7fd59977 114 return Standard_False;
115 }
116 }
d585e74e 117 else if(aLastInd >= 1)
7fd59977 118 {
119 Standard_CString aValueStr = Standard_CString(aString.GetString());
120 for (ind = aFirstInd; ind <= aLastInd; ind++)
121 {
e13b9464 122 Standard_Real aValue;
7fd59977 123 if (!XmlObjMgt::GetReal(aValueStr, aValue)) {
124 TCollection_ExtendedString aMessageString =
125 TCollection_ExtendedString("Cannot retrieve real member"
126 " for RealList attribute as \"")
127 + aValueStr + "\"";
e13b9464 128 myMessageDriver->Send(aMessageString, Message_Warning);
00cdefc7 129 // skip the first space, if exists
130 while (*aValueStr != 0 && IsSpace (*aValueStr))
131 ++aValueStr;
e13b9464 132 // skip to the next space separator
00cdefc7 133 while (*aValueStr != 0 && !IsSpace (*aValueStr))
134 ++aValueStr;
7fd59977 135 }
136 aRealList->Append(aValue);
137 }
138 }
139
140 return Standard_True;
141}
142
143//=======================================================================
144//function : Paste
145//purpose : transient -> persistent (store)
146//=======================================================================
147void XmlMDataStd_RealListDriver::Paste(const Handle(TDF_Attribute)& theSource,
5a1271c8 148 XmlObjMgt_Persistent& theTarget,
149 XmlObjMgt_SRelocationTable& ) const
7fd59977 150{
d585e74e 151 const Handle(TDataStd_RealList) aRealList = Handle(TDataStd_RealList)::DownCast(theSource);
7fd59977 152
153 Standard_Integer anU = aRealList->Extent();
7fd59977 154 theTarget.Element().setAttribute(::LastIndexString(), anU);
d585e74e 155 // Allocation of 25 chars for each double value including the space:
156 // An example: -3.1512678732195273e+020
157 NCollection_LocalArray<Standard_Character> str(25 * anU + 1);
158 if(anU == 0) str[0] = 0;
159 else if (anU >= 1)
160 {
f7b4312f 161 Standard_Integer iChar = 0;
7fd59977 162 TColStd_ListIteratorOfListOfReal itr(aRealList->List());
163 for (; itr.More(); itr.Next())
164 {
f7b4312f 165 const Standard_Real& realValue = itr.Value();
166 iChar += Sprintf(&(str[iChar]), "%.17g ", realValue);
7fd59977 167 }
168 }
d585e74e 169 XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True);
5a1271c8 170
171 if(aRealList->ID() != TDataStd_RealList::GetID()) {
172 //convert GUID
173 Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
174 Standard_PCharacter pGuidStr = aGuidStr;
175 aRealList->ID().ToCString (pGuidStr);
176 theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);
177 }
7fd59977 178}