0032352: Coding Rules - clean up code from compatibility hacks with pre-C++11 compilers
[occt.git] / src / XmlLDrivers / XmlLDrivers_DocumentStorageDriver.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.hxx>
20 #include <Message_Messenger.hxx>
21 #include <Message_ProgressScope.hxx>
22 #include <LDOM_DocumentType.hxx>
23 #include <LDOM_LDOMImplementation.hxx>
24 #include <LDOM_XmlWriter.hxx>
25 #include <OSD_Environment.hxx>
26 #include <OSD_File.hxx>
27 #include <OSD_FileSystem.hxx>
28 #include <PCDM.hxx>
29 #include <PCDM_ReadWriter.hxx>
30 #include <Standard_ErrorHandler.hxx>
31 #include <Standard_Failure.hxx>
32 #include <Standard_Type.hxx>
33 #include <Storage_Data.hxx>
34 #include <TCollection_AsciiString.hxx>
35 #include <TCollection_ExtendedString.hxx>
36 #include <TColStd_SequenceOfAsciiString.hxx>
37 #include <TDocStd_Document.hxx>
38 #include <XmlLDrivers.hxx>
39 #include <XmlLDrivers_DocumentStorageDriver.hxx>
40 #include <XmlLDrivers_NamespaceDef.hxx>
41 #include <XmlMDF.hxx>
42 #include <XmlMDF_ADriverTable.hxx>
43 #include <XmlObjMgt.hxx>
44 #include <XmlObjMgt_Document.hxx>
45 #include <XmlObjMgt_SRelocationTable.hxx>
46
47 #include <locale.h>
48 IMPLEMENT_STANDARD_RTTIEXT(XmlLDrivers_DocumentStorageDriver,PCDM_StorageDriver)
49
50 #define STORAGE_VERSION      "STORAGE_VERSION: "
51 #define REFERENCE_COUNTER    "REFERENCE_COUNTER: "
52 #define MODIFICATION_COUNTER "MODIFICATION_COUNTER: "
53 #define START_REF            "START_REF"
54 #define END_REF              "END_REF"
55
56 #define FAILSTR "Failed to write xsi:schemaLocation : "
57
58 //#define TAKE_TIMES
59 static void take_time (const Standard_Integer, const char *,
60                        const Handle(Message_Messenger)&)
61 #ifdef TAKE_TIMES
62 ;
63 #else
64 {}
65 #endif
66
67 //=======================================================================
68 //function : XmlLDrivers_DocumentStorageDriver
69 //purpose  : Constructor
70 //=======================================================================
71 XmlLDrivers_DocumentStorageDriver::XmlLDrivers_DocumentStorageDriver
72                                 (const TCollection_ExtendedString& theCopyright)
73      : myCopyright (theCopyright)
74
75 }
76
77 //=======================================================================
78 //function : AddNamespace
79 //purpose  : 
80 //=======================================================================
81
82 void XmlLDrivers_DocumentStorageDriver::AddNamespace
83                                 (const TCollection_AsciiString& thePrefix,
84                                  const TCollection_AsciiString& theURI)
85 {
86   for (Standard_Integer i = 1; i <= mySeqOfNS.Length(); i++)
87     if (thePrefix == mySeqOfNS(i).Prefix()) return;
88   mySeqOfNS.Append (XmlLDrivers_NamespaceDef(thePrefix, theURI));
89 }
90
91 //=======================================================================
92 //function : Write
93 //purpose  : 
94 //=======================================================================
95 void XmlLDrivers_DocumentStorageDriver::Write (const Handle(CDM_Document)&       theDocument,
96                                                const TCollection_ExtendedString& theFileName,
97                                                const Message_ProgressRange&      theRange)
98 {
99   myFileName = theFileName;
100
101   const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem();
102   std::shared_ptr<std::ostream> aFileStream = aFileSystem->OpenOStream (theFileName, std::ios::out);
103   if (aFileStream.get() != NULL && aFileStream->good())
104   {
105     Write (theDocument, *aFileStream, theRange);
106   }
107   else
108   {
109     SetIsError (Standard_True);
110     SetStoreStatus(PCDM_SS_WriteFailure);
111     
112     TCollection_ExtendedString aMsg = TCollection_ExtendedString("Error: the file ") +
113                                       theFileName + " cannot be opened for writing";
114
115     theDocument->Application()->MessageDriver()->Send (aMsg.ToExtString(), Message_Fail);
116     throw Standard_Failure("File cannot be opened for writing");
117   }
118 }
119
120 //=======================================================================
121 //function : Write
122 //purpose  : 
123 //=======================================================================
124 void XmlLDrivers_DocumentStorageDriver::Write (const Handle(CDM_Document)&  theDocument,
125                                                Standard_OStream&            theOStream,
126                                                const Message_ProgressRange& theRange)
127 {
128   Handle(Message_Messenger) aMessageDriver = theDocument->Application()->MessageDriver();
129   ::take_time (~0, " +++++ Start STORAGE procedures ++++++", aMessageDriver);
130
131   // Create new DOM_Document
132   XmlObjMgt_Document aDOMDoc = XmlObjMgt_Document::createDocument ("document");
133
134   // Fill the document with data
135   XmlObjMgt_Element anElement = aDOMDoc.getDocumentElement();
136
137   if (WriteToDomDocument (theDocument, anElement, theRange) == Standard_False) {
138
139     LDOM_XmlWriter aWriter;
140     aWriter.SetIndentation(1);
141   
142     if (theOStream.good())
143     {
144       aWriter.Write (theOStream, aDOMDoc);
145     }
146     else
147     {
148       SetIsError (Standard_True);
149       SetStoreStatus(PCDM_SS_WriteFailure);
150
151       TCollection_ExtendedString aMsg = TCollection_ExtendedString("Error: the stream is bad and") +
152                                         " cannot be used for writing";
153       theDocument->Application()->MessageDriver()->Send (aMsg.ToExtString(), Message_Fail);
154       
155       throw Standard_Failure("File cannot be opened for writing");
156     }
157
158     ::take_time (0, " +++++ Fin formatting to XML : ", aMessageDriver);
159   }
160 }
161
162 //=======================================================================
163 //function : WriteToDomDocument
164 //purpose  : management of the macro-structure of XML document data
165 //remark   : If the application needs to use myRelocTable to store additional
166 //           data to XML, this method should be reimplemented avoiding step 3
167 //=======================================================================
168
169 Standard_Boolean XmlLDrivers_DocumentStorageDriver::WriteToDomDocument
170                           (const Handle(CDM_Document)&  theDocument,
171                            XmlObjMgt_Element&           theElement,
172                            const Message_ProgressRange& theRange)
173 {
174   SetIsError(Standard_False);
175   Handle(Message_Messenger) aMessageDriver =
176     theDocument -> Application() -> MessageDriver();
177   // 1. Write header information
178   Standard_Integer i;
179   XmlObjMgt_Document aDOMDoc = theElement.getOwnerDocument();
180
181   // 1.a File Format
182   TCollection_AsciiString aStorageFormat (theDocument->StorageFormat(), '?');
183   theElement.setAttribute ("format", aStorageFormat.ToCString());
184 //  theElement.setAttribute ("schema", "XSD");
185
186   theElement.setAttribute ("xmlns" , "http://www.opencascade.org/OCAF/XML");
187   for (i = 1; i <= mySeqOfNS.Length(); i++) {
188     TCollection_AsciiString aPrefix =
189       TCollection_AsciiString("xmlns:") + mySeqOfNS(i).Prefix().ToCString();
190     theElement.setAttribute (aPrefix.ToCString(),
191                              mySeqOfNS(i).URI().ToCString());
192   }
193   theElement.setAttribute ("xmlns:xsi",
194                            "http://www.w3.org/2001/XMLSchema-instance");
195   //mkv 15.09.05 OCC10001
196   //theElement.setAttribute ("xsi:schemaLocation",
197   //                         "http://www.opencascade.org/OCAF/XML"
198   //                         " http://www.nnov.matra-dtv.fr/~agv/XmlOcaf.xsd");
199   //
200   // the order of search : by CSF_XmlOcafResource and then by CASROOT
201   TCollection_AsciiString anHTTP = "http://www.opencascade.org/OCAF/XML";
202   Standard_Boolean aToSetCSFVariable = Standard_False;
203   const char * aCSFVariable [2] = {
204     "CSF_XmlOcafResource",
205     "CASROOT"
206   };
207   OSD_Environment anEnv (aCSFVariable[0]);
208   TCollection_AsciiString aResourceDir = anEnv.Value();
209   if (aResourceDir.IsEmpty()) {
210     // now try by CASROOT
211     OSD_Environment anEnv2(aCSFVariable[1]);
212     aResourceDir = anEnv2.Value();
213     if ( !aResourceDir.IsEmpty() ) {
214       aResourceDir += "/src/XmlOcafResource" ;
215       aToSetCSFVariable = Standard_True; //CSF variable to be set later
216     }
217 #ifdef OCCT_DEBUGXML
218     else {
219       TCollection_ExtendedString aWarn = FAILSTR "Neither ";
220       aWarn = (aWarn + aCSFVariable[0] + ", nor " + aCSFVariable[1]
221                + " variables have been set");
222       aMessageDriver->Send (aWarn.ToExtString(), Message_Warning);
223     }
224 #endif
225   }
226   if (!aResourceDir.IsEmpty()) {
227     TCollection_AsciiString aResourceFileName =  aResourceDir + "/XmlOcaf.xsd";
228     // search directory name that has been constructed, now check whether
229     // it and the file exist
230     OSD_File aResourceFile ( aResourceFileName );
231     if ( aResourceFile.Exists() ) {
232       if (aToSetCSFVariable) {
233         OSD_Environment aCSFVarEnv ( aCSFVariable[0], aResourceDir );
234         aCSFVarEnv.Build();
235 #ifdef OCCT_DEBUGXML
236         TCollection_ExtendedString aWarn1 = "Variable ";
237         aWarn1 = (aWarn1 + aCSFVariable[0]
238                   + " has not been explicitly defined. Set to " + aResourceDir);
239         aMessageDriver->Send (aWarn1.ToExtString(), Message_Warning);
240 #endif
241         if ( aCSFVarEnv.Failed() ) {
242           TCollection_ExtendedString aWarn = FAILSTR "Failed to initialize ";
243           aWarn = aWarn + aCSFVariable[0] + " with " + aResourceDir;
244           aMessageDriver->Send (aWarn.ToExtString(), Message_Fail);
245         }
246       }
247     }
248 #ifdef OCCT_DEBUGXML
249     else {
250       TCollection_ExtendedString aWarn = FAILSTR "Schema definition file ";
251       aWarn += (aResourceFileName + " was not found");
252       aMessageDriver->Send (aWarn.ToExtString(), Message_Warning);
253     }
254 #endif
255     anHTTP = anHTTP + ' ' + aResourceFileName;
256   }
257   theElement.setAttribute ("xsi:schemaLocation", anHTTP.ToCString() );
258
259   // 1.b Info section
260   XmlObjMgt_Element anInfoElem = aDOMDoc.createElement("info");
261   theElement.appendChild(anInfoElem);
262
263   TCollection_AsciiString aCreationDate = XmlLDrivers::CreationDate();
264
265 //  anInfoElem.setAttribute("dbv", 0);
266   anInfoElem.setAttribute("date", aCreationDate.ToCString());
267   anInfoElem.setAttribute("schemav", 0);
268 //  anInfoElem.setAttribute("appv", anAppVersion.ToCString());
269
270   // Document version
271   Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast (theDocument);
272   TDocStd_FormatVersion aFormatVersion = TDocStd_Document::CurrentStorageFormatVersion(); // the last version of the format
273   if (TDocStd_Document::CurrentStorageFormatVersion() < aDoc->StorageFormatVersion())
274   {
275     TCollection_ExtendedString anErrorString("Unacceptable storage format version, the last version is used");
276     aMessageDriver->Send (anErrorString.ToExtString(), Message_Warning);
277   }
278   else
279   {
280     aFormatVersion = aDoc->StorageFormatVersion();
281   }
282   const TCollection_AsciiString aStringFormatVersion (aFormatVersion);
283   anInfoElem.setAttribute ("DocVersion", aStringFormatVersion.ToCString());
284  
285   // User info with Copyright
286   TColStd_SequenceOfAsciiString aUserInfo;
287   if (myCopyright.Length() > 0)
288     aUserInfo.Append (TCollection_AsciiString(myCopyright,'?'));
289
290   Handle(Storage_Data) theData = new Storage_Data;
291   //PCDM_ReadWriter::WriteFileFormat( theData, theDocument );
292   PCDM_ReadWriter::Writer()->WriteReferenceCounter(theData,theDocument);
293   PCDM_ReadWriter::Writer()->WriteReferences(theData,theDocument, myFileName);
294   PCDM_ReadWriter::Writer()->WriteExtensions(theData,theDocument);
295   PCDM_ReadWriter::Writer()->WriteVersion(theData,theDocument);
296
297   const TColStd_SequenceOfAsciiString& aRefs = theData->UserInfo();
298   for(i = 1; i <= aRefs.Length(); i++)
299     aUserInfo.Append(aRefs.Value(i));
300
301   // Keep format version in Reloc. table
302   Handle(Storage_HeaderData) aHeaderData = theData->HeaderData();
303   aHeaderData->SetStorageVersion(aFormatVersion);
304   myRelocTable.Clear();
305   myRelocTable.SetHeaderData(aHeaderData);
306
307   for (i = 1; i <= aUserInfo.Length(); i++)
308   {
309     XmlObjMgt_Element aUIItem = aDOMDoc.createElement ("iitem");
310     anInfoElem.appendChild (aUIItem);
311     LDOM_Text aUIText = aDOMDoc.createTextNode (aUserInfo(i).ToCString());
312     aUIItem.appendChild (aUIText);
313   }
314
315   // 1.c Comments section
316   TColStd_SequenceOfExtendedString aComments;
317   theDocument->Comments(aComments);
318
319   XmlObjMgt_Element aCommentsElem = aDOMDoc.createElement ("comments");
320   theElement.appendChild (aCommentsElem);
321
322   for (i = 1; i <= aComments.Length(); i++)
323   {
324     XmlObjMgt_Element aCItem = aDOMDoc.createElement ("citem");
325     aCommentsElem.appendChild (aCItem);
326     XmlObjMgt::SetExtendedString (aCItem, aComments(i));
327   }
328   Message_ProgressScope aPS(theRange, "Writing", 2);
329   // 2a. Write document contents
330   Standard_Integer anObjNb = 0;
331   {
332     try
333     {
334       OCC_CATCH_SIGNALS
335       anObjNb = MakeDocument(theDocument, theElement, aPS.Next());
336       if (!aPS.More())
337       {
338         SetIsError(Standard_True);
339         SetStoreStatus(PCDM_SS_UserBreak);
340         return IsError();
341       }
342     }
343     catch (Standard_Failure const& anException)
344     {
345       SetIsError (Standard_True);
346       SetStoreStatus(PCDM_SS_Failure);
347       TCollection_ExtendedString anErrorString (anException.GetMessageString());
348       aMessageDriver ->Send (anErrorString.ToExtString(), Message_Fail);
349     }
350   }
351   if (anObjNb <= 0 && IsError() == Standard_False) {
352     SetIsError (Standard_True);
353     SetStoreStatus(PCDM_SS_No_Obj);
354     TCollection_ExtendedString anErrorString ("error occurred");
355     aMessageDriver ->Send (anErrorString.ToExtString(), Message_Fail);
356   }
357   // 2b. Write number of objects into the info section
358   anInfoElem.setAttribute("objnb", anObjNb);
359   ::take_time (0, " +++++ Fin DOM data for OCAF : ", aMessageDriver);
360
361   // 3. Clear relocation table
362   //    If the application needs to use myRelocTable to store additional
363   //    data to XML, this method should be reimplemented avoiding this step
364   myRelocTable.Clear();
365
366   // 4. Write Shapes section
367   if (WriteShapeSection(theElement, aFormatVersion, aPS.Next()))
368     ::take_time (0, " +++ Fin DOM data for Shapes : ", aMessageDriver);
369   if (!aPS.More())
370   {
371     SetIsError(Standard_True);
372     SetStoreStatus(PCDM_SS_UserBreak);
373     return IsError();
374   }
375   return IsError();
376 }
377
378 //=======================================================================
379 //function : MakeDocument
380 //purpose  : 
381 //=======================================================================
382 Standard_Integer XmlLDrivers_DocumentStorageDriver::MakeDocument
383                                     (const Handle(CDM_Document)&  theTDoc,
384                                      XmlObjMgt_Element&           theElement,
385                                      const Message_ProgressRange& theRange)
386 {  
387   TCollection_ExtendedString aMessage;
388   Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);  
389   if (!TDOC.IsNull()) 
390   {
391 //    myRelocTable.SetDocument (theElement.getOwnerDocument());
392     Handle(TDF_Data) aTDF = TDOC->GetData();
393
394 //      Find MessageDriver and pass it to AttributeDrivers()
395     Handle(CDM_Application) anApplication= theTDoc -> Application();
396     Handle(Message_Messenger) aMessageDriver;
397     if (anApplication.IsNull()) {
398       aMessageDriver = Message::DefaultMessenger();
399       aMessageDriver->ChangePrinters().Clear();
400     }
401     else
402       aMessageDriver = anApplication -> MessageDriver();
403     if (myDrivers.IsNull()) myDrivers = AttributeDrivers (aMessageDriver);
404
405 //      Retrieve from DOM_Document
406     XmlMDF::FromTo (aTDF, theElement, myRelocTable, myDrivers, theRange);
407 #ifdef OCCT_DEBUGXML
408     aMessage = "First step successful";
409     aMessageDriver -> Send (aMessage.ToExtString(), Message_Warning);
410 #endif
411     return myRelocTable.Extent();
412   }
413 #ifdef OCCT_DEBUG
414   std::cout << "First step failed" << std::endl;  // No MessageDriver available
415 #endif
416   return -1; // error
417 }
418
419 //=======================================================================
420 //function : AttributeDrivers
421 //purpose  : 
422 //=======================================================================
423 Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentStorageDriver::AttributeDrivers
424        (const Handle(Message_Messenger)& theMessageDriver) 
425 {
426   return XmlLDrivers::AttributeDrivers (theMessageDriver);
427 }
428
429 //=======================================================================
430 //function : take_time
431 //class    : static
432 //purpose  : output astronomical time elapsed
433 //=======================================================================
434 #ifdef TAKE_TIMES
435 #include <time.h>
436 #include <sys/timeb.h>
437 #include <sys/types.h>
438 #include <stdio.h>
439 #ifndef _WIN32
440 extern "C" int ftime (struct timeb *tp);
441 #endif
442 struct timeb  tmbuf0;
443
444 static void take_time (const Standard_Integer isReset, const char * aHeader,
445                        const Handle(Message_Messenger)& aMessageDriver)
446 {
447   struct timeb  tmbuf;
448   ftime (&tmbuf);
449   TCollection_ExtendedString aMessage ((Standard_CString)aHeader);
450   if (isReset) tmbuf0 = tmbuf;
451   else {
452     char take_tm_buf [64];
453     Sprintf (take_tm_buf, "%9.2f s ++++",
454              double(tmbuf.time - tmbuf0.time) +
455              double(tmbuf.millitm - tmbuf0.millitm)/1000.);
456     aMessage += take_tm_buf;
457   }
458   aMessageDriver ->Send (aMessage.ToExtString(), Message_Trace);
459 }
460 #endif
461
462 //=======================================================================
463 //function : WriteShapeSection
464 //purpose  : defines WriteShapeSection
465 //=======================================================================
466 Standard_Boolean XmlLDrivers_DocumentStorageDriver::WriteShapeSection
467                                 (XmlObjMgt_Element&           /*theElement*/,
468                                  const TDocStd_FormatVersion /*theStorageFormatVersion*/,
469                                  const Message_ProgressRange&  /*theRange*/)
470 {
471   // empty; should be redefined in subclasses
472   return Standard_False;
473 }
474