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 <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>
27 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_IntegerListDriver,XmlMDF_ADriver)
28 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
29 IMPLEMENT_DOMSTRING (LastIndexString, "last")
30 IMPLEMENT_DOMSTRING (AttributeIDString, "intlistattguid")
32 //=======================================================================
33 //function : XmlMDataStd_IntegerListDriver
34 //purpose : Constructor
35 //=======================================================================
36 XmlMDataStd_IntegerListDriver::XmlMDataStd_IntegerListDriver(const Handle(Message_Messenger)& theMsgDriver)
37 : XmlMDF_ADriver (theMsgDriver, NULL)
42 //=======================================================================
45 //=======================================================================
46 Handle(TDF_Attribute) XmlMDataStd_IntegerListDriver::NewEmpty() const
48 return new TDataStd_IntegerList();
51 //=======================================================================
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
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 IntegerList 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 IntegerList attribute as \"")
83 myMessageDriver->Send (aMessageString, Message_Fail);
84 return Standard_False;
87 const Handle(TDataStd_IntegerList) anIntList = Handle(TDataStd_IntegerList)::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 IntegerList attribute as \"");
97 myMessageDriver->Send (aMessageString, Message_Fail);
98 return Standard_False;
100 anIntList->Append(anInteger);
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 IntegerList attribute as \"")
113 myMessageDriver->Send (aMessageString, Message_Fail);
114 return Standard_False;
116 anIntList->Append(aValue);
122 XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
123 if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
124 aGUID = TDataStd_IntegerList::GetID(); //default case
126 aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
128 anIntList->SetID(aGUID);
130 return Standard_True;
133 //=======================================================================
135 //purpose : transient -> persistent (store)
136 //=======================================================================
137 void XmlMDataStd_IntegerListDriver::Paste(const Handle(TDF_Attribute)& theSource,
138 XmlObjMgt_Persistent& theTarget,
139 XmlObjMgt_SRelocationTable& ) const
141 const Handle(TDataStd_IntegerList) anIntList = Handle(TDataStd_IntegerList)::DownCast(theSource);
143 Standard_Integer anU = anIntList->Extent();
144 theTarget.Element().setAttribute(::LastIndexString(), anU);
145 NCollection_LocalArray<Standard_Character> str(12 * anU + 1);
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())
156 const Standard_Integer& intValue = itr.Value();
157 iChar += Sprintf(&(str[iChar]), "%d ", intValue);
160 // No occurrence of '&', '<' and other irregular XML characters
161 XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True);
163 if(anIntList->ID() != TDataStd_IntegerList::GetID()) {
165 Standard_Character aGuidStr [Standard_GUID_SIZE_ALLOC];
166 Standard_PCharacter pGuidStr = aGuidStr;
167 anIntList->ID().ToCString (pGuidStr);
168 theTarget.Element().setAttribute (::AttributeIDString(), aGuidStr);