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_HArray1OfByte.hxx> |
21 | #include <TDataStd_BooleanArray.hxx> |
22 | #include <TDF_Attribute.hxx> |
23 | #include <XmlMDataStd_BooleanArrayDriver.hxx> |
7fd59977 |
24 | #include <XmlObjMgt.hxx> |
42cf5bc1 |
25 | #include <XmlObjMgt_Persistent.hxx> |
7fd59977 |
26 | |
92efcf78 |
27 | IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_BooleanArrayDriver,XmlMDF_ADriver) |
7fd59977 |
28 | IMPLEMENT_DOMSTRING (FirstIndexString, "first") |
29 | IMPLEMENT_DOMSTRING (LastIndexString, "last") |
5a1271c8 |
30 | IMPLEMENT_DOMSTRING (AttributeIDString, "boolarrattguid") |
7fd59977 |
31 | //======================================================================= |
32 | //function : XmlMDataStd_BooleanArrayDriver |
33 | //purpose : Constructor |
34 | //======================================================================= |
83ae3591 |
35 | XmlMDataStd_BooleanArrayDriver::XmlMDataStd_BooleanArrayDriver(const Handle(Message_Messenger)& theMsgDriver) |
7fd59977 |
36 | : XmlMDF_ADriver (theMsgDriver, NULL) |
37 | { |
38 | |
39 | } |
40 | |
41 | //======================================================================= |
42 | //function : NewEmpty |
43 | //purpose : |
44 | //======================================================================= |
45 | Handle(TDF_Attribute) XmlMDataStd_BooleanArrayDriver::NewEmpty() const |
46 | { |
47 | return new TDataStd_BooleanArray(); |
48 | } |
49 | |
50 | //======================================================================= |
51 | //function : Paste |
52 | //purpose : persistent -> transient (retrieve) |
53 | //======================================================================= |
54 | Standard_Boolean XmlMDataStd_BooleanArrayDriver::Paste(const XmlObjMgt_Persistent& theSource, |
5a1271c8 |
55 | const Handle(TDF_Attribute)& theTarget, |
56 | XmlObjMgt_RRelocationTable& ) const |
7fd59977 |
57 | { |
6e6cd5d9 |
58 | Standard_Integer aFirstInd, aLastInd, aValue; |
7fd59977 |
59 | const XmlObjMgt_Element& anElement = theSource; |
60 | |
61 | // Read the FirstIndex; if the attribute is absent initialize to 1 |
62 | XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString()); |
63 | if (aFirstIndex == NULL) |
64 | aFirstInd = 1; |
65 | else if (!aFirstIndex.GetInteger(aFirstInd)) |
66 | { |
67 | TCollection_ExtendedString aMessageString = |
68 | TCollection_ExtendedString("Cannot retrieve the first index" |
69 | " for BooleanArray attribute as \"") |
70 | + aFirstIndex + "\""; |
83ae3591 |
71 | myMessageDriver->Send (aMessageString, Message_Fail); |
7fd59977 |
72 | return Standard_False; |
73 | } |
74 | |
75 | // Read the LastIndex; the attribute should be present |
76 | if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) |
77 | { |
78 | TCollection_ExtendedString aMessageString = |
79 | TCollection_ExtendedString("Cannot retrieve the last index" |
80 | " for BooleanArray attribute as \"") |
81 | + aFirstIndex + "\""; |
83ae3591 |
82 | myMessageDriver->Send (aMessageString, Message_Fail); |
7fd59977 |
83 | return Standard_False; |
84 | } |
85 | |
86 | if (aFirstInd > aLastInd) |
87 | { |
88 | TCollection_ExtendedString aMessageString = |
89 | TCollection_ExtendedString("The last index is greater than the first index" |
90 | " for BooleanArray attribute \""); |
83ae3591 |
91 | myMessageDriver->Send (aMessageString, Message_Fail); |
7fd59977 |
92 | return Standard_False; |
93 | } |
94 | |
95 | Handle(TDataStd_BooleanArray) aBooleanArray = Handle(TDataStd_BooleanArray)::DownCast(theTarget); |
cbc4faa9 |
96 | // attribute id |
97 | Standard_GUID aGUID; |
98 | XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString()); |
99 | if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL) |
100 | aGUID = TDataStd_BooleanArray::GetID(); //default case |
101 | else |
102 | aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case |
103 | aBooleanArray->SetID(aGUID); |
104 | |
7fd59977 |
105 | aBooleanArray->Init(aFirstInd, aLastInd); |
106 | Standard_Integer length = aLastInd - aFirstInd + 1; |
f7b4312f |
107 | Handle(TColStd_HArray1OfByte) hArr = new TColStd_HArray1OfByte(0, length >> 3); |
108 | TColStd_Array1OfByte& arr = hArr->ChangeArray1(); |
f7b4312f |
109 | Standard_Integer i = 0, upper = arr.Upper(); |
7fd59977 |
110 | Standard_CString aValueStr = Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString()); |
cbc4faa9 |
111 | |
7fd59977 |
112 | for (; i <= upper; i++) |
113 | { |
114 | if (!XmlObjMgt::GetInteger(aValueStr, aValue)) |
115 | { |
116 | TCollection_ExtendedString aMessageString = |
5a1271c8 |
117 | TCollection_ExtendedString("Cannot retrieve integer member" |
118 | " for BooleanArray attribute as \"") |
119 | + aValueStr + "\""; |
cbc4faa9 |
120 | myMessageDriver->Send (aMessageString, Message_Warning); |
121 | aValue = 0; |
7fd59977 |
122 | } |
f7b4312f |
123 | arr.SetValue(i, (Standard_Byte) aValue); |
7fd59977 |
124 | } |
f7b4312f |
125 | aBooleanArray->SetInternalArray(hArr); |
5a1271c8 |
126 | |
7fd59977 |
127 | return Standard_True; |
128 | } |
129 | |
130 | //======================================================================= |
131 | //function : Paste |
132 | //purpose : transient -> persistent (store) |
133 | //======================================================================= |
134 | void XmlMDataStd_BooleanArrayDriver::Paste(const Handle(TDF_Attribute)& theSource, |
5a1271c8 |
135 | XmlObjMgt_Persistent& theTarget, |
136 | XmlObjMgt_SRelocationTable& ) const |
7fd59977 |
137 | { |
138 | Handle(TDataStd_BooleanArray) aBooleanArray = Handle(TDataStd_BooleanArray)::DownCast(theSource); |
139 | |
140 | Standard_Integer aL = aBooleanArray->Lower(); |
141 | Standard_Integer anU = aBooleanArray->Upper(); |
7fd59977 |
142 | |
143 | theTarget.Element().setAttribute(::FirstIndexString(), aL); |
144 | theTarget.Element().setAttribute(::LastIndexString(), anU); |
145 | |
f7b4312f |
146 | const Handle(TColStd_HArray1OfByte)& hArr = aBooleanArray->InternalArray(); |
147 | const TColStd_Array1OfByte& arr = hArr->Array1(); |
148 | |
149 | // Allocation of 4 chars for each byte. |
150 | Standard_Integer iChar = 0; |
151 | NCollection_LocalArray<Standard_Character> str; |
67908e84 |
152 | if (!arr.IsEmpty()) |
153 | { |
154 | str.Allocate (4 * arr.Length() + 1); |
155 | } |
f7b4312f |
156 | |
157 | // Convert integers - compressed boolean values, to a string. |
67908e84 |
158 | const Standard_Integer upper = arr.Upper(); |
159 | for (Standard_Integer i = arr.Lower(); i <= upper; i++) |
7fd59977 |
160 | { |
f7b4312f |
161 | const Standard_Byte& byte = arr.Value(i); |
162 | iChar += Sprintf(&(str[iChar]), "%d ", byte); |
7fd59977 |
163 | } |
f7b4312f |
164 | |
67908e84 |
165 | if (!arr.IsEmpty()) |
166 | { |
f7b4312f |
167 | XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True); |
67908e84 |
168 | } |
169 | |
170 | if (aBooleanArray->ID() != TDataStd_BooleanArray::GetID()) |
171 | { |
5a1271c8 |
172 | //convert GUID |
173 | Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC]; |
174 | Standard_PCharacter pGuidStr = aGuidStr; |
175 | aBooleanArray->ID().ToCString (pGuidStr); |
176 | theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr); |
177 | } |
7fd59977 |
178 | } |