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>
33 #include <TDF_Attribute.hxx>
35 IMPLEMENT_DOMSTRING (TagString, "tag")
36 IMPLEMENT_DOMSTRING (LabelString, "label")
37 #define DATATYPE_MIGRATION
38 //#define DATATYPE_MIGRATION_DEB
39 //=======================================================================
40 //function : UnsuppTypesMap
42 //=======================================================================
44 static TColStd_MapOfTransient& UnsuppTypesMap ()
46 static TColStd_MapOfTransient anUnsuppTypes;
50 //=======================================================================
52 //purpose : Paste transient data into DOM_Element
53 //=======================================================================
54 void XmlMDF::FromTo (const Handle(TDF_Data)& theData,
55 XmlObjMgt_Element& theElement,
56 XmlObjMgt_SRelocationTable& theRelocTable,
57 const Handle(XmlMDF_ADriverTable)& theDrivers)
59 UnsuppTypesMap().Clear();
60 // Standard_Integer count =
61 WriteSubTree(theData->Root(), theElement, theRelocTable, theDrivers);
62 UnsuppTypesMap().Clear();
65 //=======================================================================
66 //function : WriteSubTree
68 //=======================================================================
69 Standard_Integer XmlMDF::WriteSubTree
70 (const TDF_Label& theLabel,
71 XmlObjMgt_Element& theElement,
72 XmlObjMgt_SRelocationTable& theRelocTable,
73 const Handle(XmlMDF_ADriverTable)& theDrivers)
75 XmlObjMgt_Document aDoc = theElement.getOwnerDocument();
77 // create element "label"
78 XmlObjMgt_Element aLabElem = aDoc.createElement (::LabelString());
80 // Extraction of the driver subset.
81 const XmlMDF_TypeADriverMap& aDriverMap = theDrivers->GetDrivers();
84 Standard_Integer count = 0;
85 TDF_AttributeIterator itr1 (theLabel);
86 for ( ; itr1.More(); itr1.Next())
88 const Handle(TDF_Attribute)& tAtt = itr1.Value();
89 const Handle(Standard_Type)& aType = tAtt->DynamicType();
90 if (aDriverMap.IsBound (aType))
92 const Handle(XmlMDF_ADriver)& aDriver = aDriverMap.Find (aType);
95 // Add source to relocation table
96 Standard_Integer anId = theRelocTable.Add (tAtt);
98 // Create DOM data item
99 XmlObjMgt_Persistent pAtt;
100 pAtt.CreateElement (aLabElem, aDriver->TypeName().ToCString(), anId);
103 aDriver -> Paste (tAtt, pAtt, theRelocTable);
106 else if (!UnsuppTypesMap().Contains (aType))
108 cout << "attribute driver for type "<< aType -> Name()<< " not found"<< endl;
109 UnsuppTypesMap().Add (aType);
115 TDF_ChildIterator itr2 (theLabel);
116 for ( ; itr2.More(); itr2.Next())
118 const TDF_Label& aChildLab = itr2.Value();
119 count += WriteSubTree(aChildLab, aLabElem, theRelocTable, theDrivers);
124 theElement.appendChild(aLabElem);
126 // set attribute "tag"
127 aLabElem.setAttribute (::TagString(), theLabel.Tag());
133 //=======================================================================
135 //purpose : Paste data from DOM_Element into transient document
136 //=======================================================================
137 Standard_Boolean XmlMDF::FromTo (const XmlObjMgt_Element& theElement,
138 Handle(TDF_Data)& theData,
139 XmlObjMgt_RRelocationTable& theRelocTable,
140 const Handle(XmlMDF_ADriverTable)& theDrivers)
142 TDF_Label aRootLab = theData->Root();
143 XmlMDF_MapOfDriver aDriverMap;
144 CreateDrvMap (theDrivers, aDriverMap);
146 Standard_Integer count = 0;
148 LDOM_Node theNode = theElement.getFirstChild();
149 XmlObjMgt_Element anElem = (const XmlObjMgt_Element&)theNode;
150 while ( !anElem.isNull() )
152 if ( anElem.getNodeName().equals (::LabelString()) )
154 Standard_Integer subcount =
155 ReadSubTree(anElem, aRootLab, theRelocTable, aDriverMap);
158 return Standard_False;
161 //anElem = (const XmlObjMgt_Element &) anElem.getNextSibling();
162 LDOM_Node theNode1 = anElem.getNextSibling();
163 anElem = (const XmlObjMgt_Element &) theNode1;
166 return Standard_True;
169 //=======================================================================
170 //function : ReadSubTree
172 //=======================================================================
173 Standard_Integer XmlMDF::ReadSubTree (const XmlObjMgt_Element& theElement,
174 const TDF_Label& theLabel,
175 XmlObjMgt_RRelocationTable& theRelocTable,
176 const XmlMDF_MapOfDriver& theDriverMap)
178 // Extraction of the driver subset.
179 Standard_Integer count = 0;
181 //XmlObjMgt_Element anElem = (const XmlObjMgt_Element &) theElement.getFirstChild();
182 LDOM_Node theNode = theElement.getFirstChild();
183 XmlObjMgt_Element anElem = (const XmlObjMgt_Element &) theNode;
184 while ( !anElem.isNull() )
186 if ( anElem.getNodeType() == LDOM_Node::ELEMENT_NODE )
188 if ( anElem.getNodeName().equals (::LabelString()) )
191 Standard_Integer tag;
192 XmlObjMgt_DOMString aTag (anElem.getAttribute(::TagString()));
193 if ( !aTag.GetInteger (tag) ) {
194 TCollection_ExtendedString anErrorMessage =
195 TCollection_ExtendedString ("Wrong Tag value for OCAF Label: ")
197 theDriverMap.Find("TDF_TagSource") -> WriteMessage (anErrorMessage);
201 TDF_Label aLab = theLabel.FindChild(tag, Standard_True);
204 Standard_Integer subcount =
205 ReadSubTree(anElem, aLab, theRelocTable, theDriverMap);
214 XmlObjMgt_DOMString aName = anElem.getNodeName();
216 #ifdef DATATYPE_MIGRATION
217 TCollection_AsciiString newName;
218 if(Storage_Schema::CheckTypeMigration(aName, newName)) {
220 cout << "CheckTypeMigration:OldType = " <<aName.GetString() << " Len = "<<strlen(aName.GetString())<<endl;
221 cout << "CheckTypeMigration:NewType = " <<newName << " Len = "<< newName.Length()<<endl;
223 aName = newName.ToCString();
227 if (theDriverMap.IsBound (aName))
230 const Handle(XmlMDF_ADriver)& driver = theDriverMap.Find(aName);
231 XmlObjMgt_Persistent pAtt (anElem);
232 Standard_Integer anID = pAtt.Id ();
233 if (anID <= 0) { // check for ID validity
234 TCollection_ExtendedString anErrorMessage =
235 TCollection_ExtendedString("Wrong ID of OCAF attribute with type ")
237 driver -> WriteMessage (anErrorMessage);
240 Handle(TDF_Attribute) tAtt;
241 Standard_Boolean isBound = theRelocTable.IsBound(anID);
243 tAtt = Handle(TDF_Attribute)::DownCast(theRelocTable.Find(anID));
245 tAtt = driver -> NewEmpty();
246 if (tAtt->Label().IsNull())
247 theLabel.AddAttribute (tAtt);
250 (TCollection_ExtendedString("XmlDriver warning: ") +
251 "attempt to attach attribute " +
252 aName + " to a second label");
254 if (! driver -> Paste (pAtt, tAtt, theRelocTable))
256 // error converting persistent to transient
258 (TCollection_ExtendedString("XmlDriver warning: ") +
259 "failure reading attribute " + aName);
261 else if (isBound == Standard_False)
262 theRelocTable.Bind (anID, tAtt);
267 const TCollection_AsciiString anAsciiName = aName;
268 cerr << "XmlDriver warning: "
269 << "label contains object of unknown type "<< anAsciiName<< endl;
274 //anElem = (const XmlObjMgt_Element &) anElem.getNextSibling();
275 LDOM_Node theNode1 = anElem.getNextSibling();
276 anElem = (const XmlObjMgt_Element &) theNode1;
287 //=======================================================================
288 //function : AddDrivers
290 //=======================================================================
291 void XmlMDF::AddDrivers (const Handle(XmlMDF_ADriverTable)& aDriverTable,
292 const Handle(CDM_MessageDriver)& aMessageDriver)
294 aDriverTable->AddDriver (new XmlMDF_TagSourceDriver(aMessageDriver));
295 aDriverTable->AddDriver (new XmlMDF_ReferenceDriver(aMessageDriver));
298 //=======================================================================
299 //function : CreateDrvMap
301 //=======================================================================
303 void XmlMDF::CreateDrvMap (const Handle(XmlMDF_ADriverTable)& theDrivers,
304 XmlMDF_MapOfDriver& theAsciiDriverMap)
306 const XmlMDF_TypeADriverMap& aDriverMap = theDrivers->GetDrivers();
307 XmlMDF_DataMapIteratorOfTypeADriverMap anIter (aDriverMap);
308 while (anIter.More()) {
309 const Handle(XmlMDF_ADriver)& aDriver = anIter.Value();
310 const TCollection_AsciiString aTypeName = aDriver -> TypeName();
311 if (theAsciiDriverMap.IsBound (aTypeName) == Standard_False)
312 theAsciiDriverMap.Bind (aTypeName, aDriver);
314 aDriver -> WriteMessage
315 (TCollection_ExtendedString ("Warning: skipped driver name: \"")