7fd59977 |
1 | // File: XmlMDataStd_IntPackedMapDriver.cxx |
2 | // Created: Wed Aug 22 09:48:41 2007 |
3 | // Author: Sergey ZARITCHNY |
4 | // <sergey.zaritchny@opencascade.com> |
5 | //Copyright: Open CasCade SA 2007 |
6 | |
7 | #include <XmlMDataStd_IntPackedMapDriver.ixx> |
8 | #include <XmlObjMgt.hxx> |
9 | #include <XmlObjMgt_RRelocationTable.hxx> |
10 | #include <XmlObjMgt_SRelocationTable.hxx> |
11 | #include <CDM_MessageDriver.hxx> |
12 | #include <TColStd_PackedMapOfInteger.hxx> |
13 | #include <TColStd_HPackedMapOfInteger.hxx> |
14 | #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx> |
15 | #include <XmlMDF_ADriver.hxx> |
16 | #include <TDF_Attribute.hxx> |
17 | #include <TDataStd_IntPackedMap.hxx> |
18 | #include <XmlMDataStd.hxx> |
19 | |
20 | IMPLEMENT_DOMSTRING (IntPackedMapSize, "mapsize") |
21 | IMPLEMENT_DOMSTRING (IsDeltaOn, "delta") |
22 | //======================================================================= |
23 | //function : XmlMDataStd_IntPackedMapDriver |
24 | //purpose : Constructor |
25 | //======================================================================= |
26 | |
27 | XmlMDataStd_IntPackedMapDriver::XmlMDataStd_IntPackedMapDriver |
28 | (const Handle(CDM_MessageDriver)& theMsgDriver) |
29 | : XmlMDF_ADriver (theMsgDriver, STANDARD_TYPE(TDataStd_IntPackedMap)->Name()) |
30 | {} |
31 | |
32 | //======================================================================= |
33 | //function : NewEmpty() |
34 | //purpose : |
35 | //======================================================================= |
36 | Handle(TDF_Attribute) XmlMDataStd_IntPackedMapDriver::NewEmpty () const |
37 | { |
38 | return (new TDataStd_IntPackedMap()); |
39 | } |
40 | |
41 | //======================================================================= |
42 | //function : Paste() |
43 | //purpose : persistent -> transient (retrieve) |
44 | //======================================================================= |
45 | Standard_Boolean XmlMDataStd_IntPackedMapDriver::Paste |
46 | (const XmlObjMgt_Persistent& theSource, |
47 | const Handle(TDF_Attribute)& theTarget, |
48 | XmlObjMgt_RRelocationTable& ) const |
49 | { |
50 | Handle(TDataStd_IntPackedMap) aPackedMap = |
51 | Handle(TDataStd_IntPackedMap)::DownCast(theTarget); |
52 | if(!aPackedMap.IsNull()) { |
53 | Standard_Integer aSize; |
54 | const XmlObjMgt_Element& anElement = theSource; |
55 | XmlObjMgt_DOMString aSizeDStr = anElement.getAttribute(::IntPackedMapSize()); |
56 | if(aSizeDStr == NULL) |
57 | aSize = 0; |
58 | else if (!aSizeDStr.GetInteger(aSize)) { |
59 | TCollection_ExtendedString aMessageString = |
60 | TCollection_ExtendedString("Cannot retrieve the Map size" |
61 | " for IntPackedMap attribute as \"") |
62 | + aSize + "\""; |
63 | WriteMessage (aMessageString); |
64 | return Standard_False; |
65 | } |
66 | Handle(TColStd_HPackedMapOfInteger) aHMap = new TColStd_HPackedMapOfInteger (); |
67 | Standard_Boolean Ok = Standard_True; |
68 | if(aSize) { |
69 | Standard_CString aValueString = |
70 | Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString()); |
71 | // Handle(TColStd_HPackedMapOfInteger) aHMap = new TColStd_HPackedMapOfInteger (); |
72 | for (Standard_Integer i = 1; i <= aSize; i++) { |
73 | Standard_Integer aValue; |
74 | if (!XmlObjMgt::GetInteger(aValueString, aValue)) { |
75 | Ok = Standard_False; break; |
76 | } |
77 | if(!aHMap->ChangeMap().Add( aValue )) { |
78 | Ok = Standard_False; break; |
79 | } |
80 | } |
81 | if(!Ok) { |
82 | TCollection_ExtendedString aMessageString = |
83 | TCollection_ExtendedString("Cannot retrieve integer member" |
84 | " for IntPackedMap attribute as \"") |
85 | + aValueString + "\""; |
86 | WriteMessage (aMessageString); |
87 | return Standard_False; |
88 | } |
89 | if(aPackedMap->ChangeMap(aHMap)) |
90 | Ok = Standard_True; |
91 | } |
92 | // |
93 | #ifdef DEB |
94 | //cout << "CurDocVersion = " << XmlMDataStd::DocumentVersion() <<endl; |
95 | #endif |
96 | if(Ok) { |
97 | Standard_Boolean aDelta(Standard_False); |
98 | |
99 | if(XmlMDataStd::DocumentVersion() > 2) { |
100 | Standard_Integer aDeltaValue; |
101 | if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue)) |
102 | { |
103 | TCollection_ExtendedString aMessageString = |
104 | TCollection_ExtendedString("Cannot retrieve the isDelta value" |
105 | " for IntPackedMap attribute as \"") |
106 | + aDeltaValue + "\""; |
107 | WriteMessage (aMessageString); |
108 | return Standard_False; |
109 | } |
110 | else |
111 | aDelta = (Standard_Boolean)aDeltaValue; |
112 | } |
113 | #ifdef DEB |
114 | else if(XmlMDataStd::DocumentVersion() == -1) |
115 | cout << "Current DocVersion field is not initialized. " <<endl; |
116 | #endif |
117 | aPackedMap->SetDelta(aDelta); |
118 | return Standard_True; |
119 | } |
120 | } |
121 | WriteMessage("error retrieving Map for type TDataStd_IntPackedMap"); |
122 | return Standard_False; |
123 | } |
124 | |
125 | //======================================================================= |
126 | //function : Paste() |
127 | //purpose : transient -> persistent (store) |
128 | //======================================================================= |
129 | void XmlMDataStd_IntPackedMapDriver::Paste (const Handle(TDF_Attribute)& theSource, |
130 | XmlObjMgt_Persistent& theTarget, |
131 | XmlObjMgt_SRelocationTable& ) const |
132 | { |
133 | Handle(TDataStd_IntPackedMap) aS = Handle(TDataStd_IntPackedMap)::DownCast(theSource); |
134 | if (aS.IsNull()) { |
135 | WriteMessage ("IntPackedMapDriver:: The source attribute is Null."); |
136 | return; |
137 | } |
138 | |
139 | Standard_Integer aSize = (aS->IsEmpty()) ? 0 : aS->Extent(); |
140 | theTarget.Element().setAttribute(::IntPackedMapSize(), aSize); |
141 | theTarget.Element().setAttribute(::IsDeltaOn(),aS->GetDelta()); |
142 | |
143 | TCollection_AsciiString aValueString; |
144 | if(aSize) { |
145 | TColStd_MapIteratorOfPackedMapOfInteger anIt(aS->GetMap()); |
146 | for(;anIt.More();anIt.Next()) { |
147 | aValueString += TCollection_AsciiString(anIt.Key()); |
148 | aValueString += ' '; |
149 | } |
150 | // No occurrence of '&', '<' and other irregular XML characters |
151 | XmlObjMgt::SetStringValue (theTarget, aValueString.ToCString(), Standard_True); |
152 | } |
153 | } |