0026936: Drawbacks of inlining in new type system in OCCT 7.0 -- automatic
[occt.git] / src / CDF / CDF_Application.cxx
CommitLineData
b311480e 1// Created on: 1997-08-08
2// Created by: Jean-Louis Frenkel
3// Copyright (c) 1997-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
b311480e 16
7fd59977 17// Modified by rmi, Wed Jan 14 08:17:35 1998
18
42cf5bc1 19#include <CDF_Application.hxx>
7fd59977 20#include <CDF_Directory.hxx>
21#include <CDF_MetaDataDriver.hxx>
42cf5bc1 22#include <CDF_Session.hxx>
23#include <CDF_Timer.hxx>
24#include <CDM_CanCloseStatus.hxx>
25#include <CDM_Document.hxx>
26#include <CDM_MetaData.hxx>
27#include <PCDM_Reader.hxx>
15e8b082 28#include <PCDM_ReaderStatus.hxx>
7fd59977 29#include <PCDM_ReadWriter.hxx>
30#include <PCDM_RetrievalDriver.hxx>
31#include <PCDM_StorageDriver.hxx>
7fd59977 32#include <Plugin.hxx>
42cf5bc1 33#include <Standard_ErrorHandler.hxx>
34#include <Standard_GUID.hxx>
35#include <Standard_NoSuchObject.hxx>
36#include <Standard_ProgramError.hxx>
37#include <Standard_Type.hxx>
38#include <TCollection_ExtendedString.hxx>
7fd59977 39#include <UTL.hxx>
40
92efcf78 41IMPLEMENT_STANDARD_RTTIEXT(CDF_Application,CDM_Application)
42
7fd59977 43#define theMetaDataDriver CDF_Session::CurrentSession()->MetaDataDriver()
44
45
46//=======================================================================
47//function :
48//purpose :
49//=======================================================================
15e8b082 50CDF_Application::CDF_Application():myRetrievableStatus(PCDM_RS_OK) {}
7fd59977 51
52//=======================================================================
53//function : Load
54//purpose :
55//=======================================================================
56Handle(CDF_Application) CDF_Application::Load(const Standard_GUID& aGUID) {
57 return Handle(CDF_Application)::DownCast(Plugin::Load(aGUID));
58}
59
60//=======================================================================
61//function : Open
62//purpose :
63//=======================================================================
64void CDF_Application::Open(const Handle(CDM_Document)& aDocument) {
65 CDF_Session::CurrentSession()->Directory()->Add(aDocument);
66 aDocument->Open(this);
67 Activate(aDocument,CDF_TOA_New);
68}
69
70//=======================================================================
71//function : CanClose
72//purpose :
73//=======================================================================
74CDM_CanCloseStatus CDF_Application::CanClose(const Handle(CDM_Document)& aDocument) {
75 return aDocument->CanClose();
76}
77
78//=======================================================================
79//function : Close
80//purpose :
81//=======================================================================
82void CDF_Application::Close(const Handle(CDM_Document)& aDocument) {
83 CDF_Session::CurrentSession()->Directory()->Remove(aDocument);
84 aDocument->Close();
85}
86
87//=======================================================================
88//function : Retrieve
89//purpose :
90//=======================================================================
91Handle(CDM_Document) CDF_Application::Retrieve(const TCollection_ExtendedString& aFolder,
92 const TCollection_ExtendedString& aName,
93 const Standard_Boolean UseStorageConfiguration) {
94 TCollection_ExtendedString nullVersion;
95 return Retrieve(aFolder,aName,nullVersion,UseStorageConfiguration);
96}
97
98//=======================================================================
99//function : Retrieve
100//purpose :
101//=======================================================================
102Handle(CDM_Document) CDF_Application::Retrieve(const TCollection_ExtendedString& aFolder,
103 const TCollection_ExtendedString& aName,
104 const TCollection_ExtendedString& aVersion,
105 const Standard_Boolean UseStorageConfiguration) {
0797d9d3 106#ifdef OCCT_DEBUG
7fd59977 107 CDF_Timer theTimer;
108#endif
109
110 Handle(CDM_MetaData) theMetaData;
111
112 if(aVersion.Length() == 0)
113 theMetaData=theMetaDataDriver->MetaData(aFolder,aName);
114 else
115 theMetaData=theMetaDataDriver->MetaData(aFolder,aName,aVersion);
116
0797d9d3 117#ifdef OCCT_DEBUG
7fd59977 118 theTimer.ShowAndRestart("Getting MetaData: ");
119#endif
120
121 CDF_TypeOfActivation theTypeOfActivation=TypeOfActivation(theMetaData);
122 Handle(CDM_Document) theDocument=Retrieve(theMetaData,UseStorageConfiguration,Standard_False);
123
0797d9d3 124#ifdef OCCT_DEBUG
7fd59977 125 theTimer.ShowAndRestart("Creating Transient: ");
126#endif
127
128 CDF_Session::CurrentSession()->Directory()->Add(theDocument);
129 Activate(theDocument,theTypeOfActivation);
130
0797d9d3 131#ifdef OCCT_DEBUG
7fd59977 132 theTimer.ShowAndStop("Activate: ");
133#endif
134
135 theDocument->Open(this);
136 return theDocument;
137
138
139}
140
141//=======================================================================
142//function : CanRetrieve
143//purpose :
144//=======================================================================
15e8b082 145PCDM_ReaderStatus CDF_Application::CanRetrieve(const TCollection_ExtendedString& aFolder, const TCollection_ExtendedString& aName) {
7fd59977 146 TCollection_ExtendedString aVersion;
147 return CanRetrieve(aFolder,aName,aVersion);
148}
149
150//=======================================================================
151//function : CanRetrieve
152//purpose :
153//=======================================================================
15e8b082 154PCDM_ReaderStatus CDF_Application::CanRetrieve(const TCollection_ExtendedString& aFolder, const TCollection_ExtendedString& aName, const TCollection_ExtendedString& aVersion) {
7fd59977 155
0797d9d3 156#ifdef OCCT_DEBUG
7fd59977 157 CDF_Timer theTimer;
158#endif
159
160 if (!theMetaDataDriver->Find(aFolder,aName,aVersion))
15e8b082 161 return PCDM_RS_UnknownDocument;
7fd59977 162 else if (!theMetaDataDriver->HasReadPermission(aFolder,aName,aVersion))
15e8b082 163 return PCDM_RS_PermissionDenied;
7fd59977 164 else {
0797d9d3 165#ifdef OCCT_DEBUG
7fd59977 166 theTimer.ShowAndRestart("theMetaDataDriver->Find: ");
167#endif
168
169 Handle(CDM_MetaData) theMetaData = theMetaDataDriver->MetaData(aFolder,aName,aVersion);
170
0797d9d3 171#ifdef OCCT_DEBUG
7fd59977 172 theTimer.ShowAndStop("Getting MetaData: ");
173#endif
174
175 if(theMetaData->IsRetrieved()) {
176 return theMetaData->Document()->IsModified()
15e8b082 177 ? PCDM_RS_AlreadyRetrievedAndModified : PCDM_RS_AlreadyRetrieved;
7fd59977 178 }
179 else {
180 TCollection_ExtendedString theFileName=theMetaData->FileName();
181 TCollection_ExtendedString theFormat=PCDM_ReadWriter::FileFormat(theFileName);
182 if(theFormat.Length()==0) {
183 TCollection_ExtendedString ResourceName=UTL::Extension(theFileName);
184 ResourceName+=".FileFormat";
185 if(UTL::Find(Resources(),ResourceName)) {
186 theFormat=UTL::Value(Resources(),ResourceName);
187 }
188 else
15e8b082 189 return PCDM_RS_UnrecognizedFileFormat;
7fd59977 190 }
15e8b082 191 if(!FindReaderFromFormat(theFormat)) return PCDM_RS_NoDriver;
7fd59977 192 }
193 }
15e8b082 194 return PCDM_RS_OK;
7fd59977 195
196}
197
198
199
200//=======================================================================
201//function : Activate
202//purpose :
203//=======================================================================
204//void CDF_Application::Activate(const Handle(CDM_Document)& aDocument,const CDF_TypeOfActivation aTypeOfActivation) {
205void CDF_Application::Activate(const Handle(CDM_Document)& ,const CDF_TypeOfActivation ) {
206}
207
208//=======================================================================
209//function : DefaultFolder
210//purpose :
211//=======================================================================
212Standard_ExtString CDF_Application::DefaultFolder(){
213 if(myDefaultFolder.Length() == 0) {
214 myDefaultFolder=CDF_Session::CurrentSession()->MetaDataDriver()->DefaultFolder();
215 }
216 return myDefaultFolder.ToExtString();
217}
218
219//=======================================================================
220//function : SetDefaultFolder
221//purpose :
222//=======================================================================
223Standard_Boolean CDF_Application::SetDefaultFolder(const Standard_ExtString aFolder) {
224 Standard_Boolean found = CDF_Session::CurrentSession()->MetaDataDriver()->FindFolder(aFolder);
225 if(found) myDefaultFolder=aFolder;
226 return found;
227}
228
229//=======================================================================
230//function : DefaultExtension
231//purpose :
232//=======================================================================
233Standard_ExtString CDF_Application::DefaultExtension() {
234 static TCollection_ExtendedString theDefaultExtension;
235 theDefaultExtension="*";
236 TColStd_SequenceOfExtendedString theFormats;
237 Formats(theFormats);
238
239 for (Standard_Integer i=1; i<= theFormats.Length(); i++) {
240 TCollection_ExtendedString theResource(theFormats(i));
241 theResource+=".FileExtension";
242 if(UTL::Find(Resources(),theResource)) {
243 theDefaultExtension=UTL::Value(Resources(),theResource);
244 return theDefaultExtension.ToExtString();
245 }
246 }
247 return theDefaultExtension.ToExtString();
248}
249
250//=======================================================================
251//function : Retrieve
252//purpose :
253//=======================================================================
254Handle(CDM_Document) CDF_Application::Retrieve(const Handle(CDM_MetaData)& aMetaData,const Standard_Boolean UseStorageConfiguration) {
255 return Retrieve(aMetaData,UseStorageConfiguration,Standard_True);
256}
257
258//=======================================================================
7fd59977 259//function : Retrieve
260//purpose :
261//=======================================================================
262Handle(CDM_Document) CDF_Application::Retrieve(const Handle(CDM_MetaData)& aMetaData,const Standard_Boolean UseStorageConfiguration, const Standard_Boolean IsComponent) {
263
264 Handle(CDM_Document) theDocumentToReturn;
15e8b082 265 myRetrievableStatus = PCDM_RS_DriverFailure;
7fd59977 266 if(IsComponent) {
267 Standard_SStream aMsg;
268 switch (CanRetrieve(aMetaData)) {
15e8b082 269 case PCDM_RS_UnknownDocument:
7fd59977 270 aMsg << "could not find the referenced document: " << aMetaData->Path() << "; not found." <<(char)0 << endl;
15e8b082 271 myRetrievableStatus = PCDM_RS_UnknownDocument;
7fd59977 272 Standard_Failure::Raise(aMsg);
273 break;
15e8b082 274 case PCDM_RS_PermissionDenied:
7fd59977 275 aMsg << "Could not find the referenced document: " << aMetaData->Path() << "; permission denied. " <<(char)0 << endl;
15e8b082 276 myRetrievableStatus = PCDM_RS_PermissionDenied;
7fd59977 277 Standard_Failure::Raise(aMsg);
278 break;
279 default:
280 break;
281 }
282
283 }
284 Standard_Boolean AlreadyRetrieved=aMetaData->IsRetrieved();
15e8b082 285 if(AlreadyRetrieved) myRetrievableStatus = PCDM_RS_AlreadyRetrieved;
7fd59977 286 Standard_Boolean Modified=AlreadyRetrieved && aMetaData->Document()->IsModified();
15e8b082 287 if(Modified) myRetrievableStatus = PCDM_RS_AlreadyRetrievedAndModified;
7fd59977 288 if(!AlreadyRetrieved || Modified) {
289
290 Handle(PCDM_Reader) theReader=Reader(aMetaData->FileName());
291
292
293 Handle(CDM_Document) theDocument;
294
295 if(Modified) {
296 theDocument=aMetaData->Document();
297 theDocument->RemoveAllReferences();
298 }
299 else
300 theDocument=theReader->CreateDocument();
301
302 SetReferenceCounter(theDocument,PCDM_RetrievalDriver::ReferenceCounter(aMetaData->FileName(), MessageDriver()));
303
304 SetDocumentVersion(theDocument,aMetaData);
305 theMetaDataDriver->ReferenceIterator()->LoadReferences(theDocument,aMetaData,this,UseStorageConfiguration);
306
307 try {
308 OCC_CATCH_SIGNALS
309 theReader->Read(aMetaData->FileName(),theDocument,this);
310 }
311 catch (Standard_Failure) {
15e8b082
M
312 myRetrievableStatus = theReader->GetStatus();
313 if(myRetrievableStatus > PCDM_RS_AlreadyRetrieved){
7fd59977 314 Standard_SStream aMsg;
315 aMsg << Standard_Failure::Caught() << endl;
316 Standard_Failure::Raise(aMsg);
317 }
318 }
15e8b082 319 myRetrievableStatus = theReader->GetStatus();
7fd59977 320 theDocument->SetMetaData(aMetaData);
321
322 theDocumentToReturn=theDocument;
323 }
324 else
325 theDocumentToReturn=aMetaData->Document();
326
327 return theDocumentToReturn;
328}
329
330//=======================================================================
331//function : DocumentVersion
332//purpose :
333//=======================================================================
334Standard_Integer CDF_Application::DocumentVersion(const Handle(CDM_MetaData)& theMetaData) {
335// const Handle(CDM_MessageDriver)& aMsgDriver = MessageDriver();
336 return PCDM_RetrievalDriver::DocumentVersion(theMetaData->FileName(), MessageDriver());
337}
338
339//=======================================================================
340//function : TypeOfActivation
341//purpose :
342//=======================================================================
343CDF_TypeOfActivation CDF_Application::TypeOfActivation(const Handle(CDM_MetaData)& aMetaData) {
344
345 if(aMetaData->IsRetrieved()) {
346 Handle(CDM_Document) theDocument=aMetaData->Document();
347 if(theDocument->IsOpened()) {
348 if(theDocument->IsModified())
349 return CDF_TOA_Modified;
350 else
351 return CDF_TOA_Unchanged;
352 }
353
354 else
355 return CDF_TOA_New;
356 }
357 return CDF_TOA_New;
358}
359
360
361//=======================================================================
362//function : FindReader
363//purpose :
364//=======================================================================
365Standard_Boolean CDF_Application::FindReader(const TCollection_ExtendedString& aFileName) {
366 Standard_GUID voidGUID;
367 TCollection_ExtendedString voidResourceName;
368 return FindReader(aFileName,voidGUID,voidResourceName);
369}
370
371//=======================================================================
372//function : Reader
373//purpose : code dp
374//=======================================================================
375Handle(PCDM_Reader) CDF_Application::Reader (const TCollection_ExtendedString& aFileName) {
376 TCollection_ExtendedString theFormat;
377 if (!Format(aFileName,theFormat)) {
378 Standard_SStream aMsg;
379 aMsg << "Could not found the format" <<(char)0;
380 Standard_NoSuchObject::Raise(aMsg);
381
382 }
383 return ReaderFromFormat (theFormat);
384}
385
386//=======================================================================
387//function : FindReaderFromFormat
388//purpose :
389//=======================================================================
390Standard_Boolean CDF_Application::FindReaderFromFormat(const TCollection_ExtendedString& aFormat) {
391 Standard_GUID voidGUID;
392 TCollection_ExtendedString voidResourceName;
393 return FindReaderFromFormat(aFormat,voidGUID,voidResourceName);
394}
395
396
397//=======================================================================
398//function : ReaderFromFormat
399//purpose :
400//=======================================================================
401Handle(PCDM_Reader) CDF_Application::ReaderFromFormat(const TCollection_ExtendedString& aFormat) {
402 TCollection_ExtendedString UnfoundResourceName;
403 Standard_GUID thePluginId;
404 if(!FindReaderFromFormat(aFormat,thePluginId,UnfoundResourceName)) {
405 Standard_SStream aMsg;
406 aMsg << "Could not found the item:" << UnfoundResourceName <<(char)0;
15e8b082 407 myRetrievableStatus = PCDM_RS_WrongResource;
7fd59977 408 Standard_NoSuchObject::Raise(aMsg);
409 }
410 Handle(PCDM_Reader) R;
411 try {
412 OCC_CATCH_SIGNALS
413 R = Handle(PCDM_Reader)::DownCast(Plugin::Load(thePluginId));
414 }
415 catch (Standard_Failure) {
15e8b082 416 myRetrievableStatus = PCDM_RS_WrongResource;
7fd59977 417 Standard_SStream aMsg;
418 aMsg << Standard_Failure::Caught() << endl;
419 Standard_Failure::Raise(aMsg);
420 }
421 Handle(PCDM_RetrievalDriver) RD = Handle(PCDM_RetrievalDriver)::DownCast(R);
422 if (!RD.IsNull()) {
423 RD->SetFormat(aFormat);
424 return RD;
425 } else
15e8b082 426 myRetrievableStatus = PCDM_RS_WrongResource;
7fd59977 427 return R;
428}
429
430//=======================================================================
431//function : FindReader
432//purpose :
433//=======================================================================
434Standard_Boolean CDF_Application::FindReader(const TCollection_ExtendedString& aFileName, Standard_GUID& thePluginId, TCollection_ExtendedString& ResourceName) {
435
436 TCollection_ExtendedString theFormat=PCDM_ReadWriter::FileFormat(aFileName);
437
0d969553 438// It is good if the format is in the file. Otherwise base on the extension.
7fd59977 439
440 if(theFormat.Length()==0) {
441 ResourceName=UTL::Extension(aFileName);
442 ResourceName+=".FileFormat";
443
444 if(UTL::Find(Resources(),ResourceName)) {
445 theFormat=UTL::Value(Resources(),ResourceName);
446 }
447 else
448 return Standard_False;
449 }
450 return FindReaderFromFormat(theFormat,thePluginId,ResourceName);
451}
452
453//=======================================================================
454//function : Format
455//purpose : dp
456//=======================================================================
457Standard_Boolean CDF_Application::Format(const TCollection_ExtendedString& aFileName,
458 TCollection_ExtendedString& theFormat)
459{
460
461 theFormat = PCDM_ReadWriter::FileFormat(aFileName);
0d969553 462// It is good if the format is in the file. Otherwise base on the extension.
7fd59977 463 if(theFormat.Length()==0) {
464 TCollection_ExtendedString ResourceName;
465 ResourceName=UTL::Extension(aFileName);
466 ResourceName+=".FileFormat";
467
468 if(UTL::Find(Resources(),ResourceName)) {
469 theFormat = UTL::Value(Resources(),ResourceName);
470 }
471 else return Standard_False;
472 }
473 return Standard_True;
474}
475
476//=======================================================================
477//function : FindReaderFromFormat
478//purpose :
479//=======================================================================
480Standard_Boolean CDF_Application::FindReaderFromFormat(const TCollection_ExtendedString& aFormat, Standard_GUID& thePluginId, TCollection_ExtendedString& ResourceName) {
481
482 ResourceName=aFormat;
483 ResourceName+=".RetrievalPlugin";
484
485 if(UTL::Find(Resources(),ResourceName)) {
bf4bf038 486 // Get GUID as a string.
487 TCollection_ExtendedString strPluginId = UTL::Value(Resources(),ResourceName);
488
489 // If the GUID (as a string) contains blanks, remove them.
490 if (strPluginId.Search(' ') != -1)
491 strPluginId.RemoveAll(' ');
492
493 // Convert to GUID.
494 thePluginId=UTL::GUID(strPluginId);
7fd59977 495 return Standard_True;
496 }
497 return Standard_False;
498}
499
500//=======================================================================
501//function : CanRetrieve
502//purpose :
503//=======================================================================
15e8b082 504PCDM_ReaderStatus CDF_Application::CanRetrieve(const Handle(CDM_MetaData)& aMetaData) {
7fd59977 505 if(aMetaData->HasVersion())
506 return CanRetrieve(aMetaData->Folder(),aMetaData->Name(),aMetaData->Version());
507 else
508 return CanRetrieve(aMetaData->Folder(),aMetaData->Name());
509}