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