0033041: Coding - get rid of unused headers [TopTools to Xw]
[occt.git] / src / XmlMDataStd / XmlMDataStd_IntegerListDriver.cxx
1 // Created on: 2007-05-29
2 // Created by: Vlad Romashko
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <Message_Messenger.hxx>
18 #include <NCollection_LocalArray.hxx>
19 #include <Standard_Type.hxx>
20 #include <TDataStd_IntegerList.hxx>
21 #include <TDF_Attribute.hxx>
22 #include <XmlMDataStd_IntegerListDriver.hxx>
23 #include <XmlObjMgt.hxx>
24 #include <XmlObjMgt_Persistent.hxx>
25
26 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_IntegerListDriver,XmlMDF_ADriver)
27 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
28 IMPLEMENT_DOMSTRING (LastIndexString,  "last")
29 IMPLEMENT_DOMSTRING (AttributeIDString, "intlistattguid")
30
31 //=======================================================================
32 //function : XmlMDataStd_IntegerListDriver
33 //purpose  : Constructor
34 //=======================================================================
35 XmlMDataStd_IntegerListDriver::XmlMDataStd_IntegerListDriver(const Handle(Message_Messenger)& theMsgDriver)
36 : XmlMDF_ADriver (theMsgDriver, NULL)
37 {
38
39 }
40
41 //=======================================================================
42 //function : NewEmpty
43 //purpose  : 
44 //=======================================================================
45 Handle(TDF_Attribute) XmlMDataStd_IntegerListDriver::NewEmpty() const
46 {
47   return new TDataStd_IntegerList();
48 }
49
50 //=======================================================================
51 //function : Paste
52 //purpose  : persistent -> transient (retrieve)
53 //=======================================================================
54 Standard_Boolean XmlMDataStd_IntegerListDriver::Paste(const XmlObjMgt_Persistent&  theSource,
55                                                       const Handle(TDF_Attribute)& theTarget,
56                                                       XmlObjMgt_RRelocationTable&  ) const
57 {
58   Standard_Integer aFirstInd, aLastInd, aValue, ind;
59   const XmlObjMgt_Element& anElement = theSource;
60
61   // Read the FirstIndex; if the attribute is absent initialize to 1
62   XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
63   if (aFirstIndex == NULL)
64     aFirstInd = 1;
65   else if (!aFirstIndex.GetInteger(aFirstInd)) 
66   {
67     TCollection_ExtendedString aMessageString =
68       TCollection_ExtendedString("Cannot retrieve the first index"
69                                  " for IntegerList attribute as \"")
70         + aFirstIndex + "\"";
71     myMessageDriver->Send (aMessageString, Message_Fail);
72     return Standard_False;
73   }
74
75   // Read the LastIndex; the attribute should be present
76   if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) 
77   {
78     TCollection_ExtendedString aMessageString =
79       TCollection_ExtendedString("Cannot retrieve the last index"
80                                  " for IntegerList attribute as \"")
81         + aFirstIndex + "\"";
82     myMessageDriver->Send (aMessageString, Message_Fail);
83     return Standard_False;
84   }
85
86   const Handle(TDataStd_IntegerList) anIntList = Handle(TDataStd_IntegerList)::DownCast(theTarget);
87   
88   // attribute id
89   Standard_GUID aGUID;
90   XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
91   if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
92     aGUID = TDataStd_IntegerList::GetID(); //default case
93   else
94     aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
95
96   anIntList->SetID(aGUID);
97
98   if(aLastInd == 0) aFirstInd = 0;
99   if (aFirstInd == aLastInd && aLastInd > 0) 
100   {
101     if (!XmlObjMgt::GetStringValue(anElement).GetInteger(aValue)) 
102     {
103       TCollection_ExtendedString aMessageString =
104         TCollection_ExtendedString("Cannot retrieve integer member"
105                                    " for IntegerList attribute as \"");
106       myMessageDriver->Send (aMessageString, Message_Warning);
107       aValue = 0;
108     }
109     anIntList->Append(aValue);
110   }
111   else if(aLastInd >= 1)
112   {
113     Standard_CString aValueStr = Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
114     for (ind = aFirstInd; ind <= aLastInd; ind++)
115     {
116       if (!XmlObjMgt::GetInteger(aValueStr, aValue)) 
117       {
118         TCollection_ExtendedString aMessageString =
119           TCollection_ExtendedString("Cannot retrieve integer member"
120                                      " for IntegerList attribute as \"")
121             + aValueStr + "\"";
122         myMessageDriver->Send (aMessageString, Message_Warning);
123         aValue = 0;
124       }
125       anIntList->Append(aValue);
126     }
127   }
128
129   return Standard_True;
130 }
131
132 //=======================================================================
133 //function : Paste
134 //purpose  : transient -> persistent (store)
135 //=======================================================================
136 void XmlMDataStd_IntegerListDriver::Paste(const Handle(TDF_Attribute)& theSource,
137                                           XmlObjMgt_Persistent&        theTarget,
138                                           XmlObjMgt_SRelocationTable&  ) const
139 {
140   const Handle(TDataStd_IntegerList) anIntList = Handle(TDataStd_IntegerList)::DownCast(theSource);
141
142   Standard_Integer anU = anIntList->Extent();
143   theTarget.Element().setAttribute(::LastIndexString(), anU);
144   NCollection_LocalArray<Standard_Character> str(12 * anU + 1);
145   if(anU == 0)
146     str[0] = 0;
147   else if (anU >= 1)
148   {
149     // Allocation of 12 chars for each integer including the space.
150     // An example: -2 147 483 648
151     Standard_Integer iChar = 0;
152     TColStd_ListIteratorOfListOfInteger itr(anIntList->List());
153     for (; itr.More(); itr.Next())
154     {
155       const Standard_Integer& intValue = itr.Value();
156       iChar += Sprintf(&(str[iChar]), "%d ", intValue);
157     }  
158   }
159   // No occurrence of '&', '<' and other irregular XML characters
160   XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True);
161
162   if(anIntList->ID() != TDataStd_IntegerList::GetID()) {
163     //convert GUID
164     Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
165     Standard_PCharacter pGuidStr = aGuidStr;
166     anIntList->ID().ToCString (pGuidStr);
167     theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);
168   }
169 }