0029220: Application Framework - replace CDM_MessageDriver interface by Message_Messe...
[occt.git] / src / XmlMDataStd / XmlMDataStd_IntegerListDriver.cxx
CommitLineData
b311480e 1// Created on: 2007-05-29
2// Created by: Vlad Romashko
973c2be1 3// Copyright (c) 2007-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
42cf5bc1 16
83ae3591 17#include <Message_Messenger.hxx>
f7b4312f 18#include <NCollection_LocalArray.hxx>
42cf5bc1 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>
7fd59977 24#include <XmlObjMgt.hxx>
42cf5bc1 25#include <XmlObjMgt_Persistent.hxx>
7fd59977 26
92efcf78 27IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_IntegerListDriver,XmlMDF_ADriver)
7fd59977 28IMPLEMENT_DOMSTRING (FirstIndexString, "first")
29IMPLEMENT_DOMSTRING (LastIndexString, "last")
5a1271c8 30IMPLEMENT_DOMSTRING (AttributeIDString, "intlistattguid")
7fd59977 31
32//=======================================================================
33//function : XmlMDataStd_IntegerListDriver
34//purpose : Constructor
35//=======================================================================
83ae3591 36XmlMDataStd_IntegerListDriver::XmlMDataStd_IntegerListDriver(const Handle(Message_Messenger)& theMsgDriver)
5a1271c8 37: XmlMDF_ADriver (theMsgDriver, NULL)
7fd59977 38{
39
40}
41
42//=======================================================================
43//function : NewEmpty
44//purpose :
45//=======================================================================
46Handle(TDF_Attribute) XmlMDataStd_IntegerListDriver::NewEmpty() const
47{
48 return new TDataStd_IntegerList();
49}
50
51//=======================================================================
52//function : Paste
53//purpose : persistent -> transient (retrieve)
54//=======================================================================
55Standard_Boolean XmlMDataStd_IntegerListDriver::Paste(const XmlObjMgt_Persistent& theSource,
5a1271c8 56 const Handle(TDF_Attribute)& theTarget,
57 XmlObjMgt_RRelocationTable& ) const
7fd59977 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 + "\"";
83ae3591 72 myMessageDriver->Send (aMessageString, Message_Fail);
7fd59977 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 + "\"";
83ae3591 83 myMessageDriver->Send (aMessageString, Message_Fail);
7fd59977 84 return Standard_False;
85 }
86
d585e74e 87 const Handle(TDataStd_IntegerList) anIntList = Handle(TDataStd_IntegerList)::DownCast(theTarget);
88 if(aLastInd == 0) aFirstInd = 0;
89 if (aFirstInd == aLastInd && aLastInd > 0)
7fd59977 90 {
91 Standard_Integer anInteger;
92 if (!XmlObjMgt::GetStringValue(anElement).GetInteger(anInteger))
93 {
94 TCollection_ExtendedString aMessageString =
95 TCollection_ExtendedString("Cannot retrieve integer member"
96 " for IntegerList attribute as \"");
83ae3591 97 myMessageDriver->Send (aMessageString, Message_Fail);
7fd59977 98 return Standard_False;
99 }
100 anIntList->Append(anInteger);
101 }
d585e74e 102 else if(aLastInd >= 1)
7fd59977 103 {
104 Standard_CString aValueStr = Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
105 for (ind = aFirstInd; ind <= aLastInd; ind++)
106 {
107 if (!XmlObjMgt::GetInteger(aValueStr, aValue))
108 {
109 TCollection_ExtendedString aMessageString =
110 TCollection_ExtendedString("Cannot retrieve integer member"
111 " for IntegerList attribute as \"")
112 + aValueStr + "\"";
83ae3591 113 myMessageDriver->Send (aMessageString, Message_Fail);
7fd59977 114 return Standard_False;
115 }
116 anIntList->Append(aValue);
117 }
118 }
5a1271c8 119
120 // attribute id
121 Standard_GUID aGUID;
122 XmlObjMgt_DOMString aGUIDStr = anElement.getAttribute(::AttributeIDString());
123 if (aGUIDStr.Type() == XmlObjMgt_DOMString::LDOM_NULL)
124 aGUID = TDataStd_IntegerList::GetID(); //default case
125 else
126 aGUID = Standard_GUID(Standard_CString(aGUIDStr.GetString())); // user defined case
127
128 anIntList->SetID(aGUID);
129
7fd59977 130 return Standard_True;
131}
132
133//=======================================================================
134//function : Paste
135//purpose : transient -> persistent (store)
136//=======================================================================
137void XmlMDataStd_IntegerListDriver::Paste(const Handle(TDF_Attribute)& theSource,
5a1271c8 138 XmlObjMgt_Persistent& theTarget,
139 XmlObjMgt_SRelocationTable& ) const
7fd59977 140{
d585e74e 141 const Handle(TDataStd_IntegerList) anIntList = Handle(TDataStd_IntegerList)::DownCast(theSource);
7fd59977 142
143 Standard_Integer anU = anIntList->Extent();
7fd59977 144 theTarget.Element().setAttribute(::LastIndexString(), anU);
d585e74e 145 NCollection_LocalArray<Standard_Character> str(12 * anU + 1);
146 if(anU == 0)
147 str[0] = 0;
148 else if (anU >= 1)
7fd59977 149 {
f7b4312f 150 // Allocation of 12 chars for each integer including the space.
151 // An example: -2 147 483 648
152 Standard_Integer iChar = 0;
7fd59977 153 TColStd_ListIteratorOfListOfInteger itr(anIntList->List());
154 for (; itr.More(); itr.Next())
155 {
f7b4312f 156 const Standard_Integer& intValue = itr.Value();
157 iChar += Sprintf(&(str[iChar]), "%d ", intValue);
d585e74e 158 }
7fd59977 159 }
d585e74e 160 // No occurrence of '&', '<' and other irregular XML characters
161 XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True);
5a1271c8 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 }
7fd59977 170}