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