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