c44e0c099008f9c311d004509d9393bea31acd67
[occt.git] / src / PCDM / PCDM_StorageDriver.cxx
1 // Created on: 1997-08-07
2 // Created by: Jean-Louis Frenkel
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <CDM_Document.hxx>
19 #include <FSD_CmpFile.hxx>
20 #include <PCDM.hxx>
21 #include <PCDM_Document.hxx>
22 #include <PCDM_DriverError.hxx>
23 #include <PCDM_ReadWriter.hxx>
24 #include <PCDM_SequenceOfDocument.hxx>
25 #include <PCDM_StorageDriver.hxx>
26 #include <Resource_Manager.hxx>
27 #include <Standard_ErrorHandler.hxx>
28 #include <Standard_NoSuchObject.hxx>
29 #include <Standard_NotImplemented.hxx>
30 #include <Standard_Type.hxx>
31 #include <Storage_Data.hxx>
32 #include <Storage_OpenMode.hxx>
33 #include <Storage_Schema.hxx>
34 #include <TCollection_AsciiString.hxx>
35 #include <TCollection_ExtendedString.hxx>
36 #include <TColStd_SequenceOfExtendedString.hxx>
37
38 #include <locale.h>
39 IMPLEMENT_STANDARD_RTTIEXT(PCDM_StorageDriver,PCDM_Writer)
40
41 #define STORAGE_VERSION "STORAGE_VERSION:"
42
43
44
45 void PCDM_StorageDriver::Write(const Handle(CDM_Document)& aDocument, const TCollection_ExtendedString&  aFileName) 
46 {
47   Handle(Storage_Schema) theSchema = new Storage_Schema;
48
49   Handle(Storage_Data) theData = new Storage_Data;
50
51   static Standard_Boolean Failure;
52   Failure=Standard_False;
53   Standard_SStream aMsg; aMsg << "error during Make:";
54   PCDM_SequenceOfDocument thePersistentDocuments;
55   {
56     try { OCC_CATCH_SIGNALS Make(aDocument,thePersistentDocuments);}
57     catch (Standard_Failure const& anException) {
58       aMsg << anException<<(char)0;
59       Failure=Standard_True;
60     }
61   }
62
63   if(Failure) throw PCDM_DriverError(aMsg.str().c_str());
64
65   if(thePersistentDocuments.IsEmpty()) {
66     aMsg << "the storage driver: " << DynamicType()->Name() << "returned no documents to store" << (char) 0;
67     throw PCDM_DriverError(aMsg.str().c_str());
68   }
69
70   Standard_Integer i=1;
71   for(; i<=thePersistentDocuments.Length(); i++) {
72     theData->AddRoot(thePersistentDocuments(i));
73   }
74
75   TCollection_AsciiString ligne(STORAGE_VERSION);
76   ligne+=PCDM_ReadWriter::Writer()->Version();
77   theData->AddToUserInfo(ligne);
78
79   PCDM_ReadWriter::WriteFileFormat(theData,aDocument);
80   PCDM_ReadWriter::Writer()->WriteReferenceCounter(theData,aDocument);
81   PCDM_ReadWriter::Writer()->WriteReferences(theData,aDocument,aFileName);
82   PCDM_ReadWriter::Writer()->WriteExtensions(theData,aDocument);
83   PCDM_ReadWriter::Writer()->WriteVersion(theData,aDocument);
84
85   // add document comments
86   TColStd_SequenceOfExtendedString aComments;
87   aDocument->Comments(aComments);
88   Standard_Integer aLen = aComments.Length();
89   for (i = 1; i <= aLen; i++)
90   {
91     theData->AddToComments(aComments(i));
92   }
93
94   Handle(FSD_CmpFile) theFile = new FSD_CmpFile;
95   PCDM_ReadWriter::Open(theFile,aFileName,Storage_VSWrite);
96   theSchema->Write(theFile,theData);
97   theFile->Close();
98
99   if ( theData->ErrorStatus() != Storage_VSOk )
100     throw PCDM_DriverError(theData->ErrorStatusExtension().ToCString());
101 }
102
103
104 //=======================================================================
105 //function : Write
106 //purpose  : 
107 //=======================================================================
108 void PCDM_StorageDriver::Write (const Handle(CDM_Document)& /*aDocument*/, Standard_OStream& /*theOStream*/) 
109 {
110   
111 }
112
113 //Handle(PCDM_Document) PCDM_StorageDriver::Make(const Handle(CDM_Document)& aDocument) {
114 Handle(PCDM_Document) PCDM_StorageDriver::Make(const Handle(CDM_Document)& ) {
115   Handle(PCDM_Document) voidDocument;
116   Standard_SStream aMsg;
117   aMsg << "No Make method were implemented in this Driver" << DynamicType()->Name() << (char) 0;
118   throw Standard_NotImplemented(aMsg.str().c_str());
119 }
120
121 void PCDM_StorageDriver::Make(const Handle(CDM_Document)& aDocument, PCDM_SequenceOfDocument& Documents) {
122   Documents.Append(Make(aDocument));
123 }
124
125 //=======================================================================
126 //function : SetFormat
127 //purpose  : 
128 //=======================================================================
129
130 void PCDM_StorageDriver::SetFormat (const TCollection_ExtendedString& aformat)
131 {
132   myFormat = aformat;
133 }
134
135
136 //=======================================================================
137 //function : GetFormat
138 //purpose  : 
139 //=======================================================================
140
141 TCollection_ExtendedString PCDM_StorageDriver::GetFormat () const
142 {
143   return myFormat;
144 }
145
146 //=======================================================================
147 //function : IsError
148 //purpose  :
149 //=======================================================================
150
151 Standard_Boolean PCDM_StorageDriver::IsError () const 
152 {
153   return myIsError;
154 }
155
156 //=======================================================================
157 //function : SetIsError
158 //purpose  :
159 //=======================================================================
160 void PCDM_StorageDriver::SetIsError(const Standard_Boolean theIsError)
161 {
162     myIsError = theIsError;
163 }
164
165 //=======================================================================
166 //function : GetStoreStatus
167 //purpose  :
168 //=======================================================================
169
170 PCDM_StoreStatus PCDM_StorageDriver::GetStoreStatus () const 
171 {
172   return myStoreStatus;
173 }
174 //=======================================================================
175 //function : SetStoreStatus
176 //purpose  :
177 //=======================================================================
178 void PCDM_StorageDriver::SetStoreStatus(const PCDM_StoreStatus theStoreStatus)
179 {
180     myStoreStatus = theStoreStatus;
181 }