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