0023024: Update headers of OCCT files
[occt.git] / src / XmlMDataStd / XmlMDataStd_IntPackedMapDriver.cxx
1 // Created on: 2007-08-22
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2007-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
21 #include <XmlMDataStd_IntPackedMapDriver.ixx>
22 #include <XmlObjMgt.hxx>
23 #include <XmlObjMgt_RRelocationTable.hxx>
24 #include <XmlObjMgt_SRelocationTable.hxx>
25 #include <CDM_MessageDriver.hxx>
26 #include <TColStd_PackedMapOfInteger.hxx>
27 #include <TColStd_HPackedMapOfInteger.hxx>
28 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
29 #include <XmlMDF_ADriver.hxx>
30 #include <TDF_Attribute.hxx>
31 #include <TDataStd_IntPackedMap.hxx>
32 #include <XmlMDataStd.hxx>
33
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     }
106     //
107 #ifdef DEB
108   //cout << "CurDocVersion = " << XmlMDataStd::DocumentVersion() <<endl;
109 #endif
110     if(Ok) {
111       Standard_Boolean aDelta(Standard_False);
112   
113       if(XmlMDataStd::DocumentVersion() > 2) {
114         Standard_Integer aDeltaValue;
115         if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue)) 
116           {
117             TCollection_ExtendedString aMessageString =
118               TCollection_ExtendedString("Cannot retrieve the isDelta value"
119                                          " for IntPackedMap attribute as \"")
120                 + aDeltaValue + "\"";
121             WriteMessage (aMessageString);
122             return Standard_False;
123           } 
124         else
125           aDelta = (Standard_Boolean)aDeltaValue;
126       }
127 #ifdef DEB
128       else if(XmlMDataStd::DocumentVersion() == -1)
129         cout << "Current DocVersion field is not initialized. "  <<endl;
130 #endif
131       aPackedMap->SetDelta(aDelta);
132       return Standard_True;
133     }  
134   }
135   WriteMessage("error retrieving Map for type TDataStd_IntPackedMap");
136   return Standard_False;
137 }
138
139 //=======================================================================
140 //function : Paste()
141 //purpose  : transient -> persistent (store)
142 //=======================================================================
143 void XmlMDataStd_IntPackedMapDriver::Paste (const Handle(TDF_Attribute)& theSource,
144                                     XmlObjMgt_Persistent&        theTarget,
145                                     XmlObjMgt_SRelocationTable&  ) const
146 {
147   Handle(TDataStd_IntPackedMap) aS = Handle(TDataStd_IntPackedMap)::DownCast(theSource);
148   if (aS.IsNull()) {
149     WriteMessage ("IntPackedMapDriver:: The source attribute is Null.");
150     return;
151   }
152
153   Standard_Integer aSize = (aS->IsEmpty()) ? 0 : aS->Extent();
154   theTarget.Element().setAttribute(::IntPackedMapSize(), aSize);
155   theTarget.Element().setAttribute(::IsDeltaOn(),aS->GetDelta());
156
157   TCollection_AsciiString aValueString;
158   if(aSize) {
159     TColStd_MapIteratorOfPackedMapOfInteger anIt(aS->GetMap());
160     for(;anIt.More();anIt.Next()) {
161       aValueString += TCollection_AsciiString(anIt.Key());
162       aValueString += ' ';
163     }
164     // No occurrence of '&', '<' and other irregular XML characters
165     XmlObjMgt::SetStringValue (theTarget, aValueString.ToCString(), Standard_True);
166   }
167 }