0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / XmlMDataStd / XmlMDataStd_RealListDriver.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 <CDM_MessageDriver.hxx>
18 #include <NCollection_LocalArray.hxx>
19 #include <Standard_Type.hxx>
20 #include <TColStd_ListIteratorOfListOfReal.hxx>
21 #include <TDataStd_RealList.hxx>
22 #include <TDF_Attribute.hxx>
23 #include <XmlMDataStd_RealListDriver.hxx>
24 #include <XmlObjMgt.hxx>
25 #include <XmlObjMgt_Persistent.hxx>
26
27 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_RealListDriver,XmlMDF_ADriver)
28 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
29 IMPLEMENT_DOMSTRING (LastIndexString,  "last")
30 IMPLEMENT_DOMSTRING (AttributeIDString, "reallistattguid")
31 //=======================================================================
32 //function : XmlMDataStd_RealListDriver
33 //purpose  : Constructor
34 //=======================================================================
35 XmlMDataStd_RealListDriver::XmlMDataStd_RealListDriver(const Handle(CDM_MessageDriver)& theMsgDriver)
36      : XmlMDF_ADriver (theMsgDriver, NULL)
37 {
38
39 }
40
41 //=======================================================================
42 //function : NewEmpty
43 //purpose  : 
44 //=======================================================================
45 Handle(TDF_Attribute) XmlMDataStd_RealListDriver::NewEmpty() const
46 {
47   return new TDataStd_RealList();
48 }
49
50 //=======================================================================
51 //function : Paste
52 //purpose  : persistent -> transient (retrieve)
53 //=======================================================================
54 Standard_Boolean XmlMDataStd_RealListDriver::Paste(const XmlObjMgt_Persistent&  theSource,
55                                                    const Handle(TDF_Attribute)& theTarget,
56                                                    XmlObjMgt_RRelocationTable&  ) const
57 {
58   Standard_Real aValue;
59   Standard_Integer aFirstInd, aLastInd, 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 RealList attribute as \"")
71         + aFirstIndex + "\"";
72     WriteMessage (aMessageString);
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 RealList attribute as \"")
82         + aFirstIndex + "\"";
83     WriteMessage (aMessageString);
84     return Standard_False;
85   }
86
87   const Handle(TDataStd_RealList) aRealList = Handle(TDataStd_RealList)::DownCast(theTarget);
88   // Check the type of LDOMString
89   const XmlObjMgt_DOMString& aString = XmlObjMgt::GetStringValue(anElement);
90   if(aLastInd == 0) aFirstInd = 0;
91   if (aString.Type() == LDOMBasicString::LDOM_Integer) 
92   {
93     if (aFirstInd == aLastInd  && aLastInd > 0) 
94     {
95       Standard_Integer anIntValue;
96       if (aString.GetInteger(anIntValue))
97         aRealList->Append(Standard_Real(anIntValue));
98     } 
99     else 
100     {
101       TCollection_ExtendedString aMessageString =
102         TCollection_ExtendedString("Cannot retrieve array of real members"
103                                    " for RealList attribute from Integer \"")
104         + aString + "\"";
105       WriteMessage (aMessageString);
106       return Standard_False;
107     }
108   } 
109   else if(aLastInd >= 1)
110   {
111     Standard_CString aValueStr = Standard_CString(aString.GetString());
112     for (ind = aFirstInd; ind <= aLastInd; ind++)
113     {
114       if (!XmlObjMgt::GetReal(aValueStr, aValue)) {
115         TCollection_ExtendedString aMessageString =
116           TCollection_ExtendedString("Cannot retrieve real member"
117                                      " for RealList attribute as \"")
118             + aValueStr + "\"";
119         WriteMessage (aMessageString);
120         return Standard_False;
121       }
122       aRealList->Append(aValue);
123     }
124   }
125
126   // attribute id
127   Standard_GUID aGUID;
128   XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
129   if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
130     aGUID = TDataStd_RealList::GetID(); //default case
131   else
132     aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
133
134   aRealList->SetID(aGUID);
135
136   return Standard_True;
137 }
138
139 //=======================================================================
140 //function : Paste
141 //purpose  : transient -> persistent (store)
142 //=======================================================================
143 void XmlMDataStd_RealListDriver::Paste(const Handle(TDF_Attribute)& theSource,
144                                        XmlObjMgt_Persistent&        theTarget,
145                                        XmlObjMgt_SRelocationTable&  ) const
146 {
147   const Handle(TDataStd_RealList) aRealList = Handle(TDataStd_RealList)::DownCast(theSource);
148
149   Standard_Integer anU = aRealList->Extent();
150   theTarget.Element().setAttribute(::LastIndexString(), anU);
151   // Allocation of 25 chars for each double value including the space:
152   // An example: -3.1512678732195273e+020
153   NCollection_LocalArray<Standard_Character> str(25 * anU + 1);
154   if(anU == 0) str[0] = 0;
155   else if (anU >= 1)
156   {   
157     Standard_Integer iChar = 0;
158     TColStd_ListIteratorOfListOfReal itr(aRealList->List());
159     for (; itr.More(); itr.Next())
160     {
161       const Standard_Real& realValue = itr.Value();
162       iChar += Sprintf(&(str[iChar]), "%.17g ", realValue);
163     }
164   }
165   XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True);
166
167   if(aRealList->ID() != TDataStd_RealList::GetID()) {
168     //convert GUID
169     Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
170     Standard_PCharacter pGuidStr = aGuidStr;
171     aRealList->ID().ToCString (pGuidStr);
172     theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);
173   }
174 }