1 // Created on: 2007-05-29
2 // Created by: Vlad Romashko
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 <Message_Messenger.hxx>
18 #include <NCollection_LocalArray.hxx>
19 #include <Standard_Type.hxx>
20 #include <TDataStd_BooleanList.hxx>
21 #include <TDataStd_ListIteratorOfListOfByte.hxx>
22 #include <TDF_Attribute.hxx>
23 #include <XmlMDataStd_BooleanListDriver.hxx>
24 #include <XmlObjMgt.hxx>
25 #include <XmlObjMgt_Persistent.hxx>
27 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_BooleanListDriver,XmlMDF_ADriver)
28 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
29 IMPLEMENT_DOMSTRING (LastIndexString, "last")
30 IMPLEMENT_DOMSTRING (AttributeIDString, "boollistattguid")
32 //=======================================================================
33 //function : XmlMDataStd_BooleanListDriver
34 //purpose : Constructor
35 //=======================================================================
36 XmlMDataStd_BooleanListDriver::XmlMDataStd_BooleanListDriver(const Handle(Message_Messenger)& theMsgDriver)
37 : XmlMDF_ADriver (theMsgDriver, NULL)
42 //=======================================================================
45 //=======================================================================
46 Handle(TDF_Attribute) XmlMDataStd_BooleanListDriver::NewEmpty() const
48 return new TDataStd_BooleanList();
51 //=======================================================================
53 //purpose : persistent -> transient (retrieve)
54 //=======================================================================
55 Standard_Boolean XmlMDataStd_BooleanListDriver::Paste(const XmlObjMgt_Persistent& theSource,
56 const Handle(TDF_Attribute)& theTarget,
57 XmlObjMgt_RRelocationTable& ) const
59 Standard_Integer aFirstInd, aLastInd, aValue, ind;
60 const XmlObjMgt_Element& anElement = theSource;
62 // Read the FirstIndex; if the attribute is absent initialize to 1
63 XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
64 if (aFirstIndex == NULL)
66 else if (!aFirstIndex.GetInteger(aFirstInd))
68 TCollection_ExtendedString aMessageString =
69 TCollection_ExtendedString("Cannot retrieve the first index"
70 " for BooleanList attribute as \"")
72 myMessageDriver->Send (aMessageString, Message_Fail);
73 return Standard_False;
76 // Read the LastIndex; the attribute should be present
77 if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd))
79 TCollection_ExtendedString aMessageString =
80 TCollection_ExtendedString("Cannot retrieve the last index"
81 " for BooleanList attribute as \"")
83 myMessageDriver->Send (aMessageString, Message_Fail);
84 return Standard_False;
87 const Handle(TDataStd_BooleanList) aBooleanList = Handle(TDataStd_BooleanList)::DownCast(theTarget);
88 if(aLastInd == 0) aFirstInd = 0;
89 if (aFirstInd == aLastInd && aLastInd > 0)
91 Standard_Integer anInteger;
92 if (!XmlObjMgt::GetStringValue(anElement).GetInteger(anInteger))
94 TCollection_ExtendedString aMessageString =
95 TCollection_ExtendedString("Cannot retrieve integer member"
96 " for BooleanList attribute as \"");
97 myMessageDriver->Send (aMessageString, Message_Fail);
98 return Standard_False;
100 aBooleanList->Append(anInteger ? Standard_True : Standard_False);
102 else if(aLastInd >= 1)
104 Standard_CString aValueStr = Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
105 for (ind = aFirstInd; ind <= aLastInd; ind++)
107 if (!XmlObjMgt::GetInteger(aValueStr, aValue))
109 TCollection_ExtendedString aMessageString =
110 TCollection_ExtendedString("Cannot retrieve integer member"
111 " for BooleanList attribute as \"")
113 myMessageDriver->Send (aMessageString, Message_Fail);
114 return Standard_False;
116 aBooleanList->Append(aValue ? Standard_True : Standard_False);
122 XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
123 if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
124 aGUID = TDataStd_BooleanList::GetID(); //default case
126 aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
128 aBooleanList->SetID(aGUID);
130 return Standard_True;
133 //=======================================================================
135 //purpose : transient -> persistent (store)
136 //=======================================================================
137 void XmlMDataStd_BooleanListDriver::Paste(const Handle(TDF_Attribute)& theSource,
138 XmlObjMgt_Persistent& theTarget,
139 XmlObjMgt_SRelocationTable& ) const
141 const Handle(TDataStd_BooleanList) aBooleanList = Handle(TDataStd_BooleanList)::DownCast(theSource);
143 Standard_Integer anU = aBooleanList->Extent();
144 theTarget.Element().setAttribute(::LastIndexString(), anU);
145 // Allocation of 1 char for each boolean value + a space.
146 NCollection_LocalArray<Standard_Character> str(2 * anU + 1);
147 if(anU == 0) str[0] = 0;
150 Standard_Integer iChar(0);
151 TDataStd_ListIteratorOfListOfByte itr(aBooleanList->List());
152 for (; itr.More(); itr.Next())
154 const Standard_Byte& byte = itr.Value();
155 iChar += Sprintf(&(str[iChar]), "%d ", byte);
158 XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True);
160 if(aBooleanList->ID() != TDataStd_BooleanList::GetID()) {
162 Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
163 Standard_PCharacter pGuidStr = aGuidStr;
164 aBooleanList->ID().ToCString (pGuidStr);
165 theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);