1 // Created on: 2001-07-09
2 // Created by: Julia DOROVSKIKH
3 // Copyright (c) 2001-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 <XmlMDF_ADriver.hxx>
18 #include <XmlMDF_TagSourceDriver.hxx>
19 #include <XmlMDF_ReferenceDriver.hxx>
20 #include <XmlMDF_DataMapIteratorOfTypeADriverMap.hxx>
22 #include <XmlObjMgt_Persistent.hxx>
23 #include <XmlObjMgt_DOMString.hxx>
24 #include <XmlObjMgt_Document.hxx>
26 #include <TDF_TagSource.hxx>
27 #include <TDF_Label.hxx>
28 #include <TDF_Tool.hxx>
29 #include <TDF_AttributeIterator.hxx>
30 #include <TDF_ChildIterator.hxx>
31 #include <TColStd_MapOfTransient.hxx>
32 #include <Storage_Schema.hxx>
34 IMPLEMENT_DOMSTRING (TagString, "tag")
35 IMPLEMENT_DOMSTRING (LabelString, "label")
36 #define DATATYPE_MIGRATION
37 //#define DATATYPE_MIGRATION_DEB
38 //=======================================================================
39 //function : UnsuppTypesMap
41 //=======================================================================
43 static TColStd_MapOfTransient& UnsuppTypesMap ()
45 static TColStd_MapOfTransient anUnsuppTypes;
49 //=======================================================================
51 //purpose : Paste transient data into DOM_Element
52 //=======================================================================
53 void XmlMDF::FromTo (const Handle(TDF_Data)& theData,
54 XmlObjMgt_Element& theElement,
55 XmlObjMgt_SRelocationTable& theRelocTable,
56 const Handle(XmlMDF_ADriverTable)& theDrivers)
58 UnsuppTypesMap().Clear();
59 // Standard_Integer count =
60 WriteSubTree(theData->Root(), theElement, theRelocTable, theDrivers);
61 UnsuppTypesMap().Clear();
64 //=======================================================================
65 //function : WriteSubTree
67 //=======================================================================
68 Standard_Integer XmlMDF::WriteSubTree
69 (const TDF_Label& theLabel,
70 XmlObjMgt_Element& theElement,
71 XmlObjMgt_SRelocationTable& theRelocTable,
72 const Handle(XmlMDF_ADriverTable)& theDrivers)
74 XmlObjMgt_Document aDoc = theElement.getOwnerDocument();
76 // create element "label"
77 XmlObjMgt_Element aLabElem = aDoc.createElement (::LabelString());
79 // Extraction of the driver subset.
80 const XmlMDF_TypeADriverMap& aDriverMap = theDrivers->GetDrivers();
83 Standard_Integer count = 0;
84 TDF_AttributeIterator itr1 (theLabel);
85 for ( ; itr1.More(); itr1.Next())
87 const Handle(TDF_Attribute)& tAtt = itr1.Value();
88 const Handle(Standard_Type)& aType = tAtt->DynamicType();
89 if (aDriverMap.IsBound (aType))
91 const Handle(XmlMDF_ADriver)& aDriver = aDriverMap.Find (aType);
94 // Add source to relocation table
95 Standard_Integer anId = theRelocTable.Add (tAtt);
97 // Create DOM data item
98 XmlObjMgt_Persistent pAtt;
99 pAtt.CreateElement (aLabElem, aDriver->TypeName().ToCString(), anId);
102 aDriver -> Paste (tAtt, pAtt, theRelocTable);
105 else if (!UnsuppTypesMap().Contains (aType))
107 cout << "attribute driver for type "<< aType -> Name()<< " not found"<< endl;
108 UnsuppTypesMap().Add (aType);
114 TDF_ChildIterator itr2 (theLabel);
115 for ( ; itr2.More(); itr2.Next())
117 const TDF_Label& aChildLab = itr2.Value();
118 count += WriteSubTree(aChildLab, aLabElem, theRelocTable, theDrivers);
123 theElement.appendChild(aLabElem);
125 // set attribute "tag"
126 aLabElem.setAttribute (::TagString(), theLabel.Tag());
132 //=======================================================================
134 //purpose : Paste data from DOM_Element into transient document
135 //=======================================================================
136 Standard_Boolean XmlMDF::FromTo (const XmlObjMgt_Element& theElement,
137 Handle(TDF_Data)& theData,
138 XmlObjMgt_RRelocationTable& theRelocTable,
139 const Handle(XmlMDF_ADriverTable)& theDrivers)
141 TDF_Label aRootLab = theData->Root();
142 XmlMDF_MapOfDriver aDriverMap;
143 CreateDrvMap (theDrivers, aDriverMap);
145 Standard_Integer count = 0;
147 LDOM_Node theNode = theElement.getFirstChild();
148 XmlObjMgt_Element anElem = (const XmlObjMgt_Element&)theNode;
149 while ( !anElem.isNull() )
151 if ( anElem.getNodeName().equals (::LabelString()) )
153 Standard_Integer subcount =
154 ReadSubTree(anElem, aRootLab, theRelocTable, aDriverMap);
157 return Standard_False;
160 //anElem = (const XmlObjMgt_Element &) anElem.getNextSibling();
161 LDOM_Node theNode1 = anElem.getNextSibling();
162 anElem = (const XmlObjMgt_Element &) theNode1;
165 return Standard_True;
168 //=======================================================================
169 //function : ReadSubTree
171 //=======================================================================
172 Standard_Integer XmlMDF::ReadSubTree (const XmlObjMgt_Element& theElement,
173 const TDF_Label& theLabel,
174 XmlObjMgt_RRelocationTable& theRelocTable,
175 const XmlMDF_MapOfDriver& theDriverMap)
177 // Extraction of the driver subset.
178 Standard_Integer count = 0;
180 //XmlObjMgt_Element anElem = (const XmlObjMgt_Element &) theElement.getFirstChild();
181 LDOM_Node theNode = theElement.getFirstChild();
182 XmlObjMgt_Element anElem = (const XmlObjMgt_Element &) theNode;
183 while ( !anElem.isNull() )
185 if ( anElem.getNodeType() == LDOM_Node::ELEMENT_NODE )
187 if ( anElem.getNodeName().equals (::LabelString()) )
190 Standard_Integer tag;
191 XmlObjMgt_DOMString aTag (anElem.getAttribute(::TagString()));
192 if ( !aTag.GetInteger (tag) ) {
193 TCollection_ExtendedString anErrorMessage =
194 TCollection_ExtendedString ("Wrong Tag value for OCAF Label: ")
196 theDriverMap.Find("TDF_TagSource") -> WriteMessage (anErrorMessage);
200 TDF_Label aLab = theLabel.FindChild(tag, Standard_True);
203 Standard_Integer subcount =
204 ReadSubTree(anElem, aLab, theRelocTable, theDriverMap);
213 XmlObjMgt_DOMString aName = anElem.getNodeName();
215 #ifdef DATATYPE_MIGRATION
216 TCollection_AsciiString newName;
217 if(Storage_Schema::CheckTypeMigration(aName, newName)) {
218 #ifdef DATATYPE_MIGRATION_DEB
219 cout << "CheckTypeMigration:OldType = " <<aName.GetString() << " Len = "<<strlen(aName.GetString())<<endl;
220 cout << "CheckTypeMigration:NewType = " <<newName << " Len = "<< newName.Length()<<endl;
222 aName = newName.ToCString();
226 if (theDriverMap.IsBound (aName))
229 const Handle(XmlMDF_ADriver)& driver = theDriverMap.Find(aName);
230 XmlObjMgt_Persistent pAtt (anElem);
231 Standard_Integer anID = pAtt.Id ();
232 if (anID <= 0) { // check for ID validity
233 TCollection_ExtendedString anErrorMessage =
234 TCollection_ExtendedString("Wrong ID of OCAF attribute with type ")
236 driver -> WriteMessage (anErrorMessage);
239 Handle(TDF_Attribute) tAtt;
240 Standard_Boolean isBound = theRelocTable.IsBound(anID);
242 tAtt = Handle(TDF_Attribute)::DownCast(theRelocTable.Find(anID));
244 tAtt = driver -> NewEmpty();
245 if (tAtt->Label().IsNull())
246 theLabel.AddAttribute (tAtt);
249 (TCollection_ExtendedString("XmlDriver warning: ") +
250 "attempt to attach attribute " +
251 aName + " to a second label");
253 if (! driver -> Paste (pAtt, tAtt, theRelocTable))
255 // error converting persistent to transient
257 (TCollection_ExtendedString("XmlDriver warning: ") +
258 "failure reading attribute " + aName);
260 else if (isBound == Standard_False)
261 theRelocTable.Bind (anID, tAtt);
263 #ifdef XMLMDATASTD_DEB
266 const TCollection_AsciiString anAsciiName = aName;
267 cerr << "XmlDriver warning: "
268 << "label contains object of unknown type "<< anAsciiName<< endl;
273 //anElem = (const XmlObjMgt_Element &) anElem.getNextSibling();
274 LDOM_Node theNode1 = anElem.getNextSibling();
275 anElem = (const XmlObjMgt_Element &) theNode1;
286 //=======================================================================
287 //function : AddDrivers
289 //=======================================================================
290 void XmlMDF::AddDrivers (const Handle(XmlMDF_ADriverTable)& aDriverTable,
291 const Handle(CDM_MessageDriver)& aMessageDriver)
293 aDriverTable->AddDriver (new XmlMDF_TagSourceDriver(aMessageDriver));
294 aDriverTable->AddDriver (new XmlMDF_ReferenceDriver(aMessageDriver));
297 //=======================================================================
298 //function : CreateDrvMap
300 //=======================================================================
302 void XmlMDF::CreateDrvMap (const Handle(XmlMDF_ADriverTable)& theDrivers,
303 XmlMDF_MapOfDriver& theAsciiDriverMap)
305 const XmlMDF_TypeADriverMap& aDriverMap = theDrivers->GetDrivers();
306 XmlMDF_DataMapIteratorOfTypeADriverMap anIter (aDriverMap);
307 while (anIter.More()) {
308 const Handle(XmlMDF_ADriver)& aDriver = anIter.Value();
309 const TCollection_AsciiString aTypeName = aDriver -> TypeName();
310 if (theAsciiDriverMap.IsBound (aTypeName) == Standard_False)
311 theAsciiDriverMap.Bind (aTypeName, aDriver);
313 aDriver -> WriteMessage
314 (TCollection_ExtendedString ("Warning: skipped driver name: \"")