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