1 // Created on: 2007-08-22
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <CDM_MessageDriver.hxx>
18 #include <NCollection_LocalArray.hxx>
19 #include <Standard_Type.hxx>
20 #include <TColStd_HPackedMapOfInteger.hxx>
21 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
22 #include <TColStd_PackedMapOfInteger.hxx>
23 #include <TDataStd_IntPackedMap.hxx>
24 #include <TDF_Attribute.hxx>
25 #include <XmlMDataStd.hxx>
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>
33 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_IntPackedMapDriver,XmlMDF_ADriver)
34 IMPLEMENT_DOMSTRING (IntPackedMapSize, "mapsize")
35 IMPLEMENT_DOMSTRING (IsDeltaOn, "delta")
36 //=======================================================================
37 //function : XmlMDataStd_IntPackedMapDriver
38 //purpose : Constructor
39 //=======================================================================
41 XmlMDataStd_IntPackedMapDriver::XmlMDataStd_IntPackedMapDriver
42 (const Handle(CDM_MessageDriver)& theMsgDriver)
43 : XmlMDF_ADriver (theMsgDriver, STANDARD_TYPE(TDataStd_IntPackedMap)->Name())
46 //=======================================================================
47 //function : NewEmpty()
49 //=======================================================================
50 Handle(TDF_Attribute) XmlMDataStd_IntPackedMapDriver::NewEmpty () const
52 return (new TDataStd_IntPackedMap());
55 //=======================================================================
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
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());
72 else if (!aSizeDStr.GetInteger(aSize)) {
73 TCollection_ExtendedString aMessageString =
74 TCollection_ExtendedString("Cannot retrieve the Map size"
75 " for IntPackedMap attribute as \"")
77 WriteMessage (aMessageString);
78 return Standard_False;
80 Handle(TColStd_HPackedMapOfInteger) aHMap = new TColStd_HPackedMapOfInteger ();
81 Standard_Boolean Ok = Standard_True;
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;
91 if(!aHMap->ChangeMap().Add( aValue )) {
92 Ok = Standard_False; break;
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;
103 if(aPackedMap->ChangeMap(aHMap))
107 Standard_Boolean aDelta(Standard_False);
109 if(XmlMDataStd::DocumentVersion() > 2) {
110 Standard_Integer aDeltaValue;
111 if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue))
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;
121 aDelta = (Standard_Boolean)aDeltaValue;
124 else if(XmlMDataStd::DocumentVersion() == -1)
125 cout << "Current DocVersion field is not initialized. " <<endl;
127 aPackedMap->SetDelta(aDelta);
128 return Standard_True;
131 WriteMessage("error retrieving Map for type TDataStd_IntPackedMap");
132 return Standard_False;
135 //=======================================================================
137 //purpose : transient -> persistent (store)
138 //=======================================================================
139 void XmlMDataStd_IntPackedMapDriver::Paste (const Handle(TDF_Attribute)& theSource,
140 XmlObjMgt_Persistent& theTarget,
141 XmlObjMgt_SRelocationTable& ) const
143 Handle(TDataStd_IntPackedMap) aS = Handle(TDataStd_IntPackedMap)::DownCast(theSource);
145 WriteMessage ("IntPackedMapDriver:: The source attribute is Null.");
149 Standard_Integer aSize = (aS->IsEmpty()) ? 0 : aS->Extent();
150 theTarget.Element().setAttribute(::IntPackedMapSize(), aSize);
151 theTarget.Element().setAttribute(::IsDeltaOn(),aS->GetDelta());
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);
160 TColStd_MapIteratorOfPackedMapOfInteger anIt(aS->GetMap());
161 for(;anIt.More();anIt.Next())
163 const Standard_Integer intValue = anIt.Key();
164 iChar += Sprintf(&(str[iChar]), "%d ", intValue);
167 // No occurrence of '&', '<' and other irregular XML characters
168 XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True);