b311480e |
1 | // Created on: 2007-08-22 |
2 | // Created by: Sergey ZARITCHNY |
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 | |
16 | #include <XmlMDataStd_IntPackedMapDriver.ixx> |
17 | #include <XmlObjMgt.hxx> |
18 | #include <XmlObjMgt_RRelocationTable.hxx> |
19 | #include <XmlObjMgt_SRelocationTable.hxx> |
20 | #include <CDM_MessageDriver.hxx> |
21 | #include <TColStd_PackedMapOfInteger.hxx> |
22 | #include <TColStd_HPackedMapOfInteger.hxx> |
23 | #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx> |
24 | #include <XmlMDF_ADriver.hxx> |
25 | #include <TDF_Attribute.hxx> |
26 | #include <TDataStd_IntPackedMap.hxx> |
f7b4312f |
27 | #include <NCollection_LocalArray.hxx> |
7fd59977 |
28 | #include <XmlMDataStd.hxx> |
29 | |
30 | IMPLEMENT_DOMSTRING (IntPackedMapSize, "mapsize") |
31 | IMPLEMENT_DOMSTRING (IsDeltaOn, "delta") |
32 | //======================================================================= |
33 | //function : XmlMDataStd_IntPackedMapDriver |
34 | //purpose : Constructor |
35 | //======================================================================= |
36 | |
37 | XmlMDataStd_IntPackedMapDriver::XmlMDataStd_IntPackedMapDriver |
38 | (const Handle(CDM_MessageDriver)& theMsgDriver) |
39 | : XmlMDF_ADriver (theMsgDriver, STANDARD_TYPE(TDataStd_IntPackedMap)->Name()) |
40 | {} |
41 | |
42 | //======================================================================= |
43 | //function : NewEmpty() |
44 | //purpose : |
45 | //======================================================================= |
46 | Handle(TDF_Attribute) XmlMDataStd_IntPackedMapDriver::NewEmpty () const |
47 | { |
48 | return (new TDataStd_IntPackedMap()); |
49 | } |
50 | |
51 | //======================================================================= |
52 | //function : Paste() |
53 | //purpose : persistent -> transient (retrieve) |
54 | //======================================================================= |
55 | Standard_Boolean XmlMDataStd_IntPackedMapDriver::Paste |
56 | (const XmlObjMgt_Persistent& theSource, |
57 | const Handle(TDF_Attribute)& theTarget, |
58 | XmlObjMgt_RRelocationTable& ) const |
59 | { |
60 | Handle(TDataStd_IntPackedMap) aPackedMap = |
61 | Handle(TDataStd_IntPackedMap)::DownCast(theTarget); |
62 | if(!aPackedMap.IsNull()) { |
63 | Standard_Integer aSize; |
64 | const XmlObjMgt_Element& anElement = theSource; |
65 | XmlObjMgt_DOMString aSizeDStr = anElement.getAttribute(::IntPackedMapSize()); |
66 | if(aSizeDStr == NULL) |
67 | aSize = 0; |
68 | else if (!aSizeDStr.GetInteger(aSize)) { |
69 | TCollection_ExtendedString aMessageString = |
70 | TCollection_ExtendedString("Cannot retrieve the Map size" |
71 | " for IntPackedMap attribute as \"") |
72 | + aSize + "\""; |
73 | WriteMessage (aMessageString); |
74 | return Standard_False; |
75 | } |
76 | Handle(TColStd_HPackedMapOfInteger) aHMap = new TColStd_HPackedMapOfInteger (); |
77 | Standard_Boolean Ok = Standard_True; |
78 | if(aSize) { |
79 | Standard_CString aValueString = |
80 | Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString()); |
81 | // Handle(TColStd_HPackedMapOfInteger) aHMap = new TColStd_HPackedMapOfInteger (); |
82 | for (Standard_Integer i = 1; i <= aSize; i++) { |
83 | Standard_Integer aValue; |
84 | if (!XmlObjMgt::GetInteger(aValueString, aValue)) { |
85 | Ok = Standard_False; break; |
86 | } |
87 | if(!aHMap->ChangeMap().Add( aValue )) { |
88 | Ok = Standard_False; break; |
89 | } |
90 | } |
91 | if(!Ok) { |
92 | TCollection_ExtendedString aMessageString = |
93 | TCollection_ExtendedString("Cannot retrieve integer member" |
94 | " for IntPackedMap attribute as \"") |
95 | + aValueString + "\""; |
96 | WriteMessage (aMessageString); |
97 | return Standard_False; |
98 | } |
99 | if(aPackedMap->ChangeMap(aHMap)) |
100 | Ok = Standard_True; |
101 | } |
7fd59977 |
102 | if(Ok) { |
103 | Standard_Boolean aDelta(Standard_False); |
104 | |
105 | if(XmlMDataStd::DocumentVersion() > 2) { |
106 | Standard_Integer aDeltaValue; |
107 | if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue)) |
108 | { |
109 | TCollection_ExtendedString aMessageString = |
110 | TCollection_ExtendedString("Cannot retrieve the isDelta value" |
111 | " for IntPackedMap attribute as \"") |
112 | + aDeltaValue + "\""; |
113 | WriteMessage (aMessageString); |
114 | return Standard_False; |
115 | } |
116 | else |
117 | aDelta = (Standard_Boolean)aDeltaValue; |
118 | } |
0797d9d3 |
119 | #ifdef OCCT_DEBUG |
7fd59977 |
120 | else if(XmlMDataStd::DocumentVersion() == -1) |
121 | cout << "Current DocVersion field is not initialized. " <<endl; |
122 | #endif |
123 | aPackedMap->SetDelta(aDelta); |
124 | return Standard_True; |
125 | } |
126 | } |
127 | WriteMessage("error retrieving Map for type TDataStd_IntPackedMap"); |
128 | return Standard_False; |
129 | } |
130 | |
131 | //======================================================================= |
132 | //function : Paste() |
133 | //purpose : transient -> persistent (store) |
134 | //======================================================================= |
135 | void XmlMDataStd_IntPackedMapDriver::Paste (const Handle(TDF_Attribute)& theSource, |
136 | XmlObjMgt_Persistent& theTarget, |
137 | XmlObjMgt_SRelocationTable& ) const |
138 | { |
139 | Handle(TDataStd_IntPackedMap) aS = Handle(TDataStd_IntPackedMap)::DownCast(theSource); |
140 | if (aS.IsNull()) { |
141 | WriteMessage ("IntPackedMapDriver:: The source attribute is Null."); |
142 | return; |
143 | } |
144 | |
145 | Standard_Integer aSize = (aS->IsEmpty()) ? 0 : aS->Extent(); |
146 | theTarget.Element().setAttribute(::IntPackedMapSize(), aSize); |
147 | theTarget.Element().setAttribute(::IsDeltaOn(),aS->GetDelta()); |
148 | |
f7b4312f |
149 | if(aSize) |
150 | { |
151 | // Allocation of 12 chars for each integer including the space. |
152 | // An example: -2 147 483 648 |
153 | Standard_Integer iChar = 0; |
154 | NCollection_LocalArray<Standard_Character> str(12 * aSize + 1); |
155 | |
7fd59977 |
156 | TColStd_MapIteratorOfPackedMapOfInteger anIt(aS->GetMap()); |
f7b4312f |
157 | for(;anIt.More();anIt.Next()) |
158 | { |
159 | const Standard_Integer intValue = anIt.Key(); |
160 | iChar += Sprintf(&(str[iChar]), "%d ", intValue); |
7fd59977 |
161 | } |
f7b4312f |
162 | |
7fd59977 |
163 | // No occurrence of '&', '<' and other irregular XML characters |
f7b4312f |
164 | XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True); |
7fd59977 |
165 | } |
166 | } |