summaryrefslogtreecommitdiff
path: root/src/XmlLDrivers/XmlLDrivers_DocumentRetrievalDriver.cxx
blob: abc76bc0972a3223fa720587009a01f4e605006f (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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// File:      XmlLDrivers_DocumentRetrievalDriver.cxx
// Created:   Mon Jul  9 12:29:49 MSK DST 2001
// Author:    Julia DOROVSKIKH
// Copyright: Open Cascade 2001

#include <XmlLDrivers_DocumentRetrievalDriver.ixx>

#include <XmlLDrivers.hxx>
#include <XmlMDF.hxx>
#include <XmlObjMgt_RRelocationTable.hxx>
#include <XmlObjMgt_Document.hxx>
#include <XmlObjMgt.hxx>
#include <XmlMDataStd.hxx>
#include <LDOM_LDOMImplementation.hxx>
#include <LDOM_DocumentType.hxx>
#include <LDOMParser.hxx>

#include <TDF_Data.hxx>
#include <TDocStd_Owner.hxx>
#include <TDocStd_Document.hxx>
#include <CDM_MessageDriver.hxx>
#include <CDM_MetaData.hxx>
#include <TCollection_AsciiString.hxx>
#include <UTL.hxx>
#include <OSD_Path.hxx>

#ifdef WNT
# include <tchar.h>
#endif  // WNT

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

#define START_REF         "START_REF"
#define END_REF           "END_REF"
#define REFERENCE_COUNTER "REFERENCE_COUNTER"


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

static Standard_Integer RemoveExtraSeparator(TCollection_AsciiString& aString) {

  Standard_Integer i, j, len ;

  len = aString.Length() ;
#ifdef WNT
  // Case of network path, such as \\MACHINE\dir
  for (i = j = 2 ; j <= len ; i++,j++) {
#else
  for (i = j = 1 ; j <= len ; i++,j++) {
#endif
      Standard_Character c = aString.Value(j) ;
      aString.SetValue(i,c) ;
      if (c == '/')
          while(j < len && aString.Value(j+1) == '/') j++ ;
  }
  len = i-1 ;
  if (aString.Value(len) == '/') len-- ;  
  aString.Trunc(len) ;
  return len ;
}
static TCollection_AsciiString GetDirFromFile(const TCollection_ExtendedString& aFileName) {
  TCollection_AsciiString theCFile=UTL::CString(aFileName);
  TCollection_AsciiString theDirectory;
  Standard_Integer i=theCFile.SearchFromEnd("/");
#ifdef WNT    
//    if(i==-1) i=theCFile.SearchFromEnd("\\");
  if(theCFile.SearchFromEnd("\\") > i)
    i=theCFile.SearchFromEnd("\\");
#endif
  if(i!=-1) theDirectory=theCFile.SubString(1,i);
  return theDirectory;
}

static TCollection_AsciiString AbsolutePath(
                            const TCollection_AsciiString& aDirPath,
                            const TCollection_AsciiString& aRelFilePath)
{
  TCollection_AsciiString EmptyString = "" ;
#ifdef WNT
  if (aRelFilePath.Search(":") == 2 ||
      (aRelFilePath.Search("\\") == 1 && aRelFilePath.Value(2) == '\\'))
#else
  if(aRelFilePath.Search("/") == 1)
#endif
    return aRelFilePath ;
  
  TCollection_AsciiString DirPath = aDirPath, RelFilePath = aRelFilePath  ;
  Standard_Integer i,len ;
  
#ifdef WNT
  if(DirPath.Search(":") != 2 &&
     (DirPath.Search("\\") != 1 || DirPath.Value(2) != '\\'))
#else
  if (DirPath.Search("/") != 1 )
#endif
    return EmptyString ;

#ifdef WNT
  DirPath.ChangeAll('\\','/') ;
  RelFilePath.ChangeAll('\\','/') ;      
#endif
  
  RemoveExtraSeparator(DirPath) ;
  len = RemoveExtraSeparator(RelFilePath) ;
  
  while (RelFilePath.Search("../") == 1) {
      if (len == 3)
    return EmptyString ;
      RelFilePath = RelFilePath.SubString(4,len) ;
      len -= 3 ;
      if (DirPath.IsEmpty())
    return EmptyString ;
      i = DirPath.SearchFromEnd("/") ;
      if (i < 0)
          return EmptyString ;
      DirPath.Trunc(i-1) ;
  }  
  TCollection_AsciiString retx;
  retx= DirPath;
  retx+= "/";
  retx+=RelFilePath ;
  return retx;
}

//=======================================================================
//function : XmlLDrivers_DocumentRetrievalDriver
//purpose  : Constructor
//=======================================================================
XmlLDrivers_DocumentRetrievalDriver::XmlLDrivers_DocumentRetrievalDriver()
{
  myReaderStatus = PCDM_RS_OK;
}

//=======================================================================
//function : CreateDocument
//purpose  : pure virtual method definition
//=======================================================================
Handle(CDM_Document) XmlLDrivers_DocumentRetrievalDriver::CreateDocument()
{
  return new TDocStd_Document(PCDM_RetrievalDriver::GetFormat());
}

//=======================================================================
//function : SchemaName
//purpose  : pure virtual method definition
//=======================================================================
TCollection_ExtendedString XmlLDrivers_DocumentRetrievalDriver::SchemaName() const
{
  TCollection_ExtendedString schemaname;
  return schemaname; 
}

//=======================================================================
//function : Make
//purpose  : pure virtual method definition
//=======================================================================
void XmlLDrivers_DocumentRetrievalDriver::Make (const Handle(PCDM_Document)&,
                                               const Handle(CDM_Document&))
{
}

//=======================================================================
//function : Read
//purpose  : 
//=======================================================================
void XmlLDrivers_DocumentRetrievalDriver::Read
  (const TCollection_ExtendedString& theFileName,
   const Handle(CDM_Document)&       theNewDocument,
   const Handle(CDM_Application)&    theApplication)
{
  myReaderStatus = PCDM_RS_DriverFailure;
  myFileName = theFileName;
  const TCollection_AsciiString anOldNumLocale =
    (Standard_CString) setlocale (LC_NUMERIC, NULL);
  setlocale(LC_NUMERIC, "C");
  
  Handle(CDM_MessageDriver) aMessageDriver = theApplication -> MessageDriver();
  ::take_time (~0, " +++++ Start RETRIEVE procedures ++++++", aMessageDriver);

  // 1. Read DOM_Document from file
  LDOMParser aParser;
  TCollection_AsciiString aName (theFileName,'?');
  if (aParser.parse(aName.ToCString()))
  {
    TCollection_AsciiString aData;
    cout << aParser.GetError(aData) << ": " << aData << endl;
    myReaderStatus = PCDM_RS_FormatFailure;
    return;
  }
  const XmlObjMgt_Element anElement= aParser.getDocument().getDocumentElement();
  ::take_time (0, " +++++ Fin parsing XML :       ", aMessageDriver);

  ReadFromDomDocument (anElement, theNewDocument, theApplication);

  setlocale(LC_NUMERIC, (char *) anOldNumLocale.ToCString()) ;
}

//=======================================================================
//function : ReadFromDomDocument
//purpose  : management of the macro-structure of XML document data
//remark   : If the application needs to use myRelocTable to retrieve additional
//           data from LDOM, this method should be reimplemented
//=======================================================================

void XmlLDrivers_DocumentRetrievalDriver::ReadFromDomDocument
                                (const XmlObjMgt_Element&       theElement,
                                 const Handle(CDM_Document)&    theNewDocument,
                                 const Handle(CDM_Application)& theApplication)
{
  const Handle(CDM_MessageDriver) aMsgDriver =
    theApplication -> MessageDriver();
  // 1. Read info // to be done
  TCollection_AsciiString anAbsoluteDirectory = GetDirFromFile(myFileName);
  Standard_Integer aCurDocVersion = 0;
  TCollection_ExtendedString anInfo;
  const XmlObjMgt_Element anInfoElem =
    theElement.GetChildByTagName ("info");
  if (anInfoElem != NULL) {
    XmlObjMgt_DOMString aDocVerStr = anInfoElem.getAttribute("DocVersion");
    if(aDocVerStr == NULL)
      aCurDocVersion = 2;
    else if (!aDocVerStr.GetInteger(aCurDocVersion)) {
      TCollection_ExtendedString aMsg =
        TCollection_ExtendedString ("Cannot retrieve the current Document version"
                                    " attribute as \"") + aDocVerStr + "\"";
      if(!aMsgDriver.IsNull()) 
        aMsgDriver->Write(aMsg.ToExtString());
    }
    
    // oan: OCC22305 - check a document verison and if it's greater than
    // current version of storage driver set an error status and return
    if( aCurDocVersion > XmlLDrivers::StorageVersion().IntegerValue() )
    {
      TCollection_ExtendedString aMsg =
        TCollection_ExtendedString ("error: wrong file version: ") +
                                    aDocVerStr  + " while current is " +
                                    XmlLDrivers::StorageVersion();
      myReaderStatus = PCDM_RS_NoVersion;
      if(!aMsgDriver.IsNull()) 
        aMsgDriver->Write(aMsg.ToExtString());
      return;
    }

    if( aCurDocVersion < 2) aCurDocVersion = 2;

    PropagateDocumentVersion(aCurDocVersion);

    Standard_Boolean isRef = Standard_False;
    for (LDOM_Node aNode = anInfoElem.getFirstChild();
         aNode != NULL; aNode = aNode.getNextSibling()) {
      if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE) {
        if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, anInfo)) {

    // Read ref counter
    if(anInfo.Search(REFERENCE_COUNTER) != -1) {
      try {
        OCC_CATCH_SIGNALS
        TCollection_AsciiString anInf(anInfo,'?');
      }
      catch (Standard_Failure) { 
        //    cout << "warning: could not read the reference counter in " << aFileName << endl;
        TCollection_ExtendedString aMsg("Warning: ");
        aMsg = aMsg.Cat("could not read the reference counter").Cat("\0");
        if(!aMsgDriver.IsNull()) 
    aMsgDriver->Write(aMsg.ToExtString());
      }
    }
    
    if(anInfo == END_REF)
      isRef = Standard_False;
    if(isRef) { // Process References
      
      Standard_Integer pos=anInfo.Search(" ");
      if(pos != -1) {
        // Parce RefId, DocumentVersion and FileName
        Standard_Integer aRefId;
        TCollection_ExtendedString aFileName;
        Standard_Integer aDocumentVersion;


        TCollection_ExtendedString aRest=anInfo.Split(pos);
        aRefId = UTL::IntegerValue(anInfo);
        
        Standard_Integer pos2 = aRest.Search(" ");
        
        aFileName = aRest.Split(pos2);
        aDocumentVersion = UTL::IntegerValue(aRest);
        
        TCollection_AsciiString aPath = UTL::CString(aFileName);
        TCollection_AsciiString anAbsolutePath;
        if(!anAbsoluteDirectory.IsEmpty()) {
    anAbsolutePath = AbsolutePath(anAbsoluteDirectory,aPath);
    if(!anAbsolutePath.IsEmpty()) aPath=anAbsolutePath;
        }
        if(!aMsgDriver.IsNull()) {
    //      cout << "reference found; ReferenceIdentifier: " << theReferenceIdentifier << "; File:" << thePath << ", version:" << theDocumentVersion;
    TCollection_ExtendedString aMsg("Warning: ");
    aMsg = aMsg.Cat("reference found; ReferenceIdentifier:  ").Cat(aRefId).Cat("; File:").Cat(aPath).Cat(", version:").Cat(aDocumentVersion).Cat("\0");
    aMsgDriver->Write(aMsg.ToExtString());
        }
        // Add new ref!
        /////////////
    TCollection_ExtendedString theFolder,theName;
        //TCollection_ExtendedString theFile=myReferences(myIterator).FileName();
        TCollection_ExtendedString f(aPath);
#ifndef WNT
        
        Standard_Integer i= f.SearchFromEnd("/");
        TCollection_ExtendedString n = f.Split(i); 
        f.Trunc(f.Length()-1);
        theFolder = f;
        theName = n;
#else
        OSD_Path p = UTL::Path(f);
        Standard_ExtCharacter      chr;
        TCollection_ExtendedString dir, dirRet, name;
        
        dir = UTL::Disk(p);
        dir += UTL::Trek(p);
        
        for ( int i = 1; i <= dir.Length (); ++i ) {
    
    chr = dir.Value ( i );
    
    switch ( chr ) {
      
    case _TEXT( '|' ):
      dirRet += _TEXT( "/" );
      break;
      
    case _TEXT( '^' ):
      
      dirRet += _TEXT( ".." );
      break;
      
    default:
      dirRet += chr;
      
    }  
        }
        theFolder = dirRet;
        theName   = UTL::Name(p); theName+= UTL::Extension(p);
#endif  // WNT
        
        Handle(CDM_MetaData) aMetaData =  CDM_MetaData::LookUp(theFolder,theName,aPath,aPath,UTL::IsReadOnly(aFileName));
////////////
        theNewDocument->CreateReference(aMetaData,aRefId,
             theApplication,aDocumentVersion,Standard_False);

        
      }

      
    }
    if(anInfo == START_REF)
      isRef = Standard_True;
        }
      }
    }
  }

  // 2. Read comments
  TCollection_ExtendedString aComment;
  const XmlObjMgt_Element aCommentsElem =
    theElement.GetChildByTagName ("comments");
  if (aCommentsElem != NULL)
  {
    for (LDOM_Node aNode = aCommentsElem.getFirstChild();
         aNode != NULL; aNode = aNode.getNextSibling())
    {
      if (aNode.getNodeType() == LDOM_Node::ELEMENT_NODE)
      {
        if (XmlObjMgt::GetExtendedString ((LDOM_Element&)aNode, aComment))
        {
          theNewDocument->AddComment(aComment);
        }
      }
    }
  }

  // 2. Read Shapes section
  if (myDrivers.IsNull()) myDrivers = AttributeDrivers (aMsgDriver);  
  const Handle(XmlMDF_ADriver) aNSDriver = ReadShapeSection(theElement, aMsgDriver);
  if(!aNSDriver.IsNull())
    ::take_time (0, " +++++ Fin reading Shapes :    ", aMsgDriver);

  // 5. Read document contents
  try
  {
    OCC_CATCH_SIGNALS
#if defined(DEB) && !defined(TAKE_TIMES)
    TCollection_ExtendedString aMessage ("PasteDocument");
    aMsgDriver -> Write (aMessage.ToExtString());
#endif
    if (!MakeDocument(theElement, theNewDocument))
      myReaderStatus = PCDM_RS_MakeFailure;
    else
      myReaderStatus = PCDM_RS_OK;
  }
  catch (Standard_Failure)
  {
    TCollection_ExtendedString anErrorString (Standard_Failure::Caught()->GetMessageString());
    aMsgDriver -> Write (anErrorString.ToExtString());
  }

  //    Wipe off the shapes written to the <shapes> section
  ShapeSetCleaning(aNSDriver);

  //    Clean the relocation table.
  //    If the application needs to use myRelocTable to retrieve additional
  //    data from LDOM, this method should be reimplemented avoiding this step
  myRelocTable.Clear();
  ::take_time (0, " +++++ Fin reading data OCAF : ", aMsgDriver);
}

//=======================================================================
//function : MakeDocument
//purpose  : 
//=======================================================================
Standard_Boolean XmlLDrivers_DocumentRetrievalDriver::MakeDocument
                                    (const XmlObjMgt_Element&    theElement,
                                     const Handle(CDM_Document)& theTDoc)
{
  Standard_Boolean aResult = Standard_False;
  Handle(TDocStd_Document) TDOC = Handle(TDocStd_Document)::DownCast(theTDoc);
  myRelocTable.Clear();
  if (!TDOC.IsNull()) 
  {
    Handle(TDF_Data) aTDF = new TDF_Data();
    aResult = XmlMDF::FromTo (theElement, aTDF, myRelocTable, myDrivers);
    if (aResult) {
      TDOC->SetData (aTDF);
      TDocStd_Owner::SetDocument (aTDF, TDOC);
    }
  }
  return aResult;
}

//=======================================================================
//function : AttributeDrivers
//purpose  : 
//=======================================================================
Handle(XmlMDF_ADriverTable) XmlLDrivers_DocumentRetrievalDriver::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
extern 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 : PropagateDocumentVersion
//purpose  : 
//=======================================================================
void XmlLDrivers_DocumentRetrievalDriver::PropagateDocumentVersion(
                                   const Standard_Integer theDocVersion )
{
#ifdef DEB
//    cout << "DocCurVersion =" << theDocVersion <<endl;
#endif
  XmlMDataStd::SetDocumentVersion(theDocVersion);
}

//=======================================================================
//function : ReadShapeSection
//purpose  : definition of ReadShapeSection
//=======================================================================
Handle(XmlMDF_ADriver) XmlLDrivers_DocumentRetrievalDriver::ReadShapeSection(
                               const XmlObjMgt_Element&       /*theElement*/,
             const Handle(CDM_MessageDriver)& /*aMsgDriver*/)
{
  Handle(XmlMDF_ADriver) aDriver;
  //empty; to be redefined
  return aDriver;
}

//=======================================================================
//function : ShapeSetCleaning
//purpose  : definition of ShapeSetCleaning
//=======================================================================
void XmlLDrivers_DocumentRetrievalDriver::ShapeSetCleaning(
            const Handle(XmlMDF_ADriver)& /*theDriver*/) 
{}