0022922: Clean up warnings on uninitialized / unused variables
[occt.git] / src / XmlLDrivers / XmlLDrivers_DocumentRetrievalDriver.cxx
1 // File:      XmlLDrivers_DocumentRetrievalDriver.cxx
2 // Created:   Mon Jul  9 12:29:49 MSK DST 2001
3 // Author:    Julia DOROVSKIKH
4 // Copyright: Open Cascade 2001
5
6 #include <XmlLDrivers_DocumentRetrievalDriver.ixx>
7
8 #include <XmlLDrivers.hxx>
9 #include <XmlMDF.hxx>
10 #include <XmlObjMgt_RRelocationTable.hxx>
11 #include <XmlObjMgt_Document.hxx>
12 #include <XmlObjMgt.hxx>
13 #include <XmlMDataStd.hxx>
14 #include <LDOM_LDOMImplementation.hxx>
15 #include <LDOM_DocumentType.hxx>
16 #include <LDOMParser.hxx>
17
18 #include <TDF_Data.hxx>
19 #include <TDocStd_Owner.hxx>
20 #include <TDocStd_Document.hxx>
21 #include <CDM_MessageDriver.hxx>
22 #include <CDM_MetaData.hxx>
23 #include <TCollection_AsciiString.hxx>
24 #include <UTL.hxx>
25 #include <OSD_Path.hxx>
26
27 #ifdef WNT
28 # include <tchar.h>
29 #endif  // WNT
30
31 #include <locale.h>
32 #include <Standard_Failure.hxx>
33 #include <Standard_ErrorHandler.hxx>
34
35 #define START_REF         "START_REF"
36 #define END_REF           "END_REF"
37 #define REFERENCE_COUNTER "REFERENCE_COUNTER"
38
39 static  Standard_Integer myDocumentVersion = 0;
40
41 //#define TAKE_TIMES
42 static void take_time (const Standard_Integer, const char *,
43                        const Handle(CDM_MessageDriver)&)
44 #ifdef TAKE_TIMES
45 ;
46 #else
47 {}
48 #endif
49
50 static Standard_Integer RemoveExtraSeparator(TCollection_AsciiString& aString) {
51
52   Standard_Integer i, j, len ;
53
54   len = aString.Length() ;
55 #ifdef WNT
56   // Case of network path, such as \\MACHINE\dir
57   for (i = j = 2 ; j <= len ; i++,j++) {
58 #else
59   for (i = j = 1 ; j <= len ; i++,j++) {
60 #endif
61       Standard_Character c = aString.Value(j) ;
62       aString.SetValue(i,c) ;
63       if (c == '/')
64           while(j < len && aString.Value(j+1) == '/') j++ ;
65   }
66   len = i-1 ;
67   if (aString.Value(len) == '/') len-- ;  
68   aString.Trunc(len) ;
69   return len ;
70 }
71 static TCollection_AsciiString GetDirFromFile(const TCollection_ExtendedString& aFileName) {
72   TCollection_AsciiString theCFile=UTL::CString(aFileName);
73   TCollection_AsciiString theDirectory;
74   Standard_Integer i=theCFile.SearchFromEnd("/");
75 #ifdef WNT    
76 //    if(i==-1) i=theCFile.SearchFromEnd("\\");
77   if(theCFile.SearchFromEnd("\\") > i)
78     i=theCFile.SearchFromEnd("\\");
79 #endif
80   if(i!=-1) theDirectory=theCFile.SubString(1,i);
81   return theDirectory;
82 }
83
84 static TCollection_AsciiString AbsolutePath(
85                             const TCollection_AsciiString& aDirPath,
86                             const TCollection_AsciiString& aRelFilePath)
87 {
88   TCollection_AsciiString EmptyString = "" ;
89 #ifdef WNT
90   if (aRelFilePath.Search(":") == 2 ||
91       (aRelFilePath.Search("\\") == 1 && aRelFilePath.Value(2) == '\\'))
92 #else
93   if(aRelFilePath.Search("/") == 1)
94 #endif
95     return aRelFilePath ;
96   
97   TCollection_AsciiString DirPath = aDirPath, RelFilePath = aRelFilePath  ;
98   Standard_Integer i,len ;
99   
100 #ifdef WNT
101   if(DirPath.Search(":") != 2 &&
102      (DirPath.Search("\\") != 1 || DirPath.Value(2) != '\\'))
103 #else
104   if (DirPath.Search("/") != 1 )
105 #endif
106     return EmptyString ;
107
108 #ifdef WNT
109   DirPath.ChangeAll('\\','/') ;
110   RelFilePath.ChangeAll('\\','/') ;      
111 #endif
112   
113   RemoveExtraSeparator(DirPath) ;
114   len = RemoveExtraSeparator(RelFilePath) ;
115   
116   while (RelFilePath.Search("../") == 1) {
117       if (len == 3)
118     return EmptyString ;
119       RelFilePath = RelFilePath.SubString(4,len) ;
120       len -= 3 ;
121       if (DirPath.IsEmpty())
122     return EmptyString ;
123       i = DirPath.SearchFromEnd("/") ;
124       if (i < 0)
125           return EmptyString ;
126       DirPath.Trunc(i-1) ;
127   }  
128   TCollection_AsciiString retx;
129   retx= DirPath;
130   retx+= "/";
131   retx+=RelFilePath ;
132   return retx;
133 }
134
135 //=======================================================================
136 //function : XmlLDrivers_DocumentRetrievalDriver
137 //purpose  : Constructor
138 //=======================================================================
139 XmlLDrivers_DocumentRetrievalDriver::XmlLDrivers_DocumentRetrievalDriver()
140 {
141   myReaderStatus = PCDM_RS_OK;
142 }
143
144 //=======================================================================
145 //function : CreateDocument
146 //purpose  : pure virtual method definition
147 //=======================================================================
148 Handle(CDM_Document) XmlLDrivers_DocumentRetrievalDriver::CreateDocument()
149 {
150   return new TDocStd_Document(PCDM_RetrievalDriver::GetFormat());
151 }
152
153 //=======================================================================
154 //function : SchemaName
155 //purpose  : pure virtual method definition
156 //=======================================================================
157 TCollection_ExtendedString XmlLDrivers_DocumentRetrievalDriver::SchemaName() const
158 {
159   TCollection_ExtendedString schemaname;
160   return schemaname; 
161 }
162
163 //=======================================================================
164 //function : Make
165 //purpose  : pure virtual method definition
166 //=======================================================================
167 void XmlLDrivers_DocumentRetrievalDriver::Make (const Handle(PCDM_Document)&,
168                                                const Handle(CDM_Document&))
169 {
170 }
171
172 //=======================================================================
173 //function : Read
174 //purpose  : 
175 //=======================================================================
176 void XmlLDrivers_DocumentRetrievalDriver::Read
177   (const TCollection_ExtendedString& theFileName,
178    const Handle(CDM_Document)&       theNewDocument,
179    const Handle(CDM_Application)&    theApplication)
180 {
181   myReaderStatus = PCDM_RS_DriverFailure;
182   myFileName = theFileName;
183   const TCollection_AsciiString anOldNumLocale =
184     (Standard_CString) setlocale (LC_NUMERIC, NULL);
185   setlocale(LC_NUMERIC, "C");
186   
187   Handle(CDM_MessageDriver) aMessageDriver = theApplication -> MessageDriver();
188   ::take_time (~0, " +++++ Start RETRIEVE procedures ++++++", aMessageDriver);
189
190   // 1. Read DOM_Document from file
191   LDOMParser aParser;
192   TCollection_AsciiString aName (theFileName,'?');
193   if (aParser.parse(aName.ToCString()))
194   {
195     TCollection_AsciiString aData;
196     cout << aParser.GetError(aData) << ": " << aData << endl;
197     myReaderStatus = PCDM_RS_FormatFailure;
198     return;
199   }
200   const XmlObjMgt_Element anElement= aParser.getDocument().getDocumentElement();
201   ::take_time (0, " +++++ Fin parsing XML :       ", aMessageDriver);
202
203   ReadFromDomDocument (anElement, theNewDocument, theApplication);
204
205   setlocale(LC_NUMERIC, (char *) anOldNumLocale.ToCString()) ;
206 }
207
208 //=======================================================================
209 //function : ReadFromDomDocument
210 //purpose  : management of the macro-structure of XML document data
211 //remark   : If the application needs to use myRelocTable to retrieve additional
212 //           data from LDOM, this method should be reimplemented
213 //=======================================================================
214
215 void XmlLDrivers_DocumentRetrievalDriver::ReadFromDomDocument
216                                 (const XmlObjMgt_Element&       theElement,
217                                  const Handle(CDM_Document)&    theNewDocument,
218                                  const Handle(CDM_Application)& theApplication)
219 {
220   const Handle(CDM_MessageDriver) aMsgDriver =
221     theApplication -> MessageDriver();
222   // 1. Read info // to be done
223   TCollection_AsciiString anAbsoluteDirectory = GetDirFromFile(myFileName);
224   Standard_Integer aCurDocVersion = 0;
225   TCollection_ExtendedString anInfo;
226   const XmlObjMgt_Element anInfoElem =
227     theElement.GetChildByTagName ("info");
228   if (anInfoElem != NULL) {
229     XmlObjMgt_DOMString aDocVerStr = anInfoElem.getAttribute("DocVersion");
230     if(aDocVerStr == NULL)
231       aCurDocVersion = 2;
232     else if (!aDocVerStr.GetInteger(aCurDocVersion)) {
233       TCollection_ExtendedString aMsg =
234         TCollection_ExtendedString ("Cannot retrieve the current Document version"
235                                     " attribute as \"") + aDocVerStr + "\"";
236       if(!aMsgDriver.IsNull()) 
237         aMsgDriver->Write(aMsg.ToExtString());
238     }
239     
240     // oan: OCC22305 - check a document verison and if it's greater than
241     // current version of storage driver set an error status and return
242     if( aCurDocVersion > XmlLDrivers::StorageVersion().IntegerValue() )
243     {
244       TCollection_ExtendedString aMsg =
245         TCollection_ExtendedString ("error: wrong file version: ") +
246                                     aDocVerStr  + " while current is " +
247                                     XmlLDrivers::StorageVersion();
248       myReaderStatus = PCDM_RS_NoVersion;
249       if(!aMsgDriver.IsNull()) 
250         aMsgDriver->Write(aMsg.ToExtString());
251       return;
252     }
253
254     if( aCurDocVersion < 2) aCurDocVersion = 2;
255
256     PropagateDocumentVersion(aCurDocVersion);
257
258     Standard_Boolean isRef = Standard_False;
259     for (LDOM_Node aNode = anInfoElem.getFirstChild();
260          aNode != NULL; aNode = aNode.getNextSibling()) {
261       if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE) {
262         if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, anInfo)) {
263
264     // Read ref counter
265     if(anInfo.Search(REFERENCE_COUNTER) != -1) {
266       try {
267         OCC_CATCH_SIGNALS
268         TCollection_AsciiString anInf(anInfo,'?');
269         //Standard_Integer aRefCounter = anInf.Token(" ",2).IntegerValue();
270         //theNewDocument->SetReferenceCounter(aRefCounter);
271       }
272       catch (Standard_Failure) { 
273         //    cout << "warning: could not read the reference counter in " << aFileName << endl;
274         TCollection_ExtendedString aMsg("Warning: ");
275         aMsg = aMsg.Cat("could not read the reference counter").Cat("\0");
276         if(!aMsgDriver.IsNull()) 
277     aMsgDriver->Write(aMsg.ToExtString());
278       }
279     }
280     
281     if(anInfo == END_REF)
282       isRef = Standard_False;
283     if(isRef) { // Process References
284       
285       Standard_Integer pos=anInfo.Search(" ");
286       if(pos != -1) {
287         // Parce RefId, DocumentVersion and FileName
288         Standard_Integer aRefId;
289         TCollection_ExtendedString aFileName;
290         Standard_Integer aDocumentVersion;
291
292
293         TCollection_ExtendedString aRest=anInfo.Split(pos);
294         aRefId = UTL::IntegerValue(anInfo);
295         
296         Standard_Integer pos2 = aRest.Search(" ");
297         
298         aFileName = aRest.Split(pos2);
299         aDocumentVersion = UTL::IntegerValue(aRest);
300         
301         TCollection_AsciiString aPath = UTL::CString(aFileName);
302         TCollection_AsciiString anAbsolutePath;
303         if(!anAbsoluteDirectory.IsEmpty()) {
304     anAbsolutePath = AbsolutePath(anAbsoluteDirectory,aPath);
305     if(!anAbsolutePath.IsEmpty()) aPath=anAbsolutePath;
306         }
307         if(!aMsgDriver.IsNull()) {
308     //      cout << "reference found; ReferenceIdentifier: " << theReferenceIdentifier << "; File:" << thePath << ", version:" << theDocumentVersion;
309     TCollection_ExtendedString aMsg("Warning: ");
310     aMsg = aMsg.Cat("reference found; ReferenceIdentifier:  ").Cat(aRefId).Cat("; File:").Cat(aPath).Cat(", version:").Cat(aDocumentVersion).Cat("\0");
311     aMsgDriver->Write(aMsg.ToExtString());
312         }
313         // Add new ref!
314         /////////////
315     TCollection_ExtendedString theFolder,theName;
316         //TCollection_ExtendedString theFile=myReferences(myIterator).FileName();
317         TCollection_ExtendedString f(aPath);
318 #ifndef WNT
319         
320         Standard_Integer i= f.SearchFromEnd("/");
321         TCollection_ExtendedString n = f.Split(i); 
322         f.Trunc(f.Length()-1);
323         theFolder = f;
324         theName = n;
325 #else
326         OSD_Path p = UTL::Path(f);
327         Standard_ExtCharacter      chr;
328         TCollection_ExtendedString dir, dirRet, name;
329         
330         dir = UTL::Disk(p);
331         dir += UTL::Trek(p);
332         
333         for ( int i = 1; i <= dir.Length (); ++i ) {
334     
335     chr = dir.Value ( i );
336     
337     switch ( chr ) {
338       
339     case _TEXT( '|' ):
340       dirRet += _TEXT( "/" );
341       break;
342       
343     case _TEXT( '^' ):
344       
345       dirRet += _TEXT( ".." );
346       break;
347       
348     default:
349       dirRet += chr;
350       
351     }  
352         }
353         theFolder = dirRet;
354         theName   = UTL::Name(p); theName+= UTL::Extension(p);
355 #endif  // WNT
356         
357         Handle(CDM_MetaData) aMetaData =  CDM_MetaData::LookUp(theFolder,theName,aPath,aPath,UTL::IsReadOnly(aFileName));
358 ////////////
359         theNewDocument->CreateReference(aMetaData,aRefId,
360              theApplication,aDocumentVersion,Standard_False);
361
362         
363       }
364
365       
366     }
367     if(anInfo == START_REF)
368       isRef = Standard_True;
369         }
370       }
371     }
372   }
373
374   // 2. Read comments
375   TCollection_ExtendedString aComment;
376   const XmlObjMgt_Element aCommentsElem =
377     theElement.GetChildByTagName ("comments");
378   if (aCommentsElem != NULL)
379   {
380     for (LDOM_Node aNode = aCommentsElem.getFirstChild();
381          aNode != NULL; aNode = aNode.getNextSibling())
382     {
383       if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE)
384       {
385         if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, aComment))
386         {
387           theNewDocument->AddComment(aComment);
388         }
389       }
390     }
391   }
392
393   // 2. Read Shapes section
394   if (myDrivers.IsNull()) myDrivers = AttributeDrivers (aMsgDriver);  
395   const Handle(XmlMDF_ADriver) aNSDriver = ReadShapeSection(theElement, aMsgDriver);
396   if(!aNSDriver.IsNull())
397     ::take_time (0, " +++++ Fin reading Shapes :    ", aMsgDriver);
398
399   // 5. Read document contents
400   try
401   {
402     OCC_CATCH_SIGNALS
403 #if defined(DEB) && !defined(TAKE_TIMES)
404     TCollection_ExtendedString aMessage ("PasteDocument");
405     aMsgDriver -> Write (aMessage.ToExtString());
406 #endif
407     if (!MakeDocument(theElement, theNewDocument))
408       myReaderStatus = PCDM_RS_MakeFailure;
409     else
410       myReaderStatus = PCDM_RS_OK;
411   }
412   catch (Standard_Failure)
413   {
414     TCollection_ExtendedString anErrorString (Standard_Failure::Caught()->GetMessageString());
415     aMsgDriver -> Write (anErrorString.ToExtString());
416   }
417
418   //    Wipe off the shapes written to the <shapes> section
419   ShapeSetCleaning(aNSDriver);
420
421   //    Clean the relocation table.
422   //    If the application needs to use myRelocTable to retrieve additional
423   //    data from LDOM, this method should be reimplemented avoiding this step
424   myRelocTable.Clear();
425   ::take_time (0, " +++++ Fin reading data OCAF : ", aMsgDriver);
426 }
427
428 //=======================================================================
429 //function : MakeDocument
430 //purpose  : 
431 //=======================================================================
432 Standard_Boolean XmlLDrivers_DocumentRetrievalDriver::MakeDocument
433                                     (const XmlObjMgt_Element&    theElement,
434                                      const Handle(CDM_Document)& theTDoc)
435 {
436   Standard_Boolean aResult = Standard_False;
437   Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);
438   myRelocTable.Clear();
439   if (!TDOC.IsNull()) 
440   {
441     Handle(TDF_Data) aTDF = new TDF_Data();
442     aResult = XmlMDF::FromTo (theElement, aTDF, myRelocTable, myDrivers);
443     if (aResult) {
444       TDOC->SetData (aTDF);
445       TDocStd_Owner::SetDocument (aTDF, TDOC);
446     }
447   }
448   return aResult;
449 }
450
451 //=======================================================================
452 //function : AttributeDrivers
453 //purpose  : 
454 //=======================================================================
455 Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentRetrievalDriver::AttributeDrivers
456        (const Handle(CDM_MessageDriver)& theMessageDriver) 
457 {
458   return XmlLDrivers::AttributeDrivers (theMessageDriver);
459 }
460
461 //=======================================================================
462 //function : take_time
463 //class    : static
464 //purpose  : output astronomical time elapsed
465 //=======================================================================
466 #ifdef TAKE_TIMES
467 #include <time.h>
468 #include <sys/timeb.h>
469 #include <sys/types.h>
470 #include <stdio.h>
471 #ifndef WNT
472 extern "C" int ftime (struct timeb *tp);
473 #endif
474 extern struct timeb  tmbuf0;
475
476 static void take_time (const Standard_Integer isReset, const char * aHeader,
477                        const Handle(CDM_MessageDriver)& aMessageDriver)
478 {
479   struct timeb  tmbuf;
480   ftime (&tmbuf);
481   TCollection_ExtendedString aMessage ((Standard_CString)aHeader);
482   if (isReset) tmbuf0 = tmbuf;
483   else {
484     char take_tm_buf [64];
485     sprintf (take_tm_buf, "%9.2f s ++++",
486              double(tmbuf.time - tmbuf0.time) +
487              double(tmbuf.millitm - tmbuf0.millitm)/1000.);
488     aMessage += take_tm_buf;
489   }
490   aMessageDriver -> Write (aMessage.ToExtString());
491 }
492 #endif
493
494 //=======================================================================
495 //function : PropagateDocumentVersion
496 //purpose  : 
497 //=======================================================================
498 void XmlLDrivers_DocumentRetrievalDriver::PropagateDocumentVersion(
499                                    const Standard_Integer theDocVersion )
500 {
501 #ifdef DEB
502 //    cout << "DocCurVersion =" << theDocVersion <<endl;
503 #endif
504   XmlMDataStd::SetDocumentVersion(theDocVersion);
505 }
506
507 //=======================================================================
508 //function : ReadShapeSection
509 //purpose  : definition of ReadShapeSection
510 //=======================================================================
511 Handle(XmlMDF_ADriver) XmlLDrivers_DocumentRetrievalDriver::ReadShapeSection(
512                                const XmlObjMgt_Element&       /*theElement*/,
513              const Handle(CDM_MessageDriver)& /*aMsgDriver*/)
514 {
515   Handle(XmlMDF_ADriver) aDriver;
516   //empty; to be redefined
517   return aDriver;
518 }
519
520 //=======================================================================
521 //function : ShapeSetCleaning
522 //purpose  : definition of ShapeSetCleaning
523 //=======================================================================
524 void XmlLDrivers_DocumentRetrievalDriver::ShapeSetCleaning(
525             const Handle(XmlMDF_ADriver)& /*theDriver*/) 
526 {}