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