summaryrefslogtreecommitdiff
path: root/src/XmlTObjDrivers/XmlTObjDrivers_IntSparseArrayDriver.cxx
blob: 4889f9f46b8b91db84280c222d642c08e7c5d089 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// File      : XmlTObjDrivers_IntSparseArrayDriver.cxx
// Created   : 30.03.2007
// Author    : Michael SAZONOV
// Copyright:   Open CASCADE  2007
// The original implementation Copyright: (C) RINA S.p.A

#include <XmlTObjDrivers_IntSparseArrayDriver.hxx>
#include <CDM_MessageDriver.hxx>
#include <TDF_Attribute.hxx>
#include <XmlObjMgt_Persistent.hxx>
#include <TObj_TIntSparseArray.hxx>
#include <TObj_Assistant.hxx>
#include <TCollection_AsciiString.hxx>
#include <TDF_Tool.hxx>

IMPLEMENT_STANDARD_HANDLE(XmlTObjDrivers_IntSparseArrayDriver,XmlMDF_ADriver)
IMPLEMENT_STANDARD_RTTIEXT(XmlTObjDrivers_IntSparseArrayDriver,XmlMDF_ADriver)

#define ITEM_ID "itemId_"
#define ITEM_VALUE "itemValue_"

//=======================================================================
//function : XmlTObjDrivers_IntSparseArrayDriver
//purpose  : constructor
//=======================================================================

XmlTObjDrivers_IntSparseArrayDriver::XmlTObjDrivers_IntSparseArrayDriver
                         (const Handle(CDM_MessageDriver)& theMessageDriver)
: XmlMDF_ADriver( theMessageDriver, NULL)
{
}

//=======================================================================
//function : NewEmpty
//purpose  : Creates a new attribute
//=======================================================================

Handle(TDF_Attribute) XmlTObjDrivers_IntSparseArrayDriver::NewEmpty() const
{
  return new TObj_TIntSparseArray;
}

//=======================================================================
//function : Paste
//purpose  : Retrieve. Translate the contents of <theSource> and put it
//           into <theTarget>.
//=======================================================================

Standard_Boolean XmlTObjDrivers_IntSparseArrayDriver::Paste
                         (const XmlObjMgt_Persistent&  theSource,
                          const Handle(TDF_Attribute)& theTarget,
                          XmlObjMgt_RRelocationTable&) const
{
  const XmlObjMgt_Element& anElement = theSource;
  Handle(TObj_TIntSparseArray) aTarget =
    Handle(TObj_TIntSparseArray)::DownCast(theTarget);

  // get pairs (ID, value) while ID != 0
  Standard_Integer i = 1;
  TCollection_AsciiString anItemID;
  TCollection_AsciiString anIdStr = TCollection_AsciiString( ITEM_ID ) + 
    TCollection_AsciiString( i );
  anItemID = anElement.getAttribute( anIdStr.ToCString() );
  while( anItemID.IsIntegerValue() && anItemID.IntegerValue() != 0 )
  {
    TCollection_AsciiString aStrIndex = TCollection_AsciiString( ITEM_VALUE ) +
      TCollection_AsciiString( i );
    TCollection_AsciiString anItemValue = anElement.getAttribute( aStrIndex.ToCString() );
    if ( anItemValue.IsIntegerValue() )
    {
      // store the value in the target array
      aTarget->SetDoBackup (Standard_False);
      aTarget->SetValue (anItemID.IntegerValue(), anItemValue.IntegerValue());
      aTarget->SetDoBackup (Standard_True);
    }
    i++;
  }
  return Standard_True;
}

//=======================================================================
//function : Paste
//purpose  : Store. Translate the contents of <theSource> and put it
//           into <theTarget>
//=======================================================================

void XmlTObjDrivers_IntSparseArrayDriver::Paste
                         (const Handle(TDF_Attribute)& theSource,
                          XmlObjMgt_Persistent&        theTarget,
                          XmlObjMgt_SRelocationTable&) const
{
  Handle(TObj_TIntSparseArray) aSource =
    Handle(TObj_TIntSparseArray)::DownCast (theSource);

  // put only non-null values as pairs (ID, value)
  // terminate the list by ID=0
  TObj_TIntSparseArray::Iterator anIt = aSource->GetIterator();
  Standard_Integer i = 1;
  for ( ; anIt.More() ; anIt.Next() )
  {
    Standard_Integer aValue = anIt.Value();
    if (aValue == 0)
      continue;
    TCollection_AsciiString anIdStr = TCollection_AsciiString( ITEM_ID ) +
      TCollection_AsciiString( i );
    TCollection_AsciiString aStrIndex = TCollection_AsciiString( ITEM_VALUE ) +
      TCollection_AsciiString( i );
    theTarget.Element().setAttribute( anIdStr.ToCString(), anIt.Index() );
    theTarget.Element().setAttribute( aStrIndex.ToCString(), anIt.Value() );
    i++;
  }
  // write last item
  TCollection_AsciiString anIdStr = TCollection_AsciiString( ITEM_ID ) +
    TCollection_AsciiString( i );
  TCollection_AsciiString aStrIndex = TCollection_AsciiString( ITEM_VALUE ) +
    TCollection_AsciiString( i );
  theTarget.Element().setAttribute( anIdStr.ToCString(), 0 );
  theTarget.Element().setAttribute( aStrIndex.ToCString(), 0 );
}