0026229: Add the possibility in OCAF to open/save a document from/to a stream object
[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 <CDM_MessageDriver.hxx>
20 #include <CDM_MetaData.hxx>
21 #include <LDOM_DocumentType.hxx>
22 #include <LDOM_LDOMImplementation.hxx>
23 #include <LDOMParser.hxx>
24 #include <OSD_Path.hxx>
25 #include <OSD_OpenFile.hxx>
26 #include <PCDM_Document.hxx>
27 #include <PCDM_DOMHeaderParser.hxx>
28 #include <Standard_Type.hxx>
29 #include <TCollection_AsciiString.hxx>
30 #include <TCollection_ExtendedString.hxx>
31 #include <TDF_Data.hxx>
32 #include <TDocStd_Document.hxx>
33 #include <TDocStd_Owner.hxx>
34 #include <UTL.hxx>
35 #include <XmlLDrivers.hxx>
36 #include <XmlLDrivers_DocumentRetrievalDriver.hxx>
37 #include <XmlMDataStd.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(CDM_MessageDriver)&)
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 : CreateDocument
166 //purpose  : pure virtual method definition
167 //=======================================================================
168 Handle(CDM_Document) XmlLDrivers_DocumentRetrievalDriver::CreateDocument()
169 {
170   return new TDocStd_Document(PCDM_RetrievalDriver::GetFormat());
171 }
172
173 //=======================================================================
174 //function : SchemaName
175 //purpose  : pure virtual method definition
176 //=======================================================================
177 TCollection_ExtendedString XmlLDrivers_DocumentRetrievalDriver::SchemaName() const
178 {
179   TCollection_ExtendedString schemaname;
180   return schemaname; 
181 }
182
183 //=======================================================================
184 //function : Make
185 //purpose  : pure virtual method definition
186 //=======================================================================
187 void XmlLDrivers_DocumentRetrievalDriver::Make (const Handle(PCDM_Document)&,
188                                                const Handle(CDM_Document)&)
189 {
190 }
191
192 //=======================================================================
193 //function : Read
194 //purpose  : 
195 //=======================================================================
196 void XmlLDrivers_DocumentRetrievalDriver::Read
197   (const TCollection_ExtendedString& theFileName,
198    const Handle(CDM_Document)&       theNewDocument,
199    const Handle(CDM_Application)&    theApplication)
200 {
201   myReaderStatus = PCDM_RS_DriverFailure;
202   myFileName = theFileName;
203
204   std::ifstream aFileStream;
205   OSD_OpenStream (aFileStream, myFileName, std::ios::in);
206
207   if (aFileStream.is_open() && aFileStream.good())
208   {
209     Read (aFileStream, NULL, theNewDocument, theApplication);
210   }
211   else
212   {
213     myReaderStatus = PCDM_RS_OpenError;
214    
215     TCollection_ExtendedString aMsg = TCollection_ExtendedString("Error: the file ") +
216                                       theFileName + " cannot be opened for reading";
217
218     theApplication->MessageDriver()->Write (aMsg.ToExtString());
219     Standard_Failure::Raise("File cannot be opened for reading");
220   }
221 }
222
223 //=======================================================================
224 //function : Read
225 //purpose  : 
226 //=======================================================================
227 void XmlLDrivers_DocumentRetrievalDriver::Read (Standard_IStream&              theIStream,
228                                                 const Handle(Storage_Data)&    /*theStorageData*/,
229                                                 const Handle(CDM_Document)&    theNewDocument,
230                                                 const Handle(CDM_Application)& theApplication)
231 {
232   Handle(CDM_MessageDriver) aMessageDriver = theApplication -> MessageDriver();
233   ::take_time (~0, " +++++ Start RETRIEVE procedures ++++++", aMessageDriver);
234
235   // 1. Read DOM_Document from file
236   LDOMParser aParser;
237
238   if (aParser.parse(theIStream))
239   {
240     TCollection_AsciiString aData;
241     cout << aParser.GetError(aData) << ": " << aData << endl;
242     myReaderStatus = PCDM_RS_FormatFailure;
243     return;
244   }
245   const XmlObjMgt_Element anElement= aParser.getDocument().getDocumentElement();
246   ::take_time (0, " +++++ Fin parsing XML :       ", aMessageDriver);
247
248   ReadFromDomDocument (anElement, theNewDocument, theApplication);
249 }
250
251 //=======================================================================
252 //function : ReadFromDomDocument
253 //purpose  : management of the macro-structure of XML document data
254 //remark   : If the application needs to use myRelocTable to retrieve additional
255 //           data from LDOM, this method should be reimplemented
256 //=======================================================================
257
258 void XmlLDrivers_DocumentRetrievalDriver::ReadFromDomDocument
259                                 (const XmlObjMgt_Element&       theElement,
260                                  const Handle(CDM_Document)&    theNewDocument,
261                                  const Handle(CDM_Application)& theApplication)
262 {
263   const Handle(CDM_MessageDriver) aMsgDriver =
264     theApplication -> MessageDriver();
265   // 1. Read info // to be done
266   TCollection_AsciiString anAbsoluteDirectory = GetDirFromFile(myFileName);
267   Standard_Integer aCurDocVersion = 0;
268   TCollection_ExtendedString anInfo;
269   const XmlObjMgt_Element anInfoElem =
270     theElement.GetChildByTagName ("info");
271   if (anInfoElem != NULL) {
272     XmlObjMgt_DOMString aDocVerStr = anInfoElem.getAttribute("DocVersion");
273     if(aDocVerStr == NULL)
274       aCurDocVersion = 2;
275     else if (!aDocVerStr.GetInteger(aCurDocVersion)) {
276       TCollection_ExtendedString aMsg =
277         TCollection_ExtendedString ("Cannot retrieve the current Document version"
278                                     " attribute as \"") + aDocVerStr + "\"";
279       if(!aMsgDriver.IsNull()) 
280         aMsgDriver->Write(aMsg.ToExtString());
281     }
282     
283     // oan: OCC22305 - check a document verison and if it's greater than
284     // current version of storage driver set an error status and return
285     if( aCurDocVersion > XmlLDrivers::StorageVersion().IntegerValue() )
286     {
287       TCollection_ExtendedString aMsg =
288         TCollection_ExtendedString ("error: wrong file version: ") +
289                                     aDocVerStr  + " while current is " +
290                                     XmlLDrivers::StorageVersion();
291       myReaderStatus = PCDM_RS_NoVersion;
292       if(!aMsgDriver.IsNull()) 
293         aMsgDriver->Write(aMsg.ToExtString());
294       return;
295     }
296
297     if( aCurDocVersion < 2) aCurDocVersion = 2;
298
299     PropagateDocumentVersion(aCurDocVersion);
300
301     Standard_Boolean isRef = Standard_False;
302     for (LDOM_Node aNode = anInfoElem.getFirstChild();
303          aNode != NULL; aNode = aNode.getNextSibling()) {
304       if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE) {
305         if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, anInfo)) {
306
307     // Read ref counter
308     if(anInfo.Search(REFERENCE_COUNTER) != -1) {
309       try {
310         OCC_CATCH_SIGNALS
311         TCollection_AsciiString anInf(anInfo,'?');
312         Standard_Integer aRefCounter = anInf.Token(" ",2).IntegerValue();
313         theNewDocument->SetReferenceCounter(aRefCounter);
314       }
315       catch (Standard_Failure) { 
316         //    cout << "warning: could not read the reference counter in " << aFileName << endl;
317         TCollection_ExtendedString aMsg("Warning: ");
318         aMsg = aMsg.Cat("could not read the reference counter").Cat("\0");
319         if(!aMsgDriver.IsNull()) 
320     aMsgDriver->Write(aMsg.ToExtString());
321       }
322     }
323     else if (anInfo.Search(MODIFICATION_COUNTER) != -1) {
324       try {
325         OCC_CATCH_SIGNALS
326         
327         TCollection_AsciiString anInf(anInfo,'?');
328         Standard_Integer aModCounter = anInf.Token(" ",2).IntegerValue();
329         theNewDocument->SetModifications (aModCounter);
330       }
331       catch (Standard_Failure) { 
332         TCollection_ExtendedString aMsg("Warning: could not read the modification counter\0");
333         if(!aMsgDriver.IsNull()) 
334           aMsgDriver->Write(aMsg.ToExtString());
335       }
336     }
337     
338     if(anInfo == END_REF)
339       isRef = Standard_False;
340     if(isRef) { // Process References
341       
342       Standard_Integer pos=anInfo.Search(" ");
343       if(pos != -1) {
344         // Parce RefId, DocumentVersion and FileName
345         Standard_Integer aRefId;
346         TCollection_ExtendedString aFileName;
347         Standard_Integer aDocumentVersion;
348
349
350         TCollection_ExtendedString aRest=anInfo.Split(pos);
351         aRefId = UTL::IntegerValue(anInfo);
352         
353         Standard_Integer pos2 = aRest.Search(" ");
354         
355         aFileName = aRest.Split(pos2);
356         aDocumentVersion = UTL::IntegerValue(aRest);
357         
358         TCollection_AsciiString aPath = UTL::CString(aFileName);
359         TCollection_AsciiString anAbsolutePath;
360         if(!anAbsoluteDirectory.IsEmpty()) {
361     anAbsolutePath = AbsolutePath(anAbsoluteDirectory,aPath);
362     if(!anAbsolutePath.IsEmpty()) aPath=anAbsolutePath;
363         }
364         if(!aMsgDriver.IsNull()) {
365     //      cout << "reference found; ReferenceIdentifier: " << theReferenceIdentifier << "; File:" << thePath << ", version:" << theDocumentVersion;
366     TCollection_ExtendedString aMsg("Warning: ");
367     aMsg = aMsg.Cat("reference found; ReferenceIdentifier:  ").Cat(aRefId).Cat("; File:").Cat(aPath).Cat(", version:").Cat(aDocumentVersion).Cat("\0");
368     aMsgDriver->Write(aMsg.ToExtString());
369         }
370         // Add new ref!
371         /////////////
372     TCollection_ExtendedString theFolder,theName;
373         //TCollection_ExtendedString theFile=myReferences(myIterator).FileName();
374         TCollection_ExtendedString f(aPath);
375 #ifndef _WIN32
376         
377         Standard_Integer i= f.SearchFromEnd("/");
378         TCollection_ExtendedString n = f.Split(i); 
379         f.Trunc(f.Length()-1);
380         theFolder = f;
381         theName = n;
382 #else
383         OSD_Path p = UTL::Path(f);
384         Standard_ExtCharacter      chr;
385         TCollection_ExtendedString dir, dirRet, name;
386         
387         dir = UTL::Disk(p);
388         dir += UTL::Trek(p);
389         
390         for ( int i = 1; i <= dir.Length (); ++i ) {
391     
392     chr = dir.Value ( i );
393     
394     switch ( chr ) {
395       
396     case _TEXT( '|' ):
397       dirRet += _TEXT( "/" );
398       break;
399       
400     case _TEXT( '^' ):
401       
402       dirRet += _TEXT( ".." );
403       break;
404       
405     default:
406       dirRet += chr;
407       
408     }  
409         }
410         theFolder = dirRet;
411         theName   = UTL::Name(p); theName+= UTL::Extension(p);
412 #endif  // _WIN32
413         
414         Handle(CDM_MetaData) aMetaData =  CDM_MetaData::LookUp(theFolder,theName,aPath,aPath,UTL::IsReadOnly(aFileName));
415 ////////////
416         theNewDocument->CreateReference(aMetaData,aRefId,
417              theApplication,aDocumentVersion,Standard_False);
418
419         
420       }
421
422       
423     }
424     if(anInfo == START_REF)
425       isRef = Standard_True;
426         }
427       }
428     }
429   }
430
431   // 2. Read comments
432   TCollection_ExtendedString aComment;
433   const XmlObjMgt_Element aCommentsElem =
434     theElement.GetChildByTagName ("comments");
435   if (aCommentsElem != NULL)
436   {
437     for (LDOM_Node aNode = aCommentsElem.getFirstChild();
438          aNode != NULL; aNode = aNode.getNextSibling())
439     {
440       if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE)
441       {
442         if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, aComment))
443         {
444           theNewDocument->AddComment(aComment);
445         }
446       }
447     }
448   }
449
450   // 2. Read Shapes section
451   if (myDrivers.IsNull()) myDrivers = AttributeDrivers (aMsgDriver);  
452   const Handle(XmlMDF_ADriver) aNSDriver = ReadShapeSection(theElement, aMsgDriver);
453   if(!aNSDriver.IsNull())
454     ::take_time (0, " +++++ Fin reading Shapes :    ", aMsgDriver);
455
456   // 5. Read document contents
457   try
458   {
459     OCC_CATCH_SIGNALS
460 #ifdef OCCT_DEBUG
461     TCollection_ExtendedString aMessage ("PasteDocument");
462     aMsgDriver -> Write (aMessage.ToExtString());
463 #endif
464     if (!MakeDocument(theElement, theNewDocument))
465       myReaderStatus = PCDM_RS_MakeFailure;
466     else
467       myReaderStatus = PCDM_RS_OK;
468   }
469   catch (Standard_Failure)
470   {
471     TCollection_ExtendedString anErrorString (Standard_Failure::Caught()->GetMessageString());
472     aMsgDriver -> Write (anErrorString.ToExtString());
473   }
474
475   //    Wipe off the shapes written to the <shapes> section
476   ShapeSetCleaning(aNSDriver);
477
478   //    Clean the relocation table.
479   //    If the application needs to use myRelocTable to retrieve additional
480   //    data from LDOM, this method should be reimplemented avoiding this step
481   myRelocTable.Clear();
482   ::take_time (0, " +++++ Fin reading data OCAF : ", aMsgDriver);
483 }
484
485 //=======================================================================
486 //function : MakeDocument
487 //purpose  : 
488 //=======================================================================
489 Standard_Boolean XmlLDrivers_DocumentRetrievalDriver::MakeDocument
490                                     (const XmlObjMgt_Element&    theElement,
491                                      const Handle(CDM_Document)& theTDoc)
492 {
493   Standard_Boolean aResult = Standard_False;
494   Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);
495   myRelocTable.Clear();
496   if (!TDOC.IsNull()) 
497   {
498     Handle(TDF_Data) aTDF = new TDF_Data();
499     aResult = XmlMDF::FromTo (theElement, aTDF, myRelocTable, myDrivers);
500     if (aResult) {
501       TDOC->SetData (aTDF);
502       TDocStd_Owner::SetDocument (aTDF, TDOC);
503     }
504   }
505   return aResult;
506 }
507
508 //=======================================================================
509 //function : AttributeDrivers
510 //purpose  : 
511 //=======================================================================
512 Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentRetrievalDriver::AttributeDrivers
513        (const Handle(CDM_MessageDriver)& theMessageDriver) 
514 {
515   return XmlLDrivers::AttributeDrivers (theMessageDriver);
516 }
517
518 //=======================================================================
519 //function : take_time
520 //class    : static
521 //purpose  : output astronomical time elapsed
522 //=======================================================================
523 #ifdef TAKE_TIMES
524 #include <time.h>
525 #include <sys/timeb.h>
526 #include <sys/types.h>
527 #include <stdio.h>
528 #ifndef _WIN32
529 extern "C" int ftime (struct timeb *tp);
530 #endif
531 extern struct timeb  tmbuf0;
532
533 static void take_time (const Standard_Integer isReset, const char * aHeader,
534                        const Handle(CDM_MessageDriver)& aMessageDriver)
535 {
536   struct timeb  tmbuf;
537   ftime (&tmbuf);
538   TCollection_ExtendedString aMessage ((Standard_CString)aHeader);
539   if (isReset) tmbuf0 = tmbuf;
540   else {
541     char take_tm_buf [64];
542     Sprintf (take_tm_buf, "%9.2f s ++++",
543              double(tmbuf.time - tmbuf0.time) +
544              double(tmbuf.millitm - tmbuf0.millitm)/1000.);
545     aMessage += take_tm_buf;
546   }
547   aMessageDriver -> Write (aMessage.ToExtString());
548 }
549 #endif
550
551 //=======================================================================
552 //function : PropagateDocumentVersion
553 //purpose  : 
554 //=======================================================================
555 void XmlLDrivers_DocumentRetrievalDriver::PropagateDocumentVersion(
556                                    const Standard_Integer theDocVersion )
557 {
558   XmlMDataStd::SetDocumentVersion(theDocVersion);
559 }
560
561 //=======================================================================
562 //function : ReadShapeSection
563 //purpose  : definition of ReadShapeSection
564 //=======================================================================
565 Handle(XmlMDF_ADriver) XmlLDrivers_DocumentRetrievalDriver::ReadShapeSection(
566                                const XmlObjMgt_Element&       /*theElement*/,
567              const Handle(CDM_MessageDriver)& /*aMsgDriver*/)
568 {
569   Handle(XmlMDF_ADriver) aDriver;
570   //empty; to be redefined
571   return aDriver;
572 }
573
574 //=======================================================================
575 //function : ShapeSetCleaning
576 //purpose  : definition of ShapeSetCleaning
577 //=======================================================================
578 void XmlLDrivers_DocumentRetrievalDriver::ShapeSetCleaning(
579             const Handle(XmlMDF_ADriver)& /*theDriver*/) 
580 {}