b311480e |
1 | // Created on: 2001-08-24 |
2 | // Created by: Alexnder GRIGORIEV |
3 | // Copyright (c) 2001-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 | //AGV 150202: Changed prototype XmlObjMgt::SetStringValue() |
7fd59977 |
21 | |
7fd59977 |
22 | # include <stdio.h> |
23 | #include <XmlMDataStd_RealArrayDriver.ixx> |
24 | #include <TDataStd_RealArray.hxx> |
25 | #include <XmlObjMgt.hxx> |
26 | #include <XmlMDataStd.hxx> |
f7b4312f |
27 | #include <TColStd_HArray1OfReal.hxx> |
28 | #include <NCollection_LocalArray.hxx> |
7fd59977 |
29 | |
30 | IMPLEMENT_DOMSTRING (FirstIndexString, "first") |
31 | IMPLEMENT_DOMSTRING (LastIndexString, "last") |
32 | IMPLEMENT_DOMSTRING (IsDeltaOn, "delta") |
33 | |
34 | //======================================================================= |
35 | //function : XmlMDataStd_RealArrayDriver |
36 | //purpose : Constructor |
37 | //======================================================================= |
38 | |
39 | XmlMDataStd_RealArrayDriver::XmlMDataStd_RealArrayDriver |
40 | (const Handle(CDM_MessageDriver)& theMsgDriver) |
41 | : XmlMDF_ADriver (theMsgDriver, NULL) |
42 | {} |
43 | |
44 | //======================================================================= |
45 | //function : NewEmpty |
46 | //purpose : |
47 | //======================================================================= |
48 | Handle(TDF_Attribute) XmlMDataStd_RealArrayDriver::NewEmpty() const |
49 | { |
50 | return (new TDataStd_RealArray()); |
51 | } |
52 | |
53 | //======================================================================= |
54 | //function : Paste |
55 | //purpose : persistent -> transient (retrieve) |
56 | //======================================================================= |
57 | Standard_Boolean XmlMDataStd_RealArrayDriver::Paste |
58 | (const XmlObjMgt_Persistent& theSource, |
59 | const Handle(TDF_Attribute)& theTarget, |
60 | XmlObjMgt_RRelocationTable& ) const |
61 | { |
62 | Standard_Integer aFirstInd, aLastInd, ind; |
63 | Standard_Real aValue; |
64 | const XmlObjMgt_Element& anElement = theSource; |
65 | |
66 | // Read the FirstIndex; if the attribute is absent initialize to 1 |
67 | XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString()); |
68 | if (aFirstIndex == NULL) |
69 | aFirstInd = 1; |
70 | else if (!aFirstIndex.GetInteger(aFirstInd)) { |
71 | TCollection_ExtendedString aMessageString = |
72 | TCollection_ExtendedString("Cannot retrieve the first index" |
73 | " for RealArray attribute as \"") |
74 | + aFirstIndex + "\""; |
75 | WriteMessage (aMessageString); |
76 | return Standard_False; |
77 | } |
78 | |
79 | // Read LastIndex; the attribute should be present |
80 | if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) { |
81 | TCollection_ExtendedString aMessageString = |
82 | TCollection_ExtendedString("Cannot retrieve the last index" |
83 | " for RealArray attribute as \"") |
84 | + aFirstIndex + "\""; |
85 | WriteMessage (aMessageString); |
86 | return Standard_False; |
87 | } |
88 | |
89 | Handle(TDataStd_RealArray) aRealArray = |
90 | Handle(TDataStd_RealArray)::DownCast(theTarget); |
91 | aRealArray->Init(aFirstInd, aLastInd); |
92 | |
93 | // Check the type of LDOMString |
94 | const XmlObjMgt_DOMString& aString = XmlObjMgt::GetStringValue(anElement); |
95 | if (aString.Type() == LDOMBasicString::LDOM_Integer) { |
96 | if (aFirstInd == aLastInd) { |
97 | Standard_Integer anIntValue; |
98 | if (aString.GetInteger(anIntValue)) |
99 | aRealArray -> SetValue (aFirstInd, Standard_Real(anIntValue)); |
100 | } else { |
101 | TCollection_ExtendedString aMessageString = |
102 | TCollection_ExtendedString("Cannot retrieve array of real members" |
103 | " for RealArray attribute from Integer \"") |
104 | + aString + "\""; |
105 | WriteMessage (aMessageString); |
106 | return Standard_False; |
107 | } |
108 | } else { |
109 | Standard_CString aValueStr = Standard_CString(aString.GetString()); |
110 | for (ind = aFirstInd; ind <= aLastInd; ind++) |
111 | { |
112 | if (!XmlObjMgt::GetReal(aValueStr, aValue)) { |
113 | TCollection_ExtendedString aMessageString = |
114 | TCollection_ExtendedString("Cannot retrieve real member" |
115 | " for RealArray attribute as \"") |
116 | + aValueStr + "\""; |
117 | WriteMessage (aMessageString); |
118 | return Standard_False; |
119 | } |
120 | aRealArray->SetValue(ind, aValue); |
121 | } |
122 | } |
123 | #ifdef DEB |
124 | //cout << "CurDocVersion = " << XmlMDataStd::DocumentVersion() <<endl; |
125 | #endif |
126 | Standard_Boolean aDelta(Standard_False); |
127 | |
128 | if(XmlMDataStd::DocumentVersion() > 2) { |
129 | Standard_Integer aDeltaValue; |
130 | if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue)) |
131 | { |
132 | TCollection_ExtendedString aMessageString = |
133 | TCollection_ExtendedString("Cannot retrieve the isDelta value" |
134 | " for RealArray attribute as \"") |
135 | + aDeltaValue + "\""; |
136 | WriteMessage (aMessageString); |
137 | return Standard_False; |
138 | } |
139 | else |
140 | aDelta = (Standard_Boolean)aDeltaValue; |
141 | } |
142 | #ifdef DEB |
143 | else if(XmlMDataStd::DocumentVersion() == -1) |
144 | cout << "Current DocVersion field is not initialized. " <<endl; |
145 | #endif |
146 | aRealArray->SetDelta(aDelta); |
147 | |
148 | return Standard_True; |
149 | } |
150 | |
151 | //======================================================================= |
152 | //function : Paste |
153 | //purpose : transient -> persistent (store) |
154 | //======================================================================= |
155 | void XmlMDataStd_RealArrayDriver::Paste (const Handle(TDF_Attribute)& theSource, |
156 | XmlObjMgt_Persistent& theTarget, |
157 | XmlObjMgt_SRelocationTable& ) const |
158 | { |
159 | Handle(TDataStd_RealArray) aRealArray = |
160 | Handle(TDataStd_RealArray)::DownCast(theSource); |
f7b4312f |
161 | const Handle(TColStd_HArray1OfReal)& hRealArray = aRealArray->Array(); |
162 | const TColStd_Array1OfReal& realArray = hRealArray->Array1(); |
163 | Standard_Integer aL = realArray.Lower(), anU = realArray.Upper(); |
7fd59977 |
164 | |
165 | if (aL != 1) theTarget.Element().setAttribute(::FirstIndexString(), aL); |
166 | theTarget.Element().setAttribute(::LastIndexString(), anU); |
167 | theTarget.Element().setAttribute(::IsDeltaOn(), aRealArray->GetDelta()); |
168 | |
f7b4312f |
169 | // Allocation of 25 chars for each double value including the space: |
170 | // An example: -3.1512678732195273e+020 |
171 | Standard_Integer iChar = 0; |
172 | NCollection_LocalArray<Standard_Character> str; |
173 | if (realArray.Length()) |
174 | str.Allocate(25 * realArray.Length() + 1); |
175 | |
7fd59977 |
176 | Standard_Integer i = aL; |
302f96fb |
177 | for (;;) |
f7b4312f |
178 | { |
302f96fb |
179 | iChar += Sprintf(&(str[iChar]), "%.17g ", realArray.Value(i)); |
f7b4312f |
180 | if (i >= anU) |
181 | break; |
7fd59977 |
182 | ++i; |
183 | } |
f7b4312f |
184 | |
7fd59977 |
185 | // No occurrence of '&', '<' and other irregular XML characters |
f7b4312f |
186 | if (realArray.Length()) |
187 | { |
188 | str[iChar - 1] = '\0'; |
189 | XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True); |
190 | } |
7fd59977 |
191 | } |