summaryrefslogtreecommitdiff
path: root/src/XmlLDrivers/XmlLDrivers_DocumentStorageDriver.cxx
blob: 74896465e5602a6bdf181b1fce70026a4d92830b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// File:      XmlLDrivers_DocumentStorageDriver.cxx
// Created:   Mon Jul  9 12:29:49 MSK DST 2001
// Author:    Julia DOROVSKIKH
// Copyright: Open Cascade 2001

#include <XmlLDrivers_DocumentStorageDriver.ixx>

#include <XmlLDrivers.hxx>
#include <XmlLDrivers_NamespaceDef.hxx>
#include <XmlMDF.hxx>
#include <XmlMDF_ADriverTable.hxx>

#include <XmlObjMgt.hxx>
#include <XmlObjMgt_SRelocationTable.hxx>

#include <LDOM_XmlWriter.hxx>
#include <LDOM_LDOMImplementation.hxx>
#include <LDOM_DocumentType.hxx>
#include <XmlObjMgt_Document.hxx>

#include <Storage_Data.hxx>
#include <PCDM.hxx>
#include <PCDM_ReadWriter.hxx>
#include <CDM_NullMessageDriver.hxx>
#include <CDM_Document.hxx>
#include <CDM_Application.hxx>
#include <TDocStd_Document.hxx>
#include <TColStd_SequenceOfAsciiString.hxx>

#include <locale.h>
#include <Standard_Failure.hxx>
#include <Standard_ErrorHandler.hxx>

#include <OSD_File.hxx>
#include <OSD_Environment.hxx>

#define STORAGE_VERSION      "STORAGE_VERSION: "
#define REFERENCE_COUNTER    "REFERENCE_COUNTER: "
#define MODIFICATION_COUNTER "MODIFICATION_COUNTER: "
#define START_REF            "START_REF"
#define END_REF              "END_REF"

#define FAILSTR "Failed to write xsi:schemaLocation : "

//#define TAKE_TIMES
static void take_time (const Standard_Integer, const char *,
                       const Handle(CDM_MessageDriver)&)
#ifdef TAKE_TIMES
;
#else
{}
#endif

//=======================================================================
//function : XmlLDrivers_DocumentStorageDriver
//purpose  : Constructor
//=======================================================================
XmlLDrivers_DocumentStorageDriver::XmlLDrivers_DocumentStorageDriver
                                (const TCollection_ExtendedString& theCopyright)
     : myCopyright (theCopyright)
{ 
}

//=======================================================================
//function : SchemaName
//purpose  : 
//=======================================================================
TCollection_ExtendedString XmlLDrivers_DocumentStorageDriver::SchemaName() const
{
  TCollection_ExtendedString schemaname;
  return schemaname; 
}

//=======================================================================
//function : AddNamespace
//purpose  : 
//=======================================================================

void XmlLDrivers_DocumentStorageDriver::AddNamespace
                                (const TCollection_AsciiString& thePrefix,
                                 const TCollection_AsciiString& theURI)
{
  for (Standard_Integer i = 1; i <= mySeqOfNS.Length(); i++)
    if (thePrefix == mySeqOfNS(i).Prefix()) return;
  mySeqOfNS.Append (XmlLDrivers_NamespaceDef(thePrefix, theURI));
}

//=======================================================================
//function : Write
//purpose  : 
//=======================================================================
void XmlLDrivers_DocumentStorageDriver::Write
                          (const Handle(CDM_Document)&       theDocument,
                           const TCollection_ExtendedString& theFileName)
{
  const TCollection_AsciiString anOldNumLocale =
    (Standard_CString) setlocale (LC_NUMERIC, NULL);
  setlocale(LC_NUMERIC, "C");

  Handle(CDM_MessageDriver) aMessageDriver =
    theDocument -> Application() -> MessageDriver();
  ::take_time (~0, " +++++ Start STORAGE procedures ++++++", aMessageDriver);

  // Create new DOM_Document
  XmlObjMgt_Document aDOMDoc = XmlObjMgt_Document::createDocument ("document");

  // Fill the document with data
  XmlObjMgt_Element anElement = aDOMDoc.getDocumentElement();

  if (WriteToDomDocument (theDocument, anElement, theFileName) == Standard_False) {
    // Write DOM_Document into XML file,
    TCollection_AsciiString aFileName (theFileName, '?');
    FILE * aFile = fopen(aFileName.ToCString(), "wt");

    if (aFile) {
      LDOM_XmlWriter aWriter (aFile);
      aWriter.SetIndentation(1);
      aWriter << aDOMDoc;
      fclose(aFile);
      ::take_time (0, " +++++ Fin formatting to XML : ", aMessageDriver);

    }else{
      SetIsError (Standard_True);
      TCollection_ExtendedString aMsg =
        TCollection_ExtendedString("Error: the file ") + aFileName +
          " cannot be opened for writing";
      aMessageDriver -> Write (aMsg.ToExtString());
        Standard_Failure::Raise("File cannot be opened for writing");
    }
  }
  setlocale(LC_NUMERIC, (char *) anOldNumLocale.ToCString()) ;
}

//=======================================================================
//function : WriteToDomDocument
//purpose  : management of the macro-structure of XML document data
//remark   : If the application needs to use myRelocTable to store additional
//           data to XML, this method should be reimplemented avoiding step 3
//=======================================================================

Standard_Boolean XmlLDrivers_DocumentStorageDriver::WriteToDomDocument
                                  (const Handle(CDM_Document)&  theDocument,
                                   XmlObjMgt_Element&           theElement,
				   const TCollection_ExtendedString& theFileName)
{
  SetIsError(Standard_False);
  Handle(CDM_MessageDriver) aMessageDriver =
    theDocument -> Application() -> MessageDriver();
  // 1. Write header information
  Standard_Integer i;
  XmlObjMgt_Document aDOMDoc = theElement.getOwnerDocument();

  // 1.a File Format
  TCollection_AsciiString aStorageFormat (theDocument->StorageFormat(), '?');
  theElement.setAttribute ("format", aStorageFormat.ToCString());
//  theElement.setAttribute ("schema", "XSD");

  theElement.setAttribute ("xmlns" , "http://www.opencascade.org/OCAF/XML");
  for (i = 1; i <= mySeqOfNS.Length(); i++) {
    TCollection_AsciiString aPrefix =
      TCollection_AsciiString("xmlns:") + mySeqOfNS(i).Prefix().ToCString();
    theElement.setAttribute (aPrefix.ToCString(),
                             mySeqOfNS(i).URI().ToCString());
  }
  theElement.setAttribute ("xmlns:xsi",
                           "http://www.w3.org/2001/XMLSchema-instance");
  //mkv 15.09.05 OCC10001
  //theElement.setAttribute ("xsi:schemaLocation",
  //                         "http://www.opencascade.org/OCAF/XML"
  //                         " http://www.nnov.matra-dtv.fr/~agv/XmlOcaf.xsd");
  //
  // the order of search : by CSF_XmlOcafResource and then by CASROOT
  TCollection_AsciiString anHTTP = "http://www.opencascade.org/OCAF/XML";
  Standard_Boolean aToSetCSFVariable = Standard_False;
  const char * aCSFVariable [2] = {
    "CSF_XmlOcafResource",
    "CASROOT"
  };
  TCollection_AsciiString aResourceDir = "";
  aResourceDir = getenv (aCSFVariable[0]);
  if (aResourceDir.IsEmpty()) {
    // now try by CASROOT
    aResourceDir = getenv (aCSFVariable[1]);
#ifdef OCE_INSTALL_DATA_DIR
    if (aResourceDir.IsEmpty()) {
      aResourceDir = OCE_INSTALL_DATA_DIR;
    }
#endif
    if ( !aResourceDir.IsEmpty() ) {
      aResourceDir += "/src/XmlOcafResource" ;
      aToSetCSFVariable = Standard_True; //CSF variable to be set later
    }
#ifdef DEB
    else {
      TCollection_ExtendedString aWarn = FAILSTR "Neither ";
      aWarn = (aWarn + aCSFVariable[0] + ", nor " + aCSFVariable[1]
               + " variables have been set");
      aMessageDriver->Write (aWarn.ToExtString());
    }
#endif
  }
  if (!aResourceDir.IsEmpty()) {
    TCollection_AsciiString aResourceFileName =  aResourceDir + "/XmlOcaf.xsd";
    // search directory name that has been constructed, now check whether
    // it and the file exist
    OSD_File aResourceFile ( aResourceFileName );
    if ( aResourceFile.Exists() ) {
      if (aToSetCSFVariable) {
        OSD_Environment aCSFVarEnv ( aCSFVariable[0], aResourceDir );
        aCSFVarEnv.Build();
#ifdef DEB
        TCollection_ExtendedString aWarn1 = "Variable ";
        aWarn1 = (aWarn1 + aCSFVariable[0]
                  + " has not been explicitly defined. Set to " + aResourceDir);
        aMessageDriver->Write (aWarn1.ToExtString());
#endif
        if ( aCSFVarEnv.Failed() ) {
          TCollection_ExtendedString aWarn = FAILSTR "Failed to initialize ";
          aWarn = aWarn + aCSFVariable[0] + " with " + aResourceDir;
          aMessageDriver->Write (aWarn.ToExtString());
        }
      }
    }
#ifdef DEB
    else {
      TCollection_ExtendedString aWarn = FAILSTR "Schema definition file ";
      aWarn += (aResourceFileName + " was not found");
      aMessageDriver->Write (aWarn.ToExtString());
    }
#endif
    anHTTP = anHTTP + ' ' + aResourceFileName;
  }
  theElement.setAttribute ("xsi:schemaLocation", anHTTP.ToCString() );

  // 1.b Info section
  XmlObjMgt_Element anInfoElem = aDOMDoc.createElement("info");
  theElement.appendChild(anInfoElem);

  TCollection_AsciiString aCreationDate = XmlLDrivers::CreationDate();

//  anInfoElem.setAttribute("dbv", 0);
  anInfoElem.setAttribute("date", aCreationDate.ToCString());
  anInfoElem.setAttribute("schemav", 0);
//  anInfoElem.setAttribute("appv", anAppVersion.ToCString());

  // Document version
  anInfoElem.setAttribute("DocVersion", XmlLDrivers::StorageVersion().ToCString());
 
  // User info with Copyright
  TColStd_SequenceOfAsciiString aUserInfo;
  if (myCopyright.Length() > 0)
    aUserInfo.Append (TCollection_AsciiString(myCopyright,'?'));

  Handle(Storage_Data) theData = new Storage_Data;
  //PCDM_ReadWriter::WriteFileFormat( theData, theDocument );
  PCDM_ReadWriter::Writer()->WriteReferenceCounter(theData,theDocument);
  PCDM_ReadWriter::Writer()->WriteReferences(theData,theDocument,theFileName);
  PCDM_ReadWriter::Writer()->WriteExtensions(theData,theDocument);
  PCDM_ReadWriter::Writer()->WriteVersion(theData,theDocument);

  const TColStd_SequenceOfAsciiString& aRefs = theData->UserInfo();
  for(i = 1; i <= aRefs.Length(); i++)
    aUserInfo.Append(aRefs.Value(i));

  for (i = 1; i <= aUserInfo.Length(); i++)
  {
    XmlObjMgt_Element aUIItem = aDOMDoc.createElement ("iitem");
    anInfoElem.appendChild (aUIItem);
    LDOM_Text aUIText = aDOMDoc.createTextNode (aUserInfo(i).ToCString());
    aUIItem.appendChild (aUIText);
  }

  // 1.c Comments section
  TColStd_SequenceOfExtendedString aComments;
  theDocument->Comments(aComments);

  XmlObjMgt_Element aCommentsElem = aDOMDoc.createElement ("comments");
  theElement.appendChild (aCommentsElem);

  for (i = 1; i <= aComments.Length(); i++)
  {
    XmlObjMgt_Element aCItem = aDOMDoc.createElement ("citem");
    aCommentsElem.appendChild (aCItem);
    XmlObjMgt::SetExtendedString (aCItem, aComments(i));
  }

  // 2a. Write document contents
  Standard_Integer anObjNb = 0;
  {
    try
    {
      OCC_CATCH_SIGNALS
      anObjNb = MakeDocument(theDocument, theElement);
    }
    catch (Standard_Failure)
    {
      SetIsError (Standard_True);
      TCollection_ExtendedString anErrorString (Standard_Failure::Caught()->GetMessageString());
      aMessageDriver -> Write (anErrorString.ToExtString());
    }
  }
  if (anObjNb <= 0 && IsError() == Standard_False) {
    SetIsError (Standard_True);
    TCollection_ExtendedString anErrorString ("error occurred");
    aMessageDriver -> Write (anErrorString.ToExtString());
  }
  // 2b. Write number of objects into the info section
  anInfoElem.setAttribute("objnb", anObjNb);
  ::take_time (0, " +++++ Fin DOM data for OCAF : ", aMessageDriver);

  // 3. Clear relocation table
  //    If the application needs to use myRelocTable to store additional
  //    data to XML, this method should be reimplemented avoiding this step
  myRelocTable.Clear();

  // 4. Write Shapes section
  if(WriteShapeSection(theElement))
    ::take_time (0, " +++ Fin DOM data for Shapes : ", aMessageDriver);
  return IsError();
}

//=======================================================================
//function : MakeDocument
//purpose  : 
//=======================================================================
Standard_Integer XmlLDrivers_DocumentStorageDriver::MakeDocument
                                    (const Handle(CDM_Document)& theTDoc,
                                     XmlObjMgt_Element&          theElement) 
{  
  TCollection_ExtendedString aMessage;
  Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);  
  myRelocTable.Clear();
  if (!TDOC.IsNull()) 
  {
//    myRelocTable.SetDocument (theElement.getOwnerDocument());
    Handle(TDF_Data) aTDF = TDOC->GetData();

//      Find MessageDriver and pass it to AttributeDrivers()
    Handle(CDM_Application) anApplication= theTDoc -> Application();
    Handle(CDM_MessageDriver) aMessageDriver;
    if (anApplication.IsNull())
      aMessageDriver = new CDM_NullMessageDriver;
    else
      aMessageDriver = anApplication -> MessageDriver();
    if (myDrivers.IsNull()) myDrivers = AttributeDrivers (aMessageDriver);

//      Retrieve from DOM_Document
    XmlMDF::FromTo (aTDF, theElement, myRelocTable, myDrivers); 
#if defined(DEB) && !defined(TAKE_TIMES)
    aMessage = "First step successfull";
    aMessageDriver -> Write (aMessage.ToExtString());
#endif
    return myRelocTable.Extent();
  }
#ifdef DEB
  cout << "First step failed" << endl;  // No MessageDriver available
#endif
  return -1; // error
}

//=======================================================================
//function : AttributeDrivers
//purpose  : 
//=======================================================================
Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentStorageDriver::AttributeDrivers
       (const Handle(CDM_MessageDriver)& theMessageDriver) 
{
  return XmlLDrivers::AttributeDrivers (theMessageDriver);
}

//=======================================================================
//function : take_time
//class    : static
//purpose  : output astronomical time elapsed
//=======================================================================
#ifdef TAKE_TIMES
#include <time.h>
#include <sys/timeb.h>
#include <sys/types.h>
#include <stdio.h>
#ifndef WNT
extern "C" int ftime (struct timeb *tp);
#endif
struct timeb  tmbuf0;

static void take_time (const Standard_Integer isReset, const char * aHeader,
                       const Handle(CDM_MessageDriver)& aMessageDriver)
{
  struct timeb  tmbuf;
  ftime (&tmbuf);
  TCollection_ExtendedString aMessage ((Standard_CString)aHeader);
  if (isReset) tmbuf0 = tmbuf;
  else {
    char take_tm_buf [64];
    sprintf (take_tm_buf, "%9.2f s ++++",
             double(tmbuf.time - tmbuf0.time) +
             double(tmbuf.millitm - tmbuf0.millitm)/1000.);
    aMessage += take_tm_buf;
  }
  aMessageDriver -> Write (aMessage.ToExtString());
}
#endif

//=======================================================================
//function : WriteShapeSection
//purpose  : defines WriteShapeSection
//=======================================================================
Standard_Boolean XmlLDrivers_DocumentStorageDriver::WriteShapeSection
                                (XmlObjMgt_Element&  /*theElement*/)
{
  // empty; should be redefined in subclasses
  return Standard_False;
}