summaryrefslogtreecommitdiff
path: root/src/XmlMDF/XmlMDF.cxx
blob: d3b5f71a1660609ad471e2598adbf2cc7705ad65 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// File:      XmlMDF.cxx
// Created:   Mon Jul  9 12:29:49 MSK DST 2001
// Author:    Julia DOROVSKIKH
// Copyright: Matra Datavision 2001

#include <XmlMDF.ixx>
#include <XmlMDF_ADriver.hxx>
#include <XmlMDF_TagSourceDriver.hxx>
#include <XmlMDF_ReferenceDriver.hxx>
#include <XmlMDF_DataMapIteratorOfTypeADriverMap.hxx>

#include <XmlObjMgt_Persistent.hxx>
#include <XmlObjMgt_DOMString.hxx>
#include <XmlObjMgt_Document.hxx>

#include <TDF_TagSource.hxx>
#include <TDF_Label.hxx>
#include <TDF_Tool.hxx>
#include <TDF_AttributeIterator.hxx>
#include <TDF_ChildIterator.hxx>
#include <TColStd_MapOfTransient.hxx>
#include <Storage_Schema.hxx>

IMPLEMENT_DOMSTRING (TagString,         "tag")
IMPLEMENT_DOMSTRING (LabelString,       "label")
#define DATATYPE_MIGRATION
//#define DATATYPE_MIGRATION_DEB
//=======================================================================
//function : UnsuppTypesMap
//purpose  : 
//=======================================================================

static TColStd_MapOfTransient& UnsuppTypesMap ()
{
  static TColStd_MapOfTransient anUnsuppTypes;
  return anUnsuppTypes;
}

//=======================================================================
//function : FromTo
//purpose  : Paste transient data into DOM_Element
//=======================================================================
void XmlMDF::FromTo (const Handle(TDF_Data)&             theData,
                     XmlObjMgt_Element&                  theElement,
                     XmlObjMgt_SRelocationTable&         theRelocTable,
                     const Handle(XmlMDF_ADriverTable)&  theDrivers)
{
  UnsuppTypesMap().Clear();
//  Standard_Integer count =
  WriteSubTree(theData->Root(), theElement, theRelocTable, theDrivers);
  UnsuppTypesMap().Clear();
}

//=======================================================================
//function : WriteSubTree
//purpose  : 
//=======================================================================
Standard_Integer XmlMDF::WriteSubTree
                      (const TDF_Label&                    theLabel,
                       XmlObjMgt_Element&                  theElement,
                       XmlObjMgt_SRelocationTable&         theRelocTable,
                       const Handle(XmlMDF_ADriverTable)&  theDrivers)
{
  XmlObjMgt_Document aDoc = theElement.getOwnerDocument();

  // create element "label"
  XmlObjMgt_Element aLabElem = aDoc.createElement (::LabelString());

  // Extraction of the driver subset.
  const XmlMDF_TypeADriverMap& aDriverMap = theDrivers->GetDrivers();

  // write attributes
  Standard_Integer count = 0;
  TDF_AttributeIterator itr1 (theLabel);
  for ( ; itr1.More(); itr1.Next())
  {
    const Handle(TDF_Attribute)& tAtt = itr1.Value();
    const Handle(Standard_Type)& aType = tAtt->DynamicType();
    if (aDriverMap.IsBound (aType))
    {
      const Handle(XmlMDF_ADriver)& aDriver = aDriverMap.Find (aType);
      count++;

      //    Add source to relocation table
      Standard_Integer anId      = theRelocTable.Add (tAtt);

      //    Create DOM data item
      XmlObjMgt_Persistent pAtt;
      pAtt.CreateElement (aLabElem, aDriver->TypeName().ToCString(), anId);

      //    Paste
      aDriver -> Paste (tAtt, pAtt, theRelocTable);
    }
#ifdef DEB
    else if (!UnsuppTypesMap().Contains (aType))
    {
      cout << "attribute driver for type "<< aType -> Name()<< " not found"<< endl;
      UnsuppTypesMap().Add (aType);
    }
#endif
  }

  // write sub-labels
  TDF_ChildIterator itr2 (theLabel);
  for ( ; itr2.More(); itr2.Next())
  {
    const TDF_Label& aChildLab = itr2.Value();
    count += WriteSubTree(aChildLab, aLabElem, theRelocTable, theDrivers);
  }

  if (count > 0)
  {
    theElement.appendChild(aLabElem);

    // set attribute "tag"
    aLabElem.setAttribute (::TagString(), theLabel.Tag());
  }

  return count;
}

//=======================================================================
//function : FromTo
//purpose  : Paste data from DOM_Element into transient document
//=======================================================================
Standard_Boolean XmlMDF::FromTo (const XmlObjMgt_Element&         theElement,
                                 Handle(TDF_Data)&                theData,
                                 XmlObjMgt_RRelocationTable&      theRelocTable,
                                 const Handle(XmlMDF_ADriverTable)& theDrivers)
{
  TDF_Label aRootLab = theData->Root();
  XmlMDF_MapOfDriver aDriverMap;
  CreateDrvMap (theDrivers, aDriverMap);

  Standard_Integer count = 0;

  LDOM_Node theNode = theElement.getFirstChild();
  XmlObjMgt_Element anElem = (const XmlObjMgt_Element&)theNode;
  while ( !anElem.isNull() )
  {
    if ( anElem.getNodeName().equals (::LabelString()) )
    {
      Standard_Integer subcount =
        ReadSubTree(anElem, aRootLab, theRelocTable, aDriverMap);
      // check for error
      if (subcount < 0)
        return Standard_False;
      count += subcount;
    }
    //anElem = (const XmlObjMgt_Element &) anElem.getNextSibling();
    LDOM_Node theNode1 = anElem.getNextSibling();
    anElem = (const XmlObjMgt_Element &) theNode1;
  }

  return Standard_True;
}

//=======================================================================
//function : ReadSubTree
//purpose  : 
//=======================================================================
Standard_Integer XmlMDF::ReadSubTree (const XmlObjMgt_Element&    theElement,
                                      const TDF_Label&            theLabel,
                                      XmlObjMgt_RRelocationTable& theRelocTable,
                                      const XmlMDF_MapOfDriver&   theDriverMap)
{
  // Extraction of the driver subset.
  Standard_Integer count = 0;

  //XmlObjMgt_Element anElem = (const XmlObjMgt_Element &) theElement.getFirstChild();
  LDOM_Node theNode = theElement.getFirstChild();
  XmlObjMgt_Element anElem = (const XmlObjMgt_Element &) theNode;
  while ( !anElem.isNull() )
  {
    if ( anElem.getNodeType() == LDOM_Node::ELEMENT_NODE )
    {
      if ( anElem.getNodeName().equals (::LabelString()) )
      {
        // read tag
        Standard_Integer tag;
        XmlObjMgt_DOMString aTag (anElem.getAttribute(::TagString()));
        if ( !aTag.GetInteger (tag) ) {
          TCollection_ExtendedString anErrorMessage =
            TCollection_ExtendedString ("Wrong Tag value for OCAF Label: ")
              + aTag;
          theDriverMap.Find("TDF_TagSource") -> WriteMessage (anErrorMessage);
          return -1;
        }
        // create label
        TDF_Label aLab = theLabel.FindChild(tag, Standard_True);

        // read sub-tree
        Standard_Integer subcount =
          ReadSubTree(anElem, aLab, theRelocTable, theDriverMap);
        // check for error
        if (subcount == -1)
          return -1;
        count += subcount;
      }
      else
      {
        // read attribute
        XmlObjMgt_DOMString aName = anElem.getNodeName();

#ifdef DATATYPE_MIGRATION
	TCollection_AsciiString  newName;	
	if(Storage_Schema::CheckTypeMigration(aName, newName)) {
#ifdef DATATYPE_MIGRATION_DEB
	  cout << "CheckTypeMigration:OldType = " <<aName.GetString() << " Len = "<<strlen(aName.GetString())<<endl;
	  cout << "CheckTypeMigration:NewType = " <<newName  << " Len = "<< newName.Length()<<endl;
#endif
	  aName = newName.ToCString();
	}
#endif  
       
        if (theDriverMap.IsBound (aName))
        {
          count++;
          const Handle(XmlMDF_ADriver)& driver = theDriverMap.Find(aName);
          XmlObjMgt_Persistent pAtt (anElem);
          Standard_Integer anID = pAtt.Id ();
          if (anID <= 0) {      // check for ID validity
            TCollection_ExtendedString anErrorMessage =
             TCollection_ExtendedString("Wrong ID of OCAF attribute with type ")
               + aName;
            driver -> WriteMessage (anErrorMessage);
            return -1;
          }
          Handle(TDF_Attribute) tAtt;
          Standard_Boolean isBound = theRelocTable.IsBound(anID);
          if (isBound)
            tAtt = Handle(TDF_Attribute)::DownCast(theRelocTable.Find(anID));
          else
            tAtt = driver -> NewEmpty();
	  if (tAtt->Label().IsNull())
	    theLabel.AddAttribute (tAtt);
	  else
	    driver->WriteMessage
	      (TCollection_ExtendedString("XmlDriver warning: ") +
	       "attempt to attach attribute " +
	       aName + " to a second label");

          if (! driver -> Paste (pAtt, tAtt, theRelocTable))
          {
            // error converting persistent to transient
            driver->WriteMessage
              (TCollection_ExtendedString("XmlDriver warning: ") +
               "failure reading attribute " + aName);
          }
          else if (isBound == Standard_False)
            theRelocTable.Bind (anID, tAtt);
        }
#ifdef DEB
        else
        {
          const TCollection_AsciiString anAsciiName = aName;
          cerr << "XmlDriver warning: "
               << "label contains object of unknown type "<< anAsciiName<< endl;
        }
#endif
      }
    }
    //anElem = (const XmlObjMgt_Element &) anElem.getNextSibling();
    LDOM_Node theNode1 = anElem.getNextSibling();
    anElem = (const XmlObjMgt_Element &) theNode1;
  }

  // AfterRetrieval
  if (count > 0)
  {
  }

  return count;
}

//=======================================================================
//function : AddDrivers
//purpose  : 
//=======================================================================
void XmlMDF::AddDrivers (const Handle(XmlMDF_ADriverTable)& aDriverTable,
                         const Handle(CDM_MessageDriver)&   aMessageDriver)
{
  aDriverTable->AddDriver (new XmlMDF_TagSourceDriver(aMessageDriver)); 
  aDriverTable->AddDriver (new XmlMDF_ReferenceDriver(aMessageDriver));
}

//=======================================================================
//function : CreateDrvMap
//purpose  : 
//=======================================================================

void XmlMDF::CreateDrvMap (const Handle(XmlMDF_ADriverTable)& theDrivers,
                           XmlMDF_MapOfDriver&                theAsciiDriverMap)
{
  const XmlMDF_TypeADriverMap& aDriverMap = theDrivers->GetDrivers();
  XmlMDF_DataMapIteratorOfTypeADriverMap anIter (aDriverMap);
  while (anIter.More()) {
    const Handle(XmlMDF_ADriver)& aDriver = anIter.Value();
    const TCollection_AsciiString aTypeName = aDriver -> TypeName();
    if (theAsciiDriverMap.IsBound (aTypeName) == Standard_False)
      theAsciiDriverMap.Bind (aTypeName, aDriver);
    else
      aDriver -> WriteMessage
        (TCollection_ExtendedString ("Warning: skipped driver name: \"")
         + aTypeName + '\"');
    anIter.Next();
  }
}