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
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.
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.
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.
21 // Modified by rmi, Wed Jan 14 08:17:35 1998
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>
43 #include <CDF_Timer.hxx>
45 #define theMetaDataDriver CDF_Session::CurrentSession()->MetaDataDriver()
48 //=======================================================================
51 //=======================================================================
52 CDF_Application::CDF_Application():myRetrievableStatus(PCDM_RS_OK) {}
54 //=======================================================================
57 //=======================================================================
58 Handle(CDF_Application) CDF_Application::Load(const Standard_GUID& aGUID) {
59 return Handle(CDF_Application)::DownCast(Plugin::Load(aGUID));
62 //=======================================================================
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);
72 //=======================================================================
75 //=======================================================================
76 CDM_CanCloseStatus CDF_Application::CanClose(const Handle(CDM_Document)& aDocument) {
77 return aDocument->CanClose();
80 //=======================================================================
83 //=======================================================================
84 void CDF_Application::Close(const Handle(CDM_Document)& aDocument) {
85 CDF_Session::CurrentSession()->Directory()->Remove(aDocument);
89 //=======================================================================
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);
100 //=======================================================================
101 //function : Retrieve
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) {
112 Handle(CDM_MetaData) theMetaData;
114 if(aVersion.Length() == 0)
115 theMetaData=theMetaDataDriver->MetaData(aFolder,aName);
117 theMetaData=theMetaDataDriver->MetaData(aFolder,aName,aVersion);
120 theTimer.ShowAndRestart("Getting MetaData: ");
123 CDF_TypeOfActivation theTypeOfActivation=TypeOfActivation(theMetaData);
124 Handle(CDM_Document) theDocument=Retrieve(theMetaData,UseStorageConfiguration,Standard_False);
127 theTimer.ShowAndRestart("Creating Transient: ");
130 CDF_Session::CurrentSession()->Directory()->Add(theDocument);
131 Activate(theDocument,theTypeOfActivation);
134 theTimer.ShowAndStop("Activate: ");
137 theDocument->Open(this);
143 //=======================================================================
144 //function : CanRetrieve
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);
152 //=======================================================================
153 //function : CanRetrieve
155 //=======================================================================
156 PCDM_ReaderStatus CDF_Application::CanRetrieve(const TCollection_ExtendedString& aFolder, const TCollection_ExtendedString& aName, const TCollection_ExtendedString& aVersion) {
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;
168 theTimer.ShowAndRestart("theMetaDataDriver->Find: ");
171 Handle(CDM_MetaData) theMetaData = theMetaDataDriver->MetaData(aFolder,aName,aVersion);
174 theTimer.ShowAndStop("Getting MetaData: ");
177 if(theMetaData->IsRetrieved()) {
178 return theMetaData->Document()->IsModified()
179 ? PCDM_RS_AlreadyRetrievedAndModified : PCDM_RS_AlreadyRetrieved;
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);
191 return PCDM_RS_UnrecognizedFileFormat;
193 if(!FindReaderFromFormat(theFormat)) return PCDM_RS_NoDriver;
202 //=======================================================================
203 //function : Activate
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 ) {
210 //=======================================================================
211 //function : DefaultFolder
213 //=======================================================================
214 Standard_ExtString CDF_Application::DefaultFolder(){
215 if(myDefaultFolder.Length() == 0) {
216 myDefaultFolder=CDF_Session::CurrentSession()->MetaDataDriver()->DefaultFolder();
218 return myDefaultFolder.ToExtString();
221 //=======================================================================
222 //function : SetDefaultFolder
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;
231 //=======================================================================
232 //function : DefaultExtension
234 //=======================================================================
235 Standard_ExtString CDF_Application::DefaultExtension() {
236 static TCollection_ExtendedString theDefaultExtension;
237 theDefaultExtension="*";
238 TColStd_SequenceOfExtendedString theFormats;
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();
249 return theDefaultExtension.ToExtString();
252 //=======================================================================
253 //function : Retrieve
255 //=======================================================================
256 Handle(CDM_Document) CDF_Application::Retrieve(const Handle(CDM_MetaData)& aMetaData,const Standard_Boolean UseStorageConfiguration) {
257 return Retrieve(aMetaData,UseStorageConfiguration,Standard_True);
260 //=======================================================================
261 //function : Retrieve
263 //=======================================================================
264 Handle(CDM_Document) CDF_Application::Retrieve(const Handle(CDM_MetaData)& aMetaData,const Standard_Boolean UseStorageConfiguration, const Standard_Boolean IsComponent) {
266 Handle(CDM_Document) theDocumentToReturn;
267 myRetrievableStatus = PCDM_RS_DriverFailure;
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);
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);
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) {
292 Handle(PCDM_Reader) theReader=Reader(aMetaData->FileName());
295 Handle(CDM_Document) theDocument;
298 theDocument=aMetaData->Document();
299 theDocument->RemoveAllReferences();
302 theDocument=theReader->CreateDocument();
304 SetReferenceCounter(theDocument,PCDM_RetrievalDriver::ReferenceCounter(aMetaData->FileName(), MessageDriver()));
306 SetDocumentVersion(theDocument,aMetaData);
307 theMetaDataDriver->ReferenceIterator()->LoadReferences(theDocument,aMetaData,this,UseStorageConfiguration);
311 theReader->Read(aMetaData->FileName(),theDocument,this);
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);
321 myRetrievableStatus = theReader->GetStatus();
322 theDocument->SetMetaData(aMetaData);
324 theDocumentToReturn=theDocument;
327 theDocumentToReturn=aMetaData->Document();
329 return theDocumentToReturn;
332 //=======================================================================
333 //function : DocumentVersion
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());
341 //=======================================================================
342 //function : TypeOfActivation
344 //=======================================================================
345 CDF_TypeOfActivation CDF_Application::TypeOfActivation(const Handle(CDM_MetaData)& aMetaData) {
347 if(aMetaData->IsRetrieved()) {
348 Handle(CDM_Document) theDocument=aMetaData->Document();
349 if(theDocument->IsOpened()) {
350 if(theDocument->IsModified())
351 return CDF_TOA_Modified;
353 return CDF_TOA_Unchanged;
363 //=======================================================================
364 //function : FindReader
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);
373 //=======================================================================
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);
385 return ReaderFromFormat (theFormat);
388 //=======================================================================
389 //function : FindReaderFromFormat
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);
399 //=======================================================================
400 //function : ReaderFromFormat
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);
412 Handle(PCDM_Reader) R;
415 R = Handle(PCDM_Reader)::DownCast(Plugin::Load(thePluginId));
417 catch (Standard_Failure) {
418 myRetrievableStatus = PCDM_RS_WrongResource;
419 Standard_SStream aMsg;
420 aMsg << Standard_Failure::Caught() << endl;
421 Standard_Failure::Raise(aMsg);
423 Handle(PCDM_RetrievalDriver) RD = Handle(PCDM_RetrievalDriver)::DownCast(R);
425 RD->SetFormat(aFormat);
428 myRetrievableStatus = PCDM_RS_WrongResource;
432 //=======================================================================
433 //function : FindReader
435 //=======================================================================
436 Standard_Boolean CDF_Application::FindReader(const TCollection_ExtendedString& aFileName, Standard_GUID& thePluginId, TCollection_ExtendedString& ResourceName) {
438 TCollection_ExtendedString theFormat=PCDM_ReadWriter::FileFormat(aFileName);
440 // It is good if the format is in the file. Otherwise base on the extension.
442 if(theFormat.Length()==0) {
443 ResourceName=UTL::Extension(aFileName);
444 ResourceName+=".FileFormat";
446 if(UTL::Find(Resources(),ResourceName)) {
447 theFormat=UTL::Value(Resources(),ResourceName);
450 return Standard_False;
452 return FindReaderFromFormat(theFormat,thePluginId,ResourceName);
455 //=======================================================================
458 //=======================================================================
459 Standard_Boolean CDF_Application::Format(const TCollection_ExtendedString& aFileName,
460 TCollection_ExtendedString& theFormat)
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";
470 if(UTL::Find(Resources(),ResourceName)) {
471 theFormat = UTL::Value(Resources(),ResourceName);
473 else return Standard_False;
475 return Standard_True;
478 //=======================================================================
479 //function : FindReaderFromFormat
481 //=======================================================================
482 Standard_Boolean CDF_Application::FindReaderFromFormat(const TCollection_ExtendedString& aFormat, Standard_GUID& thePluginId, TCollection_ExtendedString& ResourceName) {
484 ResourceName=aFormat;
485 ResourceName+=".RetrievalPlugin";
487 if(UTL::Find(Resources(),ResourceName)) {
488 thePluginId=UTL::GUID(UTL::Value(Resources(),ResourceName));
489 return Standard_True;
491 return Standard_False;
494 //=======================================================================
495 //function : CanRetrieve
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());
502 return CanRetrieve(aMetaData->Folder(),aMetaData->Name());