1 // Created on: 2001-07-09
2 // Created by: Julia DOROVSKIKH
3 // Copyright (c) 2001-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
22 #include <XmlMDF_ADriver.hxx>
23 #include <XmlMDF_TagSourceDriver.hxx>
24 #include <XmlMDF_ReferenceDriver.hxx>
25 #include <XmlMDF_DataMapIteratorOfTypeADriverMap.hxx>
27 #include <XmlObjMgt_Persistent.hxx>
28 #include <XmlObjMgt_DOMString.hxx>
29 #include <XmlObjMgt_Document.hxx>
31 #include <TDF_TagSource.hxx>
32 #include <TDF_Label.hxx>
33 #include <TDF_Tool.hxx>
34 #include <TDF_AttributeIterator.hxx>
35 #include <TDF_ChildIterator.hxx>
36 #include <TColStd_MapOfTransient.hxx>
37 #include <Storage_Schema.hxx>
39 IMPLEMENT_DOMSTRING (TagString, "tag")
40 IMPLEMENT_DOMSTRING (LabelString, "label")
41 #define DATATYPE_MIGRATION
42 //#define DATATYPE_MIGRATION_DEB
43 //=======================================================================
44 //function : UnsuppTypesMap
46 //=======================================================================
48 static TColStd_MapOfTransient& UnsuppTypesMap ()
50 static TColStd_MapOfTransient anUnsuppTypes;
54 //=======================================================================
56 //purpose : Paste transient data into DOM_Element
57 //=======================================================================
58 void XmlMDF::FromTo (const Handle(TDF_Data)& theData,
59 XmlObjMgt_Element& theElement,
60 XmlObjMgt_SRelocationTable& theRelocTable,
61 const Handle(XmlMDF_ADriverTable)& theDrivers)
63 UnsuppTypesMap().Clear();
64 // Standard_Integer count =
65 WriteSubTree(theData->Root(), theElement, theRelocTable, theDrivers);
66 UnsuppTypesMap().Clear();
69 //=======================================================================
70 //function : WriteSubTree
72 //=======================================================================
73 Standard_Integer XmlMDF::WriteSubTree
74 (const TDF_Label& theLabel,
75 XmlObjMgt_Element& theElement,
76 XmlObjMgt_SRelocationTable& theRelocTable,
77 const Handle(XmlMDF_ADriverTable)& theDrivers)
79 XmlObjMgt_Document aDoc = theElement.getOwnerDocument();
81 // create element "label"
82 XmlObjMgt_Element aLabElem = aDoc.createElement (::LabelString());
84 // Extraction of the driver subset.
85 const XmlMDF_TypeADriverMap& aDriverMap = theDrivers->GetDrivers();
88 Standard_Integer count = 0;
89 TDF_AttributeIterator itr1 (theLabel);
90 for ( ; itr1.More(); itr1.Next())
92 const Handle(TDF_Attribute)& tAtt = itr1.Value();
93 const Handle(Standard_Type)& aType = tAtt->DynamicType();
94 if (aDriverMap.IsBound (aType))
96 const Handle(XmlMDF_ADriver)& aDriver = aDriverMap.Find (aType);
99 // Add source to relocation table
100 Standard_Integer anId = theRelocTable.Add (tAtt);
102 // Create DOM data item
103 XmlObjMgt_Persistent pAtt;
104 pAtt.CreateElement (aLabElem, aDriver->TypeName().ToCString(), anId);
107 aDriver -> Paste (tAtt, pAtt, theRelocTable);
110 else if (!UnsuppTypesMap().Contains (aType))
112 cout << "attribute driver for type "<< aType -> Name()<< " not found"<< endl;
113 UnsuppTypesMap().Add (aType);
119 TDF_ChildIterator itr2 (theLabel);
120 for ( ; itr2.More(); itr2.Next())
122 const TDF_Label& aChildLab = itr2.Value();
123 count += WriteSubTree(aChildLab, aLabElem, theRelocTable, theDrivers);
128 theElement.appendChild(aLabElem);
130 // set attribute "tag"
131 aLabElem.setAttribute (::TagString(), theLabel.Tag());
137 //=======================================================================
139 //purpose : Paste data from DOM_Element into transient document
140 //=======================================================================
141 Standard_Boolean XmlMDF::FromTo (const XmlObjMgt_Element& theElement,
142 Handle(TDF_Data)& theData,
143 XmlObjMgt_RRelocationTable& theRelocTable,
144 const Handle(XmlMDF_ADriverTable)& theDrivers)
146 TDF_Label aRootLab = theData->Root();
147 XmlMDF_MapOfDriver aDriverMap;
148 CreateDrvMap (theDrivers, aDriverMap);
150 Standard_Integer count = 0;
152 LDOM_Node theNode = theElement.getFirstChild();
153 XmlObjMgt_Element anElem = (const XmlObjMgt_Element&)theNode;
154 while ( !anElem.isNull() )
156 if ( anElem.getNodeName().equals (::LabelString()) )
158 Standard_Integer subcount =
159 ReadSubTree(anElem, aRootLab, theRelocTable, aDriverMap);
162 return Standard_False;
165 //anElem = (const XmlObjMgt_Element &) anElem.getNextSibling();
166 LDOM_Node theNode1 = anElem.getNextSibling();
167 anElem = (const XmlObjMgt_Element &) theNode1;
170 return Standard_True;
173 //=======================================================================
174 //function : ReadSubTree
176 //=======================================================================
177 Standard_Integer XmlMDF::ReadSubTree (const XmlObjMgt_Element& theElement,
178 const TDF_Label& theLabel,
179 XmlObjMgt_RRelocationTable& theRelocTable,
180 const XmlMDF_MapOfDriver& theDriverMap)
182 // Extraction of the driver subset.
183 Standard_Integer count = 0;
185 //XmlObjMgt_Element anElem = (const XmlObjMgt_Element &) theElement.getFirstChild();
186 LDOM_Node theNode = theElement.getFirstChild();
187 XmlObjMgt_Element anElem = (const XmlObjMgt_Element &) theNode;
188 while ( !anElem.isNull() )
190 if ( anElem.getNodeType() == LDOM_Node::ELEMENT_NODE )
192 if ( anElem.getNodeName().equals (::LabelString()) )
195 Standard_Integer tag;
196 XmlObjMgt_DOMString aTag (anElem.getAttribute(::TagString()));
197 if ( !aTag.GetInteger (tag) ) {
198 TCollection_ExtendedString anErrorMessage =
199 TCollection_ExtendedString ("Wrong Tag value for OCAF Label: ")
201 theDriverMap.Find("TDF_TagSource") -> WriteMessage (anErrorMessage);
205 TDF_Label aLab = theLabel.FindChild(tag, Standard_True);
208 Standard_Integer subcount =
209 ReadSubTree(anElem, aLab, theRelocTable, theDriverMap);
218 XmlObjMgt_DOMString aName = anElem.getNodeName();
220 #ifdef DATATYPE_MIGRATION
221 TCollection_AsciiString newName;
222 if(Storage_Schema::CheckTypeMigration(aName, newName)) {
223 #ifdef DATATYPE_MIGRATION_DEB
224 cout << "CheckTypeMigration:OldType = " <<aName.GetString() << " Len = "<<strlen(aName.GetString())<<endl;
225 cout << "CheckTypeMigration:NewType = " <<newName << " Len = "<< newName.Length()<<endl;
227 aName = newName.ToCString();
231 if (theDriverMap.IsBound (aName))
234 const Handle(XmlMDF_ADriver)& driver = theDriverMap.Find(aName);
235 XmlObjMgt_Persistent pAtt (anElem);
236 Standard_Integer anID = pAtt.Id ();
237 if (anID <= 0) { // check for ID validity
238 TCollection_ExtendedString anErrorMessage =
239 TCollection_ExtendedString("Wrong ID of OCAF attribute with type ")
241 driver -> WriteMessage (anErrorMessage);
244 Handle(TDF_Attribute) tAtt;
245 Standard_Boolean isBound = theRelocTable.IsBound(anID);
247 tAtt = Handle(TDF_Attribute)::DownCast(theRelocTable.Find(anID));
249 tAtt = driver -> NewEmpty();
250 if (tAtt->Label().IsNull())
251 theLabel.AddAttribute (tAtt);
254 (TCollection_ExtendedString("XmlDriver warning: ") +
255 "attempt to attach attribute " +
256 aName + " to a second label");
258 if (! driver -> Paste (pAtt, tAtt, theRelocTable))
260 // error converting persistent to transient
262 (TCollection_ExtendedString("XmlDriver warning: ") +
263 "failure reading attribute " + aName);
265 else if (isBound == Standard_False)
266 theRelocTable.Bind (anID, tAtt);
271 const TCollection_AsciiString anAsciiName = aName;
272 cerr << "XmlDriver warning: "
273 << "label contains object of unknown type "<< anAsciiName<< endl;
278 //anElem = (const XmlObjMgt_Element &) anElem.getNextSibling();
279 LDOM_Node theNode1 = anElem.getNextSibling();
280 anElem = (const XmlObjMgt_Element &) theNode1;
291 //=======================================================================
292 //function : AddDrivers
294 //=======================================================================
295 void XmlMDF::AddDrivers (const Handle(XmlMDF_ADriverTable)& aDriverTable,
296 const Handle(CDM_MessageDriver)& aMessageDriver)
298 aDriverTable->AddDriver (new XmlMDF_TagSourceDriver(aMessageDriver));
299 aDriverTable->AddDriver (new XmlMDF_ReferenceDriver(aMessageDriver));
302 //=======================================================================
303 //function : CreateDrvMap
305 //=======================================================================
307 void XmlMDF::CreateDrvMap (const Handle(XmlMDF_ADriverTable)& theDrivers,
308 XmlMDF_MapOfDriver& theAsciiDriverMap)
310 const XmlMDF_TypeADriverMap& aDriverMap = theDrivers->GetDrivers();
311 XmlMDF_DataMapIteratorOfTypeADriverMap anIter (aDriverMap);
312 while (anIter.More()) {
313 const Handle(XmlMDF_ADriver)& aDriver = anIter.Value();
314 const TCollection_AsciiString aTypeName = aDriver -> TypeName();
315 if (theAsciiDriverMap.IsBound (aTypeName) == Standard_False)
316 theAsciiDriverMap.Bind (aTypeName, aDriver);
318 aDriver -> WriteMessage
319 (TCollection_ExtendedString ("Warning: skipped driver name: \"")